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 (#120)
by joseph
21:56
created

php$0 ➔ fixturesUseTheCorrectFakerDataProviders()   A

Complexity

Conditions 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\Entity\Testing;
4
5
use Doctrine\Common\Cache\FilesystemCache;
6
use Doctrine\Common\DataFixtures\Loader;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Factory\EntityFactory;
9
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\EnumFieldInterface;
10
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String\EnumFieldTrait;
11
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
12
use EdmondsCommerce\DoctrineStaticMeta\Entity\Savers\EntitySaverFactory;
13
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityGenerator\TestEntityGeneratorFactory;
14
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\AbstractEntityFixtureLoader;
15
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixtureEntitiesModifierInterface;
16
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper;
17
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database;
18
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema;
19
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractLargeTest;
20
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest;
21
22
/**
23
 * @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\AbstractEntityFixtureLoader
24
 * @covers \EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\Fixtures\FixturesHelper
25
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
 */
27
class FixturesTest extends AbstractLargeTest
28
{
29
    public const TEST_PROJECT_ROOT_NAMESPACE = 'Fixtures\\Test';
30
31
    public const WORK_DIR = AbstractTest::VAR_PATH .
32
                            self::TEST_TYPE_LARGE .
33
                            '/FixturesTest';
34
35
    private const ENTITY_WITHOUT_MODIFIER = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Entities\\Person';
36
37
    private const ENTITY_WITH_MODIFIER = self::TEST_PROJECT_ROOT_NAMESPACE . '\\Entities\\Attributes\\Address';
38
39
    protected static $buildOnce = true;
40
    /**
41
     * @var FixturesHelper
42
     */
43
    private $helper;
44
45
    public function setup(): void
46
    {
47
        parent::setup();
48
        if (false === self::$built) {
49
            $this->getTestCodeGenerator()
50
                 ->copyTo(self::WORK_DIR, self::TEST_PROJECT_ROOT_NAMESPACE);
51
            $this->getFieldSetter()
52
                 ->setEntityHasField(
53
                     self::ENTITY_WITHOUT_MODIFIER,
54
                     EnumFieldTrait::class
55
                 );
56
        }
57
        $this->setupCopiedWorkDirAndCreateDatabase();
58
        $cacheDir = $this->copiedWorkDir . '/cache';
59
        mkdir($cacheDir, 0777, true);
60
        $this->helper = new FixturesHelper(
61
            $this->getEntityManager(),
62
            $this->container->get(Database::class),
63
            $this->container->get(Schema::class),
64
            new FilesystemCache($cacheDir)
65
        );
66
    }
67
68
    private function getFixture(
69
        string $entityFqn,
70
        ?FixtureEntitiesModifierInterface $modifier = null
71
    ): AbstractEntityFixtureLoader {
72
        $fixtureFqn = $this->getNamespaceHelper()->getFixtureFqnFromEntityFqn($entityFqn);
73
74
        return new $fixtureFqn(
75
            $this->container->get(TestEntityGeneratorFactory::class),
76
            $this->container->get(EntitySaverFactory::class),
77
            $this->container->get(NamespaceHelper::class),
78
            $modifier
79
        );
80
    }
81
82
    private function getUnmodifiedFixture(): AbstractEntityFixtureLoader
83
    {
84
        return $this->getFixture($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER));
85
    }
86
87
    private function getModifiedFixture(): AbstractEntityFixtureLoader
88
    {
89
        return $this->getFixture(
90
            $this->getCopiedFqn(self::ENTITY_WITH_MODIFIER),
91
            $this->getFixtureModifier()
92
        );
93
    }
94
95
    private function getFixtureModifier(): FixtureEntitiesModifierInterface
96
    {
97
        return new class(
98
            $this->getCopiedFqn(self::ENTITY_WITH_MODIFIER),
99
            $this->getEntityFactory()
100
        )
101
            implements FixtureEntitiesModifierInterface
102
        {
103
            /**
104
             * @var string
105
             */
106
            protected $entityFqn;
107
            /**
108
             * @var EntityFactory
109
             */
110
            protected $factory;
111
            /**
112
             * @var array|EntityInterface[]
113
             */
114
            private $entities;
115
116
            public function __construct(string $entityFqn, EntityFactory $factory)
117
            {
118
                $this->entityFqn = $entityFqn;
119
                $this->factory   = $factory;
120
            }
121
122
            /**
123
             * Update the entities array by reference
124
             *
125
             * @param array $entities
126
             */
127
            public function modifyEntities(array &$entities): void
128
            {
129
                $this->entities = &$entities;
130
                $this->updateFirstEntity();
131
                $this->addAnotherEntity();
132
            }
133
134
            private function updateFirstEntity(): void
135
            {
136
                $this->entities[0]->setString('This has been overridden');
137
            }
138
139
            private function addAnotherEntity(): void
140
            {
141
                $entity = $this->factory->create($this->entityFqn);
142
                $entity->setString('This has been created');
0 ignored issues
show
Bug introduced by
The method setString() does not exist on EdmondsCommerce\Doctrine...erfaces\EntityInterface. ( Ignorable by Annotation )

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

142
                $entity->/** @scrutinizer ignore-call */ 
143
                         setString('This has been created');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
143
                $this->entities[] = $entity;
144
            }
145
        };
146
    }
147
148
    /**
149
     * @test
150
     * @large
151
     */
152
    public function itLoadsAllTheFixturesWithRandomDataByDefault(): array
153
    {
154
        $this->helper->setCacheKey(__CLASS__ . '_unmodified');
155
        $fixture = $this->getUnmodifiedFixture();
156
        $this->helper->addFixture($fixture);
157
        $this->helper->createDb();
158
        $actual      = $this->getEntityManager()
159
                            ->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER))
