Skip to content

Commit e965685

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 is currently based on the current entity mapping. But because the middleware generated schema is not available in the pipeline, this cannot be changed. This change removes the deploy connection from doctrine, and creates the schema by using PDO. Resolves #402
1 parent e36a88e commit e965685

File tree

10 files changed

+174
-96
lines changed

10 files changed

+174
-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,33 @@ 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+
1018
Surfnet\StepupGateway\Behat\Repository\SecondFactorRepository:
1119
class: Surfnet\StepupGateway\Behat\Repository\SecondFactorRepository
1220
arguments:
13-
- '@Surfnet\StepupGateway\Behat\Repository\Connection'
21+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
1422

1523
Surfnet\StepupGateway\Behat\Repository\SamlEntityRepository:
1624
class: Surfnet\StepupGateway\Behat\Repository\SamlEntityRepository
1725
arguments:
18-
- '@Surfnet\StepupGateway\Behat\Repository\Connection'
26+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
1927

2028
Surfnet\StepupGateway\Behat\Repository\WhitelistRepository:
2129
class: Surfnet\StepupGateway\Behat\Repository\WhitelistRepository
2230
arguments:
23-
- '@Surfnet\StepupGateway\Behat\Repository\Connection'
31+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
2432

2533
Surfnet\StepupGateway\Behat\Repository\InstitutionConfigurationRepository:
2634
class: Surfnet\StepupGateway\Behat\Repository\InstitutionConfigurationRepository
2735
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-
36+
- '@Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory'
3937

4038
Surfnet\StepupGateway\Behat\Controller\ServiceProviderController:
4139
class: Surfnet\StepupGateway\Behat\Controller\ServiceProviderController
@@ -69,6 +67,13 @@ services:
6967
- "@logger"
7068
public: false
7169

70+
Surfnet\StepupGateway\Behat\Service\DatabaseSchemaService:
71+
class: Surfnet\StepupGateway\Behat\Service\DatabaseSchemaService
72+
public: true
73+
arguments:
74+
- '@doctrine.orm.gateway_entity_manager'
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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 Doctrine\ORM\EntityManagerInterface;
22+
use Doctrine\ORM\Tools\SchemaTool;
23+
use PDO;
24+
use Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
25+
26+
/**
27+
* Manages the database schema for smoketests.
28+
* Uses Doctrine's schema tools to generate SQL from entity mappings,
29+
* but executes via PDO with elevated privileges (deploy user).
30+
*/
31+
class DatabaseSchemaService
32+
{
33+
private readonly PDO $connection;
34+
35+
public function __construct(
36+
private readonly EntityManagerInterface $entityManager,
37+
SmoketestPdoFactory $pdoFactory,
38+
) {
39+
$this->connection = $pdoFactory->createConnection();
40+
}
41+
42+
public function dropSchema(): void
43+
{
44+
$this->connection->exec('SET FOREIGN_KEY_CHECKS = 0');
45+
46+
$stmt = $this->connection->query('SHOW TABLES');
47+
$tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
48+
49+
foreach ($tables as $table) {
50+
$this->connection->exec("DROP TABLE IF EXISTS `$table`");
51+
}
52+
53+
$this->connection->exec('SET FOREIGN_KEY_CHECKS = 1');
54+
}
55+
56+
public function createSchema(): void
57+
{
58+
$schemaTool = new SchemaTool($this->entityManager);
59+
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
60+
61+
$createSql = $schemaTool->getCreateSchemaSql($metadata);
62+
63+
foreach ($createSql as $sql) {
64+
$this->connection->exec($sql);
65+
}
66+
}
67+
68+
public function resetSchema(): void
69+
{
70+
echo "Preparing test schemas\n";
71+
$this->dropSchema();
72+
$this->createSchema();
73+
}
74+
}
75+

0 commit comments

Comments
 (0)