Passed
Pull Request — master (#45)
by Raí
04:24
created

RotaRepositoryTest::testGetEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
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 Illuminate\Foundation\Testing\RefreshDatabase;
10
use Tests\TestCase;
11
12
class RotaRepositoryTest extends TestCase
13
{
14
    public function getClassName()
15
    {
16
        return 'Rota';
17
    }
18
19
    public function getRepository()
20
    {
21
        return bdRepository($this->getClassName());
22
    }
23
24
    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

24
    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...
25
    {
26
        return $this->getRepository()
27
                    ->createEntity()
28
                    ->setNome($this->faker->uuid)
29
                    ->setAlias($this->faker->uuid);
30
    }
31
32
    public function getFlushedMockObject(array $options = [])
33
    {
34
        return $this->getMockObject($options)
35
                    ->persist()
36
                    ->flush();
37
    }
38
39
    public function getMockArray(array $options = [])
40
    {
41
        return $this->getMockObject($options)
42
                    ->toArray();
43
    }
44
45
    public function getFlushedMockArray(array $options = [])
46
    {
47
        return $this->getFlushedMockObject($options)
48
                    ->toArray();
49
    }
50
51
    public function testGetEntityManager()
52
    {
53
        $this->assertInstanceOf(EntityManager::class, $this->getRepository()->getEntityManager());
54
    }
55
56
    public function testGetClassMetadata()
57
    {
58
        $this->assertInstanceOf(ClassMetadata::class, $this->getRepository()->getClassMetadata());
59
    }
60
61
    public function testCreateQueryWorker()
62
    {
63
        $this->assertInstanceOf(QueryWorker::class, $this->getRepository()->createQueryWorker());
64
    }
65
66 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...
67
    {
68
        $entity = $this->getFlushedMockObject();
69
70
        $result = $this->getRepository()
71
                       ->findAll()
72
                       ->getResult();
73
74
        $this->assertGreaterThan(0, $result->count());
75
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result[0]);
76
77
        $filteredResultById = $result->filter(function ($element) use ($entity){
78
            return $element->getId() === $entity->getId();
79
        });
80
81
        $this->assertEquals(1, $filteredResultById->count());
82
83
        $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...
84
            return !is_null($element->getDeletedAt());
85
        });
86
87
        $this->assertEquals(0, $filteredResultByDeletedAt->count());
88
    }
89
90 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...
91
    {
92
        $entity = $this->getFlushedMockObject();
93
94
        $result = $this->getRepository()
95
                       ->findOneBy(['id' => $entity->getId()]);
96
97
        $this->assertNotNull($result);
98
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
99
        $this->assertEquals($entity->getId(), $result->getId());
100
    }
101
102 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...
103
    {
104
        $entity = $this->getFlushedMockObject();
105
106
        $result = $this->getRepository()
107
                       ->find($entity->getId());
108
109
        $this->assertNotNull($result);
110
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
111
        $this->assertEquals($entity->getId(), $result->getId());
112
    }
113
114 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...
115
    {
116
        $entity = $this->getFlushedMockObject()
117
                       ->remove()
118
                       ->flush();
119
120
        $result = $this->getRepository()
121
                       ->findAllRemoved()
122
                       ->getResult();
123
124
        $this->assertGreaterThan(0, $result->count());
125
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result[0]);
126
127
        $filteredResultById = $result->filter(function ($element) use ($entity){
128
            return $element->getId() === $entity->getId();
129
        });
130
131
        $this->assertEquals(1, $filteredResultById->count());
132
133
        $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...
134
            return !$element->getDeletedAt();
135
        });
136
137
        $this->assertEquals(0, $filteredResultByDeletedAt->count());
138
    }
139
140
    public function testFindRemoved()
141
    {
142
        $entity = $this->getFlushedMockObject()
143
                       ->remove()
144
                       ->flush();
145
146
        $result = $this->getRepository()
147
                       ->findRemoved($entity->getId());
148
149
        $this->assertNotNull($result);
150
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
151
        $this->assertEquals($entity->getId(), $result->getId());
152
        $this->assertInstanceOf(DateTime::class, $result->getDeletedAt());
153
    }
154
155
    public function testCreateEntity()
156
    {
157
        $this->assertInstanceOf($this->getRepository()->getClassName(), $this->getRepository()->createEntity());
158
    }
159
160
    public function testRemove()
161
    {
162
        $entity = $this->getFlushedMockObject();
163
164
        $this->getRepository()
165
             ->remove($entity)
166
             ->flush();
167
168
        $result = $this->getRepository()
169
                       ->find($entity->getId(), false);
170
171
        $this->assertNull($result);
172
173
        $result = $this->getRepository()
174
                       ->findRemoved($entity->getId());
175
176
        $this->assertNotNull($result);
177
        $this->assertInstanceOf($this->getRepository()->getClassName(), $result);
178
        $this->assertEquals($entity->getId(), $result->getId());
179
        $this->assertInstanceOf(DateTime::class, $result->getDeletedAt());
180
    }
181
}
182