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
Push — master ( bcc1e6...b8d566 )
by joseph
19:29 queued 16:40
created

php$0 ➔ itCanTakeAModifierToCustomiseTheFixtures()   A

Complexity

Conditions 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A FixturesTest.php$0 ➔ updateFirstEntity() 0 3 1
A FixturesTest.php$0 ➔ addAnotherEntity() 0 5 1
A FixturesTest.php$0 ➔ modifyEntities() 0 5 1
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
    /**
69
     * @test
70
     * @large
71
     */
72
    public function itLoadsAllTheFixturesWithRandomDataByDefault(): array
73
    {
74
        $this->helper->setCacheKey(__CLASS__ . '_unmodified');
75
        $fixture = $this->getUnmodifiedFixture();
76
        $this->helper->addFixture($fixture);
77
        $this->helper->createDb();
78
        $actual      = $this->getEntityManager()
79
                            ->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER))
80
                            ->findAll();
81
        $actualCount = count($actual);
82
        self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE, $actualCount);
83
84
        return $actual;
85
    }
86
87
    private function getUnmodifiedFixture(): AbstractEntityFixtureLoader
88
    {
89
        return $this->getFixture($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER));
90
    }
91
92
    private function getFixture(
93
        string $entityFqn,
94
        ?FixtureEntitiesModifierInterface $modifier = null
95
    ): AbstractEntityFixtureLoader {
96
        $fixtureFqn = $this->getNamespaceHelper()->getFixtureFqnFromEntityFqn($entityFqn);
97
98
        return new $fixtureFqn(
99
            $this->container->get(TestEntityGeneratorFactory::class),
100
            $this->container->get(EntitySaverFactory::class),
101
            $this->container->get(NamespaceHelper::class),
102
            $modifier
103
        );
104
    }
105
106
    /**
107
     * @test
108
     * @large
109
     * @depends itLoadsAllTheFixturesWithRandomDataByDefault
110
     *
111
     * @param array $loadedFirstTime
112
     *
113
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
114
     * @throws \ReflectionException
115
     */
116
    public function itUsesTheCacheTheSecondTime(array $loadedFirstTime): void
117
    {
118
        $this->getFileSystem()
119
             ->mirror(
120
                 $this->copiedWorkDir .
121
                 '/../FixturesTest_itLoadsAllTheFixturesWithRandomDataByDefault_/cache',
122
                 $this->copiedWorkDir . '/cache'
123
             );
124
        $this->helper->setCacheKey(__CLASS__ . '_unmodified');
125
        $fixture = $this->getUnmodifiedFixture();
126
        $this->helper->addFixture($fixture);
127
        $this->helper->createDb();
128
        self::assertTrue($this->helper->isLoadedFromCache());
129
        /**
130
         * @var EntityInterface[] $actual
131
         */
132
        $actual        = $this->getEntityManager()
133
                              ->getRepository($this->getCopiedFqn(self::ENTITY_WITHOUT_MODIFIER))
134
                              ->findAll();
135
        $actualCount   = count($actual);
136
        $expectedCount = count($loadedFirstTime);
137
        self::assertSame($expectedCount, $actualCount);
138
        foreach ($actual as $key => $actualEntity) {
139
            $expectedEntity = $loadedFirstTime[$key];
140
            $actualId       = $actualEntity->getId();
141
            $expectedId     = $expectedEntity->getId();
142
            $expectedText   = $expectedEntity->getString();
143
            $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

143
            /** @scrutinizer ignore-call */ 
144
            $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...
144
            self::assertEquals($expectedId, $actualId, 'Cached Entity ID does not match');
145
            self::assertEquals($expectedText, $actualText, 'Cached Faker data does not match');
146
        }
147
    }
148
149
    /**
150
     * @test
151
     * @large
152
     */
153
    public function itCanTakeAModifierToCustomiseTheFixtures()
154
    {
155
        $this->helper->setCacheKey(__CLASS__ . '_modified');
156
        $fixture = $this->getModifiedFixture();
157
        $this->helper->addFixture($fixture);
158
        $this->helper->createDb();
159
        /**
160
         * @var EntityInterface[] $actual
161
         */
162
        $actual      = $this->getEntityManager()
163
                            ->getRepository($this->getCopiedFqn(self::ENTITY_WITH_MODIFIER))
164
                            ->findAll();
165
        $actualCount = count($actual);
166
        self::assertSame(AbstractEntityFixtureLoader::BULK_AMOUNT_TO_GENERATE + 1, $actualCount);
167
        $firstEntity    = $actual[0];
168
        $expectedString = 'This has been overridden';
169
        $actualString   = $firstEntity->getString();
170
        self::assertSame($expectedString, $actualString);
171
        end($actual);
172
        $lastEntity     = current($actual);
173
        $expectedString = 'This has been created';
174
        $actualString   = $lastEntity->getString();
175
        self::assertSame($expectedString, $actualString);
176
    }
177
178
    private function getModifiedFixture(): AbstractEntityFixtureLoader
179
    {
180
        return $this->getFixture(
181
            $this->getCopiedFqn(self::ENTITY_WITH_MODIFIER),
182
            $this->getFixtureModifier()
183
        );
184
    }
185
186
    private function getFixtureModifier(): FixtureEntitiesModifierInterface
187
    {
188
        return new class(
189
            $this->getCopiedFqn(self::ENTITY_WITH_MODIFIER),
190
            $this->getEntityFactory()
191
        )
192
            implements FixtureEntitiesModifierInterface
193
        {
194
            /**
195
             * @var string
196
             */
197
            protected $entityFqn;
198
            /**
199
             * @var EntityFactory
200
             */
201
            protected $factory;
202
            /**
203
             * @var array|EntityInterface[]
204
             */
205
            private $entities;
206
207
            public function __construct(string $entityFqn, EntityFactory $factory)
208
            {
209
                $this->entityFqn = $entityFqn;
210
                $this->factory   = $factory;
211
            }
212
213
            /**
214
             * Update the entities array by reference
215
             *
216
             * @param array $entities
217
             */
218
            public function modifyEntities(array &$entities): void
219
            {
220
                $this->entities = &$entities;
221
                $this->updateFirstEntity();
222
                $this->addAnotherEntity();
223
            }
224
225
            private function updateFirstEntity(): void
226
            {
227
                $this->entities[0]->setString('This has been overridden');
228
            }
229
230
            private function addAnotherEntity(): void
231
            {
232
                $entity = $this->factory->create($this->entityFqn);
233
                $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

233
                $entity->/** @scrutinizer ignore-call */ 
234
                         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...
234
                $this->entities[] = $entity;
235
            }
236
        };
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