Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0458373

Browse files
author
Amrouche Hamza
committed
[FrameworkBundle] add a notice when passing a routerInterface with warmupInterface in RouterCacheWarmer
1 parent 6a40488 commit 0458373

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@ public function __construct(ContainerInterface $container)
3838
* Warms up the cache.
3939
*
4040
* @param string $cacheDir The cache directory
41+
* @throws \LogicException
4142
*/
4243
public function warmUp($cacheDir)
4344
{
4445
$router = $this->container->get('router');
4546

4647
if ($router instanceof WarmableInterface) {
4748
$router->warmUp($cacheDir);
49+
50+
return;
51+
} else {
52+
@trigger_error(sprintf('Passing a %s without implementing %s is deprecated since 4.1.', RouterInterface::class, WarmableInterface::class), \E_USER_DEPRECATED);
4853
}
54+
55+
throw new \LogicException(sprintf('%s should implement %s in order to trigger a warmUp.', get_class($router), WarmableInterface::class));
4956
}
5057

5158
/**
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
13+
14+
use PHPUnit\Framework\Error\Notice;
15+
use Psr\Container\ContainerInterface;
16+
use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
19+
use Symfony\Component\Routing\RouterInterface;
20+
21+
class RouterCacheWarmerTest extends TestCase
22+
{
23+
public function testWarmUpWithWarmebleInterface()
24+
{
25+
$containerMock = $this->getMockBuilder(ContainerInterface::class)->setMethods(array('get', 'has'))->getMock();
26+
27+
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmebleInterface::class)->setMethods(array('match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'))->getMock();
28+
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
29+
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
30+
31+
$routerCacheWarmer->warmUp('/tmp');
32+
$routerMock->expects($this->any())->method('warmUp')->with('/tmp')->willReturn('');
33+
$this->addToAssertionCount(1);
34+
}
35+
36+
/**
37+
* @expectedDeprecation Passing a Symfony\Component\Routing\RouterInterface without implementing Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface is deprecated since 4.1.
38+
* @group legacy
39+
*/
40+
public function testWarmUpWithoutWarmebleInterface()
41+
{
42+
$containerMock = $this->getMockBuilder(ContainerInterface::class)->setMethods(array('get', 'has'))->getMock();
43+
44+
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmebleInterface::class)->setMethods(array('match', 'generate', 'getContext', 'setContext', 'getRouteCollection'))->getMock();
45+
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
46+
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
47+
48+
$class = \LogicException::class;
49+
if (method_exists($this, 'expectException')) {
50+
$this->expectExceptionMessage(sprintf('%s should implement Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface in order to trigger a warmUp.', get_class($routerMock)));
51+
$this->expectException($class);
52+
} else {
53+
$this->setExpectedException($class, sprintf('%s should implement Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface in order to trigger a warmUp.', get_class($routerMock)));
54+
}
55+
56+
$routerCacheWarmer->warmUp('/tmp');
57+
}
58+
}
59+
60+
interface testRouterInterfaceWithWarmebleInterface extends RouterInterface, WarmableInterface
61+
{
62+
}
63+
64+
interface testRouterInterfaceWithoutWarmebleInterface extends RouterInterface
65+
{
66+
}

0 commit comments

Comments
 (0)