Skip to content

Commit

Permalink
Solve Symfony 4.x deprecations (#184)
Browse files Browse the repository at this point in the history
* Allowed testing of PHP 7.3 With Symfony 4.2 in testing matrix and fixed tests

* Removed useless boostrap file

* Removed unmaintained version from travis testing matrix

* Solve Symfony 4.x deprecations

* Re-enable strict deprecation helper for all Travis envs

* Solve last Symfony 4.x deprecations
  • Loading branch information
ostrolucky authored and yann-eugone committed Sep 26, 2019
1 parent afa677e commit 691e45c
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 83 deletions.
22 changes: 6 additions & 16 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@ language: php

matrix:
include:
- php: 5.6
env: SYMFONY_VERSION=2.3.*
- php: 5.6
env: SYMFONY_VERSION=2.7.*
- php: 5.6
env: SYMFONY_VERSION=2.8.*
- php: 5.6
env: SYMFONY_VERSION=3.0.*
- php: 7.0
env: SYMFONY_VERSION=3.2.*
- php: 7.0
env: SYMFONY_VERSION=3.3.*
- php: 7.0
env: SYMFONY_VERSION=3.4.*
- php: 7.1
env: SYMFONY_VERSION=3.4.*
- php: 7.1
env: SYMFONY_VERSION=4.0.*
- php: 7.2
env: SYMFONY_VERSION=3.4.* SYMFONY_DEPRECATIONS_HELPER=weak
- php: 7.2
env: SYMFONY_VERSION=4.0.* SYMFONY_DEPRECATIONS_HELPER=weak
env: SYMFONY_VERSION=3.4.*
- php: 7.3
env: SYMFONY_VERSION=3.4.*
- php: 7.3
env: SYMFONY_VERSION=4.2.*

env:
global:
Expand All @@ -40,7 +30,7 @@ before_install:
- if [ "$PHPCS" = "yes" ]; then phpenv rehash; fi
- if [ "$PHPCS" != "yes"]; then composer selfupdate; fi
- if [ "$SYMFONY_VERSION" != "" ]; then composer require --no-update symfony/symfony:${SYMFONY_VERSION}; fi
- if [ "$SYMFONY_VERSION" = "3.4.*" ] || [ "$SYMFONY_VERSION" = "4.0.*" ]; then rm -f phpunit.xml; cp phpunit.sf4.xml.dist phpunit.xml; fi
- if [ "$SYMFONY_VERSION" = "3.4.*" ] || [ "$SYMFONY_VERSION" = "4.2.*" ]; then rm -f phpunit.xml; cp phpunit.sf4.xml.dist phpunit.xml; fi

install: php -d memory_limit=-1 $(phpenv which composer) update --no-suggest --prefer-dist

Expand Down
47 changes: 30 additions & 17 deletions Controller/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,53 @@
namespace Presta\SitemapBundle\Controller;

use Presta\SitemapBundle\Service\GeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
* Provides action to render sitemap files
*
* @author David Epely <depely@prestaconcept.net>
*/
class SitemapController extends Controller
class SitemapController
{
/**
* @var GeneratorInterface
*/
private $generator;

/**
* Time to live of the response in seconds
*
* @var int
*/
private $ttl;

/**
* @param int $ttl
*/
public function __construct(GeneratorInterface $generator, $ttl)
{
$this->generator = $generator;
$this->ttl = $ttl;
}

/**
* list sitemaps
*
* @return Response
*/
public function indexAction()
{
$sitemapindex = $this->getGenerator()->fetch('root');
$sitemapindex = $this->generator->fetch('root');

if (!$sitemapindex) {
throw $this->createNotFoundException();
throw new NotFoundHttpException('Not found');
}

$response = Response::create($sitemapindex->toXml());
$response->setPublic();
$response->setClientTtl($this->getTtl());
$response->setClientTtl($this->ttl);

return $response;
}
Expand All @@ -51,15 +72,15 @@ public function indexAction()
*/
public function sectionAction($name)
{
$section = $this->getGenerator()->fetch($name);
$section = $this->generator->fetch($name);

if (!$section) {
throw $this->createNotFoundException();
throw new NotFoundHttpException('Not found');
}

$response = Response::create($section->toXml());
$response->setPublic();
$response->setClientTtl($this->getTtl());
$response->setClientTtl($this->ttl);

return $response;
}
Expand All @@ -71,14 +92,6 @@ public function sectionAction($name)
*/
protected function getTtl()
{
return $this->container->getParameter('presta_sitemap.timetolive');
}

/**
* @return GeneratorInterface
*/
private function getGenerator()
{
return $this->get('presta_sitemap.generator');
return $this->ttl;
}
}
4 changes: 2 additions & 2 deletions Resources/config/routing.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
PrestaSitemapBundle_index:
path: "/%presta_sitemap.sitemap_file_prefix%.{_format}"
defaults: { _controller: PrestaSitemapBundle:Sitemap:index }
defaults: { _controller: Presta\SitemapBundle\Controller\SitemapController::index }
requirements:
_format: xml

PrestaSitemapBundle_section:
path: "/%presta_sitemap.sitemap_file_prefix%.{name}.{_format}"
defaults: { _controller: PrestaSitemapBundle:Sitemap:section }
defaults: { _controller: Presta\SitemapBundle\Controller\SitemapController::section }
requirements:
_format: xml
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
</service>

<service id="Presta\SitemapBundle\Service\DumperInterface" alias="presta_sitemap.dumper_default" />

<service id="presta_sitemap.controller" class="Presta\SitemapBundle\Controller\SitemapController">
<argument type="service" id="presta_sitemap.generator" />
<argument>%presta_sitemap.timetolive</argument>
</service>
</services>

</container>
16 changes: 10 additions & 6 deletions Tests/Command/DumpSitemapsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DumpSitemapsCommandTest extends WebTestCase
/**
* @var ContainerInterface
*/
private $container;
protected static $container;

private $fixturesDir;

Expand All @@ -44,13 +44,16 @@ protected function setUp()
$this->webDir = realpath(__DIR__ . '/../web');

self::createClient(['debug' => false]);
$this->container = self::$kernel->getContainer();
$router = $this->container->get('router');
if (self::$container === null) {
self::$container = self::$kernel->getContainer();
}

$router = self::$container->get('router');
/* @var $router RouterInterface */

$router->getContext()->fromRequest(Request::create('http://sitemap.php54.local'));

$this->container->get('event_dispatcher')
self::$container->get('event_dispatcher')
->addListener(
SitemapPopulateEvent::ON_SITEMAP_POPULATE,
function (SitemapPopulateEvent $event) use ($router) {
Expand All @@ -75,6 +78,7 @@ function (SitemapPopulateEvent $event) use ($router) {
protected function tearDown()
{
parent::tearDown();
self::$container = null;
foreach (glob($this->webDir . '/*{.xml,.xml.gz}', GLOB_BRACE) as $file) {
unlink($file);
}
Expand Down Expand Up @@ -129,8 +133,8 @@ private function executeDumpWithOptions(array $input = array())
$application = new Application(self::$kernel);
$application->add(
new DumpSitemapsCommand(
$this->container->get('router'),
new Dumper($this->container->get('event_dispatcher'), $this->container->get('filesystem')),
self::$container->get('router'),
new Dumper(self::$container->get('event_dispatcher'), self::$container->get('filesystem')),
'public'
)
);
Expand Down
20 changes: 15 additions & 5 deletions Tests/Controller/SitemapControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ class SitemapControllerTest extends WebTestCase
/**
* @var ContainerInterface
*/
private $container;
protected static $container;

public function setUp()
{
//boot appKernel
self::createClient(['debug' => false]);
$this->container = static::$kernel->getContainer();
if (self::$container === null) {
self::$container = self::$kernel->getContainer();
}

//set controller to test
$this->controller = new Controller\SitemapController();
$this->controller->setContainer($this->container);
$this->controller = new Controller\SitemapController(
self::$container->get('presta_sitemap.generator'),
3600
);

//-------------------
// add url to sitemap
$this->container->get('event_dispatcher')
self::$container->get('event_dispatcher')
->addListener(
SitemapPopulateEvent::ON_SITEMAP_POPULATE,
function (SitemapPopulateEvent $event) {
Expand All @@ -59,6 +63,12 @@ function (SitemapPopulateEvent $event) {
//-------------------
}

protected function tearDown()
{
parent::tearDown();
self::$container = null;
}

public function testIndexAction()
{
$response = $this->controller->indexAction();
Expand Down
29 changes: 26 additions & 3 deletions Tests/Service/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,47 @@
use Presta\SitemapBundle\Service\Generator;
use Presta\SitemapBundle\Sitemap;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* @author David Epely <depely@prestaconcept.net>
*/
class GeneratorTest extends WebTestCase
{
/**
* @var Generator
*/
protected $generator;
/** @var EventDispatcherInterface */
private $eventDispatcher;

/**
* @var ContainerInterface
*/
protected static $container;

public function setUp()
{
self::createClient(['debug' => false]);
$container = static::$kernel->getContainer();
$this->eventDispatcher = $container->get('event_dispatcher');
if (self::$container === null) {
self::$container = self::$kernel->getContainer();
}
$this->eventDispatcher = self::$container->get('event_dispatcher');

$this->generator = new Generator(
$this->eventDispatcher,
self::$container->get('router'),
null,
null,
1
);
}

$this->generator = new Generator($this->eventDispatcher, $container->get('router'), null, null, 1);
protected function tearDown()
{
parent::tearDown();
self::$container = null;
}

public function testGenerate()
Expand Down
4 changes: 2 additions & 2 deletions Tests/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public function registerBundles()
public function registerContainerConfiguration(LoaderInterface $loader)
{
// We dont need that Environment stuff, just one config
if (version_compare(self::VERSION, '3.4.0-RC1', '>=')) {
$loader->load(__DIR__.'/config.sf4.yml');
if (version_compare(self::VERSION, '3.4.0-RC1', '>=') && version_compare(self::VERSION, '4.1', '<')) {
$loader->load(__DIR__.'/config.sf3.yml');
} else {
$loader->load(__DIR__.'/config.yml');
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion Tests/app/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ PrestaSitemapBundle:

PrestaDemoBundle_homepage:
path: /
defaults: { _controller: PrestaSitemapBundle:Sitemap:index }
defaults: { _controller: Presta\SitemapBundle\Controller\SitemapController::indexAction }
29 changes: 0 additions & 29 deletions Tests/bootstrap.php

This file was deleted.

2 changes: 1 addition & 1 deletion phpunit.sf4.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
<phpunit bootstrap="vendor/autoload.php" colors="true">
<php>
<server name="KERNEL_CLASS" value="AppKernel" />
</php>
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
<phpunit bootstrap="vendor/autoload.php" colors="true">
<php>
<server name="KERNEL_DIR" value="Tests/app" />
</php>
Expand Down

0 comments on commit 691e45c

Please sign in to comment.