Example #1
0
 /**
  * @basedata false
  */
 public function testRefer()
 {
     $user = new \TestApp\Model\User();
     $ret = $user->create(array('account' => 'c9s'));
     $this->assertResultSuccess($ret);
     ok($user->id);
     $book = new \AuthorBooks\Model\Book();
     $ret = $book->create(array('title' => 'Programming Perl', 'subtitle' => 'Way Way to Roman', 'publisher_id' => '""', 'created_by' => $user->id));
     $this->assertResultSuccess($ret);
 }
 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();
 }
 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));
 }
Example #4
0
 /**
  * @rebuild false
  */
 public function testUpdateWithReloadOption()
 {
     $b = new \AuthorBooks\Model\Book();
     $ret = $b->create(array('title' => 'Create for reload test', 'view' => 0));
     $this->assertResultSuccess($ret);
     // test incremental with Raw statement
     $ret = $b->update(array('view' => new Raw('view + 1')), array('reload' => true));
     $this->assertResultSuccess($ret);
     $this->assertEquals(1, $b->view);
     $ret = $b->update(array('view' => new Raw('view + 1')), array('reload' => true));
     $this->assertResultSuccess($ret);
     $this->assertEquals(2, $b->view);
     $ret = $b->delete();
     $this->assertResultSuccess($ret);
 }