Skip to content

Commit e04a738

Browse files
committed
Remove deploy connection from doctrine dbal
Prior to this change, doctrine still had a deploy connection, just because the gateway_test schema needed to be created. All other write interaction with the gateway_test was performed via PDO. Also, the gateway_test schema would be created based on the current entity mapping. This is not semantically correct, as gateway does not determine its own schema. It relies on the schema that Middleware manages. This change removes the deploy connection from doctrine, and copies the schema supplied by middleware for the smoketests, ensuring more accurate tests. Resolves #402
1 parent e36a88e commit e04a738

File tree

10 files changed

+197
-96
lines changed

10 files changed

+197
-96
lines changed

composer.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/services_test.yaml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,34 @@ services:
77
gateway.service.gateway_api_sms:
88
class: Surfnet\StepupGateway\ApiBundle\Tests\TestDouble\Service\SmsService
99

10+
Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory:
11+
class: Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory
12+
arguments:
13+
- '%database_host%'
14+
- 'gw_deploy_user'
15+
- 'gw_deploy_secret'
16+
- 'gateway_test'
17+
- 'gateway'
18+
1019
Surfnet\StepupGateway\Behat\Repository\SecondFactorRepository:
1120
class: Surfnet\StepupGateway\Behat\Repository\SecondFactorRepository
1221
arguments:
13-
- '@Surfnet\StepupGateway\Behat\Repository\Connection'
22+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
1423

1524
Surfnet\StepupGateway\Behat\Repository\SamlEntityRepository:
1625
class: Surfnet\StepupGateway\Behat\Repository\SamlEntityRepository
1726
arguments:
18-
- '@Surfnet\StepupGateway\Behat\Repository\Connection'
27+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
1928

2029
Surfnet\StepupGateway\Behat\Repository\WhitelistRepository:
2130
class: Surfnet\StepupGateway\Behat\Repository\WhitelistRepository
2231
arguments:
23-
- '@Surfnet\StepupGateway\Behat\Repository\Connection'
32+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
2433

2534
Surfnet\StepupGateway\Behat\Repository\InstitutionConfigurationRepository:
2635
class: Surfnet\StepupGateway\Behat\Repository\InstitutionConfigurationRepository
2736
arguments:
28-
- '@Surfnet\StepupGateway\Behat\Repository\Connection'
29-
30-
Surfnet\StepupGateway\Behat\Repository\Connection:
31-
class: Surfnet\StepupGateway\Behat\Repository\Connection
32-
arguments:
33-
- '%env(APP_ENV)%'
34-
- 'gw_deploy_user'
35-
- 'gw_deploy_secret'
36-
- 'gateway_test'
37-
- '%database_host%'
38-
37+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
3938

4039
Surfnet\StepupGateway\Behat\Controller\ServiceProviderController:
4140
class: Surfnet\StepupGateway\Behat\Controller\ServiceProviderController
@@ -69,6 +68,12 @@ services:
6968
- "@logger"
7069
public: false
7170

71+
Surfnet\StepupGateway\Behat\Service\DatabaseSchemaService:
72+
class: Surfnet\StepupGateway\Behat\Service\DatabaseSchemaService
73+
public: true
74+
arguments:
75+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
76+
7277
Surfnet\StepupGateway\Behat\Service\FixtureService:
7378
class: Surfnet\StepupGateway\Behat\Service\FixtureService
7479
public: true
@@ -82,6 +87,7 @@ services:
8287
public: true
8388
arguments:
8489
$fixtureService: '@Surfnet\StepupGateway\Behat\Service\FixtureService'
90+
$databaseSchemaService: '@Surfnet\StepupGateway\Behat\Service\DatabaseSchemaService'
8591
$logger: '@logger'
8692

8793
Surfnet\StepupGateway\Behat\ServiceProviderContext:

tests/features/bootstrap/FeatureContext.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use RuntimeException;
2929
use SAML2\Compat\ContainerSingleton;
3030
use Surfnet\SamlBundle\Tests\TestSaml2Container;
31+
use Surfnet\StepupGateway\Behat\Service\DatabaseSchemaService;
3132
use Surfnet\StepupGateway\Behat\Service\FixtureService;
3233

3334
class FeatureContext implements Context
@@ -37,6 +38,11 @@ class FeatureContext implements Context
3738
*/
3839
private $fixtureService;
3940

41+
/**
42+
* @var DatabaseSchemaService
43+
*/
44+
private static $databaseSchemaService;
45+
4046
private $whitelistedInstitutions = [];
4147

4248
/**
@@ -66,9 +72,13 @@ class FeatureContext implements Context
6672
*/
6773
private $cookieDomain;
6874

