-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBasePostgresTestCase.php
More file actions
186 lines (159 loc) · 5.92 KB
/
Copy pathBasePostgresTestCase.php
File metadata and controls
186 lines (159 loc) · 5.92 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
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
<?php
declare(strict_types=1);
namespace Tests\PHPCensor;
use Phinx\Config\Config as PhinxConfig;
use Phinx\Console\Command\Migrate;
use PHPCensor\ArrayConfiguration;
use PHPCensor\Common\Application\ConfigurationInterface;
use PHPCensor\DatabaseManager;
use PHPCensor\StoreRegistry;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
class BasePostgresTestCase extends TestCase
{
protected ?\PDO $connection = null;
protected ConfigurationInterface $configuration;
protected DatabaseManager $databaseManager;
protected StoreRegistry $storeRegistry;
protected function generatePhinxConfig(): PhinxConfig
{
$phinxSettings = [
'paths' => [
'migrations' => ROOT_DIR . 'src/Migrations',
],
'environments' => [
'default_migration_table' => 'migrations',
'default_database' => 'php-censor',
'php-censor' => [
'adapter' => 'pgsql',
'host' => '127.0.0.1',
'name' => env('POSTGRESQL_DBNAME'),
'user' => env('POSTGRESQL_USER'),
'pass' => env('POSTGRESQL_PASSWORD'),
],
],
];
return new PhinxConfig($phinxSettings);
}
protected function migrateDatabaseScheme(): void
{
try {
(new Migrate())
->setConfig($this->generatePhinxConfig())
->setName('php-censor-migrations:migrate')
->run(new ArgvInput([]), new ConsoleOutput(OutputInterface::VERBOSITY_QUIET));
} catch (\Throwable $e) {
if (!env('SKIP_DB_TESTS')) {
throw $e;
}
}
}
protected function getTestData(): array
{
return [];
}
protected function migrateDatabaseData(): void
{
$testData = $this->getTestData();
foreach ($testData as $table => $data) {
$fieldNames = \array_keys($data[0]);
foreach ($fieldNames as &$fieldName) {
$fieldName = \sprintf('"%s"', $fieldName);
}
unset($fieldName);
$fieldsString = \implode(',', $fieldNames);
$recordStrings = [];
foreach ($data as $record) {
foreach ($record as &$fieldValue) {
if (\is_string($fieldValue)) {
$fieldValue = \sprintf("'%s'", $fieldValue);
}
}
unset($fieldValue);
$recordStrings[] = '(' . \implode(',', $record) . ')';
}
$recordsStrings = \implode(',', $recordStrings);
$query = \sprintf('INSERT INTO "%s" (%s) VALUES %s', $table, $fieldsString, $recordsStrings);
$this->connection->exec($query);
}
}
protected function dropTables(): void
{
$this->connection->exec('DROP TABLE IF EXISTS "migrations"');
$this->connection->exec('DROP TABLE IF EXISTS "webhook_requests"');
$this->connection->exec('DROP TABLE IF EXISTS "build_errors"');
$this->connection->exec('DROP TABLE IF EXISTS "build_metas"');
$this->connection->exec('DROP TABLE IF EXISTS "builds"');
$this->connection->exec('DROP TABLE IF EXISTS "environments"');
$this->connection->exec('DROP TABLE IF EXISTS "projects"');
$this->connection->exec('DROP TABLE IF EXISTS "project_groups"');
$this->connection->exec('DROP TABLE IF EXISTS "secrets"');
$this->connection->exec('DROP TABLE IF EXISTS "users"');
}
protected function generateAppConfiguration(): ConfigurationInterface
{
$configurationArray = [
'php-censor' => [
'database' => [
'servers' => [
'read' => [
['host' => '127.0.0.1'],
],
'write' => [
['host' => '127.0.0.1'],
],
],
'type' => 'pgsql',
'name' => env('POSTGRESQL_DBNAME'),
'username' => env('POSTGRESQL_USER'),
'password' => env('POSTGRESQL_PASSWORD'),
],
],
];
return new ArrayConfiguration($configurationArray);
}
protected function setUp(): void
{
parent::setUp();
try {
$this->connection = new \PDO(
'pgsql:host=127.0.0.1;dbname=' . env('POSTGRESQL_DBNAME'),
env('POSTGRESQL_USER'),
env('POSTGRESQL_PASSWORD')
);
$this->dropTables();
$this->migrateDatabaseScheme();
$this->migrateDatabaseData();
} catch (\Throwable $e) {
if (!env('SKIP_DB_TESTS')) {
throw $e;
}
$this->connection = null;
}
$this->getConnection();
$this->configuration = $this->generateAppConfiguration();
$this->databaseManager = new DatabaseManager($this->configuration);
$this->storeRegistry = new StoreRegistry($this->databaseManager);
}
protected function tearDown(): void
{
parent::tearDown();
if (null !== $this->connection) {
$this->dropTables();
$this->connection = null;
}
}
protected function getConnection(): ?\PDO
{
if (null === $this->connection) {
if (env('SKIP_DB_TESTS')) {
$this->markTestSkipped('Test skipped because PostgreSQL database/user/extension doesn\'t exist.');
} else {
$this->fail('Test failed because PostgreSQL database/user/extension doesn\'t exist.');
}
}
return $this->connection;
}
}