Skip to content

Commit 79bb97d

Browse files
committed
add tests for Symfony container integration
2 parents e94ce9a + 689bb5f commit 79bb97d

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"require-dev": {
2424
"consistence/coding-standard": "2.2.1",
2525
"jakub-onderka/php-parallel-lint": "0.9.2",
26+
"matthiasnoback/symfony-config-test": "3.0.1",
27+
"matthiasnoback/symfony-dependency-injection-test": "2.2.0",
2628
"phpstan/phpstan-shim": "0.8.4",
2729
"phpunit/phpunit": "6.4.3",
2830
"satooshi/php-coveralls": "1.0.1"
@@ -44,6 +46,9 @@
4446
]
4547
}
4648
},
49+
"config": {
50+
"sort-packages": true
51+
},
4752
"scripts": {
4853
"build": [
4954
"@composer validate --no-check-all",
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Mhujer\JavaScriptErrorHandlerBundle\DependencyInjection;
6+
7+
use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\Config\Definition\ConfigurationInterface;
10+
11+
class ConfigurationTest extends TestCase
12+
{
13+
14+
use ConfigurationTestCaseTrait;
15+
16+
public function testEmptyConfigurationIsValid(): void
17+
{
18+
$this->assertConfigurationIsValid(
19+
[
20+
[], // no values at all
21+
]
22+
);
23+
}
24+
25+
public function testEnabledConfigurationIsValid(): void
26+
{
27+
$this->assertConfigurationIsValid(
28+
[
29+
[
30+
'enabled' => true,
31+
],
32+
]
33+
);
34+
}
35+
36+
public function testDisabledConfigurationIsValid(): void
37+
{
38+
$this->assertConfigurationIsValid(
39+
[
40+
[
41+
'enabled' => false,
42+
],
43+
]
44+
);
45+
}
46+
47+
public function testEnabledConfigurationIsValidXX(): void
48+
{
49+
$this->assertConfigurationIsInvalid(
50+
[
51+
[
52+
'enabled' => 1,
53+
],
54+
],
55+
'Invalid type for path "java_script_error_handler.enabled". Expected boolean, but got integer.'
56+
);
57+
}
58+
59+
public function testInvalidConfigurationIsInvalid(): void
60+
{
61+
$this->assertConfigurationIsInvalid(
62+
[
63+
[
64+
'invalid_option' => 1,
65+
],
66+
],
67+
'Unrecognized option "invalid_option" under "java_script_error_handler"'
68+
);
69+
}
70+
71+
protected function getConfiguration(): ConfigurationInterface
72+
{
73+
return new Configuration(true);
74+
}
75+
76+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Mhujer\JavaScriptErrorHandlerBundle\DependencyInjection;
6+
7+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
8+
use Mhujer\JavaScriptErrorHandlerBundle\EventListener\JsErrorToAlertListener;
9+
10+
class JavaScriptErrorHandlerExtensionTest extends AbstractExtensionTestCase
11+
{
12+
13+
private const LISTENER_CLASS_NAME = JsErrorToAlertListener::class;
14+
15+
protected function getContainerExtensions()
16+
{
17+
return [
18+
new JavaScriptErrorHandlerExtension(),
19+
];
20+
}
21+
22+
public function testListenerIsRegisteredInDebugMode(): void
23+
{
24+
$this->container->setParameter('kernel.debug', true);
25+
26+
$this->load();
27+
28+
$this->assertContainerBuilderHasService(self::LISTENER_CLASS_NAME);
29+
}
30+
31+
public function testListenerIsNotRegisteredWithoutDebugMode(): void
32+
{
33+
$this->container->setParameter('kernel.debug', false);
34+
35+
$this->load();
36+
37+
$this->assertContainerBuilderNotHasService(self::LISTENER_CLASS_NAME);
38+
}
39+
40+
public function testKernelDebugCanBeOverriddenToDisable(): void
41+
{
42+
$this->container->setParameter('kernel.debug', true);
43+
44+
$this->load([
45+
'enabled' => false,
46+
]);
47+
48+
$this->assertContainerBuilderNotHasService(self::LISTENER_CLASS_NAME);
49+
}
50+
51+
public function testKernelDebugCanBeOverriddenToEnable(): void
52+
{
53+
$this->container->setParameter('kernel.debug', false);
54+
55+
$this->load([
56+
'enabled' => true,
57+
]);
58+
59+
$this->assertContainerBuilderHasService(self::LISTENER_CLASS_NAME);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)