1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\CanonicalUrlBundle\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\Routing\RequestContext; |
9
|
|
|
use Symfony\Component\Routing\Route; |
10
|
|
|
use Symfony\Component\Routing\RouteCollection; |
11
|
|
|
use Symfony\Component\Routing\Router; |
12
|
|
|
|
13
|
|
|
abstract class AbstractTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function getRouter($routeCollection = null) |
16
|
|
|
{ |
17
|
|
|
if (! $routeCollection) { |
18
|
|
|
$routeCollection = $this->getFooRouteCollection(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$loader = $this->createMock(LoaderInterface::class); |
22
|
|
|
$loader->method('load')->willReturn($routeCollection); |
23
|
|
|
|
24
|
|
|
/** @var LoaderInterface $loader */ |
25
|
|
|
$context = new RequestContext(); |
26
|
|
|
$context->setScheme('https')->setHost('example.org'); |
27
|
|
|
|
28
|
|
|
$router = new Router($loader, ''); |
29
|
|
|
$router->setContext($context); |
30
|
|
|
|
31
|
|
|
return $router; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getFooRouteCollection() |
35
|
|
|
{ |
36
|
|
|
$routeCollection = new RouteCollection(); |
37
|
|
|
$routeCollection->add('foo', new Route('/foo')); |
38
|
|
|
$routeCollection->setHost('example.org'); |
39
|
|
|
|
40
|
|
|
return $routeCollection; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param bool $secure |
45
|
|
|
* @return Request |
46
|
|
|
*/ |
47
|
|
|
protected function getFooRequest($secure = true) |
48
|
|
|
{ |
49
|
|
|
$scheme = ($secure) ? 'https' : 'http'; |
50
|
|
|
|
51
|
|
|
$request = Request::create("$scheme://example.org/foo/"); |
52
|
|
|
$request->attributes->set('_route', 'foo'); |
53
|
|
|
|
54
|
|
|
return $request; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function configProvider() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
return array( |
60
|
|
|
'config' => array( |
61
|
|
|
array( |
62
|
|
|
'site_url' => 'https://example.org', |
63
|
|
|
'redirect' => true, |
64
|
|
|
'redirect_code' => 302, |
65
|
|
|
'trailing_slash' => false, |
66
|
|
|
) |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.