Passed
Push — master ( c13efe...13a161 )
by Paweł
02:57
created

RouterTest::testFindsOperation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gorynych\Tests\Http\Routing;
6
7
use Cake\Collection\Collection;
8
use DG\BypassFinals;
9
use Gorynych\Http\Exception\MethodNotAllowedHttpException;
10
use Gorynych\Http\Exception\NotFoundHttpException;
11
use Gorynych\Http\Routing\Router;
12
use Gorynych\Operation\ResourceOperationInterface;
13
use Gorynych\Resource\AbstractResource;
14
use Gorynych\Resource\ResourceLoader;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use PHPUnit\Framework\TestCase;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class RouterTest extends TestCase
20
{
21
    /** @var ResourceLoader|MockObject  */
22
    private $resourceLoaderMock;
23
24
    public function setUp(): void
25
    {
26
        BypassFinals::enable();
27
        $this->resourceLoaderMock = $this->createMock(ResourceLoader::class);
28
    }
29
30
    public function testFindsOperation(): void
31
    {
32
        $request = $this->createMock(Request::class);
33
        $request->method('getPathInfo')->willReturn('/bar/baz');
34
        $request->method('getMethod')->willReturn(Request::METHOD_GET);
35
36
        $operation = $this->setUpRouter()->findOperation($request);
37
38
        $this->assertSame('/baz', $operation->getPath());
39
        $this->assertSame(Request::METHOD_GET, $operation->getMethod());
40
    }
41
42
    public function testThrowsNotFound(): void
43
    {
44
        $this->expectException(NotFoundHttpException::class);
45
46
        $request = $this->createMock(Request::class);
47
        $request->method('getPathInfo')->willReturn('/bar/foo');
48
        $request->method('getMethod')->willReturn(Request::METHOD_GET);
49
50
        $this->setUpRouter()->findOperation($request);
51
    }
52
53
    public function testThrowsMethodNotAllowed(): void
54
    {
55
        $this->expectException(MethodNotAllowedHttpException::class);
56
57
        $request = $this->createMock(Request::class);
58
        $request->method('getPathInfo')->willReturn('/bar/baz');
59
        $request->method('getMethod')->willReturn(Request::METHOD_POST);
60
61
        $this->setUpRouter()->findOperation($request);
62
    }
63
64
    private function setUpResource(string $path): MockObject
65
    {
66
        $operation = $this->createMock(ResourceOperationInterface::class);
67
        $operation->method('getPath')->willReturn('/baz');
68
        $operation->method('getMethod')->willReturn(Request::METHOD_GET);
69
70
        $resource = $this->createMock(AbstractResource::class);
71
        $resource->method('getOperations')->willReturn(new Collection([$operation]));
72
        $resource->method('getPath')->willReturn($path);
73
74
        return $resource;
75
    }
76
77
    private function setUpRouter(): Router
78
    {
79
        $this->resourceLoaderMock
80
            ->expects($this->once())
81
            ->method('getResources')
82
            ->willReturn(['FooResource', 'BarResource']);
83
84
        $this->resourceLoaderMock
85
            ->expects($this->exactly(2))
86
            ->method('loadResource')
87
            ->withConsecutive(['FooResource'], ['BarResource'])
88
            ->willReturnOnConsecutiveCalls(
89
                $this->setUpResource('/foo'),
90
                $this->setUpResource('/bar'),
91
            );
92
93
        return new Router($this->resourceLoaderMock);
94
    }
95
}
96