Passed
Push — master ( fff9a6...600230 )
by Andy
02:12
created

AbstractTest::getRouter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
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
    /**
16
     * @param RouteCollection|null $routeCollection
17
     *
18
     * @return Router
19
     */
20
    protected function getRouter($routeCollection = null)
21
    {
22
        if (!$routeCollection) {
23
            $routeCollection = $this->getFooRouteCollection();
24
        }
25
26
        $loader = $this->createMock(LoaderInterface::class);
27
        $loader->method('load')->willReturn($routeCollection);
28
29
        /** @var LoaderInterface $loader */
30
        $context = new RequestContext();
31
        $context->setScheme('https')->setHost('example.org');
32
33
        $router = new Router($loader, '');
34
        $router->setContext($context);
35
36
        return $router;
37
    }
38
39
    /**
40
     * @return RouteCollection
41
     */
42
    protected function getFooRouteCollection()
43
    {
44
        $routeCollection = new RouteCollection();
45
        $routeCollection->add('foo', new Route('/foo'));
46
        $routeCollection->add('baz', new Route('/baz/'));
47
        $routeCollection->setHost('example.org');
48
49
        return $routeCollection;
50
    }
51
52
    /**
53
     * @param bool $secure
54
     * @param bool $trailingSlash
55
     * @return Request
56
     */
57 View Code Duplication
    protected function getFooRequest($secure = true, $trailingSlash = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
58
    {
59
        $scheme = ($secure) ? 'https' : 'http';
60
        $uri    = "$scheme://example.org/foo";
61
62
        if ($trailingSlash) {
63
            $uri .= '/';
64
        }
65
66
        $request = Request::create($uri);
67
        $request->attributes->set('_route', 'foo');
68
69
        return $request;
70
    }
71
72
    /**
73
     * @param bool $secure
74
     * @param bool $trailingSlash
75
     * @return Request
76
     */
77 View Code Duplication
    protected function getBazRequest($secure = true, $trailingSlash = true)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
78
    {
79
        $scheme = ($secure) ? 'https' : 'http';
80
        $uri    = "$scheme://example.org/baz";
81
82
        if ($trailingSlash) {
83
            $uri .= '/';
84
        }
85
86
        $request = Request::create($uri);
87
        $request->attributes->set('_route', 'baz');
88
89
        return $request;
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function configProvider()
96
    {
97
        return [
98
            'config' => [
99
                [
100
                    'site_url'       => 'https://example.org',
101
                    'redirect'       => true,
102
                    'redirect_code'  => 302,
103
                    'trailing_slash' => false,
104
                ]
105
            ]
106
        ];
107
    }
108
}
109