public function testManyToManyRelationFetch()
 {
     $author = new Author();
     $author->create(array('name' => 'Z', 'email' => 'z@z', 'identity' => 'z'));
     // XXX: in different database engine, it's different.
     // sometimes it's string, sometimes it's integer
     // ok( is_string( $author->getValue('id') ) );
     ok(is_integer($author->get('id')));
     $book = $author->books->create(array('title' => 'Book Test'));
     ok($book);
     ok($book->id, 'book is created');
     $ret = $book->delete();
     ok($ret->success);
     $ab = new \AuthorBooks\Model\AuthorBook();
     $book = new \AuthorBooks\Model\Book();
     // should not include this
     ok($book->create(array('title' => 'Book I Ex'))->success);
     ok($book->create(array('title' => 'Book I'))->success);
     ok($ab->create(array('author_id' => $author->id, 'book_id' => $book->id))->success);
     ok($book->create(array('title' => 'Book II'))->success);
     $ab->create(array('author_id' => $author->id, 'book_id' => $book->id));
     ok($book->create(array('title' => 'Book III'))->success);
     $ab->create(array('author_id' => $author->id, 'book_id' => $book->id));
     // retrieve books from relationshipt
     $author->flushCache();
     $books = $author->books;
     is(3, $books->size(), 'We have 3 books');
     $bookTitles = array();
     foreach ($books->items() as $item) {
         $bookTitles[$item->title] = true;
         $item->delete();
     }
     count_ok(3, array_keys($bookTitles));
     ok($bookTitles['Book I']);
     ok($bookTitles['Book II']);
     ok($bookTitles['Book III']);
     ok(!isset($bookTitles['Book I Ex']));
     $author->delete();
 }
Example #2
0
 public function testManyToManyRelationFetchRecord()
 {
     $author = new \AuthorBooks\Model\Author();
     $author->create(array('name' => 'Z', 'email' => 'z@z', 'identity' => 'z'));
     $book = $author->books->create(array('title' => 'Book Test'));
     $this->assertNotNull($book);
     $this->assertNotNull($book->id, 'book is created');
     $ret = $book->delete();
     $this->assertTrue($ret->success);
     $this->assertEquals(Result::TYPE_DELETE, $ret->type);
     $ab = new \AuthorBooks\Model\AuthorBook();
     $book = new \AuthorBooks\Model\Book();
     // should not include this
     $this->assertTrue($book->create(array('title' => 'Book I Ex'))->success);
     $this->assertTrue($book->create(array('title' => 'Book I'))->success);
     $ret = $ab->create(array('author_id' => $author->id, 'book_id' => $book->id));
     $this->assertResultSuccess($ret);
     $this->assertTrue($book->create(array('title' => 'Book II'))->success);
     $ab->create(array('author_id' => $author->id, 'book_id' => $book->id));
     $this->assertTrue($book->create(array('title' => 'Book III'))->success);
     $ab->create(array('author_id' => $author->id, 'book_id' => $book->id));
     // retrieve books from relationshipt
     $author->flushCache();
     $books = $author->books;
     $this->assertEquals(3, $books->size(), 'We have 3 books');
     $bookTitles = array();
     foreach ($books->items() as $item) {
         $bookTitles[$item->title] = true;
         $item->delete();
     }
     $this->assertCount(3, array_keys($bookTitles));
     ok($bookTitles['Book I']);
     ok($bookTitles['Book II']);
     ok($bookTitles['Book III']);
     ok(!isset($bookTitles['Book I Ex']));
     $author->delete();
 }