69-
public function __construct(FixtureService $fixtureService, LoggerInterface $logger)
70-
{
75+
public function __construct(
76+
FixtureService $fixtureService,
77+
DatabaseSchemaService $databaseSchemaService,
78+
LoggerInterface $logger
79+
) {
7180
$this->fixtureService = $fixtureService;
81+
self::$databaseSchemaService = $databaseSchemaService;
7282
$this->sso2faCookieName = 'stepup-gateway_sso-on-second-factor-authentication';
7383
$this->sessCookieName = 'MOCKSESSID';
7484
$this->cookieDomain = '.gateway.dev.openconext.local';
@@ -80,10 +90,7 @@ public function __construct(FixtureService $fixtureService, LoggerInterface $log
8090
#[\Behat\Hook\BeforeFeature]
8191
public static function setupDatabase(BeforeFeatureScope $scope): void
8292
{
83-
// Generate test databases
84-
echo "Preparing test schemas\n";
85-
shell_exec("/var/www/html/bin/console doctrine:schema:drop --env=smoketest --force");
86-
shell_exec("/var/www/html/bin/console doctrine:schema:create --env=smoketest");
93+
self::$databaseSchemaService->resetSchema();
8794
}
8895

8996
#[\Behat\Hook\BeforeScenario]

tests/src/Repository/Connection.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

tests/src/Repository/InstitutionConfigurationRepository.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,20 @@
1919
namespace Surfnet\StepupGateway\Behat\Repository;
2020

2121
use Exception;
22+
use PDO;
23+
use Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
2224
use function error_get_last;
2325

2426
/**
2527
* A poor-mans repository, a pdo connection to the test database is established in the constructor
2628
*/
2729
class InstitutionConfigurationRepository
2830
{
31+
private readonly PDO $connection;
2932

30-
/**
31-
* @var Connection
32-
*/
33-
private $connection;
34-
35-
public function __construct(Connection $connection)
33+
public function __construct(SmoketestPdoFactory $factory)
3634
{
37-
$this->connection = $connection;
35+
$this->connection = $factory->createConnection();
3836
}
3937

4038
public function configure(string $institution, string $option, bool $value)

tests/src/Repository/SamlEntityRepository.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Exception;
2222
use PDO;
2323
use Ramsey\Uuid\Uuid;
24+
use Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
2425

2526
/**
2627
* A poor mans repository, a pdo connection to the test database is established in the constructor
@@ -31,14 +32,11 @@ class SamlEntityRepository
3132

3233
const SP_ADFS_SSO_LOCATION = 'https://gateway.dev.openconext.local/test/authentication/adfs/sso';
3334

34-
/**
35-
* @var Connection
36-
*/
37-
private $connection;
35+
private readonly PDO $connection;
3836

39-
public function __construct(Connection $connection)
37+
public function __construct(SmoketestPdoFactory $factory)
4038
{
41-
$this->connection = $connection;
39+
$this->connection = $factory->createConnection();
4240
}
4341

4442
public function createSpIfNotExists($entityId, $certificate, $sfoEnabled = false)

tests/src/Repository/SecondFactorRepository.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,18 @@
2222
use PDO;
2323
use Ramsey\Uuid\Uuid;
2424
use Surfnet\StepupBundle\Value\VettingType;
25+
use Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
2526

2627
/**
2728
* A poor mans repository, a pdo connection to the test database is established in the constructor
2829
*/
2930
class SecondFactorRepository
3031
{
31-
/**
32-
* @var Connection
33-
*/
34-
private $connection;
32+
private readonly PDO $connection;
3533

36-
public function __construct(Connection $connection)
34+
public function __construct(SmoketestPdoFactory $factory)
3735
{
38-
$this->connection = $connection;
36+
$this->connection = $factory->createConnection();
3937
}
4038

4139
public function create($nameId, $tokenType, $institution, bool $selfAsserted = false, $identifier = null)

tests/src/Repository/WhitelistRepository.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@
2020

2121
use Exception;
2222
use PDO;
23+
use Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
2324

2425
/**
2526
* A poor mans repository, a pdo connection to the test database is established in the constructor
2627
*/
2728
class WhitelistRepository
2829
{
29-
/**
30-
* @var Connection
31-
*/
32-
private $connection;
30+
private readonly PDO $connection;
3331

34-
public function __construct(Connection $connection)
32+
public function __construct(SmoketestPdoFactory $factory)
3533
{
36-
$this->connection = $connection;
34+
$this->connection = $factory->createConnection();
3735
}
3836

3937
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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;
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+

0 commit comments

Comments
 (0)