public function testSelectWithFieldMappings() { $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'; $books = Book::selectSQL($this->getConn(), $query, [], ['i' => 'id', 'n' => 'name', 'd' => 'description', 'a' => 'authors_id']); $this->assertSame(50, count($books)); $book = $books[0]; $expectedValues = ['id' => '1', 'name' => 'Book 1', 'description' => 'The very first book', 'authors_id' => '1']; $this->assertSame($expectedValues, $book->getValues()); }
public function testSelectSQL() { $conn = $this->getConn(); $this->loadData('bookshop'); $sql = AbstractSelector::fromConnection($conn, 'books')->getSQL(); $books = Book::selectSQL($this->getConn(), $sql); $this->assertInstanceOf('ActiveDoctrine\\Entity\\EntityCollection', $books); $this->assertSame(50, count($books)); $book = $books[0]; $this->assertInstanceOf('ActiveDoctrine\\Tests\\Fixtures\\Bookshop\\Book', $book); $this->assertSame('Book 1', $book->name); $this->assertSame('The very first book', $book->description); $this->assertSame('1', $book->authors_id); }
public function testSelectSQL() { $statement = $this->getMockBuilder('Doctrine\\DBAL\\Statement')->disableOriginalConstructor()->getMock(); $sql = 'select * from books'; $this->conn->expects($this->once())->method('prepare')->with($sql)->will($this->returnValue($statement)); for ($i = 1; $i < 4; $i++) { ${'result' . $i} = ['name' => "name{$i}", 'description' => "description{$i}"]; } $statement->expects($this->exactly(4))->method('fetch')->with()->will($this->onConsecutiveCalls($result1, $result2, $result3, false)); $collection = Book::selectSQL($this->conn, $sql); $this->assertSame(3, count($collection)); for ($i = 1; $i < 4; $i++) { $book = $collection[$i - 1]; $this->assertSame("name{$i}", $book->getRaw('name')); $this->assertSame("description{$i}", $book->getRaw('description')); $this->assertTrue($book->isStored()); } }