Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Add compatibility of symfony 4 (#72)
Browse files Browse the repository at this point in the history
* Add compatibility with symfony 4
  • Loading branch information
aivus authored and Gilles Gauthier committed Nov 24, 2017
1 parent 7da0160 commit 4b8a990
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ matrix:
env: SYMFONY_VERSION='2.8.*'
- php: 7.1
env: SYMFONY_VERSION='^3.0'
- php: 7.1
env: DEPENDENCIES=beta

before_install:
- composer self-update
- if [ "$DEPENDENCIES" = "beta" ]; then composer config minimum-stability beta; fi;
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev symfony/framework-bundle:${SYMFONY_VERSION} --no-update; fi

install: composer update $COMPOSER_FLAGS
Expand Down
37 changes: 14 additions & 23 deletions Tests/EventListener/MaintenanceListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
namespace Lexik\Bundle\MaintenanceBundle\Tests\EventListener;

use Lexik\Bundle\MaintenanceBundle\Drivers\DriverFactory;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Lexik\Bundle\MaintenanceBundle\Tests\TestHelper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Translation\MessageSelector;

/**
* Test for the maintenance listener
Expand All @@ -34,7 +33,7 @@ public function testBaseRequest()
$driverOptions = array('class' => DriverFactory::DATABASE_DRIVER, 'options' => null);

$request = Request::create('http://test.com/foo?bar=baz');
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$this->container = $this->initContainer();
Expand Down Expand Up @@ -68,7 +67,7 @@ public function testPathFilter()
$driverOptions = array('class' => DriverFactory::DATABASE_DRIVER, 'options' => null);

$request = Request::create('http://test.com/foo?bar=baz');
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$this->container = $this->initContainer();
Expand Down Expand Up @@ -99,7 +98,7 @@ public function testHostFilter()
$driverOptions = array('class' => DriverFactory::DATABASE_DRIVER, 'options' => null);

$request = Request::create('http://test.com/foo?bar=baz');
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$this->container = $this->initContainer();
Expand Down Expand Up @@ -133,7 +132,7 @@ public function testIPFilter()
$driverOptions = array('class' => DriverFactory::DATABASE_DRIVER, 'options' => null);

$request = Request::create('http://test.com/foo?bar=baz');
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$this->container = $this->initContainer();
Expand Down Expand Up @@ -167,7 +166,7 @@ public function testRouteFilter($debug, $route, $expected)
$request = Request::create('');
$request->attributes->set('_route', $route);

$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$this->container = $this->initContainer();
Expand Down Expand Up @@ -214,7 +213,7 @@ public function testQueryFilter()

$request = Request::create('http://test.com/foo?bar=baz');
$postRequest = Request::create('http://test.com/foo?bar=baz', 'POST');
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
$postEvent = new GetResponseEvent($kernel, $postRequest, HttpKernelInterface::MASTER_REQUEST);

Expand Down Expand Up @@ -255,7 +254,7 @@ public function testCookieFilter()
$driverOptions = array('class' => DriverFactory::DATABASE_DRIVER, 'options' => null);

$request = Request::create('http://test.com/foo', 'GET', array(), array('bar' => 'baz'));
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);

$this->container = $this->initContainer();
Expand Down Expand Up @@ -338,19 +337,11 @@ protected function getDatabaseDriver($lock = false)
*/
public function getTranslator()
{
if (Kernel::VERSION_ID < 30300) {
$translator = new Translator(
$this->container,
$this->getMock('Symfony\Component\Translation\MessageSelector')
);
} else {
$translator = new Translator(
$this->container,
$this->getMock('Symfony\Component\Translation\MessageSelector'),
'en'
);
}
/** @var MessageSelector|\PHPUnit_Framework_MockObject_MockObject $messageSelector */
$messageSelector = $this->getMockBuilder('Symfony\Component\Translation\MessageSelector')
->disableOriginalConstructor()
->getMock();

return $translator;
return TestHelper::getTranslator($this->container, $messageSelector);
}
}
24 changes: 8 additions & 16 deletions Tests/Maintenance/DriverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Lexik\Bundle\MaintenanceBundle\Tests\Maintenance;

