|
3 | 3 |
|
4 | 4 | use Kir\FakePDO\EventHandlers\RegistryEventHandler;
|
5 | 5 | use Kir\FakePDO\FakePDO;
|
| 6 | +use Kir\MySQL\Builder\SelectTest\TestSelect; |
6 | 7 |
|
7 | 8 | class MySQLTest extends \PHPUnit_Framework_TestCase {
|
8 | 9 | /** @var TestDB */
|
@@ -117,4 +118,89 @@ public function testOuterNestedTransaction() {
|
117 | 118 | $mysql->transactionRollback();
|
118 | 119 | $mysql->transactionRollback();
|
119 | 120 | }
|
| 121 | + |
| 122 | + public function testFetchRow() { |
| 123 | + // Closure w/o return, but with reference |
| 124 | + $row = TestSelect::create() |
| 125 | + ->field('t.id') |
| 126 | + ->from('t', 'test#test1') |
| 127 | + ->where('t.id=?', 1) |
| 128 | + ->fetchRow(function (array &$row) { |
| 129 | + $row['test'] = 10; |
| 130 | + }); |
| 131 | + $this->assertEquals(['id' => 1, 'test' => 10], $row); |
| 132 | + |
| 133 | + // Closure with return |
| 134 | + $row = TestSelect::create() |
| 135 | + ->field('t.id') |
| 136 | + ->from('t', 'test#test1') |
| 137 | + ->where('t.id=?', 1) |
| 138 | + ->fetchRow(function (array $row) { |
| 139 | + $row['test'] = 10; |
| 140 | + return $row; |
| 141 | + }); |
| 142 | + $this->assertEquals(['id' => 1, 'test' => 10], $row); |
| 143 | + } |
| 144 | + |
| 145 | + public function testFetchRows() { |
| 146 | + // Closure w/o return, but with reference |
| 147 | + $rows = TestSelect::create() |
| 148 | + ->field('t.id') |
| 149 | + ->from('t', 'test#test1') |
| 150 | + ->where('t.id=?', 1) |
| 151 | + ->fetchRows(function (array &$row) { |
| 152 | + $row['test'] = 10; |
| 153 | + }); |
| 154 | + |
| 155 | + $this->assertEquals([['id' => 1, 'test' => 10]], $rows); |
| 156 | + |
| 157 | + // Closure with return |
| 158 | + $rows = TestSelect::create() |
| 159 | + ->field('t.id') |
| 160 | + ->from('t', 'test#test1') |
| 161 | + ->where('t.id=?', 1) |
| 162 | + ->fetchRows(function (array $row) { |
| 163 | + $row['test'] = 10; |
| 164 | + return $row; |
| 165 | + }); |
| 166 | + |
| 167 | + $this->assertEquals([['id' => 1, 'test' => 10]], $rows); |
| 168 | + } |
| 169 | + |
| 170 | + public function testFetchRowsLazy() { |
| 171 | + // Closure w/o return, but with reference |
| 172 | + $rows = TestSelect::create() |
| 173 | + ->field('t.id') |
| 174 | + ->from('t', 'test#test1') |
| 175 | + ->where('t.id=?', 1) |
| 176 | + ->fetchRowsLazy(function (array &$row) { |
| 177 | + $row['test'] = 10; |
| 178 | + }); |
| 179 | + $rows = iterator_to_array($rows); |
| 180 | + $this->assertEquals([['id' => 1, 'test' => 10]], $rows); |
| 181 | + |
| 182 | + // Closure with return |
| 183 | + $rows = TestSelect::create() |
| 184 | + ->field('t.id') |
| 185 | + ->from('t', 'test#test1') |
| 186 | + ->where('t.id=?', 1) |
| 187 | + ->fetchRowsLazy(function (array $row) { |
| 188 | + $row['test'] = 10; |
| 189 | + return $row; |
| 190 | + }); |
| 191 | + $rows = iterator_to_array($rows); |
| 192 | + $this->assertEquals([['id' => 1, 'test' => 10]], $rows); |
| 193 | + |
| 194 | + // IgnoredRow |
| 195 | + $rows = TestSelect::create() |
| 196 | + ->field('t.id') |
| 197 | + ->from('t', 'test#test1') |
| 198 | + ->where('t.id=?', 1) |
| 199 | + ->fetchRowsLazy(function (array $row) { |
| 200 | + $row['test'] = 10; |
| 201 | + return $row; |
| 202 | + }); |
| 203 | + $rows = iterator_to_array($rows); |
| 204 | + $this->assertEquals([['id' => 1, 'test' => 10]], $rows); |
| 205 | + } |
120 | 206 | }
|
0 commit comments