160
                            ->findAll();
161
        $actualCount = count($actual);
162
        self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE, $actualCount);
163
164
        return $actual;
165
    }
166
167
    /**
168
     * @test
169
     * @large
170
     * @depends itLoadsAllTheFixturesWithRandomDataByDefault
171
     *
172
     * @param array $loadedFirstTime
173
     *
174
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
175
     * @throws \ReflectionException
176
     */
177
    public function itUsesTheCacheTheSecondTime(array $loadedFirstTime): void
178
    {
179
        $this->getFileSystem()
180
             ->mirror(
181
                 $this->copiedWorkDir .
182
                 '/../FixturesTest_itLoadsAllTheFixturesWithRandomDataByDefault_/cache',
183
                 $this->copiedWorkDir . '/cache'
184
             );
185
        $this->helper->setCacheKey(__CLASS__ . '_unmodified');
186
        $fixture = $this->getUnmodifiedFixture();
187
        $this->helper->addFixture($fixture);
188
        $this->helper->createDb();
189
        self::assertTrue($this->helper->isLoadedFromCache());
190
        /**
191
         * @var EntityInterface[] $actual
192
         */
193
        $actual        = $this->getEntityManager()
194
                              ->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER))
195
                              ->findAll();
196
        $actualCount   = count($actual);
197
        $expectedCount = count($loadedFirstTime);
198
        self::assertSame($expectedCount, $actualCount);
199
        foreach ($actual as $key => $actualEntity) {
200
            $expectedEntity = $loadedFirstTime[$key];
201
            $actualId       = $actualEntity->getId();
202
            $expectedId     = $expectedEntity->getId();
203
            $expectedText   = $expectedEntity->getString();
204
            $actualText     = $actualEntity->getString();
0 ignored issues
show
Bug introduced by
The method getString() does not exist on EdmondsCommerce\Doctrine...erfaces\EntityInterface. ( Ignorable by Annotation )

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

204
            /** @scrutinizer ignore-call */ 
205
            $actualText     = $actualEntity->getString();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
205
            self::assertEquals($expectedId, $actualId, 'Cached Entity ID does not match');
206
            self::assertEquals($expectedText, $actualText, 'Cached Faker data does not match');
207
        }
208
    }
209
210
    /**
211
     * @test
212
     * @large
213
     */
214
    public function itCanTakeAModifierToCustomiseTheFixtures()
215
    {
216
        $this->helper->setCacheKey(__CLASS__ . '_modified');
217
        $fixture = $this->getModifiedFixture();
218
        $this->helper->addFixture($fixture);
219
        $this->helper->createDb();
220
        /**
221
         * @var EntityInterface[] $actual
222
         */
223
        $actual      = $this->getEntityManager()
224
                            ->getRepository($this->getCopiedFqn(self::ENTITY_WITH_MODIFIER))
225
                            ->findAll();
226
        $actualCount = count($actual);
227
        self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE + 1, $actualCount);
228
        $firstEntity    = $actual[0];
229
        $expectedString = 'This has been overridden';
230
        $actualString   = $firstEntity->getString();
231
        self::assertSame($expectedString, $actualString);
232
        end($actual);
233
        $lastEntity     = current($actual);
234
        $expectedString = 'This has been created';
235
        $actualString   = $lastEntity->getString();
236
        self::assertSame($expectedString, $actualString);
237
    }
238
239
    /**
240
     * @test
241
     * @large
242
     */
243
    public function theOrderOfFixtureLoadingCanBeSet(): void
244
    {
245
        $loader   = new Loader();
246
        $fixture1 = $this->getModifiedFixture();
247
        $loader->addFixture($fixture1);
248
        $fixture2 = $this->getUnmodifiedFixture();
249
        $fixture2->setOrder(AbstractEntityFixtureLoader::ORDER_FIRST);
250
        $loader->addFixture($fixture2);
251
        $orderedFixtures = $loader->getFixtures();
252
        self::assertSame($fixture2, current($orderedFixtures));
253
    }
254
255
    /**
256
     * @test
257
     * @large
258
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
259
     * @throws \ReflectionException
260
     */
261
    public function fixturesUseTheCorrectFakerDataProviders(): void
262
    {
263
        $entityFqn = $this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER);
264
265
        $this->helper->setCacheKey(__CLASS__ . '_faker');
266
        $fixture = $this->getUnmodifiedFixture();
267
        $this->helper->addFixture($fixture);
268
        $this->helper->createDb();
269
        $actual = $this->getEntityManager()
270
                       ->getRepository($entityFqn)
271
                       ->findAll();
272
        /**
273
         * @var EntityInterface $entity
274
         */
275
        foreach ($actual as $entity) {
276
            self::assertContains($entity->getEnum(), EnumFieldInterface::ENUM_OPTIONS);
277
        }
278
279
    }
280
}
281