use Lexik\Bundle\MaintenanceBundle\Drivers\DriverFactory;
use Lexik\Bundle\MaintenanceBundle\Tests\TestHelper;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Lexik\Bundle\MaintenanceBundle\Drivers\DriverFactory;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Translation\MessageSelector;

/**
* Test driver factory
Expand Down Expand Up @@ -102,19 +102,11 @@ protected function getDatabaseDriver()

public function getTranslator()
{
if (Kernel::VERSION_ID < 30300) {
$translator = new Translator(
$this->container,
$this->getMock('Symfony\Component\Translation\MessageSelector')
);
} else {
$translator = new Translator(
$this->container,
$this->getMock('Symfony\Component\Translation\MessageSelector'),
'en'
);
}
/** @var MessageSelector|\PHPUnit_Framework_MockObject_MockObject $messageSelector */
$messageSelector = $this->getMockBuilder('Symfony\Component\Translation\MessageSelector')
->disableOriginalConstructor()
->getMock();

return $translator;
return TestHelper::getTranslator($this->container, $messageSelector);
}
}
25 changes: 8 additions & 17 deletions Tests/Maintenance/FileMaintenanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Lexik\Bundle\MaintenanceBundle\Tests\Maintenance;

use Lexik\Bundle\MaintenanceBundle\Drivers\FileDriver;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Lexik\Bundle\MaintenanceBundle\Tests\TestHelper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Translation\MessageSelector;

/**
* Test driver file
Expand Down Expand Up @@ -135,19 +134,11 @@ protected function initContainer()

public function getTranslator()
{
if (Kernel::VERSION_ID < 30300) {
$translator = new Translator(
$this->container,
$this->getMock('Symfony\Component\Translation\MessageSelector')
);
} else {
$translator = new Translator(
$this->container,
$this->getMock('Symfony\Component\Translation\MessageSelector'),
'en'
);
}

return $translator;
/** @var MessageSelector|\PHPUnit_Framework_MockObject_MockObject $messageSelector */
$messageSelector = $this->getMockBuilder('Symfony\Component\Translation\MessageSelector')
->disableOriginalConstructor()
->getMock();

return TestHelper::getTranslator($this->container, $messageSelector);
}
}
39 changes: 39 additions & 0 deletions Tests/TestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Lexik\Bundle\MaintenanceBundle\Tests;

use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Translation\Formatter\MessageFormatter;
use Symfony\Component\Translation\MessageSelector;

class TestHelper
{
public static function getTranslator(ContainerBuilder $container, MessageSelector $messageSelector)
{
if (Kernel::VERSION_ID < 30300) {
// symfony 2
$translator = new Translator(
$container,
$messageSelector
);
} elseif (Kernel::VERSION_ID >= 30300 && Kernel::VERSION_ID < 40000) {
// symfony 3
$translator = new Translator(
$container,
$messageSelector,
'en'
);
} else {
// symfony 4
$translator = new Translator(
$container,
new MessageFormatter($messageSelector),
'en'
);
}

return $translator;
}
}
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
],
"require": {
"php": ">=5.3.9",
"symfony/framework-bundle": "~2.7|~3.0",
"symfony/translation": "~2.7|~3.0"
"symfony/framework-bundle": "~2.7|~3.0|^4.0",
"symfony/translation": "~2.7|~3.0|^4.0"
},
"require-dev": {
"symfony/phpunit-bridge": "~2.7|~3.0",
"phpunit/phpunit": "~4.8"
"symfony/phpunit-bridge": "~2.7|~3.0|^4.0",
"phpunit/phpunit": "~4.8|~5.0"
},
"autoload": {
"psr-4": { "Lexik\\Bundle\\MaintenanceBundle\\": "" }
Expand Down

0 comments on commit 4b8a990

Please sign in to comment.