Skip to content

Commit 89e8d26

Browse files
authored
Merge pull request #209 from symfony-cmf/adjust-symfony-test-case
Adjust to Symfony FrameworkBundle changes
2 parents 516a8b2 + da99d27 commit 89e8d26

File tree

7 files changed

+107
-74
lines changed

7 files changed

+107
-74
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
4.0.0
5+
-----
6+
7+
* [BC-BREAK] Made bootstrap utility methods in BaseTestCase static to not conflict with recent versions of Symfony FrameworkBundle.
8+
49
3.3.0
510
-----
611

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"extra": {
4343
"branch-alias": {
44-
"dev-master": "3.x-dev"
44+
"dev-master": "4.x-dev"
4545
}
4646
}
4747
}

resources/web/router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020

2121
// otherwise lets run the application
2222
$_SERVER['DOCUMENT_ROOT'] = __DIR__;
23-
$_SERVER['SCRIPT_FILENAME'] = __DIR__.DIRECTORY_SEPARATOR.'app_test.php';
23+
$_SERVER['SCRIPT_FILENAME'] = __DIR__.\DIRECTORY_SEPARATOR.'app_test.php';
2424

2525
require 'app_test.php';

src/Functional/BaseTestCase.php

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Bundle\FrameworkBundle\Client;
1616
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
1717
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
18+
use Symfony\Cmf\Component\Testing\Functional\DbManager\ORM;
19+
use Symfony\Cmf\Component\Testing\Functional\DbManager\PHPCR;
1820
use Symfony\Cmf\Component\Testing\Functional\DbManager\PhpcrDecorator;
1921
use Symfony\Component\DependencyInjection\ContainerInterface;
2022
use Symfony\Component\HttpFoundation\Response;
@@ -48,45 +50,62 @@ abstract class BaseTestCase extends WebTestCase
4850
* * environment - The environment to use (defaults to 'phpcr')
4951
* * debug - If debug should be enabled/disabled (defaults to true)
5052
*/
51-
protected function getKernelConfiguration(): array
53+
protected static function getKernelConfiguration(): array
5254
{
5355
return [];
5456
}
5557

