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

Skip to content

Commit 6df98e5

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

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,22 @@ public function __construct(ContainerInterface $container)
3838
* Warms up the cache.
3939
*
4040
* @param string $cacheDir The cache directory
41+
*
42+
* @throws \LogicException
4143
*/
4244
public function warmUp($cacheDir)
4345
{
4446
$router = $this->container->get('router');
4547

4648
if ($router instanceof WarmableInterface) {
4749
$router->warmUp($cacheDir);
50+
51+
return;
52+
} else {
53+
@trigger_error(sprintf('Passing a %s without implementing %s is deprecated since 4.1.', RouterInterface::class, WarmableInterface::class), \E_USER_DEPRECATED);
4854
}
55+
56+
throw new \LogicException(sprintf('%s should implement %s in order to trigger a warmUp.', get_class($router), WarmableInterface::class));
4957
}
5058

5159
/**
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 Psr\Container\ContainerInterface;
15+
use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
18+
use Symfony\Component\Routing\RouterInterface;
19+
20+
class RouterCacheWarmerTest extends TestCase
21+
{
22+
public function testWarmUpWithWarmebleInterface()
23+
{
24+
$containerMock = $this->getMockBuilder(ContainerInterface::class)->setMethods(array('get', 'has'))->getMock();
25+
26+
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmebleInterface::class)->setMethods(array('match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'))->getMock();
27+
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
28+
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
29+
30+
$routerCacheWarmer->warmUp('/tmp');
31+
$routerMock->expects($this->any())->method('warmUp')->with('/tmp')->willReturn('');
32+
$this->addToAssertionCount(1);
33+
}
34+
35+
/**
36+
* @expectedDeprecation Passing a Symfony\Component\Routing\RouterInterface without implementing Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface is deprecated since 4.1.
37+
* @group legacy
38+
*/
39+
public function testWarmUpWithoutWarmebleInterface()
40+
{
41+
$containerMock = $this->getMockBuilder(ContainerInterface::class)->setMethods(array('get', 'has'))->getMock();
42+
43+
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmebleInterface::class)->setMethods(array('match', 'generate', 'getContext', 'setContext', 'getRouteCollection'))->getMock();
44+
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
45+
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
46+
47+
$class = \LogicException::class;
48+
if (method_exists($this, 'expectException')) {
49+
$this->expectExceptionMessage(sprintf('%s should implement Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface in order to trigger a warmUp.', get_class($routerMock)));
50+
$this->expectException($class);
51+
} else {
52+
$this->setExpectedException($class, sprintf('%s should implement Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface in order to trigger a warmUp.', get_class($routerMock)));
53+
}
54+
55+
$routerCacheWarmer->warmUp('/tmp');
56+
}
57+
}
58+
59+
interface testRouterInterfaceWithWarmebleInterface extends RouterInterface, WarmableInterface
60+
{
61+
}
62+
63+
interface testRouterInterfaceWithoutWarmebleInterface extends RouterInterface
64+
{
65+
}

0 commit comments

Comments
 (0)