Passed
Pull Request — main (#502)
by Johan
05:11 queued 03:08
created

DatabaseSchemaService::createSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
/**
4
 * Copyright 2025 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\Behat\Service;
20
21
use PDO;
22
use Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupGateway\Be...ect\SmoketestPdoFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
/**
25
 * Because Gateway does not manage its own schema, it relies on the already created schema.
26
 * This service copies the original schema to the database used for smoketests and functional tests.
27
 */
28
class DatabaseSchemaService
29
{
30
    private PDO $testConnection;
31
    private PDO $sourceConnection;
32
33
    public function __construct(
34
        SmoketestPdoFactory $testDbConfig,
35
    ) {
36
        // Test database (gateway_test): Get the main PDO connection
37
        // This is where we copy the schema TO
38
        $this->testConnection = $testDbConfig->createConnection();
39
40
        // Source database (gateway): Create a new connection using the same credentials
41
        // but with a different database name
42
        // This is where we copy the schema FROM (managed by external service)
43
        $this->sourceConnection = $testDbConfig->createSourceConnection();
44
    }
45
46
    public function dropSchema(): void
47
    {
48
        $this->testConnection->exec('SET FOREIGN_KEY_CHECKS = 0');
49
50
        $stmt = $this->testConnection->query('SHOW TABLES');
51
        $tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
52
53
        foreach ($tables as $table) {
54
            $this->testConnection->exec("DROP TABLE IF EXISTS `$table`");
55
        }
56
57
        $this->testConnection->exec('SET FOREIGN_KEY_CHECKS = 1');
58
    }
59
60
    public function createSchema(): void
61
    {
62
        $stmt = $this->sourceConnection->query('SHOW TABLES');
63
        $tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
64
65
        $this->testConnection->exec('SET FOREIGN_KEY_CHECKS = 0');
66
67
        foreach ($tables as $table) {
68
            $stmt = $this->sourceConnection->query("SHOW CREATE TABLE `$table`");
69
            $createTableRow = $stmt->fetch(PDO::FETCH_ASSOC);
70
            $createTableSql = $createTableRow['Create Table'];
71
72
            $this->testConnection->exec($createTableSql);
73
        }
74
75
        $this->testConnection->exec('SET FOREIGN_KEY_CHECKS = 1');
76
    }
77
78
    public function resetSchema(): void
79
    {
80
        echo "Preparing test schemas\n";
81
        $this->dropSchema();
82
        $this->createSchema();
83
    }
84
}
85
86