Skip to content

Upgrade the SymfonyBundleTest #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion EditInPlace/Activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function setSession(Session $session): void
private function getSession(): ?Session
{
$session = $this->session;
if (null === $session && $this->requestStack->getCurrentRequest()->hasSession()) {
$request = $this->requestStack->getCurrentRequest();
if (null === $session && $request && $request->hasSession()) {
$session = $this->requestStack->getSession();
}

Expand Down
3 changes: 0 additions & 3 deletions Model/SfProfilerMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ final class SfProfilerMessage
*/
private $parameters;

/**
* @return SfProfilerMessage
*/
public static function create(array $data): self
{
$message = new self();
Expand Down
26 changes: 16 additions & 10 deletions Tests/Functional/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@

namespace Translation\Bundle\Tests\Functional;

use Nyholm\BundleTest\AppKernel;
use Nyholm\BundleTest\BaseBundleTestCase;
use Nyholm\BundleTest\TestKernel;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Component\HttpKernel\Kernel;
use Translation\Bundle\TranslationBundle;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
abstract class BaseTestCase extends BaseBundleTestCase
abstract class BaseTestCase extends KernelTestCase
{
/**
* @var AppKernel
* @var TestKernel
*/
protected $kernel;
protected $testKernel;

protected function getBundleClass(): string
{
Expand All @@ -34,13 +35,18 @@ protected function getBundleClass(): string

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

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

$this->kernel = $kernel;
$kernel->addTestBundle(TwigBundle::class);
$kernel->addTestBundle(TranslationBundle::class);

$this->testKernel = $kernel;

parent::setUp();
}
Expand Down
6 changes: 4 additions & 2 deletions Tests/Functional/BundleInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class BundleInitializationTest extends BaseTestCase
{
public function testRegisterBundle(): void
{
$this->bootKernel();
$container = $this->getContainer();
$kernel = $this->testKernel;
$kernel->boot();
$container = $kernel->getContainer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always actually use $this->getContainer() and it internally keeps track on whether the kernel is booted.

Copy link
Member Author

@bocharsky-bw bocharsky-bw Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but the problem is that I somehow cannot made this work with the parent $kernel so I created a custom testKernel property in BaseTestCase to add some config files and therefore we have to call getContainer() on it.

If anyone could rewrite it with the parent $kernel and self::getContainer() and get tests passed - it would be cool, I agree... but I'm really giving up with it


$this->assertTrue($container->has(ConfigurationManager::class));
$config = $container->get(ConfigurationManager::class);
$this->assertInstanceOf(ConfigurationManager::class, $config);
Expand Down
7 changes: 3 additions & 4 deletions Tests/Functional/Catalogue/CatalogueFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ public static function setUpBeforeClass(): void

public function testFetchCatalogue(): void
{
$this->bootKernel();

$this->catalogueFetcher = $this->getContainer()->get(CatalogueFetcher::class);
$this->testKernel->boot();
$this->catalogueFetcher = $this->testKernel->getContainer()->get(CatalogueFetcher::class);

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

$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');
$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
}
}
6 changes: 3 additions & 3 deletions Tests/Functional/Command/CheckMissingCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ protected function setUp(): void
{
parent::setUp();

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

file_put_contents(
__DIR__.'/../app/Resources/translations/messages.sv.xlf',
Expand Down
10 changes: 5 additions & 5 deletions Tests/Functional/Command/ExtractCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class ExtractCommandTest extends BaseTestCase
protected function setUp(): void
{
parent::setUp();
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');

$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');

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

public function testExecute(): void
{
$this->bootKernel();
$application = new Application($this->kernel);
$this->testKernel->boot();
$application = new Application($this->testKernel);

$container = $this->getContainer();
$container = $this->testKernel->getContainer();
$application->add($container->get(ExtractCommand::class));

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

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

Expand Down
9 changes: 5 additions & 4 deletions Tests/Functional/Command/StatusCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ class StatusCommandTest extends BaseTestCase
protected function setUp(): void
{
parent::setUp();
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');

$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
}

public function testExecute(): void
{
$this->bootKernel();
$application = new Application($this->kernel);
$this->testKernel->boot();
$application = new Application($this->testKernel);

$container = $this->getContainer();
$container = $this->testKernel->getContainer();
$application->add($container->get(StatusCommand::class));

$command = $application->find('translation:status');
Expand Down
9 changes: 5 additions & 4 deletions Tests/Functional/Command/SyncCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class SyncCommandTest extends BaseTestCase
protected function setUp(): void
{
parent::setUp();
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');

$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');

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

public function testExecute(): void
{
$this->bootKernel();
$application = new Application($this->kernel);
$this->testKernel->boot();
$application = new Application($this->testKernel);

$container = $this->getContainer();
$container = $this->testKernel->getContainer();
$application->add($container->get(SyncCommand::class));

$command = $application->find('translation:sync');
Expand Down
7 changes: 4 additions & 3 deletions Tests/Functional/Controller/EditInPlaceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static function setUpBeforeClass(): void
protected function setUp(): void
{
parent::setUp();
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');

$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
}

public function testEditAction(): void
Expand All @@ -58,7 +59,7 @@ public function testEditAction(): void
'messages|key0' => 'trans0',
'messages|key1' => 'trans1',
]));
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
}

Expand All @@ -68,7 +69,7 @@ public function testEditActionError(): void
'messages|key0' => 'trans0',
'messages|' => 'trans1',
]));
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(400, $response->getStatusCode());
}
}
32 changes: 23 additions & 9 deletions Tests/Functional/Controller/EditInPlaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
namespace Translation\Bundle\Tests\Functional\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Kernel;
use Translation\Bundle\EditInPlace\Activator;
use Translation\Bundle\Tests\Functional\BaseTestCase;

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

// Activate the feature
$this->getContainer()->get(Activator::class)->activate();
$activator = $this->testKernel->getContainer()->get(Activator::class);
$session = new Session(new MockArraySessionStorage());
$activator->setSession($session);
$activator->activate();

$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);

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

public function testIfUntranslatableLabelGetsDisabled(): void
{
$this->kernel->addConfigFile(__DIR__.'/../app/config/disabled_label.yaml');
if (Kernel::VERSION_ID < 50300) {
$this->testKernel->addTestConfig(__DIR__.'/../app/config/disabled_label_legacy.yaml');
} else {
$this->testKernel->addTestConfig(__DIR__.'/../app/config/disabled_label.yaml');
}
$this->testKernel->boot();
$request = Request::create('/foobar');

// Activate the feature
$this->bootKernel();
$this->getContainer()->get(Activator::class)->activate();
$activator = $this->testKernel->getContainer()->get(Activator::class);
$session = new Session(new MockArraySessionStorage());
$activator->setSession($session);
$activator->activate();

$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);

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

public function testDeactivatedTest(): void
{
$this->bootKernel();
$this->testKernel->boot();

$request = Request::create('/foobar');
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);

self::assertSame(200, $response->getStatusCode());
self::assertStringNotContainsString('x-trans', $response->getContent());
Expand Down
21 changes: 11 additions & 10 deletions Tests/Functional/Controller/WebUIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,25 @@ public static function setUpBeforeClass(): void
protected function setUp(): void
{
parent::setUp();
$this->kernel->addConfigFile(__DIR__.'/../app/config/normal_config.yaml');

$this->testKernel->addTestConfig(__DIR__.'/../app/config/normal_config.yaml');
}

public function testIndexAction(): void
{
$request = Request::create('/_trans', 'GET');
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());

$request = Request::create('/_trans/app', 'GET');
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
}

public function testShowAction(): void
{
$request = Request::create('/_trans/app/en/messages', 'GET');
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
}

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

$request = Request::create('/_trans/app/sv/messages/new', 'POST', [], [], [], [], json_encode([
'key' => 'foo',
'message' => 'bar',
]));
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
}

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

$request = Request::create('/_trans/app/sv/messages', 'POST', [], [], [], [], json_encode([
'key' => 'key1',
'message' => 'bar',
]));
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
}

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

$request = Request::create('/_trans/app/sv/messages', 'DELETE', [], [], [], [], json_encode([
'key' => 'foo',
]));
$response = $this->kernel->handle($request);
$response = $this->testKernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
}
}
5 changes: 3 additions & 2 deletions Tests/Functional/app/Service/DummyHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@

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

use GuzzleHttp\Psr7\Response;
use Http\Client\HttpClient;
use Nyholm\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class DummyHttpClient implements HttpClient
{
public function sendRequest(RequestInterface $request)
public function sendRequest(RequestInterface $request): ResponseInterface
{
return new Response(200);
}
Expand Down
Loading