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

RequestHandlerTest::testHandlerRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 56
rs 9.069
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Povs\ListerBundle\Service;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use PHPUnit\Framework\TestCase;
7
use Povs\ListerBundle\Mapper\FilterField;
8
use Povs\ListerBundle\Mapper\FilterMapper;
9
use Povs\ListerBundle\Mapper\ListField;
10
use Povs\ListerBundle\Mapper\ListMapper;
11
use Symfony\Component\Form\FormInterface;
12
use Symfony\Component\HttpFoundation\ParameterBag;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
16
/**
17
 * @author Povilas Margaiatis <[email protected]>
18
 */
19
class RequestHandlerTest extends TestCase
20
{
21
    public function testHandlerRequest(): void
22
    {
23
        $listMapperMock = $this->createMock(ListMapper::class);
24
        $filterMapperMock = $this->createMock(FilterMapper::class);
25
        $formMock = $this->createMock(FormInterface::class);
26
        $listFieldMock = $this->createMock(ListField::class);
27
        $listFieldMock2 = $this->createMock(ListField::class);
28
        $listFieldMock->expects($this->exactly(2))
29
            ->method('setOption')
30
            ->withConsecutive(
31
                ['sort_value', 'DESC'],
32
                ['lazy', false]
33
            );
34
        $listFieldMock->expects($this->once())
35
            ->method('getId')
36
            ->willReturn('test_id');
37
        $listMapperMock->expects($this->once())
38
            ->method('getFields')
39
            ->willReturn(new ArrayCollection([$listFieldMock2]));
40
        $listFieldMock2->expects($this->once())
41
            ->method('getId')
42
            ->willReturn('another_id');
43
        $listFieldMock2->expects($this->once())
44
            ->method('setOption')
45
            ->with('sort_value', null);
46
        $listMapperMock->expects($this->once())
47
            ->method('get')
48
            ->willReturn($listFieldMock);
49
        $formMock->expects($this->once())
50
            ->method('handleRequest');
51
        $formMock->expects($this->once())
52
            ->method('isSubmitted')
53
            ->willReturn(true);
54
        $formMock->expects($this->once())
55
            ->method('isValid')
56
            ->willReturn(true);
57
        $formMock->expects($this->once())
58
            ->method('getData')
59
            ->willReturn(['field1' => 'val1', 'field2' => 'val2']);
60
        $filterFieldMock1 = $this->createMock(FilterField::class);
61
        $filterFieldMock2 = $this->createMock(FilterField::class);
62
        $filterFieldMock1->expects($this->once())
63
            ->method('setValue')
64
            ->with('val1');
65
        $filterFieldMock2->expects($this->once())
66
            ->method('setValue')
67
            ->with('val2');
68
        $filterMapperMock->expects($this->exactly(2))
69
            ->method('get')
70
            ->willReturnMap([
71
                ['field1', $filterFieldMock1],
72
                ['field2', $filterFieldMock2]
73
            ]);
74
75
        $requestHandler = $this->getRequestHandler([['sort', null, ['field2' => 'invalid_sort', 'field1' => 'desc']]], [], [['sort', 'sort']]);
76
        $requestHandler->handleRequest($listMapperMock, $filterMapperMock, $formMock);
77
    }
78
79
    public function testGetCurrentPage(): void
80
    {
81
        $handler = $this->getRequestHandler([['page', null, 20]], [], [['page', 'page']]);
82
83
        $this->assertEquals(20, $handler->getCurrentPage());
84
    }
85
86
    public function testGetLength(): void
87
    {
88
        $handler = $this->getRequestHandler([['length', null, 20]], [], [['length', 'length']]);
89
90
        $this->assertEquals(20, $handler->getLength());
91
    }
92
93
    public function testGetValue(): void
94
    {
95
        $handler = $this->getRequestHandler([['foo', null, 'custom_value']], [], [['custom_name', 'foo']]);
96
97
        $this->assertEquals('custom_value', $handler->getValue('custom_name'));
98
    }
99
100
    public function testGetName(): void
101
    {
102
        $handler = $this->getRequestHandler([], [], [['custom_name', 'foo']]);
103
104
        $this->assertEquals('foo', $handler->getName('custom_name'));
105
    }
106
107
    public function testGetRequest(): void
108
    {
109
        $handler = $this->getRequestHandler([], [], []);
110
        $this->assertNotNull($handler->getRequest());
111
    }
112
113
    private function getRequestHandler(array $query, array $attributes, array $configs): RequestHandler
114
    {
115
        $requestMock = $this->createMock(Request::class);
116
        $queryMock = $this->createMock(ParameterBag::class);
117
        $attributesMock = $this->createMock(ParameterBag::class);
118
        $configMock = $this->createMock(ConfigurationResolver::class);
119
        $requestStackMock = $this->createMock(RequestStack::class);
120
121
        $queryMock->expects($this->exactly(count($query)))
122
            ->method('get')
123
            ->willReturnMap($query);
124
        $attributesMock->expects($this->exactly(count($attributes)))
125
            ->method('get')
126
            ->willReturnMap($attributes);
127
        $configMock->expects($this->exactly(count($configs)))
128
            ->method('getRequestConfiguration')
129
            ->willReturnMap($configs);
130
        $configMock->method('isMultiColumnSortable')
131
            ->willReturn(false);
132
        $requestMock->query = $queryMock;
133
        $requestMock->attributes = $attributesMock;
134
        $requestStackMock->expects($this->once())
135
            ->method('getCurrentRequest')
136
            ->willReturn($requestMock);
137
138
        return new RequestHandler($requestStackMock, $configMock);
139
    }
140
}
141