PageRenderTime 5ms CodeModel.GetById 4ms app.highlight 47ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/runtime/epTestManager.php

https://bitbucket.org/davidmpaz/ezpdo
PHP | 2289 lines | 1176 code | 528 blank | 585 comment | 184 complexity | 65cf91ad94dca473484d0f482ce30f8c MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2
   3/**
   4 * $Id: epTestManager.php 1019 2006-11-29 06:26:43Z nauhygon $
   5 *
   6 * Copyright(c) 2005 by Oak Nauhygon. All rights reserved.
   7 *
   8 * @author Oak Nauhygon <ezpdo4php@gmail.com>
   9 * @version $Revision: 1019 $ $Date: 2006-11-29 01:26:43 -0500 (Wed, 29 Nov 2006) $
  10 * @package ezpdo.tests
  11 * @subpackage ezpdo.tests.runtime
  12 */
  13
  14/**
  15 * need epTestCase
  16 */
  17include_once(dirname(__FILE__).'/epTestRuntime.php');
  18
  19/**
  20 * The unit test class for {@link epManager}
  21 *
  22 * @author Oak Nauhygon <ezpdo4php@gmail.com>
  23 * @version $Revision: 1019 $ $Date: 2006-11-29 01:26:43 -0500 (Wed, 29 Nov 2006) $
  24 * @package ezpdo.tests
  25 * @subpackage ezpdo.tests.runtime
  26 */
  27class epTestManager extends epTestRuntime {
  28
  29    /**
  30     * The cached manager
  31     * @var epManager
  32     */
  33    protected $m = false;
  34
  35    /**
  36     * Maximum number of items to insert to a table
  37     */
  38    const MAX_ITEMS = 10;
  39
  40    /**
  41     * Maximum number of items to insert to a table
  42     */
  43    const HALF_ITEMS = 10;
  44
  45    /**
  46     * remove output dir in teardown
  47     */
  48    function tearDown() {
  49        epRmDir(dirname(__FILE__) . '/output');
  50    }
  51
  52    /**
  53     * test {@link epManager} with single object
  54     * create, set/get vars, refresh, commit, delete
  55     */
  56    function _testSingleObject() {
  57
  58        $this->assertTrue($m = & $this->m);
  59
  60        // use manager to create one object
  61        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
  62
  63        // delete all books
  64        $m->deleteAll('eptBook');
  65
  66        // test object creation
  67        $title = md5('eptBook');
  68        $this->assertTrue($o = $m->create('eptBook', $title));
  69        $this->assertFalse($o->epIsDirty());
  70        $this->assertTrue($o->title === $title);
  71        $this->assertFalse($dirty = $o->epIsDirty());
  72
  73        // call method doNothing should not change dirty flag
  74        $o->doNothing();
  75        $this->assertTrue($o->epIsDirty() == $dirty);
  76
  77        // test commit
  78        $this->assertTrue($m->commit($o));
  79
  80        // check object id
  81        $this->assertTrue(($oid = $o->epGetObjectId()));
  82
  83        // test setter/getter
  84        $pages = rand(1, 1000);
  85        $this->assertFalse(($pages0 = $o->pages) === $pages);
  86        $o->setPages($pages);
  87        $this->assertTrue($o->pages === $pages);
  88        $this->assertTrue($dirty = $o->epIsDirty());
  89
  90        // call method doNothing should not change dirty flag
  91        $o->doNothing();
  92        $this->assertTrue($o->epIsDirty() == $dirty);
  93
  94        // call refresh so $pages will be replace with old value $page0
  95        $this->assertTrue($m->refresh($o));
  96
  97        $this->assertFalse($dirty = $o->epIsDirty());
  98        $this->assertTrue($o->pages === $pages0);
  99
 100        // call method doNothing should not change dirty flag
 101        $o->doNothing();
 102        $this->assertTrue($o->epIsDirty() == $dirty);
 103
 104        // set pages again and commit
 105        $this->assertFalse(($pages0 = $o->pages) === $pages);
 106        $o->setPages($pages);
 107        $this->assertTrue($o->pages === $pages);
 108        $this->assertTrue($o->epIsDirty());
 109        $this->assertTrue($m->commit($o));
 110        // insert id should not change
 111        $this->assertTrue($oid == $o->epGetObjectId());
 112
 113        // now refresh again
 114        $this->assertTrue($o->pages !== $pages0);
 115        $this->assertTrue($o->pages === $pages);
 116
 117        // try get (should get the cached)
 118        $this->assertTrue($o2 = & $m->get('eptBook', $oid));
 119        $this->assertTrue($o2 === $o);
 120
 121        // test delete
 122        $this->assertTrue($m->delete($o));
 123
 124        // cannot get obj for the oid
 125        $this->assertFalse($m->get('eptBook', $oid));
 126
 127        // delete all books
 128        $m->deleteAll('eptBook');
 129    }
 130
 131    /**
 132     * test {@link epManager}:
 133     * {@link epManager::getAll()}
 134     * {@link epManager::flush()}
 135     */
 136    function _testMultiObjects() {
 137
 138        // configure epManager
 139        $this->assertTrue($m = & $this->m);
 140
 141        // use manager to create one object
 142        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
 143
 144        // empty db
 145        $m->deleteAll('eptBook');
 146
 147        // create self::MAX_ITEMS eptBook's
 148        $oids = array();
 149        $all_pages = array();
 150        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 151
 152            $title = "title" . md5($i);
 153            $this->assertTrue($o = & $m->create('eptBook', $title));
 154
 155            // check title
 156            $this->assertFalse($dirty = $o->epIsDirty());
 157
 158            // call method doNothing should not change dirty flag
 159            $o->doNothing();
 160            $this->assertTrue($o->epIsDirty() == $dirty );
 161
 162            $this->assertTrue($o->title === $title);
 163
 164            // set pages
 165            $pages = rand(1, 1000);
 166            $this->assertTrue($o->pages = $pages);
 167            $this->assertTrue($o->pages === $pages);
 168
 169            // chech dirty flag
 170            $this->assertTrue($o->epIsDirty());
 171
 172            // commit
 173            $this->assertTrue($m->commit($o));
 174
 175            // make sure oid are valid
 176            $this->assertTrue(($oid = $o->epGetObjectId()));
 177
 178            // keep track of oids and pages
 179            $oids[] = $oid;
 180            $all_pages[] = $pages;
 181        }
 182
 183        // change page number for the first self::HALF_ITEMS books
 184        for($i = 0; $i < self::HALF_ITEMS; $i ++) {
 185
 186            // get book by oid
 187            $this->assertTrue($o = & $m->get('eptBook', $oids[$i]));
 188
 189            // check dirty flag (false)
 190            $this->assertFalse($dirty = $o->epIsDirty());
 191
 192            // call method doNothing should not change dirty flag
 193            $o->doNothing();
 194            $this->assertTrue($o->epIsDirty() == $dirty);
 195
 196            // chanage pages
 197            $this->assertTrue(($pages = $o->pages) === $all_pages[$i]);
 198            $this->assertTrue($o->pages = $pages + 1);
 199
 200            // check dirty flag (true)
 201            $this->assertTrue($o->epIsDirty());
 202        }
 203
 204        // now use epManager::getAll(): should get nothing since everything has been cached
 205        $this->assertTrue($os = $m->getAll('eptBook'));
 206        $this->assertTrue(count($os) == self::MAX_ITEMS);
 207        $this->assertTrue($m->count('eptBook') == self::MAX_ITEMS);
 208
 209        // check: the first self::HALF_ITEMS should be retrieved from cache, page number +1 (dirty))
 210        for($i = 0; $i < self::HALF_ITEMS; $i ++) {
 211
 212            // get book by oid
 213            $this->assertTrue($o = & $m->get('eptBook', $oids[$i]));
 214
 215            // check dirty flag (true)
 216            $this->assertTrue($o->epIsDirty());
 217
 218            // chanage pages
 219            $this->assertTrue($o->pages === $all_pages[$i] + 1);
 220        }
 221
 222        // check: the second self::HALF_ITEMS should be retrieved from db, same page number (no dirty)
 223        for($i = self::HALF_ITEMS; $i < self::MAX_ITEMS; $i ++) {
 224
 225            // get book by oid
 226            $this->assertTrue($o = & $m->get('eptBook', $oids[$i]));
 227
 228            // check dirty flag (false)
 229            $this->assertFalse($dirty = $o->epIsDirty());
 230
 231            // call method doNothing should not change dirty flag
 232            $o->doNothing();
 233            $this->assertTrue($o->epIsDirty() == $dirty);
 234
 235            // chanage pages
 236            $this->assertTrue($o->pages === $all_pages[$i]);
 237        }
 238
 239        // test flush
 240        $this->assertTrue($m->flush());
 241
 242        // before refresh, change values in cached objects
 243        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 244
 245            // get book by oid
 246            $this->assertTrue($o = & $m->get('eptBook', $oids[$i]));
 247
 248            // check dirty flag (false)
 249            $this->assertFalse($dirty = $o->epIsDirty());
 250
 251            // call method doNothing should not change dirty flag
 252            $o->doNothing();
 253            $this->assertTrue($o->epIsDirty() == $dirty);
 254
 255            // chanage pages
 256            $pages = $o->pages + rand(1, 1000);
 257            $this->assertTrue($o->pages = $pages);
 258            $all_pages[$i] = $pages;
 259
 260            // check dirty flag (true)
 261            $this->assertTrue($o->epIsDirty());
 262        }
 263
 264        // now refresh so get value from db that should all differ from memory
 265        // before refresh, change values in cached objects
 266        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 267
 268            // get book by oid
 269            $this->assertTrue($o = & $m->get('eptBook', $oids[$i]));
 270
 271            // check dirty flag (false)
 272            $this->assertTrue($o->epIsDirty());
 273
 274            // refresh
 275            $this->assertTrue($m->refresh($o));
 276
 277            // check that pages are different from those stored in memory
 278            $this->assertTrue($o->pages !== $all_pages[$i]);
 279
 280            // check dirty flag (true)
 281            $this->assertFalse($dirty = $o->epIsDirty());
 282
 283            // call method doNothing should not change dirty flag
 284            $o->doNothing();
 285            $this->assertTrue($o->epIsDirty() == $dirty);
 286        }
 287
 288        // delete all objects
 289        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 290
 291            // get book by oid
 292            $this->assertTrue($o = & $m->get('eptBook', $oids[$i]));
 293
 294            // delete
 295            $this->assertTrue($m->delete($o));
 296        }
 297
 298        // make sure we have deleted all
 299        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 300            // get book by oid
 301            $this->assertFalse($o = & $m->get('eptBook', $oids[$i]));
 302        }
 303
 304    }
 305
 306    /**
 307     * test createFromArray and updateFromArray
 308     */
 309    function _testCreateFromArray() {
 310
 311        $this->assertTrue($m = & $this->m);
 312
 313        // use manager to create one object
 314        include_once(EP_TESTS.'/classes/bookstore/src/eptAuthor.php');
 315        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
 316
 317        // delete all books
 318        $m->deleteAll('eptAuthor');
 319        $m->deleteAll('eptBook');
 320
 321        // array to be used for object creation
 322        $author = array(
 323            'name' => "author-".md5('author'),
 324            'id' => rand(1, 1000),
 325            'age' => rand(1, 120),
 326            'books' => array(
 327                'title' => 'title-'.md5('book'),
 328                'pages' => rand(1, 1000),
 329                )
 330            );
 331
 332        // create object from array
 333        $a = $m->createFromArray('eptAuthor', $author);
 334
 335        // check if all vars are correct
 336        $this->assertTrue($a->name == $author['name']);
 337        $this->assertTrue($a->id == $author['id']);
 338        $this->assertTrue($a->age == $author['age']);
 339        $this->assertNotNull($a->books);
 340        $this->assertNotNull($a->books[0]->title == $author['books']['title']);
 341        $this->assertNotNull($a->books[0]->pages == $author['books']['pages']);
 342
 343        // array to be used for object update
 344        $author_diff = array(
 345            'name' => "author-".md5('author'),
 346            'age' => rand(1, 120),
 347            'books' => $a->books
 348            );
 349
 350        // update object from array
 351        $a_ = $m->updateFromArray($a, $author_diff);
 352
 353        // check if all vars are correct
 354        $this->assertTrue($a_ === $a);
 355        $this->assertTrue($a->name == $author_diff['name']);
 356        $this->assertTrue($a->id == $author['id']);
 357        $this->assertTrue($a->age == $author_diff['age']);
 358        $this->assertNotNull($a->books);
 359        $this->assertNotNull($a->books[0]->title == $author['books']['title']);
 360        $this->assertNotNull($a->books[0]->pages == $author_diff['books']['pages']);
 361
 362        // array to be used for object creation
 363        $author2 = array(
 364            'name' => "author-".md5('author2'),
 365            'id' => rand(1, 1000),
 366            'age' => rand(1, 120),
 367            'contact' => array(
 368                'phone' => '1234567',
 369                'zipcode' => '8901',
 370                ),
 371            'books' => array(
 372                array(
 373                    'title' => 'title-'.md5('book1'),
 374                    'pages' => rand(1, 1000),
 375                    ),
 376                array(
 377                    'title' => 'title-'.md5('book2'),
 378                    'pages' => rand(1, 1000),
 379                    ),
 380                ),
 381            );
 382
 383        // create object from array
 384        $a2 = $m->createFromArray('eptAuthor', $author2);
 385
 386        // check if all vars are correct
 387        $this->assertTrue($a2->name == $author2['name']);
 388        $this->assertTrue($a2->id == $author2['id']);
 389        $this->assertTrue($a2->age == $author2['age']);
 390        $this->assertNotNull($a2->contact);
 391        $this->assertNotNull($a2->contact->phone = $author2['contact']['phone']);
 392        $this->assertNotNull($a2->contact->zipcode = $author2['contact']['zipcode']);
 393        $this->assertNotNull($a2->books);
 394        $this->assertNotNull($a2->books[0]->title == $author2['books'][0]['title']);
 395        $this->assertNotNull($a2->books[0]->pages == $author2['books'][0]['pages']);
 396        $this->assertNotNull($a2->books[1]->title == $author2['books'][1]['title']);
 397        $this->assertNotNull($a2->books[1]->pages == $author2['books'][1]['pages']);
 398
 399        // flush all
 400        $m->flush();
 401
 402        // delete all books
 403        $m->deleteAll('eptAuthor');
 404        $m->deleteAll('eptBook');
 405    }
 406
 407    /**
 408     * test {@link epArray}: orderby
 409     * {@link epArray::orderBy()}
 410     */
 411    function _testArraySortBy() {
 412
 413        // configure epManager
 414        $this->assertTrue($m = & $this->m);
 415
 416        include_once(EP_TESTS.'/classes/bookstore/src/eptAuthor.php');
 417        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
 418
 419        // empty db
 420        $m->deleteAll('eptAuthor');
 421        $m->deleteAll('eptBook');
 422
 423        // create an author
 424        $this->assertTrue($author = $m->create('eptAuthor'));
 425
 426        // add books into author
 427        $book_oids = array();
 428        $book_titles = array();
 429        $book_pages = array();
 430        $book_title_page_oids = array();
 431        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 432
 433            // create a book
 434            $this->assertTrue($book = $m->create('eptBook'));
 435
 436            // set book title
 437            $title = "title" . md5('whatever');
 438            $book->title = $title;
 439            $this->assertTrue($title === $book->title);
 440            $book_titles[] = $title;
 441
 442            // set book pages
 443            $pages = rand(1, 1000);
 444            $book->pages = $pages;
 445            $this->assertTrue($pages === $book->pages);
 446            $book_pages[] = $pages;
 447
 448            // commit it
 449            $this->assertTrue($book->commit());
 450            $this->assertTrue($book->oid > 0);
 451            $book_oids[] = $book->oid;
 452
 453            // add book into author
 454            $author->books[] = $book;
 455            $this->assertTrue($author->books->count() == $i + 1);
 456
 457            $book_title_page_oids[] = array(
 458                'title' => $title,
 459                'pages' => $pages,
 460                'oid' => $book->oid
 461                );
 462        }
 463
 464        // sort by title asc
 465        $this->assertTrue($author->books->sortBy('title', SORT_ASC));
 466        sort($book_titles);
 467        $i = 0;
 468        foreach($author->books as $book) {
 469            $this->assertTrue($book_titles[$i] === $book->title);
 470            $i ++;
 471        }
 472
 473        // sort by title desc
 474        $this->assertTrue($author->books->sortBy('title', SORT_DESC));
 475        rsort($book_titles);
 476        $i = 0;
 477        foreach($author->books as $book) {
 478            $this->assertTrue($book_titles[$i] === $book->title);
 479            $i ++;
 480        }
 481
 482        // sort by pages asc
 483        $this->assertTrue($author->books->sortBy('pages', SORT_ASC));
 484        sort($book_pages);
 485        $i = 0;
 486        foreach($author->books as $book) {
 487            $this->assertTrue($book_pages[$i] === $book->pages);
 488            $i ++;
 489        }
 490
 491        // sort by pages desc
 492        $this->assertTrue($author->books->sortBy('pages', SORT_DESC));
 493        rsort($book_pages);
 494        $i = 0;
 495        foreach($author->books as $book) {
 496            $this->assertTrue($book_pages[$i] === $book->pages);
 497            $i ++;
 498        }
 499
 500        // sort by oid asc
 501        $this->assertTrue($author->books->sortBy('oid', SORT_ASC));
 502        sort($book_oids);
 503        $i = 0;
 504        foreach($author->books as $book) {
 505            $this->assertTrue($book_oids[$i] === $book->oid);
 506            $i ++;
 507        }
 508
 509        // sort by pages desc
 510        $this->assertTrue($author->books->sortBy('oid', SORT_DESC));
 511        rsort($book_oids);
 512        $i = 0;
 513        foreach($author->books as $book) {
 514            $this->assertTrue($book_oids[$i] === $book->oid);
 515            $i ++;
 516        }
 517
 518        // multisort
 519        $this->assertTrue($author->books->sortBy('title', SORT_DESC, 'pages', SORT_ASC, 'oid', SORT_DESC));
 520
 521        foreach ($book_title_page_oids as $key => $row) {
 522            $_titles[$key] = $row['title'];
 523            $_pages[$key]  = $row['pages'];
 524            $_oids[$key] = $row['oid'];
 525        }
 526        array_multisort($_titles, SORT_DESC, $_pages, SORT_ASC, $_oids, SORT_DESC, $book_title_page_oids);
 527
 528        $i = 0;
 529        foreach($author->books as $book) {
 530            $this->assertTrue($book_title_page_oids[$i]['title'] === $book->title);
 531            $this->assertTrue($book_title_page_oids[$i]['pages'] === $book->pages);
 532            $this->assertTrue($book_title_page_oids[$i]['oid'] === $book->oid);
 533            $i ++;
 534        }
 535    }
 536
 537    /**
 538     * test datatypes
 539     */
 540    function _testDataTypes() {
 541
 542        // configure epManager
 543        $this->assertTrue($m = & $this->m);
 544
 545        // use manager to create one object
 546        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
 547
 548        // empty db book
 549        $m->deleteAll('eptBook');
 550
 551        $title = "title" . md5('whatever');
 552        $this->assertTrue($b = $m->create('eptBook', $title));
 553
 554        // a new object cannot be dirty
 555        $this->assertFalse($b->epIsDirty());
 556
 557        //
 558        // do all the setting here
 559        //
 560
 561        // set title (char)
 562        $this->assertTrue($b->title === $title);
 563        $this->assertTrue(is_string($b->title));
 564
 565        // set pages (integer)
 566        $pages = rand(1, 1000);
 567        $this->assertTrue($b->pages = $pages);
 568        $this->assertTrue($b->pages === $pages);
 569        $this->assertTrue(is_integer($b->pages));
 570
 571        // set recommended (boolean)
 572        $recommended = true;
 573        $this->assertTrue($b->recommended = $recommended);
 574        $this->assertTrue($b->recommended === $recommended);
 575        $this->assertTrue(is_bool($b->recommended));
 576
 577        // set excerpt (clob(8192))
 578        $excerpt = str_repeat("excerpt", (integer)(8192/7));
 579        $this->assertTrue($b->excerpt = $excerpt);
 580        $this->assertTrue($b->excerpt === $excerpt);
 581        $this->assertTrue(is_string($b->excerpt));
 582
 583        // set coverimg (blob(8192))
 584        $part = file_get_contents(dirname(__FILE__) . '/input/mysql_logo.gif');
 585        $coverimg = str_repeat($part, (integer)(8192/strlen($part)));
 586        $this->assertTrue($b->coverimg = $coverimg);
 587        $this->assertTrue($b->coverimg === $coverimg);
 588        $this->assertTrue(is_string($b->coverimg));
 589
 590        // set pubdate (date)
 591        $pubdate = time();
 592        $this->assertTrue($b->pubdate = $pubdate);
 593        $this->assertTrue($b->pubdate === $pubdate);
 594        $this->assertTrue(is_integer($b->pubdate));
 595
 596        // chech dirty flag
 597        $this->assertTrue($b->epIsDirty());
 598
 599        // commit book
 600        $this->assertTrue($m->commit($b));
 601        $this->assertTrue($oid = $b->epGetObjectId());
 602
 603        // now evict the book from memory
 604        $this->assertTrue($m->evictAll('eptBook'));
 605        $this->assertFalse($b);
 606
 607        // read it from db
 608        $this->assertTrue($b = & $m->get('eptBook', $oid));
 609
 610        //
 611        // do all the re-checking here
 612        //
 613
 614        // check title (char)
 615        $this->assertTrue($b->title == $title);
 616        $this->assertTrue(is_string($b->title));
 617
 618        // check pages (integer)
 619        $this->assertTrue($b->pages == $pages);
 620        $this->assertTrue(is_integer($b->pages));
 621
 622        // check recommended (boolean)
 623        $this->assertTrue($b->recommended == $recommended);
 624        $this->assertTrue(is_bool($b->recommended));
 625
 626        // check excerpt (clob(8192))
 627        $this->assertTrue($b->excerpt == $excerpt);
 628        $this->assertTrue(is_string($b->excerpt));
 629
 630        // check coverimg (blob(8192))
 631        $this->assertTrue($b->coverimg == $coverimg);
 632        $this->assertTrue(is_string($b->coverimg));
 633
 634        // set pubdate (date)
 635        $this->assertTrue($b->pubdate == $pubdate);
 636        $this->assertTrue(is_integer($b->pubdate));
 637
 638        // cleanup
 639        $m->deleteAll('eptBook');
 640    }
 641
 642    /**
 643     * test {@link epManager}: find
 644     * {@link epManager::find()}
 645     */
 646    function _testObjectFind() {
 647
 648        // configure epManager
 649        $this->assertTrue($m = & $this->m);
 650
 651        // use manager to create one object
 652        include_once(EP_TESTS.'/classes/bookstore/src/eptAuthor.php');
 653
 654        // empty db
 655        $m->deleteAll('eptAuthor');
 656        $m->deleteAll('eptBook');
 657
 658        // create self::MAX_ITEMS eptBook's
 659        $oids = array(); // object ids
 660        $names = array(); // author names
 661        $ages = array(); // author ages
 662        $ids = array(); // author id
 663        $objs = array(); // all objects
 664
 665        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 666
 667            $name = "author-" . md5($i);
 668            $this->assertTrue($o = & $m->create('eptAuthor', $name));
 669
 670            // check title
 671            $this->assertFalse($o->epIsDirty());
 672            $this->assertTrue($o->name === $name);
 673
 674            // set id
 675            $id = rand(1, 1000);
 676            //$this->assertTrue($o->setId($id));
 677            $this->assertTrue($o->id = $id);
 678            $this->assertTrue($o->id === $id);
 679
 680            // set ages
 681            $age = rand(1, 120);
 682            $this->assertTrue($o->age = $age);
 683            $this->assertTrue($o->age === $age);
 684
 685            // chech dirty flag
 686            $this->assertTrue($o->epIsDirty());
 687
 688            // commit
 689            $this->assertTrue($m->commit($o));
 690
 691            // make sure oid are valid
 692            $this->assertTrue(($oid = $o->epGetObjectId()));
 693
 694            // keep track of oids and pages
 695            $names[] = $name;
 696            $ages[] = $age;
 697            $oids[] = $oid;
 698            $ids[] = $id;
 699            $objs[] = & $o;
 700        }
 701
 702        // test find - all objects are in cache
 703        $eo = & $m->create('eptAuthor'); // example object
 704
 705        // set null to all vars
 706        $eo->uuid = '';
 707        $eo->name = '';
 708        $eo->id = null;
 709        $eo->age = null;
 710
 711        // find objects in cache only
 712        $this->assertTrue($os = $m->find($eo, EP_GET_FROM_CACHE));
 713        $this->assertTrue(count($os) == self::MAX_ITEMS);
 714
 715        // evict and find in cache
 716        $this->assertTrue($m->evictAll('eptAuthor'));
 717        $this->assertFalse($os = $m->find($eo, EP_GET_FROM_CACHE));
 718
 719        // after eviction, the objects themselves are deleted (set to null)
 720        $this->assertFalse($o); // test the last one first
 721
 722        // now check everyone (should all be deleted (ie null))
 723        foreach($objs as &$o) {
 724            $this->assertFalse($o);
 725        }
 726
 727        // find objects from db
 728        $this->assertTrue($os = $m->find($eo, EP_GET_FROM_DB));
 729        $this->assertTrue(count($os) == self::MAX_ITEMS);
 730
 731        // delete all objects
 732        $m->deleteAll('eptAuthor');
 733
 734        // make sure we have deleted all
 735        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 736            // get book by oid
 737            $this->assertFalse($o = & $m->get('eptAuthor', $oids[$i]));
 738        }
 739    }
 740
 741    /**
 742     * test {@link epManager}: find
 743     * {@link epManager::find()}
 744     */
 745    function _testObjectFindByChild() {
 746
 747        // configure epManager
 748        $this->assertTrue($m = & $this->m);
 749
 750        // use manager to create one object
 751        include_once(EP_TESTS.'/classes/bookstore/src/eptAuthor.php');
 752        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
 753        include_once(EP_TESTS.'/classes/bookstore/src/eptContact.php');
 754        include_once(EP_TESTS.'/classes/bookstore/src/eptBookstore.php');
 755
 756        // empty db
 757        $m->deleteAll('eptAuthor');
 758        $m->deleteAll('eptBook');
 759        $m->deleteAll('eptContact');
 760        $m->deleteAll('eptBookstore');
 761
 762        // create self::MAX_ITEMS eptBook's
 763        $oids = array(); // object ids
 764        $names = array(); // author names
 765        $ages = array(); // author ages
 766        $ids = array(); // author ids
 767        $phones = array(); // author.contact phones
 768        $zipcodes = array(); // author.contact zipcodes
 769        $titles = array(); // book titles
 770        $pages = array(); // book pages
 771        $objs = array(); // all objects
 772
 773        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 774
 775            $name = "author-" . md5($i);
 776            $this->assertTrue($o = & $m->create('eptAuthor', $name));
 777
 778            // check title
 779            $this->assertFalse($dirty = $o->epIsDirty());
 780
 781            // call method doNothing should not change dirty flag
 782            $o->doNothing();
 783            $this->assertTrue($o->epIsDirty() == $dirty);
 784
 785            // set name
 786            $this->assertTrue($o->name === $name);
 787
 788            // set id
 789            $id = rand(1, 1000);
 790            //$this->assertTrue($o->setId($id));
 791            $this->assertTrue($o->id = $id);
 792            $this->assertTrue($o->id === $id);
 793
 794            // set ages
 795            $age = rand(1, 120);
 796            $this->assertTrue($o->age = $age);
 797            $this->assertTrue($o->age === $age);
 798
 799            // check dirty flag
 800            $this->assertTrue($o->epIsDirty());
 801
 802            // add the contact
 803            $this->assertTrue($o->contact = $m->create('eptContact'));
 804            $this->assertFalse($o->contact->epIsDirty());
 805
 806            // set phone number
 807            $phone = rand(100, 999)."-456-".rand(1000, 9999);
 808            $this->assertTrue($o->contact->phone = $phone);
 809            $this->assertTrue($o->contact->phone === $phone);
 810
 811            // set the zipcode
 812            $zipcode = rand(10000, 99999);
 813            $this->assertTrue($o->contact->zipcode = $zipcode);
 814            $this->assertTrue($o->contact->zipcode === $zipcode);
 815
 816            // check dirty flag
 817            $this->assertTrue($o->contact->epIsDirty());
 818
 819            // 2 books for each author
 820            for ($j = 0; $j < 2; $j++) {
 821                // add the book
 822                $title = 'title-'.$i.'-'.$j;
 823                $this->assertTrue($o->books[$j] = $m->create('eptBook', $title));
 824                $this->assertFalse($o->books[$j]->epIsDirty());
 825                $this->assertTrue($o->books[$j]->title === $title);
 826
 827                // set author
 828                $this->assertTrue($o->books[$j]->author = $o);
 829                $this->assertTrue($o->books[$j]->author === $o);
 830
 831                // set amount of pages
 832                $page = rand(1, 1000);
 833                $this->assertTrue($o->books[$j]->pages = $page);
 834                $this->assertTrue($o->books[$j]->pages === $page);
 835
 836                $titles[$i][] = $title;
 837                $pages[$i][] = $page;
 838            }
 839
 840            // commit
 841            $this->assertTrue($m->commit($o));
 842
 843            // make sure oid are valid
 844            $this->assertTrue(($oid = $o->epGetObjectId()));
 845
 846            // keep track of oids and pages
 847            $names[] = $name;
 848            $ages[] = $age;
 849            $oids[] = $oid;
 850            $ids[] = $id;
 851            $phones[] = $phone;
 852            $zipcodes[] = $zipcode;
 853            $objs[] = & $o;
 854        }
 855
 856        // test find - all objects are in cache
 857        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 858            // find by book title of author
 859            $eo = & $m->create('eptAuthor'); // example object
 860
 861            // set null to all vars
 862            $eo->uuid = '';
 863            $eo->name = '';
 864            $eo->id = null;
 865            $eo->age = null;
 866            $eo->books[0] = $m->create('eptBook');
 867            $eo->books[0]->title = $titles[$i][0];
 868            $eo->books[0]->uuid = null;
 869            $eo->books[0]->pages = null;
 870            $eo->books[0]->recommended = null;
 871            $eo->books[0]->pubdate = null;
 872            $eo->books[0]->coverimg = null;
 873            $eo->books[0]->excerpt = null;
 874            // match its author up again
 875            $eo->books[0]->author = $eo;
 876
 877            // find each author
 878            $this->assertTrue($os = $m->find($eo, EP_GET_FROM_CACHE));
 879            // example object should be uncommittable
 880            $this->assertFalse($eo->books[0]->epIsCommittable());
 881            $this->assertTrue(count($os) == 1);
 882            $this->assertTrue($os[0]->name === $names[$i]);
 883
 884            // find book by author's contact
 885            for($j = 0; $j < 2; $j++) {
 886                $eb = & $m->create('eptBook');
 887                $eb->uuid = null;
 888                $eb->pages = null;
 889                $eb->recommended = null;
 890                $eb->pubdate = null;
 891                $eb->coverimg = null;
 892                $eb->excerpt = null;
 893
 894                $eb->author = $m->create('eptAuthor');
 895                $eb->author->uuid = '';
 896                $eb->author->name = '';
 897                $eb->author->id = null;
 898                $eb->author->age = null;
 899                $eb->author->contact = $m->create('eptContact');
 900                $eb->author->contact->phone = $phones[$i];
 901                $eb->author->contact->zipcode = null;
 902                $eb->author->contact->uuid = null;
 903
 904                // find the book
 905                $this->assertTrue($os = $m->find($eb, EP_GET_FROM_CACHE));
 906                $this->assertFalse($eb->author->epIsCommittable());
 907                $this->assertFalse($eb->author->contact->epIsCommittable());
 908                $this->assertTrue(count($os) == 2);
 909                $this->assertTrue($os[$j]->title === $titles[$i][$j]);
 910                $this->assertTrue($os[$j]->author->name === $names[$i]);
 911            }
 912
 913        }
 914
 915        // evict and find in cache
 916        $this->assertTrue($m->evictAll('eptAuthor'));
 917        $this->assertTrue($m->evictAll('eptBook'));
 918        $this->assertTrue($m->evictAll('eptContact'));
 919        $this->assertFalse($os = $m->find($eo, EP_GET_FROM_CACHE));
 920
 921        // test find - all objects are in cache
 922        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 923            // find by book title of author
 924            $eo = & $m->create('eptAuthor'); // example object
 925
 926            // set null to all vars
 927            $eo->uuid = '';
 928            $eo->name = '';
 929            $eo->id = null;
 930            $eo->age = null;
 931            $eo->books[0] = $m->create('eptBook');
 932            $eo->books[0]->title = $titles[$i][0];
 933            $eo->books[0]->uuid = null;
 934            $eo->books[0]->pages = null;
 935            $eo->books[0]->recommended = null;
 936            $eo->books[0]->pubdate = null;
 937            $eo->books[0]->coverimg = null;
 938            $eo->books[0]->excerpt = null;
 939            // match its author up again
 940            $eo->books[0]->author = $eo;
 941
 942            // find each author
 943            $this->assertTrue($os = $m->find($eo, EP_GET_FROM_DB));
 944            // example object should be uncommittable
 945            $this->assertFalse($eo->books[0]->epIsCommittable());
 946            $this->assertTrue(count($os) == 1);
 947            $this->assertTrue($os[0]->name === $names[$i]);
 948
 949            // find book by author's contact
 950            for($j = 0; $j < 2; $j++) {
 951                $eb = & $m->create('eptBook');
 952                $eb->uuid = null;
 953                $eb->pages = null;
 954                $eb->recommended = null;
 955                $eb->pubdate = null;
 956                $eb->coverimg = null;
 957                $eb->excerpt = null;
 958
 959                $eb->author = $m->create('eptAuthor');
 960                $eb->author->uuid = '';
 961                $eb->author->name = '';
 962                $eb->author->id = null;
 963                $eb->author->age = null;
 964                $eb->author->contact = $m->create('eptContact');
 965                $eb->author->contact->phone = $phones[$i];
 966                $eb->author->contact->zipcode = null;
 967                $eb->author->contact->uuid = null;
 968
 969                // find the book
 970                $this->assertTrue($os = $m->find($eb, EP_GET_FROM_DB));
 971                $this->assertFalse($eb->author->epIsCommittable());
 972                $this->assertFalse($eb->author->contact->epIsCommittable());
 973                $this->assertTrue(count($os) == 2);
 974                // there is no way to know for certain what order the data comes
 975                // back out.  But both of them must come out
 976                if ($os[$j]->title === $titles[$i][$j]) {
 977                    $this->assertTrue($os[$j]->title === $titles[$i][$j]);
 978                } else {
 979                    $this->assertTrue($os[$j]->title === $titles[$i][$j?0:1]);
 980                }
 981                $this->assertTrue($os[$j]->author->name === $names[$i]);
 982            }
 983        }
 984
 985        // delete all objects
 986        $m->deleteAll('eptAuthor');
 987        $m->deleteAll('eptBook');
 988        $m->deleteAll('eptContact');
 989        $m->deleteAll('eptBookstore');
 990
 991        // make sure we have deleted all
 992        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
 993            // get book by oid
 994            $this->assertFalse($o = & $m->get('eptAuthor', $oids[$i]));
 995        }
 996    }
 997
 998    /**
 999     * test {@link epQuery}: query/find
1000     * More complete tests are moved to tests/query/epTestQueryRuntime.php
1001     * !!!Add new test cases there!!!
1002     * {@link epManager::find()}
1003     */
1004    function _testObjectQueryPrimitive() {
1005
1006        $this->assertTrue($m = & $this->m);
1007
1008        // use manager to create one object
1009        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
1010
1011        // empty db
1012        $m->deleteAll('eptBook');
1013
1014        // create eptBook's (self::MAX_ITEMS in total)
1015        $bk_oids = array();
1016        $bk_pages = array();
1017        $bk_titles = array();
1018        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
1019
1020            $title = "title" . md5($i);
1021            $this->assertTrue($b = & $m->create('eptBook', $title));
1022
1023            // check title
1024            $this->assertFalse($b->epIsDirty());
1025            $this->assertTrue($b->title === $title);
1026
1027            // set pages
1028            $pages = rand(1, 1000);
1029            $this->assertTrue($b->pages = $pages);
1030            $this->assertTrue($b->pages === $pages);
1031
1032            // chech dirty flag
1033            $this->assertTrue($b->epIsDirty());
1034
1035            // commit book
1036            $this->assertTrue($m->commit($b));
1037
1038            // make sure oid are valid
1039            $this->assertTrue(($oid = $b->epGetObjectId()));
1040
1041            // keep track of oids and pages
1042            $bk_oids[] = $oid;
1043            $bk_pages[] = $pages;
1044            $bk_titles[] = $title;
1045        }
1046
1047        // remove all objects from memory
1048        $this->assertTrue($m->evictAll('eptBook'));
1049
1050        // -----------------
1051        // test epQuery here
1052        // -----------------
1053
1054        // test simple expression
1055        $this->assertTrue($os = $m->query("from eptBook as book where book.pages > ?", 0));
1056        $this->assertTrue(count($os) == self::MAX_ITEMS);
1057        $this->assertTrue($m->count('eptBook') == self::MAX_ITEMS);
1058
1059        // test like
1060        $this->assertTrue($os = $m->query("from eptBook as book where book.title LIKE 'title%'"));
1061        $this->assertTrue(count($os) == self::MAX_ITEMS);
1062        $this->assertTrue($m->count('eptBook') == self::MAX_ITEMS);
1063
1064        // count array values
1065        $value_counts = array_count_values($bk_pages);
1066
1067        // search book by book title
1068        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
1069
1070            // find book by title
1071            $this->assertTrue($os = $m->query("from eptBook as book where book.title = ?", $bk_titles[$i]));
1072            $this->assertTrue(count($os) == 1);
1073            $this->assertTrue($os[0]->pages == $bk_pages[$i]);
1074
1075            // find book by pages
1076            $this->assertTrue($os = $m->query("from eptBook as book where book.pages = ?", $bk_pages[$i]));
1077            $this->assertTrue(count($os) == $value_counts[$bk_pages[$i]]);
1078        }
1079
1080        // clean up
1081        $m->deleteAll('eptBook');
1082        $m->deleteAll('eptAuthor');
1083    }
1084
1085    /**
1086     * test {@link epQuery}: query/find (with relationship fields)
1087     * More complete tests are moved to tests/query/epTestQueryRuntime.php
1088     * !!!Add new test cases there!!!
1089     * {@link epManager::find()}
1090     */
1091    function _testObjectQueryRelationship() {
1092
1093        $this->assertTrue($m = & $this->m);
1094
1095        // use manager to create one object
1096        include_once(EP_TESTS.'/classes/bookstore/src/eptAuthor.php');
1097        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
1098
1099        // empty db
1100        $m->deleteAll('eptAuthor');
1101        $m->deleteAll('eptBook');
1102
1103        // -----------------------------------------------------
1104
1105        // create one author
1106        $name = "author-test";
1107        $this->assertTrue($a = $m->create('eptAuthor', $name));
1108
1109        // check title
1110        $this->assertFalse($a->epIsDirty());
1111        $this->assertTrue($a->name === $name);
1112
1113        // set id
1114        $id = rand(1, 1000);
1115        $this->assertTrue($a->id = $id);
1116        $this->assertTrue($a->id === $id);
1117
1118        // set ages
1119        $age = rand(1, 120);
1120        $this->assertTrue($a->age = $age);
1121        $this->assertTrue($a->age === $age);
1122
1123        // chech dirty flag
1124        $this->assertTrue($a->epIsDirty());
1125
1126        // commit
1127        $this->assertTrue($m->commit($a));
1128
1129        // make sure oid are valid
1130        $this->assertTrue(($oid = $a->epGetObjectId()));
1131
1132        // -----------------------------------------------------
1133
1134        // create eptBook's (self::MAX_ITEMS in total)
1135        $bk_oids = array();
1136        $bk_pages = array();
1137        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
1138
1139            $title = "title" . md5($i);
1140            $this->assertTrue($b = $m->create('eptBook', $title));
1141
1142            // check title
1143            $this->assertFalse($b->epIsDirty());
1144            $this->assertTrue($b->title === $title);
1145
1146            // set pages
1147            $pages = rand(1, 1000);
1148            $this->assertTrue($b->pages = $pages);
1149            $this->assertTrue($b->pages === $pages);
1150
1151            // chech dirty flag
1152            $this->assertTrue($b->epIsDirty());
1153
1154            // set author to book
1155            $this->assertTrue($b->author = $a);
1156
1157            // add book into author
1158            if (!($books = $a->books)) {
1159                $books = array();
1160            }
1161            $books[] = $b;
1162
1163            // assign books to author's books
1164            $this->assertTrue($a->books = $books);
1165            $this->assertTrue(count($a->books) == count($books));
1166            $this->assertTrue($b->epIsDirty());
1167
1168            // commit book
1169            $this->assertTrue($m->commit($b));
1170
1171            // make sure oid are valid
1172            $this->assertTrue(($oid = $b->epGetObjectId()));
1173
1174            // keep track of oids and pages
1175            $bk_oids[] = $oid;
1176            $bk_pages[] = $pages;
1177        }
1178
1179        // make sure the author has the right number of books
1180        if (version_compare(phpversion(), "5.1.0", "<")) {
1181            $this->assertTrue($a->books->count() == self::MAX_ITEMS);
1182        } else {
1183            $this->assertTrue(count($a->books) == self::MAX_ITEMS);
1184        }
1185
1186        // -----------------------------------------------------
1187
1188        // remove all objects from memory
1189        $this->assertTrue($m->evictAll('eptAuthor'));
1190        $this->assertTrue($m->evictAll('eptBook'));
1191
1192        // make sure author is gone
1193        $this->assertFalse($a);
1194
1195        // test 1
1196        $this->assertTrue($os = $m->query("from eptAuthor as a where a.books.contains(book) and book.pages > ?", 0));
1197        $this->assertTrue(count($os) == $m->count('eptAuthor'));
1198
1199        // test 2
1200        $this->assertTrue($os = $m->query("from eptAuthor as a where a.books.contains(book) and book.title LIKE 'title%'"));
1201        $this->assertTrue(count($os) == $m->count('eptAuthor'));
1202
1203        // test 3
1204        $this->assertTrue($os = $m->query("from eptAuthor as a where a.books.contains(b) and b.author.name like 'author-test%'"));
1205        $this->assertTrue(count($os) == $m->count('eptAuthor'));
1206
1207        // test 4
1208        $this->assertTrue($os = $m->query("from eptAuthor as a where a.books.contains(b) and b.author.books.contains(b2) and b2.title like 'title%'"));
1209        $this->assertTrue(count($os) == $m->count('eptAuthor'));
1210
1211        // clean up
1212        $m->deleteAll('eptBook');
1213        $m->deleteAll('eptAuthor');
1214    }
1215
1216    /**
1217     * test {@link epManager}
1218     * {@link epManager::find()}
1219     */
1220    function _testObjectRelation() {
1221
1222        // get the manager
1223        $this->assertTrue($m = & $this->m);
1224
1225        // use manager to create one object
1226        include_once(EP_TESTS.'/classes/bookstore/src/eptAuthor.php');
1227        include_once(EP_TESTS.'/classes/bookstore/src/eptBook.php');
1228        include_once(EP_TESTS.'/classes/bookstore/src/eptBookstore.php');
1229
1230        // empty db
1231        $m->deleteAll('eptAuthor');
1232        $m->deleteAll('eptBook');
1233
1234        // -----------------------------------------------------
1235
1236        // create one author
1237        $name = "author-test";
1238        $this->assertTrue($a = $m->create('eptAuthor', $name));
1239
1240        // check title
1241        $this->assertFalse($a->epIsDirty());
1242        $this->assertTrue($a->name === $name);
1243
1244        // set id
1245        $id = rand(1, 1000);
1246        $this->assertTrue($a->id = $id);
1247        $this->assertTrue($a->id === $id);
1248
1249        // set ages
1250        $age = rand(1, 120);
1251        $this->assertTrue($a->age = $age);
1252        $this->assertTrue($a->age === $age);
1253
1254        // chech dirty flag
1255        $this->assertTrue($a->epIsDirty());
1256
1257        // commit
1258        $this->assertTrue($m->commit($a));
1259
1260        // make sure oid are valid
1261        $this->assertTrue(($a_oid = $a->epGetObjectId()));
1262
1263        // -----------------------------------------------------
1264
1265        $storename = 'store';
1266        $this->assertTrue($s = $m->create('eptBookstore', $storename));
1267
1268        // check title
1269        $this->assertFalse($s->epIsDirty());
1270        $this->assertTrue($s->name === $storename);
1271
1272        // commit
1273        $this->assertTrue($m->commit($s));
1274
1275        // make sure oid are valid
1276        $this->assertTrue(($s_oid = $s->epGetObjectId()));
1277
1278        // -----------------------------------------------------
1279
1280        // create eptBook's (self::MAX_ITEMS in total)
1281        $bk_oids = array();
1282        $bk_pages = array();
1283        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
1284
1285            $title = "title" . md5($i);
1286            $this->assertTrue($b = $m->create('eptBook', $title));
1287
1288            // check title
1289            $this->assertFalse($b->epIsDirty());
1290            $this->assertTrue($b->title === $title);
1291
1292            // set pages
1293            $pages = rand(1, 1000);
1294            $this->assertTrue($b->pages = $pages);
1295            $this->assertTrue($b->pages === $pages);
1296
1297            // check dirty flag
1298            $this->assertTrue($b->epIsDirty());
1299
1300            // check modified vars (both)
1301            $this->assertTrue($modified_vars = $b->epGetModifiedVars());
1302            $this->assertTrue(count($modified_vars) == 1);
1303            $this->assertTrue($modified_vars['pages'] == $pages);
1304
1305            // check modified vars (primitive)
1306            $this->assertTrue($modified_vars = $b->epGetModifiedVars(epObject::VAR_PRIMITIVE));
1307            $this->assertTrue(count($modified_vars) == 1);
1308            $this->assertTrue($modified_vars['pages'] == $pages);
1309
1310            // check modified vars (relationship)
1311            $this->assertFalse($modified_vars = $b->epGetModifiedVars(epObject::VAR_RELATIONSHIP));
1312
1313            // set author to book
1314            $this->assertTrue($b->author = $a);
1315            $this->assertTrue($b->bookstore = $s);
1316
1317            // check modified vars
1318            $this->assertTrue($modified_vars = $b->epGetModifiedVars());
1319            $this->assertTrue(count($modified_vars) == 3);
1320            $this->assertTrue($modified_vars['pages'] == $pages);
1321            $this->assertTrue($modified_vars['author'] == true);
1322
1323
1324            // check modified vars (primitive)
1325            $this->assertTrue($modified_vars = $b->epGetModifiedVars(epObject::VAR_PRIMITIVE));
1326            $this->assertTrue(count($modified_vars) == 1);
1327            $this->assertTrue($modified_vars['pages'] == $pages);
1328
1329            // check modified vars (relationship)
1330            $this->assertTrue($modified_vars = $b->epGetModifiedVars(epObject::VAR_RELATIONSHIP));
1331            $this->assertTrue(count($modified_vars) == 2);
1332            $this->assertTrue($modified_vars['author'] == true);
1333            $this->assertTrue($modified_vars['bookstore'] == true);
1334
1335            // add book into author
1336            if (!($books = $a->books)) {
1337                $books = array();
1338            }
1339            $books[] = $b;
1340
1341            // assign books to author's books
1342            $this->assertTrue($a->books = $books);
1343            $this->assertTrue(count($a->books) == count($books));
1344
1345            // add book into bookstore
1346            if (!($books = $s->books)) {
1347                $books = array();
1348            }
1349            $books[] = $b;
1350
1351            // assign books to author's books
1352            $this->assertTrue($s->books = $books);
1353            $this->assertTrue(count($s->books) == count($books));
1354
1355            $this->assertTrue($b->epIsDirty());
1356
1357            // commit book
1358            $this->assertTrue($m->commit($b));
1359
1360            // make sure oid are valid
1361            $this->assertTrue(($oid = $b->epGetObjectId()));
1362
1363            // keep track of oids and pages
1364            $bk_oids[] = $oid;
1365            $bk_pages[] = $pages;
1366        }
1367
1368        // make sure the author has the right number of books
1369        if (version_compare(phpversion(), "5.1.0", "<")) {
1370            $this->assertTrue($a->books->count() == self::MAX_ITEMS);
1371        } else {
1372            $this->assertTrue(count($a->books) == self::MAX_ITEMS);
1373        }
1374
1375        // -----------------------------------------------------
1376
1377        // remove all objects from memory
1378        $this->assertTrue($m->evictAll('eptAuthor'));
1379        $this->assertTrue($m->evictAll('eptBook'));
1380
1381        // make sure author is gone
1382        $this->assertFalse($a);
1383
1384        // retrieve author, change a value and recommit
1385        // other values should not have changed
1386        $this->assertTrue($a =& $m->get('eptAuthor', $a_oid));
1387
1388        // check old id, change id
1389        $this->assertFalse($a->epIsDirty());
1390        $this->assertTrue($a->id === $id);
1391
1392        $id = rand(1, 1000);
1393        $this->assertTrue($a->id = $id);
1394        $this->assertTrue($a->id === $id);
1395
1396        // chech dirty flag
1397        $this->assertTrue($a->epIsDirty());
1398
1399        // commit
1400        $this->assertTrue($m->commit($a));
1401
1402        // make sure oid are valid
1403        $this->assertTrue(($a_oid = $a->epGetObjectId()));
1404
1405        // remove all objects from memory
1406        $this->assertTrue($m->evictAll('eptAuthor'));
1407        $this->assertTrue($m->evictAll('eptBook'));
1408
1409        // make sure author is gone
1410        $this->assertFalse($a);
1411
1412        // all values should be good
1413        $this->assertTrue($a =& $m->get('eptAuthor', $a_oid));
1414
1415        // check title
1416        $this->assertFalse($a->epIsDirty());
1417        $this->assertTrue($a->name === $name);
1418
1419        // set id
1420        $this->assertTrue($a->id === $id);
1421
1422        // set ages
1423        $this->assertTrue($a->age === $age);
1424
1425        // make sure the author has the right number of books
1426        if (version_compare(phpversion(), "5.1.0", "<")) {
1427            $this->assertTrue($a->books->count() == self::MAX_ITEMS);
1428        } else {
1429            $this->assertTrue(count($a->books) == self::MAX_ITEMS);
1430        }
1431
1432        // -----------------------------------------------------
1433
1434        // remove all objects from memory
1435        $this->assertTrue($m->evictAll('eptAuthor'));
1436        $this->assertTrue($m->evictAll('eptBook'));
1437
1438        // make sure author is gone
1439        $this->assertFalse($a);
1440
1441        // retrieve books one by one and check its author
1442        $a0 = false;
1443        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
1444
1445            // get book by oid
1446            $this->assertTrue($b = $m->get('eptBook', $bk_oids[$i]));
1447
1448            // check pages
1449            $this->assertTrue($b->pages == $bk_pages[$i]);
1450
1451            // check author
1452            $this->assertNotNull($a = $b->author);
1453
1454            // same author (ref)
1455            if ($a0 && $a) {
1456                $this->assertTrue($a0 === $a);
1457            }
1458
1459            // does it have the right author
1460            $this->assertTrue($a->name === $name);
1461
1462            // does it have the right age
1463            $this->assertTrue($a->age == $age);
1464
1465            // does it have the right id
1466            $this->assertTrue($a->id == $id);
1467
1468            // check books the author has is self::MAX_ITEMS
1469            if ($a->books instanceof epArray) {
1470                if (version_compare(phpversion(), "5.1.0", "<")) {
1471                    $this->assertTrue($a->books->count() == self::MAX_ITEMS);
1472                } else {
1473                    $this->assertTrue(count($a->books) == self::MAX_ITEMS);
1474                }
1475            } else {
1476                $this->assertTrue(count($a->books) == self::MAX_ITEMS);
1477            }
1478
1479            $a0 = & $a;
1480        }
1481
1482        // remove author from book
1483        for($i = 0; $i < self::MAX_ITEMS; $i ++) {
1484
1485            // get book by oid
1486            $this->assertTrue($b = $m->get('eptBook', $bk_oids[$i]));
1487
1488            // check pages
1489            $this->assertTrue($b->pages == $bk_pages[$i]);
1490
1491            // remove author
1492            $b->author = null;
1493
1494            // commit
1495            $m->commit($b);
1496
1497            $this->assertFalse($b->author…

Large files files are truncated, but you can click here to view the full file