GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#57)
by joseph
16:59
created

getRepository()   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 declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories;
4
5
use Doctrine\Common\Collections\Collection;
6
use Doctrine\Common\Collections\Criteria;
7
use Doctrine\Common\Collections\Expr\Comparison;
8
use EdmondsCommerce\DoctrineStaticMeta\AbstractFunctionalTest;
9
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest;
10
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
11
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
12
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaver;
13
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
14
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\AbstractEntityTest;
15
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\TestEntityGenerator;
16
use EdmondsCommerce\DoctrineStaticMeta\FullProjectBuildFunctionalTest;
17
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
18
19
/**
20
 * Class AbstractEntityRepositoryFunctionalTest
21
 *
22
 * @package EdmondsCommerce\DoctrineStaticMeta\Entity\Repositories
23
 * @see     https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/working-with-objects.html#querying
24
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
25
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
26
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
 */
28
class AbstractEntityRepositoryFunctionalTest extends AbstractFunctionalTest
29
{
30
    public const WORK_DIR = AbstractIntegrationTest::VAR_PATH.'/'
31
                            .self::TEST_TYPE.'/AbstractEntityRepositoryFunctionalTest';
32
33
    private const TEST_ENTITY_FQN = self::TEST_PROJECT_ROOT_NAMESPACE.'\\Entities\\TestEntity';
34
35
    private const TEST_FIELD_FQN_BASE = FullProjectBuildFunctionalTest::TEST_FIELD_NAMESPACE_BASE.'\\Traits';
36
37
    private $built = false;
38
39
    private $fields = [];
40
41
    private $generatedEntities = [];
42
43
    /**
44
     * @var AbstractEntityRepository
45
     */
46
    private $repository;
47
48
    public function setup()
49
    {
50
        parent::setup();
51
        if (true !== $this->built) {
52
            $this->generateCode();
53
            $this->built = true;
54
        }
55
        $this->setupCopiedWorkDirAndCreateDatabase();
56
        $this->generateAndSaveTestEntities();
57
        $this->repository = $this->getRepository();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getRepository() of type Doctrine\Common\Persistence\ObjectRepository is incompatible with the declared type EdmondsCommerce\Doctrine...bstractEntityRepository of property $repository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        $this->built      = true;
59
    }
60
61
    protected function generateCode()
62
    {
63
        $entityGenerator = $this->getEntityGenerator();
64
65
        $entityGenerator->generateEntity(self::TEST_ENTITY_FQN);
66
        $fieldGenerator = $this->getFieldGenerator();
67
        foreach (MappingHelper::COMMON_TYPES as $type) {
68
            $this->fields[] = $fieldFqn = $fieldGenerator->generateField(
69
                self::TEST_FIELD_FQN_BASE.'\\'.ucwords($type),
70
                $type
71
            );
72
            $this->getFieldSetter()->setEntityHasField(self::TEST_ENTITY_FQN, $fieldFqn);
73
        }
74
    }
75
76
    protected function generateAndSaveTestEntities(): void
77
    {
78
        $entityGenerator         = new TestEntityGenerator(
79
            AbstractEntityTest::SEED,
80
            [],
81
            new \ReflectionClass(self::TEST_ENTITY_FQN),
82
            new EntitySaverFactory(
83
                $this->getEntityManager(),
84
                new EntitySaver($this->getEntityManager()),
85
                new NamespaceHelper()
86
            )
87
        );
88
        $this->generatedEntities = $entityGenerator->generateEntities(
89
            $this->getEntityManager(),
90
            $this->getCopiedFqn(self::TEST_ENTITY_FQN),
91
            100
92
        );
93
        $saver                   = new EntitySaver($this->getEntityManager());
94
        $saver->saveAll($this->generatedEntities);
95
    }
96
97
    protected function getRepository()
98
    {
99
        return $this->getEntityManager()->getRepository($this->getCopiedFqn(self::TEST_ENTITY_FQN));
100
    }
101
102
    public function testFind()
103
    {
104
        $expected = $this->generatedEntities[array_rand($this->generatedEntities)];
105
        $actual   = $this->repository->find($expected->getId());
106
        $this->assertSame($expected, $actual);
107
108
    }
109
110
    public function testFindAll()
111
    {
112
        $expected = $this->generatedEntities;
113
        $actual   = $this->repository->findAll();
114
        $this->assertSame($expected, $actual);
115
    }
116
117
    public function testFindBy()
118
    {
119
        foreach (MappingHelper::COMMON_TYPES as $key => $property) {
120
            $entity   = $this->generatedEntities[$key];
121
            $getter   = $this->getGetterForType($property);
122
            $criteria = [$property => $entity->$getter()];
123
            $actual   = $this->repository->findBy($criteria);
124
            $this->assertTrue($this->arrayContainsEntity($entity, $actual));
125
        }
126
    }
127
128
    protected function getGetterForType(string $type): string
129
    {
130
        $ucType = ucfirst($type);
131
        $getter = "get$ucType";
132
        if (MappingHelper::TYPE_BOOLEAN === $type) {
133
            $getter = "is$ucType";
134
        }
135
136
        return $getter;
137
    }
138
139
    public function testFindOneBy(): void
140
    {
141
        foreach (MappingHelper::COMMON_TYPES as $key => $property) {
142
            $entity   = $this->generatedEntities[$key];
143
            $getter   = $this->getGetterForType($property);
144
            $value    = $entity->$getter();
145
            $criteria = [
146
                $property => $value,
147
                'id'      => $entity->getId(),
148
            ];
149
            $actual   = $this->repository->findOneBy($criteria);
150
            $this->assertEquals(
151
                $entity,
152
                $actual,
153
                'Failed finding one expected entity (ID'.$entity->getId().') with $criteria: '
154
                ."\n".var_export($criteria, true)
155
                ."\n and \$actual: "
156
                ."\n".var_export($actual, true)
157
            );
158
        }
159
    }
160
161
    protected function arrayContainsEntity(EntityInterface $expectedEntity, array $array): bool
162
    {
163
        foreach ($array as $entity) {
164
            if ($entity->getId() === $expectedEntity->getId()) {
165
                return true;
166
            }
167
        }
168
169
        return false;
170
    }
171
172
    public function testGetClassName()
173
    {
174
        $this->assertSame(
175
            ltrim($this->getCopiedFqn(self::TEST_ENTITY_FQN), '\\'),
176
            $this->repository->getClassName()
177
        );
178
    }
179
180
    protected function collectionContainsEntity(EntityInterface $expectedEntity, Collection $collection): bool
181
    {
182
        foreach ($collection->getIterator() as $entity) {
183
            if ($entity->getId() === $expectedEntity->getId()) {
184
                return true;
185
            }
186
        }
187
188
        return false;
189
    }
190
191
    public function testMatching()
192
    {
193
        foreach (MappingHelper::COMMON_TYPES as $key => $property) {
194
            $entity   = $this->generatedEntities[$key];
195
            $getter   = $this->getGetterForType($property);
196
            $value    = $entity->$getter();
197
            $criteria = new Criteria();
198
            $criteria->where(new Comparison($property, '=', $value));
199
            $criteria->andWhere(new Comparison('id', '=', $entity->getId()));
200
            $actual = $this->repository->matching($criteria);
201
            $this->assertTrue($this->collectionContainsEntity($entity, $actual));
202
        }
203
    }
204
205
    public function testCreateQueryBuilder()
206
    {
207
        $this->repository->createQueryBuilder('foo');
208
        $this->assertTrue(true);
209
    }
210
211
    public function testCreateResultSetMappingBuilder()
212
    {
213
        $this->repository->createResultSetMappingBuilder('foo');
214
        $this->assertTrue(true);
215
    }
216
217
    public function testCreateNamedQuery()
218
    {
219
        $this->markTestIncomplete(
220
            'Need to add a named query for a test entity somehow in the meta data before we can test this'
221
        );
222
        $this->repository->createNamedQuery('foo');
223
        $this->assertTrue(true);
224
    }
225
226
    public function testClear()
227
    {
228
        $this->repository->clear();
229
        $this->assertSame(
230
            ['AbstractEntityRepositoryFunctionalTest_testClear_My\Test\Project\Entities\TestEntity' => []],
231
            $this->getEntityManager()->getUnitOfWork()->getIdentityMap()
232
        );
233
        $this->built = false;
234
    }
235
236
    public function testCount()
237
    {
238
        $this->assertSame(100, $this->repository->count([]));
239
    }
240
}
241