| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | require_once 'tools/helpers/bookstore/BookstoreEmptyTestBase.php'; |
|---|
| 4 | |
|---|
| 5 | /** |
|---|
| 6 | * Test class for PropelArrayFormatter. |
|---|
| 7 | * |
|---|
| 8 | * @author Francois Zaninotto |
|---|
| 9 | * @version $Id$ |
|---|
| 10 | * @package runtime.formatter |
|---|
| 11 | */ |
|---|
| 12 | class PropelArrayFormatterTest extends BookstoreEmptyTestBase |
|---|
| 13 | { |
|---|
| 14 | protected function setUp() |
|---|
| 15 | { |
|---|
| 16 | parent::setUp(); |
|---|
| 17 | BookstoreDataPopulator::populate(); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | public function testFormatNoCriteria() |
|---|
| 21 | { |
|---|
| 22 | $con = Propel::getConnection(BookPeer::DATABASE_NAME); |
|---|
| 23 | |
|---|
| 24 | $stmt = $con->query('SELECT * FROM book'); |
|---|
| 25 | $formatter = new PropelArrayFormatter(); |
|---|
| 26 | try { |
|---|
| 27 | $books = $formatter->format($stmt); |
|---|
| 28 | $this->fail('PropelArrayFormatter::format() throws an exception when called with no valid criteria'); |
|---|
| 29 | } catch (PropelException $e) { |
|---|
| 30 | $this->assertTrue(true,'PropelArrayFormatter::format() throws an exception when called with no valid criteria'); |
|---|
| 31 | } |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | public function testFormatManyResults() |
|---|
| 35 | { |
|---|
| 36 | $con = Propel::getConnection(BookPeer::DATABASE_NAME); |
|---|
| 37 | |
|---|
| 38 | $stmt = $con->query('SELECT * FROM book'); |
|---|
| 39 | $formatter = new PropelArrayFormatter(); |
|---|
| 40 | $formatter->setCriteria(new ModelCriteria('bookstore', 'Book')); |
|---|
| 41 | $books = $formatter->format($stmt); |
|---|
| 42 | |
|---|
| 43 | $this->assertTrue(is_array($books), 'PropelArrayFormatter::format() returns an array'); |
|---|
| 44 | $this->assertEquals(4, count($books), 'PropelArrayFormatter::format() returns as many rows as the results in the query'); |
|---|
| 45 | foreach ($books as $book) { |
|---|
| 46 | $this->assertTrue(is_array($book), 'PropelArrayFormatter::format() returns an array of arrays'); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public function testFormatOneResult() |
|---|
| 51 | { |
|---|
| 52 | $con = Propel::getConnection(BookPeer::DATABASE_NAME); |
|---|
| 53 | |
|---|
| 54 | $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "Quicksilver"'); |
|---|
| 55 | $formatter = new PropelArrayFormatter(); |
|---|
| 56 | $formatter->setCriteria(new ModelCriteria('bookstore', 'Book')); |
|---|
| 57 | $books = $formatter->format($stmt); |
|---|
| 58 | |
|---|
| 59 | $this->assertTrue(is_array($books), 'PropelArrayFormatter::format() returns an array'); |
|---|
| 60 | $this->assertEquals(1, count($books), 'PropelArrayFormatter::format() returns as many rows as the results in the query'); |
|---|
| 61 | $book = array_shift($books); |
|---|
| 62 | $this->assertTrue(is_array($book), 'PropelArrayFormatter::format() returns an array of arrays'); |
|---|
| 63 | $this->assertEquals('Quicksilver', $book['Title'], 'PropelArrayFormatter::format() returns the arrays matching the query'); |
|---|
| 64 | $expected = array('Id', 'Title', 'ISBN', 'Price', 'PublisherId', 'AuthorId'); |
|---|
| 65 | $this->assertEquals($expected, array_keys($book), 'PropelArrayFormatter::format() returns an associative array with column phpNames as keys'); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | public function testFormatNoResult() |
|---|
| 69 | { |
|---|
| 70 | $con = Propel::getConnection(BookPeer::DATABASE_NAME); |
|---|
| 71 | |
|---|
| 72 | $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); |
|---|
| 73 | $formatter = new PropelArrayFormatter(); |
|---|
| 74 | $formatter->setCriteria(new ModelCriteria('bookstore', 'Book')); |
|---|
| 75 | $books = $formatter->format($stmt); |
|---|
| 76 | |
|---|
| 77 | $this->assertTrue(is_array($books), 'PropelArrayFormatter::format() returns an array'); |
|---|
| 78 | $this->assertEquals(0, count($books), 'PropelArrayFormatter::format() returns as many rows as the results in the query'); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public function testFormatOneNoCriteria() |
|---|
| 82 | { |
|---|
| 83 | $con = Propel::getConnection(BookPeer::DATABASE_NAME); |
|---|
| 84 | |
|---|
| 85 | $stmt = $con->query('SELECT * FROM book'); |
|---|
| 86 | $formatter = new PropelArrayFormatter(); |
|---|
| 87 | try { |
|---|
| 88 | $book = $formatter->formatOne($stmt); |
|---|
| 89 | $this->fail('PropelArrayFormatter::formatOne() throws an exception when called with no valid criteria'); |
|---|
| 90 | } catch (PropelException $e) { |
|---|
| 91 | $this->assertTrue(true,'PropelArrayFormatter::formatOne() throws an exception when called with no valid criteria'); |
|---|
| 92 | } |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | public function testFormatOneManyResults() |
|---|
| 96 | { |
|---|
| 97 | $con = Propel::getConnection(BookPeer::DATABASE_NAME); |
|---|
| 98 | |
|---|
| 99 | $stmt = $con->query('SELECT * FROM book'); |
|---|
| 100 | $formatter = new PropelArrayFormatter(); |
|---|
| 101 | $formatter->setCriteria(new ModelCriteria('bookstore', 'Book')); |
|---|
| 102 | $book = $formatter->formatOne($stmt); |
|---|
| 103 | |
|---|
| 104 | $this->assertTrue(is_array($book), 'PropelArrayFormatter::formatOne() returns an array'); |
|---|
| 105 | $this->assertEquals(array('Id', 'Title', 'ISBN', 'Price', 'PublisherId', 'AuthorId'), array_keys($book), 'PropelArrayFormatter::formatOne() returns a single row even if the query has many results'); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | public function testFormatOneNoResult() |
|---|
| 109 | { |
|---|
| 110 | $con = Propel::getConnection(BookPeer::DATABASE_NAME); |
|---|
| 111 | |
|---|
| 112 | $stmt = $con->query('SELECT * FROM book WHERE book.TITLE = "foo"'); |
|---|
| 113 | $formatter = new PropelArrayFormatter(); |
|---|
| 114 | $formatter->setCriteria(new ModelCriteria('bookstore', 'Book')); |
|---|
| 115 | $book = $formatter->formatOne($stmt); |
|---|
| 116 | |
|---|
| 117 | $this->assertNull($book, 'PropelArrayFormatter::formatOne() returns null when no result'); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | } |
|---|