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