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

JoinMapperTest::testBuildListFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 53
rs 9.264
c 0
b 0
f 0
cc 2
nc 2
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\Mapper;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
/**
8
 * @author Povilas Margaiatis <[email protected]>
9
 */
10
class JoinMapperTest extends AbstractMapperTest
11
{
12
    /**
13
     * @return JoinMapper
14
     */
15
    public function testAdd(): JoinMapper
16
    {
17
        $mapper = $this->getMapper([]);
18
19
        $mapper->add('entity1', 'e1');
20
        $mapper->add('entity1.entity2', 'e2');
21
        $mapper->add('e2.entity3', 'e3');
22
        $mapper->add('e2.entity3.entity6', 'e6');
23
        $mapper->add('e3.entity4.entity5', 'e5');
24
25
        $this->assertCount(6, $mapper->getFields());
26
27
        return $mapper;
28
    }
29
30
    /**
31
     * @depends testAdd
32
     * @param JoinMapper $mapper
33
     */
34
    public function testGetByPath(JoinMapper $mapper): void
35
    {
36
        $this->assertEquals('e5', $mapper->getByPath('entity1.entity2.entity3.entity4.entity5')->getAlias());
37
        $this->assertEquals('e5', $mapper->getByPath('e5')->getAlias());
38
        $this->assertEquals('e3', $mapper->getByPath('entity1.entity2.entity3')->getAlias());
39
    }
40
41
    /**
42
     * @return JoinMapper
43
     */
44
    public function testBuildListFields(): JoinMapper
45
    {
46
        $paths = [
47
            [['prop'], false],
48
            [['entity1.prop'], true],
49
            [['entity1.prop2'], false],
50
            [['entity1.entity2.prop', 'entity1.entity2.prop2'], true],
51
            [['entity3.entity4.entity5.prop', 'entity3.prop'], false]
52
        ];
53
        $fields = [];
54
55
        foreach ($paths as $path) {
56
            $field = $this->createMock(ListField::class);
57
            $field->expects($this->exactly(5))
58
                ->method('getOption')
59
                ->willReturnMap([
60
                    ['join_type', null, 'INNER'],
61
                    ['sortable', null, true],
62
                    ['sort_value', null, 'ASC'],
63
                    ['sort_path', null, 'custom_path'],
64
                    ['lazy', null, $path[1]]
65
                ]);
66
            $field->expects($this->once())
67
                ->method('getPaths')
68
                ->willReturn($path[0]);
69
70
            $fields[] = $field;
71
        }
72
73
        $listMapperMock = $this->createMock(ListMapper::class);
74
        $listMapperMock->expects($this->once())
75
            ->method('getFields')
76
            ->willReturn(new ArrayCollection($fields));
77
78
        $filterMapperMock = $this->createMock(FilterMapper::class);
79
        $filterMapperMock->expects($this->once())
80
            ->method('getFields')
81
            ->willReturn(new ArrayCollection([]));
82
83
        $mapper = new JoinMapper($listMapperMock, $filterMapperMock);
84
        $mapper->build();
85
86
        $this->assertCount(6, $mapper->getFields());
87
        $this->assertCount(2, $mapper->getFields(true));
88
        $this->assertCount(4, $mapper->getFields(false));
89
        $this->assertEquals('a.entity1', $mapper->getByPath('entity1', true)->getJoinPath('a'));
90
        $this->assertEquals('a.entity1', $mapper->getByPath('entity1', false)->getJoinPath('a'));
91
        $this->assertEquals('entity1_a.entity2', $mapper->getByPath('entity1.entity2', true)->getJoinPath('a'));
92
        $this->assertEquals('a.entity3', $mapper->getByPath('entity3', false)->getJoinPath('a'));
93
        $this->assertEquals('entity3_a.entity4', $mapper->getByPath('entity3.entity4', false)->getJoinPath('a'));
94
        $this->assertEquals('entity3_entity4_a.entity5', $mapper->getByPath('entity3.entity4.entity5', false)->getJoinPath('a'));
95
96
        return $mapper;
97
    }
98
99
    /**
100
     * @depends testBuildListFields
101
     * @param JoinMapper $mapper
102
     */
103
    public function testAliasOverwrite(JoinMapper $mapper): void
104
    {
105
        $mapper->add('entity3.entity4.entity5', 'custom_a', []);
106
        $this->assertCount(6, $mapper->getFields());
107
        $this->assertCount(2, $mapper->getFields(true));
108
        $this->assertCount(4, $mapper->getFields(false));
109
        $this->assertEquals('custom_a', $mapper->getByPath('entity3.entity4.entity5')->getAlias());
110
    }
111
112
    public function testBuildFilterFields(): void
113
    {
114
        $paths = [
115
            ['prop'],
116
            ['entity1.prop'],
117
            ['entity1.entity2.prop', 'entity1.entity2.prop2'],
118
            ['entity3.entity4.entity5.prop', 'entity3.prop']
119
        ];
120
        $fields = [];
121
122
        foreach ($paths as $path) {
123
            $field = $this->createMock(FilterField::class);
124
            $field->expects($this->exactly(2))
125
                ->method('getOption')
126
                ->willReturnMap([
127
                    ['join_type', null, 'INNER'],
128
                    ['mapped', null, true],
129
                ]);
130
            $field->expects($this->once())
131
                ->method('getPaths')
132
                ->willReturn($path);
133
            $field->expects($this->once())
134
                ->method('hasValue')
135
                ->willReturn(true);
136
137
            $fields[] = $field;
138
        }
139
140
        $listMapperMock = $this->createMock(ListMapper::class);
141
        $listMapperMock->expects($this->once())
142
            ->method('getFields')
143
            ->willReturn(new ArrayCollection());
144
145
        $filterMapperMock = $this->createMock(FilterMapper::class);
146
        $filterMapperMock->expects($this->once())
147
            ->method('getFields')
148
            ->willReturn(new ArrayCollection($fields));
149
150
        $mapper = new JoinMapper($listMapperMock, $filterMapperMock);
151
        $mapper->build();
152
153
        $this->assertCount(5, $mapper->getFields());
154
        $this->assertEquals('a.entity1', $mapper->getByPath('entity1')->getJoinPath('a'));
155
        $this->assertEquals('entity1_a.entity2', $mapper->getByPath('entity1.entity2')->getJoinPath('a'));
156
        $this->assertEquals('a.entity3', $mapper->getByPath('entity3')->getJoinPath('a'));
157
        $this->assertEquals('entity3_a.entity4', $mapper->getByPath('entity3.entity4')->getJoinPath('a'));
158
        $this->assertEquals('entity3_entity4_a.entity5', $mapper->getByPath('entity3.entity4.entity5')->getJoinPath('a'));
159
    }
160
161
    /**
162
     * @param array $ids
163
     *
164
     * @return AbstractMapper|JoinMapper
165
     */
166
    protected function getMapper(array $ids): AbstractMapper
167
    {
168
        $listMapper = $this->createMock(ListMapper::class);
169
        $filterMapper = $this->createMock(FilterMapper::class);
170
        $mapper = new JoinMapper($listMapper, $filterMapper);
171
172
        foreach ($ids as $id) {
173
            $mapper->add($id, 'alias', []);
174
        }
175
176
        return $mapper;
177
    }
178
}
179