-
-
Notifications
You must be signed in to change notification settings - Fork 393
Expand file tree
/
Copy pathExistingTableMetadataStorageTest.php
More file actions
130 lines (105 loc) · 4.34 KB
/
ExistingTableMetadataStorageTest.php
File metadata and controls
130 lines (105 loc) · 4.34 KB
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
<?php
declare(strict_types=1);
namespace Doctrine\Migrations\Tests\Metadata\Storage;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\FilesystemMigrationsRepository;
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\MigrationsRepository;
use Doctrine\Migrations\Tests\Helper;
use Doctrine\Migrations\Version\AlphabeticalComparator;
use Doctrine\Migrations\Version\MigrationFactory;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\TestCase;
use function sprintf;
class ExistingTableMetadataStorageTest extends TestCase
{
/** @var Connection */
private $connection;
/** @var TableMetadataStorage */
private $storage;
/** @var TableMetadataStorageConfiguration */
private $config;
/** @var AbstractSchemaManager */
private $schemaManager;
/** @var MigrationsRepository */
private $migrationRepository;
private function getSqliteConnection(): Connection
{
$params = ['driver' => 'pdo_sqlite', 'memory' => true];
return DriverManager::getConnection($params);
}
public function setUp(): void
{
$this->connection = $this->getSqliteConnection();
$this->schemaManager = $this->connection->getSchemaManager();
$migration = $this->createMock(AbstractMigration::class);
$versionFactory = $this->createMock(MigrationFactory::class);
$this->migrationRepository = new FilesystemMigrationsRepository(
[],
[],
new RecursiveRegexFinder('#.*\\.php$#i'),
$versionFactory
);
Helper::registerMigrationInstance($this->migrationRepository, new Version('Foo\\5678'), $migration);
$this->config = new TableMetadataStorageConfiguration();
$this->storage = new TableMetadataStorage(
$this->connection,
new AlphabeticalComparator(),
$this->config,
$this->migrationRepository
);
// create partial table
$table = new Table($this->config->getTableName());
$table->addColumn($this->config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 24]);
$table->setPrimaryKey([$this->config->getVersionColumnName()]);
$this->schemaManager->createTable($table);
}
public function testPrimaryReadReplicaConnectionGetsConnected(): void
{
$connection = $this->createMock(PrimaryReadReplicaConnection::class);
$connection
->expects(self::atLeastOnce())
->method('ensureConnectedToPrimary');
$connection
->expects(self::atLeastOnce())
->method('getSchemaManager')
->willReturn($this->connection->getSchemaManager());
$connection
->expects(self::atLeastOnce())
->method('getDatabasePlatform')
->willReturn($this->connection->getDatabasePlatform());
$storage = new TableMetadataStorage($connection, new AlphabeticalComparator(), $this->config);
$storage->ensureInitialized();
}
public function testMigratedVersionUpdate(): void
{
$this->connection->insert($this->config->getTableName(), [$this->config->getVersionColumnName() => '1234']);
$this->connection->insert($this->config->getTableName(), [$this->config->getVersionColumnName() => '5678']);
$this->storage->ensureInitialized();
$rows = $this->connection->fetchAllAssociative(sprintf(
'SELECT * FROM %s ORDER BY %s ASC',
$this->config->getTableName(),
$this->config->getVersionColumnName()
));
self::assertCount(2, $rows);
self::assertSame([
'version' => '1234',
'executed_at' => null,
'execution_time' => null,
'reason' => 'execute',
], $rows[0]);
self::assertSame([
'version' => 'Foo\\5678',
'executed_at' => null,
'execution_time' => null,
'reason' => 'execute',
], $rows[1]);
}
}