AbstractTest::getMockRequest()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 13
rs 10
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);
0 ignored issues
show
Bug introduced by
The method method() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        $loader->/** @scrutinizer ignore-call */ 
28
                 method('load')->willReturn($routeCollection);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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('foo/{noop}', new Route('/foo/{noop}'));
47
        $routeCollection->add('baz', new Route('/baz/'));
48
        $routeCollection->setHost('example.org');
49
50
        return $routeCollection;
51
    }
52
53
    /**
54
     * @param bool $secure
55
     * @param bool $trailingSlash
56
     *
57
     * @return Request
58
     */
59
    protected function getMockRequest($path, $secure = true, $trailingSlash = true)
60
    {
61
        $scheme = ($secure) ? 'https' : 'http';
62
        $uri    = "$scheme://example.org/$path";
63
64
        if ($trailingSlash) {
65
            $uri .= '/';
66
        }
67
68
        $request = Request::create($uri);
69
        $request->attributes->set('_route', $path);
70
71
        return $request;
72
    }
73
74
    /**
75
     * @param bool $secure
76
     * @param bool $trailingSlash
77
     *
78
     * @return Request
79
     */
80
    protected function getFooRequest($secure = true, $trailingSlash = true)
81
    {
82
        return $this->getMockRequest('foo', $secure, $trailingSlash);
83
    }
84
85
    /**
86
     * @param bool $secure
87
     * @param bool $trailingSlash
88
     *
89
     * @return Request
90
     */
91
    protected function getBazRequest($secure = true, $trailingSlash = true)
92
    {
93
        return $this->getMockRequest('baz', $secure, $trailingSlash);
94
    }
95
96
    /**
97
     * @return array
98
     */
99
    public function configProvider()
100
    {
101
        return [
102
            'config' => [
103
                [
104
                    'site_url'       => 'https://example.org',
105
                    'redirect'       => true,
106
                    'redirect_code'  => 302,
107
                    'trailing_slash' => false,
108
                ],
109
            ],
110
        ];
111
    }
112
}
113