Completed
Pull Request — master (#47)
by Raí
04:39
created

RotaRepositoryTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 168
Duplicated Lines 41.67 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
dl 70
loc 168
rs 10
c 1
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateEntity() 0 3 1
A testFindRemoved() 0 13 1
A getClassName() 0 3 1
A getRepository() 0 3 1
A testFindAll() 22 22 1
A getFlushedMockArray() 0 4 1
A testGetEntityManager() 0 3 1
A testCreateQueryWorker() 0 3 1
A testFindOneBy() 10 10 1
B testFindAllRemoved() 24 24 1
A getMockObject() 0 6 1
A getMockArray() 0 4 1
A testRemove() 0 20 1
A getFlushedMockObject() 0 5 1
A testFind() 10 10 1
A testGetClassMetadata() 0 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Tests\Repositories;
4
5
use Bludata\Doctrine\ORM\Repositories\QueryWorker;
6
use DateTime;
7
use Doctrine\ORM\EntityManager;
8
use Doctrine\ORM\Mapping\ClassMetadata;
9
use Tests\TestCase;
10
11
class RotaRepositoryTest extends TestCase
12
{
13
    public function getClassName()
14
    {
15
        return 'Rota';
16
    }
17
18
    public function getRepository()
19
    {
20
        return bdRepository($this->getClassName());
21
    }
22
23
    public function getMockObject(array $options = [])
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

23
    public function getMockObject(/** @scrutinizer ignore-unused */ array $options = [])

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        return $this->getRepository()
26
                    ->createEntity()
27
                    ->setNome($this->faker->uuid)
28
                    ->setAlias($this->faker->uuid);
29
    }
30
31
    public function getFlushedMockObject(array $options = [])
32
    {
33
        return $this->getMockObject($options)
34
                    ->persist()
35
                    ->flush();
36
    }
37
38
    public function getMockArray(array $options = [])
39
    {
40
        return $this->getMockObject($options)
41
                    ->toArray();
42
    }
43
44
    public function getFlushedMockArray(array $options = [])
45
    {
46
        return $this->getFlushedMockObject($options)
47
                    ->toArray();
48
    }
49
50
    public function testGetEntityManager()
51
    {
52
        $this->assertInstanceOf(EntityManager::class, $this->getRepository()->getEntityManager());
53
    }
54
55
    public function testGetClassMetadata()
56
    {
57
        $this->assertInstanceOf(ClassMetadata::class, $this->getRepository()->getClassMetadata());
58
    }
59
60
    public function testCreateQueryWorker()
61
    {
62
        $this->assertInstanceOf(QueryWorker::class, $this->getRepository()->createQueryWorker());
63
    }
64
65 View Code Duplication
    public function testFindAll()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $entity = $this->getFlushedMockObject();
68
69
        $result = $this->getRepository()
70
                        ->findAll()
71
                        ->getResult();
72
73
        $this->assertGreaterThan(0, $result->count());
74
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result[0]);
75
76
        $filteredResultById = $result->filter(function ($element) use ($entity) {
77
            return $element->getId() === $entity->getId();
78
        });
79
80
        $this->assertEquals(1, $filteredResultById->count());
81
82
        $filteredResultByDeletedAt = $result->filter(function ($element) use ($entity) {
0 ignored issues
show
Unused Code introduced by
The import $entity is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Comprehensibility Naming introduced by
The variable name $filteredResultByDeletedAt exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
83
            return !is_null($element->getDeletedAt());
84
        });
85
86
        $this->assertEquals(0, $filteredResultByDeletedAt->count());
87
    }
88
89 View Code Duplication
    public function testFindOneBy()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $entity = $this->getFlushedMockObject();
92
93
        $result = $this->getRepository()
94
                        ->findOneBy(['id' => $entity->getId()]);
95
96
        $this->assertNotNull($result);
97
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
98
        $this->assertEquals($entity->getId(), $result->getId());
99
    }
100
101 View Code Duplication
    public function testFind()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $entity = $this->getFlushedMockObject();
104
105
        $result = $this->getRepository()
106
                        ->find($entity->getId());
107
108
        $this->assertNotNull($result);
109
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
110
        $this->assertEquals($entity->getId(), $result->getId());
111
    }
112
113 View Code Duplication
    public function testFindAllRemoved()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        $entity = $this->getFlushedMockObject()
116
                        ->remove()
117
                        ->flush();
118
119
        $result = $this->getRepository()
120
                        ->findAllRemoved()
121
                        ->getResult();
122
123
        $this->assertGreaterThan(0, $result->count());
124
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result[0]);
125
126
        $filteredResultById = $result->filter(function ($element) use ($entity) {
127
            return $element->getId() === $entity->getId();
128
        });
129
130
        $this->assertEquals(1, $filteredResultById->count());
131
132
        $filteredResultByDeletedAt = $result->filter(function ($element) use ($entity) {
0 ignored issues
show
Unused Code introduced by
The import $entity is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
Comprehensibility Naming introduced by
The variable name $filteredResultByDeletedAt exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
133
            return !$element->getDeletedAt();
134
        });
135
136
        $this->assertEquals(0, $filteredResultByDeletedAt->count());
137
    }
138
139
    public function testFindRemoved()
140
    {
141
        $entity = $this->getFlushedMockObject()
142
                        ->remove()
143
                        ->flush();
144
145
        $result = $this->getRepository()
146
                        ->findRemoved($entity->getId());
147
148
        $this->assertNotNull($result);
149
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
150
        $this->assertEquals($entity->getId(), $result->getId());
151
        $this->assertInstanceOf(DateTime::class, $result->getDeletedAt());
152
    }
153
154
    public function testCreateEntity()
155
    {
156
        $this->assertInstanceOf($this->getRepository()->getClassName(), $this->getRepository()->createEntity());
157
    }
158
159
    public function testRemove()
160
    {
161
        $entity = $this->getFlushedMockObject();
162
163
        $this->getRepository()
164
                ->remove($entity)
165
                ->flush();
166
167
        $result = $this->getRepository()
168
                        ->find($entity->getId(), false);
169
170
        $this->assertNull($result);
171
172
        $result = $this->getRepository()
173
                        ->findRemoved($entity->getId());
174
175
        $this->assertNotNull($result);
176
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
177
        $this->assertEquals($entity->getId(), $result->getId());
178
        $this->assertInstanceOf(DateTime::class, $result->getDeletedAt());
179
    }
180
}
181