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
|
<?php
namespace Doctrine\DBAL\Tests\Functional;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use LogicException;
use function array_change_key_case;
use function array_map;
use const CASE_LOWER;
class LegacyAPITest extends FunctionalTestCase
{
protected function setUp(): void
{
$table = new Table('legacy_table');
$table->addColumn('test_int', 'integer');
$table->addColumn('test_string', 'string');
$table->setPrimaryKey(['test_int']);
$this->dropAndCreateTable($table);
$this->connection->insert('legacy_table', [
'test_int' => 1,
'test_string' => 'foo',
]);
}
protected function tearDown(): void
{
$this->connection->executeStatement('DELETE FROM legacy_table WHERE test_int > 1');
}
public function testFetchWithAssociativeMode(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$row = array_change_key_case($stmt->fetch(FetchMode::ASSOCIATIVE), CASE_LOWER);
self::assertEquals(1, $row['test_int']);
}
public function testFetchWithNumericMode(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$row = $stmt->fetch(FetchMode::NUMERIC);
self::assertEquals(1, $row[0]);
}
public function testFetchWithColumnMode(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$row = $stmt->fetch(FetchMode::COLUMN);
self::assertEquals(1, $row);
}
public function testFetchWithTooManyArguments(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$this->expectException(LogicException::class);
$stmt->fetch(FetchMode::COLUMN, 2);
}
public function testFetchWithUnsupportedFetchMode(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$this->expectException(LogicException::class);
$stmt->fetch(1);
}
public function testFetchAllWithAssociativeModes(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$rows = $stmt->fetchAll(FetchMode::ASSOCIATIVE);
$rows = array_map(static function ($row) {
return array_change_key_case($row, CASE_LOWER);
}, $rows);
self::assertEquals([['test_int' => 1]], $rows);
}
public function testFetchAllWithNumericModes(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$rows = $stmt->fetchAll(FetchMode::NUMERIC);
self::assertEquals([[0 => 1]], $rows);
}
public function testFetchAllWithColumnMode(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$rows = $stmt->fetchAll(FetchMode::COLUMN);
self::assertEquals([1], $rows);
}
public function testFetchAllWithTooManyArguments(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$this->expectException(LogicException::class);
$stmt->fetchAll(FetchMode::COLUMN, 2);
}
public function testFetchAllWithUnsupportedFetchMode(): void
{
$sql = 'SELECT test_int FROM legacy_table WHERE test_int = 1';
$stmt = $this->connection->executeQuery($sql);
$this->expectException(LogicException::class);
$stmt->fetchAll(1);
}
public function testExecuteUpdate(): void
{
$this->connection->executeUpdate(
'INSERT INTO legacy_table (test_int, test_string) VALUES (?, ?)',
[2, 'bar'],
['integer', 'string'],
);
$sql = 'SELECT test_string FROM legacy_table';
$stmt = $this->connection->executeQuery($sql);
$rows = $stmt->fetchAll(FetchMode::COLUMN);
self::assertEquals(['foo', 'bar'], $rows);
}
public function testQuery(): void
{
$stmt = $this->connection->query('SELECT test_string FROM legacy_table WHERE test_int = 1');
$this->assertEquals('foo', $stmt->fetchOne());
}
public function testExec(): void
{
$this->connection->insert('legacy_table', [
'test_int' => 2,
'test_string' => 'bar',
]);
$count = $this->connection->exec('DELETE FROM legacy_table WHERE test_int > 1');
$this->assertEquals(1, $count);
}
}
|