Passed
Push — master ( a65783...53adb5 )
by Povilas
02:44
created

RouterViewTest::getRouterView()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 36
rs 9.2088
c 0
b 0
f 0
cc 5
nc 8
nop 3
1
<?php
2
3
namespace Povs\ListerBundle\View;
4
5
use PHPUnit\Framework\TestCase;
6
use Povs\ListerBundle\Service\RequestHandler;
7
use Symfony\Component\HttpFoundation\ParameterBag;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Routing\RouterInterface;
10
11
/**
12
 * @author Povilas Margaiatis <[email protected]>
13
 */
14
class RouterViewTest extends TestCase
15
{
16
    private $requestHandler;
17
    private $router;
18
19
    public function setUp()
20
    {
21
        $this->requestHandler = $this->createMock(RequestHandler::class);
22
        $this->router = $this->createMock(RouterInterface::class);
23
    }
24
25
    public function testGetPageRoute(): void
26
    {
27
        $this->requestHandler->expects($this->once())
28
            ->method('getName')
29
            ->with('page')
30
            ->willReturn('foo');
31
32
        $view = $this->getRouterView(['foo' => 5], true);
33
        $this->assertEquals('test_route', $view->getPageRoute(5));
34
    }
35
36
    public function testGetLengthRoute(): void
37
    {
38
        $this->requestHandler->expects($this->exactly(2))
39
            ->method('getName')
40
            ->willReturnMap([
41
                ['length', 'length_name'],
42
                ['page', 'page_name']
43
            ]);
44
45
        $view = $this->getRouterView(['length_name' => 100, 'page_name' => 1], true);
46
        $this->assertEquals('test_route', $view->getLengthRoute(100));
47
    }
48
49
    public function testGetSortRoute(): void
50
    {
51
        $this->requestHandler->expects($this->once())
52
            ->method('getName')
53
            ->with('sort')
54
            ->willReturn('sort_name');
55
56
        $view = $this->getRouterView(['sort_name' => ['foo' => 'bar']], true);
57
        $this->assertEquals('test_route', $view->getSortRoute('foo', 'bar'));
58
    }
59
60
    public function testGetGetRoute(): void
61
    {
62
        $view = $this->getRouterView([], false);
63
        $this->assertEquals('test_route', $view->getRoute());
64
    }
65
66
    public function testGetRequestName(): void
67
    {
68
        $this->requestHandler->expects($this->once())
69
            ->method('getName')
70
            ->with('foo')
71
            ->willReturn('bar');
72
        $view = $this->getRouterView(null);
73
        $this->assertEquals('bar', $view->getRequestName('foo'));
74
    }
75
76
    private function getRouterView(?array $params = null, bool $merge = false, array $requestParams = []): RouterView
77
    {
78
        $requestMock = $this->createMock(Request::class);
79
80
        if (true === $merge || null !== $params) {
81
            $this->requestHandler->expects($this->once())
82
                ->method('getRequest')
83
                ->willReturn($requestMock);
84
        }
85
86
        if (true === $merge) {
87
            $paramsBagMock = $this->createMock(ParameterBag::class);
88
            $paramsBagMock->expects($this->once())
89
                ->method('all')
90
                ->willReturn($requestParams);
91
            $requestMock->query = $paramsBagMock;
92
        }
93
94
        if (null !== $params) {
95
            $attributesMock = $this->createMock(ParameterBag::class);
96
            $attributesMock->expects($this->exactly(2))
97
                ->method('get')
98
                ->willReturnMap([
99
                    ['_route_params', null, ['test' => 'foo']],
100
                    ['_route', null, 'test_route']
101
                ]);
102
            $requestMock->attributes = $attributesMock;
103
            $params['test'] = 'foo';
104
105
            $this->router->expects($this->once())
106
                ->method('generate')
107
                ->with('test_route', $params, 0)
108
                ->willReturn('test_route');
109
        }
110
111
        return new RouterView($this->requestHandler, $this->router);
112
    }
113
}
114