5658
/**
57-
* @return Client|KernelBrowser
59+
* {@inheritdoc}
60+
*
61+
* Overwritten to set the default environment to 'phpcr'.
5862
*/
59-
public function getFrameworkBundleClient()
63+
protected static function createKernel(array $options = []): KernelInterface
6064
{
61-
if (null === $this->client) {
62-
// property does not exist in all symfony versions
63-
if (property_exists(self::class, 'booted') && self::$booted) {
64-
self::ensureKernelShutdown();
65-
}
66-
$this->client = self::createClient($this->getKernelConfiguration());
65+
// default environment is 'phpcr'
66+
if (!isset($options['environment'])) {
67+
$options['environment'] = 'phpcr';
6768
}
6869

69-
return $this->client;
70+
return parent::createKernel($options);
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*
76+
* Overwritten to set the kernel configuration from getKernelConfiguration.
77+
*/
78+
protected static function bootKernel(array $options = [])
79+
{
80+
return parent::bootKernel(static::getKernelConfiguration());
7081
}
7182

72-
public function getContainer(): ContainerInterface
83+
/**
84+
* BC with Symfony < 5.3 - when minimum version raises to ^5.3, we can remove this method.
85+
*/
86+
protected static function getContainer(): ContainerInterface
7387
{
74-
return $this->getKernel()->getContainer();
88+
if (method_exists(KernelTestCase::class, 'getContainer')) {
89+
return parent::getContainer();
90+
}
91+
92+
return self::getKernel()->getContainer();
7593
}
7694

77-
public function getKernel(): KernelInterface
95+
protected static function getKernel(): KernelInterface
7896
{
7997
if (null === static::$kernel) {
80-
parent::bootKernel(static::getKernelConfiguration());
98+
static::bootKernel();
8199
}
82100

83-
if (static::$kernel instanceof KernelInterface) {
101+
if (static::$kernel instanceof KernelInterface) {
84102
$kernelEnvironment = static::$kernel->getEnvironment();
85-
$expectedEnvironment = isset($this->getKernelConfiguration()['environment'])
86-
? $this->getKernelConfiguration()['environment']
103+
$expectedEnvironment = isset(static::getKernelConfiguration()['environment'])
104+
? static::getKernelConfiguration()['environment']
87105
: 'phpcr';
88106
if ($kernelEnvironment !== $expectedEnvironment) {
89-
parent::bootKernel(static::getKernelConfiguration());
107+
var_dump($kernelEnvironment, $expectedEnvironment);
108+
static::bootKernel();
90109
}
91110
}
92111

@@ -98,23 +117,35 @@ public function getKernel(): KernelInterface
98117
}
99118

100119
/**
101-
* Gets the DbManager.
120+
* @return Client|KernelBrowser
121+
*/
122+
protected function getFrameworkBundleClient()
123+
{
124+
if (null === $this->client) {
125+
// property does not exist in all symfony versions
126+
if (property_exists(self::class, 'booted') && self::$booted) {
127+
self::ensureKernelShutdown();
128+
}
129+
$this->client = self::createClient($this->getKernelConfiguration());
130+
}
131+
132+
return $this->client;
133+
}
134+
135+
/**
136+
* Shortcut for getDbManager.
102137
*
103138
* @see self::getDbManager
104139
*/
105-
public function db($type)
140+
protected function db($type)
106141
{
107142
return $this->getDbManager($type);
108143
}
109144

110145
/**
111-
* Gets the DbManager.
112-
*
113-
* @param string $type The Db type
114-
*
115-
* @return object
146+
* @return ORM|PHPCR
116147
*/
117-
public function getDbManager($type)
148+
protected function getDbManager(string $type)
118149
{
119150
if (isset($this->dbManagers[$type])) {
120151
return $this->dbManagers[$type];
@@ -143,36 +174,7 @@ public function getDbManager($type)
143174
return $dbManager;
144175
}
145176

146-
public static function getKernelClass(): string
147-
{
148-
if (isset($_SERVER['KERNEL_CLASS']) || isset($_ENV['KERNEL_CLASS'])) {
149-
$class = isset($_SERVER['KERNEL_CLASS']) ? $_SERVER['KERNEL_CLASS'] : $_ENV['KERNEL_CLASS'];
150-
if (!class_exists($class)) {
151-
throw new \RuntimeException(sprintf('Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method.', $class, static::class));
152-
}
153-
154-
return $class;
155-
}
156-
157-
return parent::getKernelClass();
158-
}
159-
160-
/**
161-
* {@inheritdoc}
162-
*
163-
* This is overriden to set the default environment to 'phpcr'
164-
*/
165-
protected static function createKernel(array $options = []): KernelInterface
166-
{
167-
// default environment is 'phpcr'
168-
if (!isset($options['environment'])) {
169-
$options['environment'] = 'phpcr';
170-
}
171-
172-
return parent::createKernel($options);
173-
}
174-
175-
protected function assertResponseSuccess(Response $response)
177+
protected static function assertResponseSuccess(Response $response)
176178
{
177179
libxml_use_internal_errors(true);
178180

@@ -186,6 +188,6 @@ protected function assertResponseSuccess(Response $response)
186188
$exception = $result->item(0)->nodeValue;
187189
}
188190

189-
$this->assertEquals(200, $response->getStatusCode(), $exception ? 'Exception: "'.$exception.'"' : '');
191+
self::assertEquals(200, $response->getStatusCode(), $exception ? 'Exception: "'.$exception.'"' : '');
190192
}
191193
}

tests/Fixtures/TestTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function setKernel(KernelInterface $kernel)
2424
protected static function createKernel(array $options = []): KernelInterface
2525
{
2626
if (null === static::$kernel) {
27-
return parent::createKernel($options);
27+
static::$kernel = parent::createKernel($options);
2828
}
2929

3030
return static::$kernel;

tests/Functional/BaseTestCaseTest.php

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPUnit\Framework\TestCase;
1717
use Symfony\Bundle\FrameworkBundle\Client;
1818
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
19+
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
1920
use Symfony\Cmf\Component\Testing\Functional\DbManager\PHPCR;
2021
use Symfony\Cmf\Component\Testing\Tests\Fixtures\TestTestCase;
2122
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -57,7 +58,12 @@ protected function setUp(): void
5758
$this->kernel = $this->createMock(KernelInterface::class);
5859
$this->kernel->expects($this->any())
5960
->method('getContainer')
60-
->willReturn($this->container);
61+
->willReturn($this->container)
62+
;
63+
$this->kernel->expects($this->any())
64+
->method('getEnvironment')
65+
->willReturn('phpcr')
66+
;
6167

6268
$this->testCase = new TestTestCase();
6369
$this->testCase->setKernel($this->kernel);
@@ -75,12 +81,33 @@ protected function setUp(): void
7581

7682
public function testGetContainer()
7783
{
78-
$this->assertEquals($this->container, $this->testCase->getContainer());
84+
$class = new \ReflectionClass(BaseTestCase::class);
85+
$method = $class->getMethod('getContainer');
86+
$method->setAccessible(true);
87+
88+
$this->assertEquals($this->container, $method->invoke(null));
7989
}
8090

8191
public function testGetKernel()
8292
{
83-
$this->assertInstanceOf(KernelInterface::class, $this->testCase->getKernel());
93+
$class = new \ReflectionClass(BaseTestCase::class);
94+
$method = $class->getMethod('getKernel');
95+
$method->setAccessible(true);
96+
97+
$this->assertInstanceOf(KernelInterface::class, $method->invoke(null));
98+
}
99+
100+
public function testItCanProvideAFrameworkBundleClient()
101+
{
102+
$class = new \ReflectionClass(BaseTestCase::class);
103+
$method = $class->getMethod('getFrameworkBundleClient');
104+
$method->setAccessible(true);
105+
106+
if (class_exists(KernelBrowser::class)) {
107+
$this->assertInstanceOf(KernelBrowser::class, $method->invoke($this->testCase));
108+
} else {
109+
$this->assertInstanceOf(Client::class, $method->invoke($this->testCase));
110+
}
84111
}
85112

86113
public function provideTestDb()
@@ -93,27 +120,26 @@ public function provideTestDb()
93120
];
94121
}
95122

96-
public function testItCanProvideAFrameworkBundleClient()
97-
{
98-
if (class_exists(KernelBrowser::class)) {
99-
$this->assertInstanceOf(KernelBrowser::class, $this->testCase->getFrameworkBundleClient());
100-
} else {
101-
$this->assertInstanceOf(Client::class, $this->testCase->getFrameworkBundleClient());
102-
}
103-
}
104-
105123
/**
106124
* @dataProvider provideTestDb
107125
* @depends testGetContainer
108126
*/
109127
public function testDb($dbName, $expected)
110128
{
129+
$class = new \ReflectionClass(BaseTestCase::class);
130+
$method = $class->getMethod('getDbManager');
131+
$method->setAccessible(true);
132+
111133
if (null === $expected) {
112134
$this->expectException('InvalidArgumentException');
113135
$this->expectExceptionMessage($dbName.'" does not exist');
114136
}
115137

116-
$res = $this->testCase->getDbManager($dbName);
138+
$res = $method->invoke($this->testCase, $dbName);
139+
if (null === $expected) {
140+
// do not do assertions if the expected exception has not been thrown.
141+
return;
142+
}
117143

118144
$className = sprintf(
119145
'Symfony\Cmf\Component\Testing\Functional\DbManager\%s',

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
$vendorDir = realpath(__DIR__.'/../vendor');
1313

1414
if (!$loader = include $vendorDir.'/autoload.php') {
15-
$nl = PHP_SAPI === 'cli' ? PHP_EOL : '<br />';
15+
$nl = \PHP_SAPI === 'cli' ? \PHP_EOL : '<br />';
1616
echo "$nl$nl";
1717
exit('You must set up the project dependencies.'.$nl.
1818
'Run the following commands in '.dirname(__DIR__).':'.$nl.$nl.

0 commit comments

Comments
 (0)