DbalReaderTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Ddeboer\DataImport\Tests\Reader;
  3. use Ddeboer\DataImport\Reader\DbalReader;
  4. use Doctrine\DBAL\Configuration;
  5. use Doctrine\DBAL\Connection;
  6. use Doctrine\DBAL\DriverManager;
  7. use Doctrine\DBAL\Platforms\SqlitePlatform;
  8. use Doctrine\DBAL\Schema\Schema;
  9. class DbalReaderTest extends \PHPUnit_Framework_TestCase
  10. {
  11. public function testCalculateRowCount()
  12. {
  13. $reader = $this->getReader();
  14. $reader->setRowCountCalculated();
  15. $this->assertTrue($reader->isRowCountCalculated());
  16. $reader->setRowCountCalculated(false);
  17. $this->assertFalse($reader->isRowCountCalculated());
  18. $reader->setRowCountCalculated(true);
  19. $this->assertTrue($reader->isRowCountCalculated());
  20. }
  21. public function testGetFields()
  22. {
  23. $fields = $this->getReader()->getFields();
  24. $this->assertInternalType('array', $fields);
  25. $this->assertEquals(array('id', 'username', 'name'), $fields);
  26. }
  27. public function testCount()
  28. {
  29. $this->assertEquals(10, $this->getReader()->count());
  30. }
  31. public function testCountInhibited()
  32. {
  33. $reader = $this->getReader();
  34. $reader->setRowCountCalculated(false);
  35. $this->assertEquals(null, $reader->count());
  36. }
  37. public function testSqlAndParamsAreMutable()
  38. {
  39. $reader = $this->getReader();
  40. $reader->setSql('SELECT * FROM groups WHERE id = :id', array('id' => 2));
  41. $this->assertAttributeEquals('SELECT * FROM groups WHERE id = :id', 'sql', $reader);
  42. $this->assertAttributeEquals(array('id' => 2), 'params', $reader);
  43. }
  44. public function testChangeSqlOrParamsClearsNumRowsAndStatement()
  45. {
  46. $reader = $this->getReader();
  47. $reader->count();
  48. $reader->getFields();
  49. $this->assertAttributeNotEmpty('rowCount', $reader);
  50. $this->assertAttributeNotEmpty('stmt', $reader);
  51. $reader->setSql('SELECT * FROM `user` WHERE id IN (:id)', array('id' => array()));
  52. $this->assertAttributeEmpty('rowCount', $reader);
  53. $this->assertAttributeEmpty('stmt', $reader);
  54. }
  55. public function testIterate()
  56. {
  57. $i=31;
  58. foreach ($this->getReader() as $key => $row) {
  59. $this->assertInternalType('array', $row);
  60. $this->assertEquals('user-'.$i, $row['username']);
  61. $this->assertEquals($i - 31, $key);
  62. $i++;
  63. }
  64. $this->assertEquals(41, $i);
  65. }
  66. public function testReaderRewindWorksCorrectly()
  67. {
  68. $reader = $this->getReader();
  69. foreach ($reader as $row) {
  70. if (!isset($row['username'])) {
  71. $this->fail('There should be a username');
  72. }
  73. if ($row['username'] == 'user-35') {
  74. break;
  75. }
  76. }
  77. $reader->rewind();
  78. $this->assertEquals(array(
  79. 'id' => 31,
  80. 'username' => 'user-31',
  81. 'name' => 'name 4',
  82. ), $reader->current());
  83. }
  84. public function testCallingCurrentTwiceShouldNotAdvance()
  85. {
  86. $reader = $this->getReader();
  87. $expected = array(
  88. 'id' => 31,
  89. 'username' => 'user-31',
  90. 'name' => 'name 4',
  91. );
  92. $this->assertEquals($expected, $reader->current());
  93. $this->assertEquals($expected, $reader->current());
  94. }
  95. public function testEmptyResultDoesNotThrowException()
  96. {
  97. $reader = $this->getReader();
  98. $reader->setSql(null, array('name' => 'unknown group'));
  99. $this->assertInternalType('array', $reader->getFields());
  100. }
  101. public function testCallValidRewindsIfNeeded()
  102. {
  103. $reader = $this->getReader();
  104. $this->assertTrue($reader->valid());
  105. $this->assertAttributeInternalType('array', 'data', $reader);
  106. }
  107. public function getConnection()
  108. {
  109. $params = array(
  110. 'driver' => 'pdo_sqlite',
  111. 'memory' => true,
  112. );
  113. $connection = DriverManager::getConnection($params, new Configuration());
  114. $schema = new Schema();
  115. $table = $schema->createTable('groups');
  116. $table->addColumn('id', 'integer', ['autoincrement' => true]);
  117. $table->addColumn('name', 'string', array('length' => 45));
  118. $table->setPrimaryKey(array('id'));
  119. $myTable = $schema->createTable('user');
  120. $myTable->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
  121. $myTable->addColumn('username', 'string', array('length' => 32));
  122. $myTable->addColumn('group_id', 'integer');
  123. $myTable->setPrimaryKey(array('id'));
  124. $myTable->addUniqueIndex(array('username'));
  125. $myTable->addForeignKeyConstraint($table, array('group_id'), array('id'));
  126. foreach ($schema->toSql(new SqlitePlatform()) as $query) {
  127. $connection->query($query);
  128. };
  129. return $connection;
  130. }
  131. protected function getReader()
  132. {
  133. $connection = $this->getConnection();
  134. $this->loadFixtures($connection);
  135. return new DbalReader($connection, implode(' ', array(
  136. 'SELECT u.id, u.username, g.name',
  137. 'FROM `user` u INNER JOIN groups g ON u.group_id = g.id',
  138. 'WHERE g.name LIKE :name',
  139. )), array(
  140. 'name' => 'name 4',
  141. ));
  142. }
  143. /**
  144. * @param Connection $connection
  145. */
  146. protected function loadFixtures($connection)
  147. {
  148. $counter = 1;
  149. for ($i = 1; $i <= 10; $i++) {
  150. $connection->insert('groups', array('name' => "name {$i}"));
  151. $id = $connection->lastInsertId();
  152. for ($j = 1; $j <= 10; $j++) {
  153. $connection->insert('user', array(
  154. 'username' => "user-{$counter}",
  155. 'group_id' => $id,
  156. ));
  157. $counter++;
  158. }
  159. }
  160. }
  161. }