From 69271dbb86f29adcf484da4f73e1776d84ec8423 Mon Sep 17 00:00:00 2001 From: "Alex \"Pierstoval\" Ancelet" Date: Mon, 16 Mar 2015 10:53:24 +0100 Subject: [PATCH] Fix BC break with Symfony 2.3 and 2.4 --- Listeners/FlushTranslations.php | 2 +- Listeners/LocaleListener.php | 3 +- .../Controller/TranslationControllerTest.php | 9 +++-- Tests/Fixtures/AbstractTestCase.php | 13 +++++--- Tests/Fixtures/App/AppKernel.php | 4 +-- Tests/Translator/ExtractionTest.php | 33 +++++++++++-------- Tests/Translator/TranslatorTest.php | 23 +++++-------- Tests/bootstrap.php | 4 +++ 8 files changed, 52 insertions(+), 39 deletions(-) diff --git a/Listeners/FlushTranslations.php b/Listeners/FlushTranslations.php index d44ad0b..139c49e 100644 --- a/Listeners/FlushTranslations.php +++ b/Listeners/FlushTranslations.php @@ -35,7 +35,7 @@ function flushTranslations() public static function getSubscribedEvents() { return array( - KernelEvents::FINISH_REQUEST => array('flushTranslations'), + 'kernel.finish_request' => array('flushTranslations'), KernelEvents::TERMINATE => array('flushTranslations'), KernelEvents::EXCEPTION => array('flushTranslations') ); diff --git a/Listeners/LocaleListener.php b/Listeners/LocaleListener.php index d2e6140..ec3413b 100644 --- a/Listeners/LocaleListener.php +++ b/Listeners/LocaleListener.php @@ -12,6 +12,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; class LocaleListener implements EventSubscriberInterface @@ -26,7 +27,7 @@ public function __construct($defaultLocale = 'en') public function onKernelRequest(GetResponseEvent $event) { $request = $event->getRequest(); - if (!$request->hasPreviousSession()) { + if (!$request->hasPreviousSession() || $event->getRequestType() !== HttpKernelInterface::MASTER_REQUEST) { return; } diff --git a/Tests/Controller/TranslationControllerTest.php b/Tests/Controller/TranslationControllerTest.php index c12f075..1af335b 100644 --- a/Tests/Controller/TranslationControllerTest.php +++ b/Tests/Controller/TranslationControllerTest.php @@ -29,9 +29,12 @@ public function testAdminAndExport() $exportLink = $crawler->filter('div.container > h1 + a'); $translationsContainer = $crawler->filter('div.container .row > div.alert'); - $this->assertContains('alert-danger', $translationsContainer->attr('class')); - $this->assertContains('/admin/translations/export', $exportLink->attr('href')); - $this->assertGreaterThan(0, $exportLink->count()); + $this->assertGreaterThan(0, count($translationsContainer)); + if (count($translationsContainer)) { + $this->assertContains('alert-danger', $translationsContainer->attr('class')); + $this->assertContains('/admin/translations/export', $exportLink->attr('href')); + $this->assertGreaterThan(0, $exportLink->count()); + } $crawler->clear(); $crawler = $client->click($exportLink->link('GET')); diff --git a/Tests/Fixtures/AbstractTestCase.php b/Tests/Fixtures/AbstractTestCase.php index 47203e8..cbb0b0c 100644 --- a/Tests/Fixtures/AbstractTestCase.php +++ b/Tests/Fixtures/AbstractTestCase.php @@ -32,13 +32,18 @@ class AbstractTestCase extends WebTestCase */ protected function getKernel(array $options = array()) { - if (!static::$kernel) { - static::bootKernel($options); - } - return static::$kernel; } + public function setUp() + { + if (static::$kernel) { + static::$kernel->shutdown(); + } + static::$kernel = static::createKernel(array()); + static::$kernel->boot(); + } + /** * Generates tokens according to normally Translator behavior * @param $source diff --git a/Tests/Fixtures/App/AppKernel.php b/Tests/Fixtures/App/AppKernel.php index bffd43b..a5a63bb 100644 --- a/Tests/Fixtures/App/AppKernel.php +++ b/Tests/Fixtures/App/AppKernel.php @@ -44,7 +44,7 @@ public function registerContainerConfiguration(LoaderInterface $loader) */ public function getCacheDir() { - return sys_get_temp_dir().'/OrbitaleTranslationBundle/cache'; + return __DIR__.'/../../../build/cache/'; } /** @@ -52,6 +52,6 @@ public function getCacheDir() */ public function getLogDir() { - return sys_get_temp_dir().'/OrbitaleTranslationBundle/logs'; + return __DIR__.'/../../../kernel_logs/'; } } diff --git a/Tests/Translator/ExtractionTest.php b/Tests/Translator/ExtractionTest.php index 1d5f171..1c79ab9 100644 --- a/Tests/Translator/ExtractionTest.php +++ b/Tests/Translator/ExtractionTest.php @@ -16,29 +16,26 @@ use Orbitale\Bundle\TranslationBundle\Tests\Fixtures\AbstractTestCase; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Output\BufferedOutput; +use Symfony\Component\Console\Output\StreamOutput; use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Yaml\Yaml; class ExtractionTest extends AbstractTestCase { - /** * @var EntityManager */ protected $em; - public function __construct($name = null, array $data = array(), $dataName = '') - { - parent::__construct($name, $data, $dataName); - $this->em = $this->getKernel()->getContainer()->get('doctrine')->getManager(); - } - /** * {@inheritdoc} */ - protected function setUp() + public function setUp() { + parent::setUp(); + $this->em = static::$kernel->getContainer()->get('doctrine')->getManager(); + // Ensure the table is empty $connection = $this->em->getConnection(); $connection->query($connection->getDatabasePlatform()->getTruncateTableSQL('orbitale_translations', true)); @@ -77,8 +74,6 @@ private function processDummyDatas() public function testSuccessfulExtractionCommand() { - static::bootKernel(); - $dummyDatas = $this->processDummyDatas(); $outputDirectory = $this->getKernel()->getContainer()->getParameter('kernel.root_dir').'/../../../vendor/_translations/'; @@ -100,7 +95,7 @@ public function testSuccessfulExtractionCommand() '--output-directory' => $outputDirectory, '--dirty' => true, )); - $output = new BufferedOutput(OutputInterface::VERBOSITY_DEBUG); + $output = new NullOutput(OutputInterface::VERBOSITY_QUIET); $command->run($arrayInput, $output); $shouldBeThereFiles = array( @@ -165,9 +160,19 @@ public function testSuccessfulExtractionCommand() '--output-directory' => $outputDirectory, '--dirty' => true, )); - $output = new BufferedOutput(OutputInterface::VERBOSITY_DEBUG); - $commandOutput = $command->run($arrayInput, $output); - $output = $output->fetch(); + + if (class_exists('Symfony\Component\Console\Output\BufferedOutput')) { + $output = new BufferedOutput(OutputInterface::VERBOSITY_DEBUG); + $commandOutput = $command->run($arrayInput, $output); + $output = $output->fetch(); + } else { + $fileName = $this->getKernel()->getRootDir().'/../../../build/_console.test.output.log'; + $f = fopen($fileName, 'w'); + $output = new StreamOutput($f, OutputInterface::VERBOSITY_DEBUG); + $commandOutput = $command->run($arrayInput, $output); + fclose($f); + $output = file_get_contents($fileName); + } $this->assertGreaterThan(0, $commandOutput); $this->assertContains('Wrong output format', $output); $this->assertContains('An unknown error has occurred, please check your configuration and datas.', $output); diff --git a/Tests/Translator/TranslatorTest.php b/Tests/Translator/TranslatorTest.php index 1983548..5258dd2 100644 --- a/Tests/Translator/TranslatorTest.php +++ b/Tests/Translator/TranslatorTest.php @@ -19,6 +19,10 @@ class TranslatorTest extends AbstractTestCase { + /** + * @var EntityManager + */ + protected $em; /** * @var Translator @@ -26,24 +30,17 @@ class TranslatorTest extends AbstractTestCase protected $translator; /** - * @var EntityManager + * {@inheritdoc} */ - protected $em; - - public function __construct($name = null, array $data = array(), $dataName = '') + public function setUp() { - parent::__construct($name, $data, $dataName); + parent::setUp(); + $this->em = $this->getKernel()->getContainer()->get('doctrine')->getManager(); + $this->translator = $this->getKernel()->getContainer()->get('orbitale_translator'); $this->translator->setFlushStrategy(Translator::FLUSH_RUNTIME); $this->translator->setFallbackLocales(array($this->getKernel()->getContainer()->getParameter('locale'))); - $this->em = $this->getKernel()->getContainer()->get('doctrine')->getManager(); - } - /** - * {@inheritdoc} - */ - protected function setUp() - { $this->translator->emptyCatalogue(); // Ensure the table is empty @@ -58,7 +55,6 @@ public function testManualConstruct() { $exception = ''; try { - $this->bootKernel(); $translator = new Translator($this->getKernel()->getContainer(), new MessageSelector()); $translator->__construct($this->getKernel()->getContainer(), new MessageSelector()); } catch (\Exception $e) { @@ -406,7 +402,6 @@ public function testTranslationsLikeDomain() public function testDestructFlush() { - $this->bootKernel(); $translator = new Translator($this->getKernel()->getContainer(), new MessageSelector()); $translator->emptyCatalogue(); $translator->setFlushStrategy(Translator::FLUSH_TERMINATE); diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index 4912c47..53a15d0 100644 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -26,6 +26,10 @@ unlink($dbFile); } +if (!is_dir(__DIR__.'/../build')) { + mkdir(__DIR__.'/../build'); +} + include __DIR__.'/Fixtures/App/AppKernel.php'; $application = new Application(new AppKernel('test', true));