Skip to content

Commit 4051a5b

Browse files
authored
Upgrade the SymfonyBundleTest (#481)
* Upgrade the SymfonyBundleTest * Fix code styles * Use either new storage_factory_id or legacy storage_id * Fix tests in EditInPlaceTest * Fix code styles * Introduce legacy config in one more spot * Ignore 29 direct deprecations to make tests green
1 parent 4bdd4cf commit 4051a5b

20 files changed

+123
-65
lines changed

EditInPlace/Activator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public function setSession(Session $session): void
5353
private function getSession(): ?Session
5454
{
5555
$session = $this->session;
56-
if (null === $session && $this->requestStack->getCurrentRequest()->hasSession()) {
56+
$request = $this->requestStack->getCurrentRequest();
57+
if (null === $session && $request && $request->hasSession()) {
5758
$session = $this->requestStack->getSession();
5859
}
5960

Model/SfProfilerMessage.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ final class SfProfilerMessage
8080
*/
8181
private $parameters;
8282

83-
/**
84-
* @return SfProfilerMessage
85-
*/
8683
public static function create(array $data): self
8784
{
8885
$message = new self();

Tests/Functional/BaseTestCase.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,22 @@
1111

1212
namespace Translation\Bundle\Tests\Functional;
1313

14-
use Nyholm\BundleTest\AppKernel;
15-
use Nyholm\BundleTest\BaseBundleTestCase;
14+
use Nyholm\BundleTest\TestKernel;
1615
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
16+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1717
use Symfony\Bundle\TwigBundle\TwigBundle;
18+
use Symfony\Component\HttpKernel\Kernel;
1819
use Translation\Bundle\TranslationBundle;
1920

2021
/**
2122
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2223
*/
23-
abstract class BaseTestCase extends BaseBundleTestCase
24+
abstract class BaseTestCase extends KernelTestCase
2425
{
2526
/**
26-
* @var AppKernel
27+
* @var TestKernel
2728
*/
28-
protected $kernel;
29+
protected $testKernel;
2930

3031
protected function getBundleClass(): string
3132
{
@@ -34,13 +35,18 @@ protected function getBundleClass(): string
3435

3536
protected function setUp(): void
3637
{
37-
$kernel = $this->createKernel();
38-
$kernel->addConfigFile(__DIR__.'/app/config/default.yaml');
38+
$kernel = self::createKernel();
3939

40-
$kernel->addBundle(TwigBundle::class);
41-
$kernel->addBundle(TranslationBundle::class);
40+
if (Kernel::VERSION_ID < 50300) {
41+
$kernel->addTestConfig(__DIR__.'/app/config/default_legacy.yaml');
42+
} else {
43+
$kernel->addTestConfig(__DIR__.'/app/config/default.yaml');
44+
}
4245

43-
$this->kernel = $kernel;
46+
$kernel->addTestBundle(TwigBundle::class);
47+
$kernel->addTestBundle(TranslationBundle::class);
48+
49+
$this->testKernel = $kernel;
4450

4551
parent::setUp();
4652
}

Tests/Functional/BundleInitializationTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ class BundleInitializationTest extends BaseTestCase
2121
{
2222
public function testRegisterBundle(): void
2323
{
24-
$this->bootKernel();
25-
$container = $this->getContainer();
24+
$kernel = $this->testKernel;
25+
$kernel->boot();
26+
$container = $kernel->getContainer();
27+
2628
$this->assertTrue($container->has(ConfigurationManager::class));
2729
$config = $container->get(ConfigurationManager::class);
2830
$this->assertInstanceOf(ConfigurationManager::class, $config);

Tests/Functional/Catalogue/CatalogueFetcherTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,8 @@ public static function setUpBeforeClass(): void
5252

5353
public function testFetchCatalogue(): void
5454
{
55-
$this->bootKernel();
56-
57-
$this->catalogueFetcher = $this->getContainer()->get(CatalogueFetcher::class);
55+
$this->testKernel->boot();
56+
$this->catalogueFetcher = $this->testKernel->getContainer()->get(CatalogueFetcher::class);
5857

5958
$data = self::getDefaultData();
6059
$data['external_translations_dirs'] = [__DIR__.'/../app/Resources/translations/'];
@@ -89,6 +88,6 @@ protected function setUp(): void
8988
{
9089
parent::setUp();
9190

92-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
91+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
9392
}
9493
}

Tests/Functional/Command/CheckMissingCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ protected function setUp(): void
1919
{
2020
parent::setUp();
2121

22-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
23-
$this->bootKernel();
24-
$this->application = new Application($this->kernel);
22+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
23+
$this->testKernel->boot();
24+
$this->application = new Application($this->testKernel);
2525

2626
file_put_contents(
2727
__DIR__.'/../app/Resources/translations/messages.sv.xlf',

Tests/Functional/Command/ExtractCommandTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class ExtractCommandTest extends BaseTestCase
2525
protected function setUp(): void
2626
{
2727
parent::setUp();
28-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
28+
29+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
2930

3031
file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML'
3132
<?xml version="1.0" encoding="utf-8"?>
@@ -67,10 +68,10 @@ protected function setUp(): void
6768

6869
public function testExecute(): void
6970
{
70-
$this->bootKernel();
71-
$application = new Application($this->kernel);
71+
$this->testKernel->boot();
72+
$application = new Application($this->testKernel);
7273

73-
$container = $this->getContainer();
74+
$container = $this->testKernel->getContainer();
7475
$application->add($container->get(ExtractCommand::class));
7576

7677
// transchoice tag have been definively removed in sf ^5.0
@@ -96,7 +97,6 @@ public function testExecute(): void
9697
$this->assertMatchesRegularExpression('|New messages +4|s', $output);
9798
$this->assertMatchesRegularExpression('|Total defined messages +8|s', $output);
9899

99-
$container = $this->getContainer();
100100
$config = $container->get(ConfigurationManager::class)->getConfiguration('app');
101101
$catalogues = $container->get(CatalogueFetcher::class)->getCatalogues($config, ['sv']);
102102

Tests/Functional/Command/StatusCommandTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ class StatusCommandTest extends BaseTestCase
2121
protected function setUp(): void
2222
{
2323
parent::setUp();
24-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
24+
25+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
2526
}
2627

2728
public function testExecute(): void
2829
{
29-
$this->bootKernel();
30-
$application = new Application($this->kernel);
30+
$this->testKernel->boot();
31+
$application = new Application($this->testKernel);
3132

32-
$container = $this->getContainer();
33+
$container = $this->testKernel->getContainer();
3334
$application->add($container->get(StatusCommand::class));
3435

3536
$command = $application->find('translation:status');

Tests/Functional/Command/SyncCommandTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class SyncCommandTest extends BaseTestCase
2121
protected function setUp(): void
2222
{
2323
parent::setUp();
24-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
24+
25+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
2526

2627
file_put_contents(__DIR__.'/../app/Resources/translations/messages.sv.xlf', <<<'XML'
2728
<?xml version="1.0" encoding="utf-8"?>
@@ -63,10 +64,10 @@ protected function setUp(): void
6364

6465
public function testExecute(): void
6566
{
66-
$this->bootKernel();
67-
$application = new Application($this->kernel);
67+
$this->testKernel->boot();
68+
$application = new Application($this->testKernel);
6869

69-
$container = $this->getContainer();
70+
$container = $this->testKernel->getContainer();
7071
$application->add($container->get(SyncCommand::class));
7172

7273
$command = $application->find('translation:sync');

Tests/Functional/Controller/EditInPlaceControllerTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public static function setUpBeforeClass(): void
4949
protected function setUp(): void
5050
{
5151
parent::setUp();
52-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
52+
53+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
5354
}
5455

5556
public function testEditAction(): void
@@ -58,7 +59,7 @@ public function testEditAction(): void
5859
'messages|key0' => 'trans0',
5960
'messages|key1' => 'trans1',
6061
]));
61-
$response = $this->kernel->handle($request);
62+
$response = $this->testKernel->handle($request);
6263
$this->assertEquals(200, $response->getStatusCode());
6364
}
6465

@@ -68,7 +69,7 @@ public function testEditActionError(): void
6869
'messages|key0' => 'trans0',
6970
'messages|' => 'trans1',
7071
]));
71-
$response = $this->kernel->handle($request);
72+
$response = $this->testKernel->handle($request);
7273
$this->assertEquals(400, $response->getStatusCode());
7374
}
7475
}

Tests/Functional/Controller/EditInPlaceTest.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
namespace Translation\Bundle\Tests\Functional\Controller;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Session\Session;
16+
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
17+
use Symfony\Component\HttpKernel\Kernel;
1518
use Translation\Bundle\EditInPlace\Activator;
1619
use Translation\Bundle\Tests\Functional\BaseTestCase;
1720

@@ -22,13 +25,16 @@ class EditInPlaceTest extends BaseTestCase
2225
{
2326
public function testActivatedTest(): void
2427
{
25-
$this->bootKernel();
28+
$this->testKernel->boot();
2629
$request = Request::create('/foobar');
2730

2831
// Activate the feature
29-
$this->getContainer()->get(Activator::class)->activate();
32+
$activator = $this->testKernel->getContainer()->get(Activator::class);
33+
$session = new Session(new MockArraySessionStorage());
34+
$activator->setSession($session);
35+
$activator->activate();
3036

31-
$response = $this->kernel->handle($request);
37+
$response = $this->testKernel->handle($request);
3238

3339
self::assertSame(200, $response->getStatusCode());
3440
self::assertStringContainsString('<!-- TranslationBundle -->', $response->getContent());
@@ -53,14 +59,21 @@ public function testActivatedTest(): void
5359

5460
public function testIfUntranslatableLabelGetsDisabled(): void
5561
{
56-
$this->kernel->addConfigFile(__DIR__.'/../app/config/disabled_label.yaml');
62+
if (Kernel::VERSION_ID < 50300) {
63+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/disabled_label_legacy.yaml');
64+
} else {
65+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/disabled_label.yaml');
66+
}
67+
$this->testKernel->boot();
5768
$request = Request::create('/foobar');
5869

5970
// Activate the feature
60-
$this->bootKernel();
61-
$this->getContainer()->get(Activator::class)->activate();
71+
$activator = $this->testKernel->getContainer()->get(Activator::class);
72+
$session = new Session(new MockArraySessionStorage());
73+
$activator->setSession($session);
74+
$activator->activate();
6275

63-
$response = $this->kernel->handle($request);
76+
$response = $this->testKernel->handle($request);
6477

6578
self::assertSame(200, $response->getStatusCode());
6679
self::assertStringContainsString('<!-- TranslationBundle -->', $response->getContent());
@@ -85,9 +98,10 @@ public function testIfUntranslatableLabelGetsDisabled(): void
8598

8699
public function testDeactivatedTest(): void
87100
{
88-
$this->bootKernel();
101+
$this->testKernel->boot();
102+
89103
$request = Request::create('/foobar');
90-
$response = $this->kernel->handle($request);
104+
$response = $this->testKernel->handle($request);
91105

92106
self::assertSame(200, $response->getStatusCode());
93107
self::assertStringNotContainsString('x-trans', $response->getContent());

Tests/Functional/Controller/WebUIControllerTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,25 @@ public static function setUpBeforeClass(): void
4646
protected function setUp(): void
4747
{
4848
parent::setUp();
49-
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
49+
50+
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
5051
}
5152

5253
public function testIndexAction(): void
5354
{
5455
$request = Request::create('/_trans', 'GET');
55-
$response = $this->kernel->handle($request);
56+
$response = $this->testKernel->handle($request);
5657
$this->assertEquals(200, $response->getStatusCode());
5758

5859
$request = Request::create('/_trans/app', 'GET');
59-
$response = $this->kernel->handle($request);
60+
$response = $this->testKernel->handle($request);
6061
$this->assertEquals(200, $response->getStatusCode());
6162
}
6263

6364
public function testShowAction(): void
6465
{
6566
$request = Request::create('/_trans/app/en/messages', 'GET');
66-
$response = $this->kernel->handle($request);
67+
$response = $this->testKernel->handle($request);
6768
$this->assertEquals(200, $response->getStatusCode());
6869
}
6970

@@ -72,14 +73,14 @@ public function testCreateAction(): void
7273
$request = Request::create('/_trans/app/sv/messages/new', 'POST', [], [], [], [], json_encode([
7374
'key' => 'foo',
7475
]));
75-
$response = $this->kernel->handle($request);
76+
$response = $this->testKernel->handle($request);
7677
$this->assertEquals(400, $response->getStatusCode());
7778

7879
$request = Request::create('/_trans/app/sv/messages/new', 'POST', [], [], [], [], json_encode([
7980
'key' => 'foo',
8081
'message' => 'bar',
8182
]));
82-
$response = $this->kernel->handle($request);
83+
$response = $this->testKernel->handle($request);
8384
$this->assertEquals(200, $response->getStatusCode());
8485
}
8586

@@ -88,14 +89,14 @@ public function testEditAction(): void
8889
$request = Request::create('/_trans/app/sv/messages', 'POST', [], [], [], [], json_encode([
8990
'key' => 'foo',
9091
]));
91-
$response = $this->kernel->handle($request);
92+
$response = $this->testKernel->handle($request);
9293
$this->assertEquals(400, $response->getStatusCode());
9394

9495
$request = Request::create('/_trans/app/sv/messages', 'POST', [], [], [], [], json_encode([
9596
'key' => 'key1',
9697
'message' => 'bar',
9798
]));
98-
$response = $this->kernel->handle($request);
99+
$response = $this->testKernel->handle($request);
99100
$this->assertEquals(200, $response->getStatusCode());
100101
}
101102

@@ -105,13 +106,13 @@ public function testDeleteAction(): void
105106
$request = Request::create('/_trans/app/sv/messages', 'DELETE', [], [], [], [], json_encode([
106107
'key' => 'empty',
107108
]));
108-
$response = $this->kernel->handle($request);
109+
$response = $this->testKernel->handle($request);
109110
$this->assertEquals(200, $response->getStatusCode());
110111

111112
$request = Request::create('/_trans/app/sv/messages', 'DELETE', [], [], [], [], json_encode([
112113
'key' => 'foo',
113114
]));
114-
$response = $this->kernel->handle($request);
115+
$response = $this->testKernel->handle($request);
115116
$this->assertEquals(200, $response->getStatusCode());
116117
}
117118
}

Tests/Functional/app/Service/DummyHttpClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
namespace Translation\Bundle\Tests\Functional\app\Service;
1313

14-
use GuzzleHttp\Psr7\Response;
1514
use Http\Client\HttpClient;
15+
use Nyholm\Psr7\Response;
1616
use Psr\Http\Message\RequestInterface;
17+
use Psr\Http\Message\ResponseInterface;
1718

1819
class DummyHttpClient implements HttpClient
1920
{
20-
public function sendRequest(RequestInterface $request)
21+
public function sendRequest(RequestInterface $request): ResponseInterface
2122
{
2223
return new Response(200);
2324
}

0 commit comments

Comments
 (0)