예제 #1
0
 public function testJoin()
 {
     $author = new Author();
     $ret = $author->create(['name' => 'Mary III', 'email' => 'zz3@zz3', 'identity' => 'zz3']);
     $this->assertResultSuccess($ret);
     $ab = new AuthorBook();
     $book = new \AuthorBooks\Model\Book();
     $ret = $book->create(array('title' => 'Book I'));
     $this->assertResultSuccess($ret);
     $ret = $ab->create(['author_id' => $author->id, 'book_id' => $book->id]);
     $this->assertResultSuccess($ret);
     $ret = $book->create(array('title' => 'Book II'));
     $this->assertResultSuccess($ret);
     $ret = $ab->create(['author_id' => $author->id, 'book_id' => $book->id]);
     $this->assertResultSuccess($ret);
     $ret = $book->create(array('title' => 'Book III'));
     $this->assertResultSuccess($ret);
     $ret = $ab->create(array('author_id' => $author->id, 'book_id' => $book->id));
     $this->assertResultSuccess($ret);
     $books = new \AuthorBooks\Model\BookCollection();
     $books->join('author_books')->as('ab')->on()->equal('ab.book_id', array('m.id'));
     $books->where()->equal('ab.author_id', $author->id);
     $items = $books->items();
     $this->assertNotEmpty($items);
     $bookTitles = array();
     foreach ($books as $book) {
         $bookTitles[$book->title] = true;
         $ret = $book->delete();
     }
     $this->assertCount(3, array_keys($bookTitles));
     ok($bookTitles['Book I']);
     ok($bookTitles['Book II']);
     ok($bookTitles['Book III']);
 }
예제 #2
0
 public function testFilter()
 {
     $book = new \AuthorBooks\Model\Book();
     $results = array();
     result_ok($results[] = $book->create(array('title' => 'My Book I')));
     result_ok($results[] = $book->create(array('title' => 'My Book II')));
     result_ok($results[] = $book->create(array('title' => 'Perl Programming')));
     result_ok($results[] = $book->create(array('title' => 'My Book IV')));
     $books = new \AuthorBooks\Model\BookCollection();
     $books->fetch();
     count_ok(4, $books);
     $perlBooks = $books->filter(function ($item) {
         return $item->title == 'Perl Programming';
     });
     ok($perlBooks);
     is(1, $perlBooks->size());
     count_ok(1, $perlBooks->items());
     foreach ($results as $result) {
         ok($result->id);
         $record = new Book($result->id);
         $record->delete();
     }
     $someBooks = $books->splice(0, 2);
     is(2, count($someBooks));
 }
예제 #3
0
 public function testJoin()
 {
     $author = new Author();
     $author->create(array('name' => 'Mary III', 'email' => 'zz3@zz3', 'identity' => 'zz3'));
     $ab = new \AuthorBooks\Model\AuthorBook();
     $book = new \AuthorBooks\Model\Book();
     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));
     $books = new \AuthorBooks\Model\BookCollection();
     $books->join('author_books')->as('ab')->on()->equal('ab.book_id', array('m.id'));
     $books->where()->equal('ab.author_id', $author->id);
     $items = $books->items();
     $bookTitles = array();
     foreach ($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']);
 }