Skip to content

Commit 6135702

Browse files
authored
Upgrade to nyholm/symfony-bundle-test v2 (#1420)
1 parent 858c539 commit 6135702

File tree

3 files changed

+95
-47
lines changed

3 files changed

+95
-47
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"async-aws/sqs": "^1.0",
2525
"async-aws/ssm": "^1.0",
2626
"matthiasnoback/symfony-config-test": "^4.1",
27-
"nyholm/symfony-bundle-test": "1.x-dev#0555e4898e1b05655a4bdf52ba10c7f56611a514 as 1.9.0",
27+
"nyholm/symfony-bundle-test": "^2.0",
2828
"symfony/cache": "^4.4 || ^5.0 || ^6.0 || ^7.0"
2929
},
3030
"autoload": {

tests/Functional/BundleInitializationTest.php

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,18 @@
1111
use AsyncAws\Ssm\SsmClient;
1212
use AsyncAws\Symfony\Bundle\AsyncAwsBundle;
1313
use AsyncAws\Symfony\Bundle\Secrets\SsmVault;
14-
use Nyholm\BundleTest\BaseBundleTestCase;
15-
use Nyholm\BundleTest\CompilerPass\PublicServicePass;
14+
use Nyholm\BundleTest\TestKernel;
15+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1616
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17+
use Symfony\Component\HttpKernel\KernelInterface;
1718

18-
class BundleInitializationTest extends BaseBundleTestCase
19+
class BundleInitializationTest extends KernelTestCase
1920
{
20-
protected function setUp(): void
21-
{
22-
parent::setUp();
23-
24-
$this->addCompilerPass(new PublicServicePass('|async_aws.*|'));
25-
$this->addCompilerPass(new PublicServicePass('|AsyncAws\.*|'));
26-
}
27-
2821
public function testInitBundle()
2922
{
30-
$kernel = $this->createKernel();
31-
$kernel->addConfigFile(__DIR__ . '/Resources/config/default.yaml');
32-
$this->bootKernel();
23+
self::bootKernel(['config' => static function (TestKernel $kernel) {
24+
$kernel->addTestConfig(__DIR__ . '/Resources/config/default.yaml');
25+
}]);
3326

3427
self::assertServiceExists('async_aws.client.s3', S3Client::class);
3528
self::assertServiceExists('async_aws.client.sqs', SqsClient::class);
@@ -48,16 +41,16 @@ public function testInitBundle()
4841
// Test secret
4942
self::assertServiceExists(SsmVault::class, SsmVault::class);
5043

51-
$container = $this->getContainer();
44+
$container = self::$kernel->getContainer();
5245
self::assertFalse($container->has(SqsClient::class . ' $notFound'));
5346
self::assertFalse($container->has(S3Client::class . ' $foobar'));
5447
}
5548

5649
public function testEmptyConfig()
5750
{
58-
$kernel = $this->createKernel();
59-
$kernel->addConfigFile(__DIR__ . '/Resources/config/empty.yaml');
60-
$this->bootKernel();
51+
self::bootKernel(['config' => static function (TestKernel $kernel) {
52+
$kernel->addTestConfig(__DIR__ . '/Resources/config/empty.yaml');
53+
}]);
6154

6255
self::assertServiceExists('async_aws.client.s3', S3Client::class);
6356
self::assertServiceExists('async_aws.client.sqs', SqsClient::class);
@@ -71,47 +64,47 @@ public function testEmptyConfig()
7164

7265
public function testNotRegisterServices()
7366
{
74-
$kernel = $this->createKernel();
75-
$kernel->addConfigFile(__DIR__ . '/Resources/config/no_services.yaml');
76-
$this->bootKernel();
67+
self::bootKernel(['config' => static function (TestKernel $kernel) {
68+
$kernel->addTestConfig(__DIR__ . '/Resources/config/no_services.yaml');
69+
}]);
7770

78-
$container = $this->getContainer();
71+
$container = self::$kernel->getContainer();
7972
self::assertFalse($container->has('async_aws.client.s3'));
8073
self::assertFalse($container->has('async_aws.client.sqs'));
8174
self::assertFalse($container->has(SqsClient::class));
8275
}
8376

8477
public function testEmptyClientsKey()
8578
{
86-
$kernel = $this->createKernel();
87-
$kernel->addConfigFile(__DIR__ . '/Resources/config/empty_clients_key.yaml');
88-
$this->bootKernel();
79+
self::bootKernel(['config' => static function (TestKernel $kernel) {
80+
$kernel->addTestConfig(__DIR__ . '/Resources/config/empty_clients_key.yaml');
81+
}]);
8982

90-
$container = $this->getContainer();
83+
$container = self::$kernel->getContainer();
9184
self::assertTrue($container->has('async_aws.client.s3'));
9285
self::assertTrue($container->has('async_aws.client.sqs'));
9386
self::assertTrue($container->has(SqsClient::class));
9487
}
9588

9689
public function testNotRegisterSqs()
9790
{
98-
$kernel = $this->createKernel();
99-
$kernel->addConfigFile(__DIR__ . '/Resources/config/no_service_sqs.yaml');
100-
$this->bootKernel();
91+
self::bootKernel(['config' => static function (TestKernel $kernel) {
92+
$kernel->addTestConfig(__DIR__ . '/Resources/config/no_service_sqs.yaml');
93+
}]);
10194

102-
$container = $this->getContainer();
95+
$container = self::$kernel->getContainer();
10396
self::assertTrue($container->has('async_aws.client.s3'));
10497
self::assertFalse($container->has('async_aws.client.sqs'));
10598
self::assertFalse($container->has(SqsClient::class));
10699
}
107100

108101
public function testConfigOverride()
109102
{
110-
$kernel = $this->createKernel();
111-
$kernel->addConfigFile(__DIR__ . '/Resources/config/override.yaml');
112-
$this->bootKernel();
103+
self::bootKernel(['config' => static function (TestKernel $kernel) {
104+
$kernel->addTestConfig(__DIR__ . '/Resources/config/override.yaml');
105+
}]);
113106

114-
$container = $this->getContainer();
107+
$container = self::$kernel->getContainer();
115108
self::assertTrue($container->has('async_aws.client.s3'));
116109
self::assertTrue($container->has('async_aws.client.ses'));
117110
self::assertTrue($container->has('async_aws.client.sqs'));
@@ -139,33 +132,46 @@ public function testExceptionWhenConfigureServiceNotInstalled()
139132
self::markTestSkipped('SNSClient is installed..');
140133
}
141134

142-
$kernel = $this->createKernel();
143-
$kernel->addConfigFile(__DIR__ . '/Resources/config/not_installed_service.yaml');
144-
145135
$this->expectException(InvalidConfigurationException::class);
146-
$this->bootKernel();
136+
137+
self::bootKernel(['config' => static function (TestKernel $kernel) {
138+
$kernel->addTestConfig(__DIR__ . '/Resources/config/not_installed_service.yaml');
139+
}]);
147140
}
148141

149142
public function testIssue793()
150143
{
151-
$kernel = $this->createKernel();
152-
$kernel->addConfigFile(__DIR__ . '/Resources/config/issue-793/default.yaml');
153-
$kernel->addConfigFile(__DIR__ . '/Resources/config/issue-793/dev.yaml');
144+
self::bootKernel(['config' => static function (TestKernel $kernel) {
145+
$kernel->addTestConfig(__DIR__ . '/Resources/config/issue-793/default.yaml');
146+
$kernel->addTestConfig(__DIR__ . '/Resources/config/issue-793/dev.yaml');
147+
}]);
154148

155-
$this->bootKernel();
156-
$container = $this->getContainer();
149+
$container = self::$kernel->getContainer();
157150
$x = $container->get(S3Client::class);
158151
self::assertSame('./docker/dynamodb/credentials', $x->getConfiguration()->get('sharedCredentialsFile'));
159152
}
160153

161-
protected function getBundleClass(): string
154+
protected static function getKernelClass(): string
162155
{
163-
return AsyncAwsBundle::class;
156+
return TestKernel::class;
157+
}
158+
159+
protected static function createKernel(array $options = []): KernelInterface
160+
{
161+
$kernel = parent::createKernel($options);
162+
\assert($kernel instanceof TestKernel);
163+
164+
$kernel->addTestBundle(AsyncAwsBundle::class);
165+
$kernel->addTestCompilerPass(new PublicServicePass('|async_aws.*|'));
166+
$kernel->addTestCompilerPass(new PublicServicePass('|AsyncAws\.*|'));
167+
$kernel->handleOptions($options);
168+
169+
return $kernel;
164170
}
165171

166172
private function assertServiceExists(string $serviceId, string $instance)
167173
{
168-
$container = $this->getContainer();
174+
$container = self::$kernel->getContainer();
169175
self::assertTrue($container->has($serviceId));
170176
self::assertInstanceOf($instance, $container->get($serviceId));
171177
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace AsyncAws\Symfony\Bundle\Tests\Functional;
4+
5+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
/**
9+
* @author Tobias Nyholm <[email protected]>
10+
*/
11+
class PublicServicePass implements CompilerPassInterface
12+
{
13+
/**
14+
* A regex to match the services that should be public.
15+
*
16+
* @var string
17+
*/
18+
private $regex;
19+
20+
/**
21+
* @param string $regex
22+
*/
23+
public function __construct($regex)
24+
{
25+
$this->regex = $regex;
26+
}
27+
28+
public function process(ContainerBuilder $container): void
29+
{
30+
foreach ($container->getDefinitions() as $id => $definition) {
31+
if (preg_match($this->regex, $id)) {
32+
$definition->setPublic(true);
33+
}
34+
}
35+
36+
foreach ($container->getAliases() as $id => $alias) {
37+
if (preg_match($this->regex, $id)) {
38+
$alias->setPublic(true);
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)