Passed
Push — master ( d1cfc2...9c8db4 )
by Antonio del
01:43
created

testNoConfigurationException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace DelOlmo\Middleware;
6
7
use DelOlmo\Middleware\SymfonyRouterMiddleware;
8
use Middlewares\Utils\Dispatcher;
9
use Middlewares\Utils\Factory;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
12
use Symfony\Component\Config\Loader\LoaderInterface;
13
use Symfony\Component\Routing\Exception\NoConfigurationException;
14
use Symfony\Component\Routing\Matcher\UrlMatcher;
15
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
16
use Symfony\Component\Routing\RequestContext;
17
use Symfony\Component\Routing\RouteCollection;
18
use Symfony\Component\Routing\Route;
19
use Symfony\Component\Routing\Router;
20
21
/**
22
 * @author Antonio del Olmo García <[email protected]>
23
 */
24
class SymfonyRouterMiddlewareTest extends TestCase
25
{
26
27
    /**
28
     *
29
     * @var \Symfony\Component\Routing\RouteCollection
30
     */
31
    private $routes = null;
32
33
    /**
34
     *
35
     * @var \Symfony\Component\Routing\Router
36
     */
37
    private $router = null;
38
39
    /**
40
     *
41
     * @var \Symfony\Component\Config\Loader\LoaderInterface
42
     */
43
    private $loader = null;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function setUp()
49
    {
50
        $this->loader = $this->getMockBuilder(LoaderInterface::class)->getMock();
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->getMockBuilder(Sy...face::class)->getMock() of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\Config\Loader\LoaderInterface of property $loader.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
        $this->router = new Router($this->loader, 'routing.yml');
52
53
        $this->routes = new RouteCollection();
54
        $this->routes->add('test', new Route('/users', [], [], [], '', [], ['GET']));
55
    }
56
57
    /**
58
     *
59
     */
60
    public function testResourceNotFoundException()
61
    {
62
        $request = Factory::createServerRequest('GET', '/posts');
63
64
        $factory = new HttpFoundationFactory();
65
66
        $symfonyRequest = $factory->createRequest($request);
67
68
        $context = (new RequestContext())
69
            ->fromRequest($symfonyRequest);
70
71
        $matcher = new UrlMatcher($this->routes, $context);
72
73
        $p = new \ReflectionProperty($this->router, 'matcher');
74
75
        $p->setAccessible(true);
76
77
        $p->setValue($this->router, $matcher);
78
79
        $response = Dispatcher::run([
80
                new SymfonyRouterMiddleware($this->router)
81
                ], $request);
82
83
        $this->assertEquals(404, $response->getStatusCode());
84
    }
85
86
    /**
87
     *
88
     */
89
    public function testMethodNotAllowedException()
90
    {
91
        $request = Factory::createServerRequest('POST', '/users');
92
93
        $factory = new HttpFoundationFactory();
94
95
        $symfonyRequest = $factory->createRequest($request);
96
97
        $context = (new RequestContext())
98
            ->fromRequest($symfonyRequest);
99
100
        $matcher = new UrlMatcher($this->routes, $context);
101
102
        $p = new \ReflectionProperty($this->router, 'matcher');
103
104
        $p->setAccessible(true);
105
106
        $p->setValue($this->router, $matcher);
107
108
        $response = Dispatcher::run([
109
                new SymfonyRouterMiddleware($this->router)
110
                ], $request);
111
112
        $this->assertEquals(405, $response->getStatusCode());
113
    }
114
115
    /**
116
     *
117
     */
118
    public function testNoConfigurationException()
119
    {
120
        $request = Factory::createServerRequest('POST', '/users');
121
122
        $matcher = $this->createMock(RequestMatcherInterface::class);
123
124
        $matcher->method("matchRequest")
1 ignored issue
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

124
        $matcher->/** @scrutinizer ignore-call */ 
125
                  method("matchRequest")

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...
125
            ->will($this->throwException(new NoConfigurationException()));
126
127
        $p = new \ReflectionProperty($this->router, "matcher");
128
129
        $p->setAccessible(true);
130
131
        $p->setValue($this->router, $matcher);
132
133
        $response = Dispatcher::run([
134
                new SymfonyRouterMiddleware($this->router)
135
                ], $request);
136
137
        $this->assertEquals(500, $response->getStatusCode());
138
    }
139
140
    /**
141
     *
142
     */
143
    public function testRouteMatched()
144
    {
145
        $request = Factory::createServerRequest('GET', '/users');
146
147
        $factory        = new HttpFoundationFactory();
148
        $symfonyRequest = $factory->createRequest($request);
149
        $context        = (new RequestContext())->fromRequest($symfonyRequest);
150
        $matcher        = new UrlMatcher($this->routes, $context);
151
        $p              = new \ReflectionProperty($this->router, 'matcher');
152
        $p->setAccessible(true);
153
        $p->setValue($this->router, $matcher);
154
155
        $response = Dispatcher::run([
156
                new SymfonyRouterMiddleware($this->router),
157
                function ($request) {
158
                    echo $request->getAttribute('_route');
159
                }
160
                ], $request);
161
162
        $this->assertEquals('test', (string) $response->getBody());
163
    }
164
}
165