1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
6
|
|
|
use Doctrine\DBAL\Schema\Table; |
7
|
|
|
use Doctrine\Tests\DbalFunctionalTestCase; |
8
|
|
|
use const CASE_LOWER; |
9
|
|
|
use function array_change_key_case; |
10
|
|
|
|
11
|
|
|
class ResetAutoincrementTest extends DbalFunctionalTestCase |
12
|
|
|
{ |
13
|
|
|
public function testAutoincrementResetsOnTruncate() |
14
|
|
|
{ |
15
|
|
|
$table = new Table('autoincremented_table'); |
16
|
|
|
$table->addColumn('id', 'integer', ['autoincrement' => true]); |
17
|
|
|
$table->addColumn('test_int', 'integer'); |
18
|
|
|
$table->setPrimaryKey(['id']); |
19
|
|
|
|
20
|
|
|
/** @var AbstractSchemaManager $sm */ |
21
|
|
|
$sm = $this->connection->getSchemaManager(); |
22
|
|
|
$sm->createTable($table); |
23
|
|
|
|
24
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 1]); |
25
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 2]); |
26
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 3]); |
27
|
|
|
|
28
|
|
|
$lastId = $this->getIdByTestInt(3); |
29
|
|
|
|
30
|
|
|
$this->assertEquals(3, $lastId); |
31
|
|
|
|
32
|
|
|
$this->connection->exec($this->connection->getDatabasePlatform()->getTruncateTableSQL('autoincremented_table')); |
33
|
|
|
|
34
|
|
|
$this->connection->insert('autoincremented_table', ['test_int' => 4]); |
35
|
|
|
$lastId = $this->getIdByTestInt(4); |
36
|
|
|
$this->assertEquals(1, $lastId); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function getIdByTestInt(int $whereTestInt) |
40
|
|
|
{ |
41
|
|
|
$row = $this->connection->fetchAssoc('SELECT id FROM autoincremented_table WHERE test_int = ?', [$whereTestInt]); |
42
|
|
|
$row = array_change_key_case($row, CASE_LOWER); |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $row['id']; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|