public function testSelectOneWithFieldMappings() { $query = 'SELECT b.id as i, b.name as n, b.description as d, b.authors_id as a FROM books b JOIN authors a ON b.authors_id = a.id'; $book = Book::selectOneSQL($this->getConn(), $query, [], ['i' => 'id', 'n' => 'name', 'd' => 'description', 'a' => 'authors_id']); $expectedValues = ['id' => '1', 'name' => 'Book 1', 'description' => 'The very first book', 'authors_id' => '1']; $this->assertSame($expectedValues, $book->getValues()); }
public function testDeleteAll() { $books = Book::select($this->getConn())->execute(); $this->assertSame(50, count($books)); Book::deleteAll($this->getConn()); $no_books = Book::select($this->getConn())->execute(); $this->assertSame(0, count($no_books)); }
public function testSetBelongsTo() { $book = Book::selectOne($this->getConn())->execute(); $this->assertFalse($book->hasRelation('details')); $details = BookDetails::selectOne($this->getConn())->execute(); $this->assertNotSame($book->id, $details->books_id); $details->book = $book; $this->assertSame($book->id, $details->books_id); $this->assertSame($book, $details->book); $this->assertTrue($details->hasRelation('book')); }
public function testNullRelationIsNotQueriedManyTimes() { $this->loadData('bookshop'); $book = Book::selectPrimaryKey($this->getConn(), 50); $this->resetQueryCount(); $this->assertNull($book->details); //make sure null has been saved and not queried for again $book->details; $book->details; $book->details; $this->assertSame(1, $this->getQueryCount()); }
public function testSelectWithEager() { $this->loadData('bookshop'); $this->resetQueryCount(); $books = Book::select($this->getConn())->with('author')->execute(); foreach ($books as $book) { //ensure the related entities are actually loaded $book->author; } $this->assertSame('Thomas Hardy', $books[0]->author->name); //only two queries should have run $this->assertSame(2, $this->getQueryCount()); }
/** * @dataProvider updateMethodProvider */ public function testUpdateCallsUpdateEvent($update_method) { Book::addEventCallBack('update', function ($book) { $book->description = 'update-' . $book->description; }); $this->loadSchema('bookshop'); $this->loadData('bookshop'); $conn = $this->getConn(); $book = Book::selectOne($this->getConn())->where('id', '=', 2)->execute(); $book->{$update_method}(); $this->assertSame('update-The second book', $book->description); Book::resetEventCallbacks(); }
public function testCountLimitNotReached() { $this->loadData('bookshop'); $count = Book::select($this->getConn())->count()->where('id', '>', 43)->limit(20)->execute(); $this->assertSame(7, $count); }
/** * @dataProvider insertMethodProvider() */ public function testInsertCallsInsertEvent($insert_method) { Book::addEventCallBack('insert', function ($book) { $book->description = 'description-' . $book->description; }); $conn = $this->getConn(); for ($i = 1; $i < 4; $i++) { $book = new Book($conn, ['name' => 'foo', 'description' => $i, 'authors_id' => 0]); $book->{$insert_method}(); } $books = Book::select($conn)->execute(); $expected = ['description-1', 'description-2', 'description-3']; $this->assertSame($expected, $books->getColumn('description')); Book::resetEventCallbacks(); }
public function testWhereInEmpty() { $this->loadData('bookshop'); $books = Book::select($this->getConn())->whereIn('id', [])->execute(); $this->assertInstanceOf('ActiveDoctrine\\Entity\\EntityCollection', $books); $this->assertSame(0, count($books)); }
public function testEventCallbacksAreClassSpecific() { Book::resetEventCallbacks(); Author::resetEventCallbacks(); $foo = function () { }; $bar = function () { }; Book::addEventCallBack('insert', $foo); Author::addEventCallBack('update', $bar); $callbacks = new \ReflectionProperty('ActiveDoctrine\\Entity\\Entity', 'callbacks'); $callbacks->setAccessible(true); $this->assertSame(['insert' => [$foo]], $callbacks->getValue()['ActiveDoctrine\\Tests\\Fixtures\\Bookshop\\Book']); $this->assertSame(['update' => [$bar]], $callbacks->getValue()['ActiveDoctrine\\Tests\\Fixtures\\Bookshop\\Author']); }