1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Tests\Functional\Connection;
use Doctrine\DBAL\Exception\NoKeyValue;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Tests\TestUtil;
use function iterator_to_array;
class FetchTest extends FunctionalTestCase
{
private string $query;
public function setUp(): void
{
$this->query = TestUtil::generateResultSetQuery(['a', 'b'], [
['foo', 1],
['bar', 2],
['baz', 3],
], $this->connection->getDatabasePlatform());
}
public function testFetchNumeric(): void
{
self::assertEquals(['foo', 1], $this->connection->fetchNumeric($this->query));
}
public function testFetchOne(): void
{
self::assertEquals('foo', $this->connection->fetchOne($this->query));
}
public function testFetchAssociative(): void
{
self::assertEquals([
'a' => 'foo',
'b' => 1,
], $this->connection->fetchAssociative($this->query));
}
public function testFetchAllNumeric(): void
{
self::assertEquals([
['foo', 1],
['bar', 2],
['baz', 3],
], $this->connection->fetchAllNumeric($this->query));
}
public function testFetchAllAssociative(): void
{
self::assertEquals([
[
'a' => 'foo',
'b' => 1,
],
[
'a' => 'bar',
'b' => 2,
],
[
'a' => 'baz',
'b' => 3,
],
], $this->connection->fetchAllAssociative($this->query));
}
public function testFetchAllKeyValue(): void
{
self::assertEquals([
'foo' => 1,
'bar' => 2,
'baz' => 3,
], $this->connection->fetchAllKeyValue($this->query));
}
/**
* This test covers the requirement for the statement result to have at least two columns,
* not exactly two as PDO requires.
*/
public function testFetchAllKeyValueWithLimit(): void
{
$platform = $this->connection->getDatabasePlatform();
if ($platform instanceof SQLServerPlatform) {
self::markTestSkipped('See https://github.com/doctrine/dbal/issues/2374');
}
$query = $platform->modifyLimitQuery($this->query, 1, 1);
self::assertEquals(['bar' => 2], $this->connection->fetchAllKeyValue($query));
}
public function testFetchAllKeyValueOneColumn(): void
{
$sql = $this->connection->getDatabasePlatform()
->getDummySelectSQL();
$this->expectException(NoKeyValue::class);
$this->connection->fetchAllKeyValue($sql);
}
public function testFetchAllAssociativeIndexed(): void
{
self::assertEquals([
'foo' => ['b' => 1],
'bar' => ['b' => 2],
'baz' => ['b' => 3],
], $this->connection->fetchAllAssociativeIndexed($this->query));
}
public function testFetchFirstColumn(): void
{
self::assertEquals([
'foo',
'bar',
'baz',
], $this->connection->fetchFirstColumn($this->query));
}
public function testIterateNumeric(): void
{
self::assertEquals([
['foo', 1],
['bar', 2],
['baz', 3],
], iterator_to_array($this->connection->iterateNumeric($this->query)));
}
public function testIterateAssociative(): void
{
self::assertEquals([
[
'a' => 'foo',
'b' => 1,
],
[
'a' => 'bar',
'b' => 2,
],
[
'a' => 'baz',
'b' => 3,
],
], iterator_to_array($this->connection->iterateAssociative($this->query)));
}
public function testIterateKeyValue(): void
{
self::assertEquals([
'foo' => 1,
'bar' => 2,
'baz' => 3,
], iterator_to_array($this->connection->iterateKeyValue($this->query)));
}
public function testIterateKeyValueOneColumn(): void
{
$sql = $this->connection->getDatabasePlatform()
->getDummySelectSQL();
$this->expectException(NoKeyValue::class);
iterator_to_array($this->connection->iterateKeyValue($sql));
}
public function testIterateAssociativeIndexed(): void
{
self::assertEquals([
'foo' => ['b' => 1],
'bar' => ['b' => 2],
'baz' => ['b' => 3],
], iterator_to_array($this->connection->iterateAssociativeIndexed($this->query)));
}
public function testIterateColumn(): void
{
self::assertEquals([
'foo',
'bar',
'baz',
], iterator_to_array($this->connection->iterateColumn($this->query)));
}
}
|