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 ( 647edc...edf045 )
by joseph
18s queued 16s
created

testStripPrefixFromHasType()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 39
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
cc 6
eloc 32
nc 6
nop 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration;
4
5
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator;
8
9
/**
10
 * Class NamespaceHelperTest
11
 *
12
 * @package EdmondsCommerce\DoctrineStaticMeta\CodeGeneration
13
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
14
 */
15
class NamespaceHelperIntegrationTest extends AbstractIntegrationTest
16
{
17
18
    public const WORK_DIR = AbstractIntegrationTest::VAR_PATH.'/'.self::TEST_TYPE.'/NamespaceHelperTest';
19
20
    public const TEST_ENTITY_FQN_BASE = self::TEST_PROJECT_ROOT_NAMESPACE.'\\'.AbstractGenerator::ENTITIES_FOLDER_NAME;
21
22
    public const TEST_ENTITIES = [
23
        self::TEST_ENTITY_FQN_BASE.'\\Blah\\Foo',
24
        self::TEST_ENTITY_FQN_BASE.'\\Bar\\Baz',
25
        self::TEST_ENTITY_FQN_BASE.'\\No\\Relative',
26
    ];
27
28
    public const TEST_ENTITY_WITH_ENTITIES_IN_PROJECT_NAME = '\\My\\EntitiesProject\\Entities\\Blah\\Foo';
29
30
    public const TEST_ENTITY_POST_CREATED        = self::TEST_ENTITY_FQN_BASE.'\\Meh';
31
    public const TEST_ENTITY_POST_CREATED_NESTED = self::TEST_ENTITY_FQN_BASE.'\\Nested\\Something\\Ho\\Hum';
32
33
    /**
34
     * @var NamespaceHelper
35
     */
36
    private $helper;
37
38
    /**
39
     *
40
     */
41
    public function setup()
42
    {
43
        parent::setup();
44
        $this->helper       = $this->container->get(NamespaceHelper::class);
45
        $entityGenerator    = $this->getEntityGenerator();
46
        $relationsGenerator = $this->getRelationsGenerator();
47
        foreach (self::TEST_ENTITIES as $fqn) {
48
            $entityGenerator->generateEntity($fqn);
49
            $relationsGenerator->generateRelationCodeForEntity($fqn);
50
        }
51
        $relationsGenerator->setEntityHasRelationToEntity(
52
            self::TEST_ENTITIES[0],
53
            RelationsGenerator::HAS_MANY_TO_MANY,
54
            self::TEST_ENTITIES[1]
55
        );
56
        /**
57
         * Something is causing PHP files to be loaded by PHP as part of the creation.
58
         * Have not been able ot track this down.
59
         * Creating a new file is a workaround for this
60
         */
61
        file_put_contents(
62
            self::WORK_DIR.'/src/Entities/Meh.php',
63
            <<<PHP
64
<?php
65
declare(strict_types=1);
66
67
namespace My\Test\Project\Entities;
68
69
use My\Test\Project\Entity\Relations\Blah\Foo\Interfaces\HasBlahFoosInterface;
70
use My\Test\Project\Entity\Relations\Blah\Foo\Interfaces\ReciprocatesBlahFooInterface;
71
use My\Test\Project\Entity\Relations\Blah\Foo\Traits\HasBlahFoos\HasBlahFoosInverseManyToMany;
72
use EdmondsCommerce\DoctrineStaticMeta\Entity as DSM;
73
74
class Meh implements DSM\Interfaces\UsesPHPMetaDataInterface, HasBlahFoosInterface, ReciprocatesBlahFooInterface {
75
76
	use DSM\Traits\UsesPHPMetaDataTrait;
77
	use DSM\Fields\Traits\PrimaryKey\IdFieldTrait;
78
	use HasBlahFoosInverseManyToMany;
79
	
80
	protected static function setCustomRepositoryClass(ClassMetadataBuilder \$builder)
81
    {
82
        
83
    }
84
}
85
86
PHP
87
        );
88
        $this->getFileSystem()->mkdir(self::WORK_DIR.'/src/Entities/Nested/Something/Ho');
89
        /**
90
         * Something is causing PHP files to be loaded by PHP as part of the creation.
91
         * Have not been able ot track this down.
92
         * Creating a new file is a workaround for this
93
         */
94
        file_put_contents(
95
            self::WORK_DIR.'/src/Entities/Nested/Something/Ho/Hum.php',
96
            <<<PHP
97
<?php
98
declare(strict_types=1);
99
100
namespace My\Test\Project\Entities\Nested\Something\Ho;
101
102
use My\Test\Project\Entity\Relations\Blah\Foo\Interfaces\HasBlahFoosInterface;
103
use My\Test\Project\Entity\Relations\Blah\Foo\Interfaces\ReciprocatesBlahFooInterface;
104
use My\Test\Project\Entity\Relations\Blah\Foo\Traits\HasBlahFoos\HasBlahFoosInverseManyToMany;
105
use EdmondsCommerce\DoctrineStaticMeta\Entity as DSM;
106
107
class Hum implements DSM\Interfaces\UsesPHPMetaDataInterface, HasBlahFoosInterface, ReciprocatesBlahFooInterface {
108
109
	use DSM\Traits\UsesPHPMetaDataTrait;
110
	use DSM\Fields\Traits\PrimaryKey\IdFieldTrait;
111
	use HasBlahFoosInverseManyToMany;
112
	
113
	protected static function setCustomRepositoryClass(ClassMetadataBuilder \$builder)
114
    {
115
        
116
    }
117
}
118
119
PHP
120
        );
121
    }
122
123
    public function testTidy()
124
    {
125
        $namespaceToExpected = [
126
            'Test\\\\Multiple\\\\\\\Separators' => 'Test\\Multiple\\Separators',
127
            'No\\Changes\\Required'             => 'No\\Changes\\Required',
128
        ];
129
        foreach ($namespaceToExpected as $namespace => $expected) {
130
            $this->assertSame($expected, $this->helper->tidy($namespace));
131
        }
132
    }
133
134
    public function testRoot()
135
    {
136
        $namespaceToExpected = [
137
            '\\Test\\\\Multiple\\\\\\\Separators' => 'Test\\Multiple\\Separators',
138
            'No\\Changes\\Required'               => 'No\\Changes\\Required',
139
        ];
140
        foreach ($namespaceToExpected as $namespace => $expected) {
141
            $this->assertSame($expected, $this->helper->root($namespace));
142
        }
143
    }
144
145
    /**
146
     */
147
    public function testCalculateProjectNamespaceRootFromEntitFqn()
148
    {
149
        $entity1Fqn = self::TEST_ENTITIES[0];
150
151
        $expected = self::TEST_PROJECT_ROOT_NAMESPACE;
152
        $actual   = $this->helper->getProjectNamespaceRootFromEntityFqn($entity1Fqn);
153
        $this->assertSame($expected, $actual);
154
155
        $entityFqnWithEntitiesInProjectName = self::TEST_ENTITY_WITH_ENTITIES_IN_PROJECT_NAME;
156
        $expected                           = '\\My\\EntitiesProject';
157
        $actual                             = $this->helper->getProjectNamespaceRootFromEntityFqn(
158
            $entityFqnWithEntitiesInProjectName
159
        );
160
        $this->assertSame($expected, $actual);
161
    }
162
163
    /**
164
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
165
     */
166
    public function testParseFullyQualifiedName()
167
    {
168
        $entity1Fqn           = self::TEST_ENTITIES[0];
169
        $srcOrTestSubFolder   = 'src';
170
        $projectRootNamespace = self::TEST_PROJECT_ROOT_NAMESPACE;
171
        $expected             = [
172
            'Foo',
173
            $projectRootNamespace.'\\'.AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Blah',
174
            [
175
                'src',
176
                'Entities',
177
                'Blah',
178
            ],
179
        ];
180
        $actual               = $this->helper->parseFullyQualifiedName(
181
            $entity1Fqn,
182
            $srcOrTestSubFolder,
183
            $projectRootNamespace
184
        );
185
        $this->assertSame($expected, $actual);
186
187
        $entity1Fqn           = '\\'.self::TEST_ENTITIES[0];
0 ignored issues
show
Unused Code introduced by
The assignment to $entity1Fqn is dead and can be removed.
Loading history...
188
        $srcOrTestSubFolder   = 'src';
189
        $projectRootNamespace = '\\'.self::TEST_PROJECT_ROOT_NAMESPACE;
190
        $expected             = [
191
            'Foo',
192
            ltrim($projectRootNamespace.'\\'.AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Blah', '\\'),
193
            [
194
                'src',
195
                'Entities',
196
                'Blah',
197
            ],
198
        ];
199
        $actual               = $this->helper->parseFullyQualifiedName(
200
            self::TEST_ENTITIES[0],
201
            $srcOrTestSubFolder,
202
            $projectRootNamespace
203
        );
204
        $this->assertSame($expected, $actual);
205
    }
206
207
    /**
208
     */
209
    public function testCalculcateOwnedHasName()
210
    {
211
        $hasType              = RelationsGenerator::HAS_MANY_TO_MANY;
212
        $ownedEntityFqn       = self::TEST_ENTITIES[0];
213
        $expected             = 'BlahFoos';
214
        $srcOrTestSubFolder   = 'src';
215
        $projectRootNamespace = '\\'.self::TEST_PROJECT_ROOT_NAMESPACE;
216
217
        $actual = $this->helper->getOwnedHasName(
218
            $hasType,
219
            $ownedEntityFqn,
220
            $srcOrTestSubFolder,
221
            $projectRootNamespace
222
        );
223
224
        $this->assertSame($expected, $actual);
225
    }
226
227
    /**
228
     */
229
    public function testGetEntitySubNamespace()
230
    {
231
        $entityFqn = self::TEST_ENTITIES[0];
232
        $expected  = 'Blah\\Foo';
233
        $actual    = $this->helper->getEntitySubNamespace($entityFqn);
234
        $this->assertSame($expected, $actual);
235
236
        $entityFqn = '\\My\\Test\\Project\\Entities\\No\\Relatives';
237
        $expected  = 'No\\Relatives';
238
        $actual    = $this->helper->getEntitySubNamespace($entityFqn);
239
        $this->assertSame($expected, $actual);
240
241
        $entityFqn = '\\My\\Test\\Project\\Entities\\Person';
242
        $expected  = 'Person';
243
        $actual    = $this->helper->getEntitySubNamespace($entityFqn);
244
        $this->assertSame($expected, $actual);
245
246
        $entityFqn = '\\My\\Test\\EntitiesProject\\Entities\\Person';
247
        $expected  = 'Person';
248
        $actual    = $this->helper->getEntitySubNamespace($entityFqn);
249
        $this->assertSame($expected, $actual);
250
    }
251
252
    /**
253
     */
254
    public function testGetEntitySubFilePath()
255
    {
256
        $entityFqn = '\\My\\Test\\Project\\Entities\\Person';
257
        $expected  = '/Person.php';
258
        $actual    = $this->helper->getEntityFileSubPath($entityFqn);
259
        $this->assertSame($expected, $actual);
260
261
        $entityFqn = self::TEST_ENTITY_WITH_ENTITIES_IN_PROJECT_NAME;
262
        $expected  = '/Blah/Foo.php';
263
        $actual    = $this->helper->getEntityFileSubPath($entityFqn);
264
        $this->assertSame($expected, $actual);
265
    }
266
267
    /**
268
     */
269
    public function testGetEntitySubPath()
270
    {
271
        $entityFqn = self::TEST_ENTITIES[0];
272
        $expected  = '/Blah/Foo';
273
        $actual    = $this->helper->getEntitySubPath($entityFqn);
274
        $this->assertSame($expected, $actual);
275
276
        $entityFqn = self::TEST_ENTITY_WITH_ENTITIES_IN_PROJECT_NAME;
277
        $expected  = '/Blah/Foo';
278
        $actual    = $this->helper->getEntitySubPath($entityFqn);
279
        $this->assertSame($expected, $actual);
280
    }
281
282
    /**
283
     */
284
    public function testGetInterfacesNamespaceForEntity()
285
    {
286
        $entityFqn                    = self::TEST_ENTITIES[0];
287
        $entityRelationsRootNamespace = self::TEST_PROJECT_ROOT_NAMESPACE
288
                                        .AbstractGenerator::ENTITY_RELATIONS_NAMESPACE;
289
        $expected                     = $entityRelationsRootNamespace.'\\Blah\\Foo\\Interfaces';
290
        $actual                       = $this->helper->getInterfacesNamespaceForEntity($entityFqn);
291
        $this->assertSame($expected, $actual);
292
    }
293
294
    /**
295
     */
296
    public function testGetTraitsNamespaceForEntity()
297
    {
298
        $entityFqn                    = self::TEST_ENTITIES[0];
299
        $entityRelationsRootNamespace = self::TEST_PROJECT_ROOT_NAMESPACE
300
                                        .AbstractGenerator::ENTITY_RELATIONS_NAMESPACE;
301
        $expected                     = $entityRelationsRootNamespace.'\\Blah\\Foo\\Traits';
302
        $actual                       = $this->helper->getTraitsNamespaceForEntity($entityFqn);
303
        $this->assertSame($expected, $actual);
304
    }
305
306
    /**
307
     * @throws \ReflectionException
308
     */
309
    public function testGetEntityNamespaceRootFromEntityReflection()
310
    {
311
312
        $entityReflection = new \ReflectionClass(self::TEST_ENTITY_POST_CREATED);
313
        $expected         = self::TEST_PROJECT_ROOT_NAMESPACE.'\\'.AbstractGenerator::ENTITIES_FOLDER_NAME;
314
        $actual           = $this->helper->getEntityNamespaceRootFromEntityReflection($entityReflection);
315
        $this->assertSame($expected, $actual);
316
317
        $entityFqn = '\\My\\Test\\Project\\Entities\\No\\Relative';
318
        $actual    = $this->helper->getEntityNamespaceRootFromEntityReflection(
319
            new \ReflectionClass($entityFqn)
320
        );
321
        $this->assertSame($expected, $actual);
322
    }
323
324
    /**
325
     */
326
    public function testGetHasPluralInterfaceFqnForEntity()
327
    {
328
        $entityFqn = self::TEST_ENTITY_POST_CREATED;
329
        $expected  = self::TEST_PROJECT_ROOT_NAMESPACE
330
                     .AbstractGenerator::ENTITY_RELATIONS_NAMESPACE
331
                     .'\\Meh\\Interfaces\\HasMehsInterface';
332
        $actual    = $this->helper->getHasPluralInterfaceFqnForEntity($entityFqn);
333
        $this->assertSame($expected, $actual);
334
335
        $entityFqn = self::TEST_ENTITY_POST_CREATED_NESTED;
336
        $expected  = self::TEST_PROJECT_ROOT_NAMESPACE
337
                     .AbstractGenerator::ENTITY_RELATIONS_NAMESPACE
338
                     .'\\Nested\\Something\\Ho\\Hum\\Interfaces\\HasNestedSomethingHoHumsInterface';
339
        $actual    = $this->helper->getHasPluralInterfaceFqnForEntity($entityFqn);
340
        $this->assertSame($expected, $actual);
341
    }
342
343
    /**
344
     */
345
    public function testgetHasSingularInterfaceFqnForEntity()
346
    {
347
        $entityFqn = self::TEST_ENTITY_POST_CREATED;
348
        $expected  = self::TEST_PROJECT_ROOT_NAMESPACE
349
                     .AbstractGenerator::ENTITY_RELATIONS_NAMESPACE
350
                     .'\\Meh\\Interfaces\\HasMehInterface';
351
        $actual    = $this->helper->getHasSingularInterfaceFqnForEntity($entityFqn);
352
        $this->assertSame($expected, $actual);
353
354
        $entityFqn = self::TEST_ENTITY_POST_CREATED_NESTED;
355
        $expected  = self::TEST_PROJECT_ROOT_NAMESPACE
356
                     .AbstractGenerator::ENTITY_RELATIONS_NAMESPACE
357
                     .'\\Nested\\Something\\Ho\\Hum\\Interfaces\\HasNestedSomethingHoHumInterface';
358
        $actual    = $this->helper->getHasSingularInterfaceFqnForEntity($entityFqn);
359
        $this->assertSame($expected, $actual);
360
    }
361
362
    /**
363
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
364
     */
365
    public function testGetProjectRootNamespaceFromComposerJson()
366
    {
367
        $expected = 'EdmondsCommerce\\DoctrineStaticMeta';
368
        $actual   = $this->helper->getProjectRootNamespaceFromComposerJson();
369
        $this->assertSame($expected, $actual);
370
    }
371
372
    /**
373
     */
374
    public function testStripPrefixFromHasType()
375
    {
376
        $expected = [
377
            'OwningOneToOne'          => 'OwningOneToOne',
378
            'InverseOneToOne'         => 'InverseOneToOne',
379
            'UnidirectionalOneToOne'  => 'UnidirectionalOneToOne',
380
            'OneToMany'               => 'OneToMany',
381
            'UnidirectionalOneToMany' => 'UnidirectionalOneToMany',
382
            'ManyToOne'               => 'ManyToOne',
383
            'UnidirectionalManyToOne' => 'UnidirectionalManyToOne',
384
            'OwningManyToMany'        => 'OwningManyToMany',
385
            'InverseManyToMany'       => 'InverseManyToMany',
386
        ];
387
        $actual   = [];
388
        foreach (RelationsGenerator::HAS_TYPES as $hasType) {
389
            $actual[$hasType] = $this->helper->stripPrefixFromHasType($hasType);
390
        }
391
        $this->assertSame($expected, $actual);
392
        foreach ($actual as $hasType => $stripped) {
393
            $ownedHasName    = $this->helper->getOwnedHasName(
394
                $hasType,
395
                "\\TemplateNamespace\\Entities\\TemplateEntity",
396
                'src',
397
                '\\TemplateNamespace'
398
            );
399
            $filePath        = realpath(AbstractGenerator::TEMPLATE_PATH)
400
                               .'/src/Entity/Relations/TemplateEntity/Traits/Has'
401
                               .$ownedHasName.'/Has'.$ownedHasName.$stripped.'.php';
402
            $longestExisting = '';
403
            foreach (explode('/', $filePath) as $part) {
404
                $maybeLongestExisting = $longestExisting.'/'.$part;
405
                if (is_file($maybeLongestExisting) || is_dir($maybeLongestExisting)) {
406
                    $longestExisting = $maybeLongestExisting;
407
                    continue;
408
                }
409
                break;
410
            }
411
            $longestExisting = substr($longestExisting, 1);
412
            $this->assertFileExists($filePath, "\n$filePath\nexists up to:\n$longestExisting\n");
413
        }
414
    }
415
416
    /**
417
     */
418
    public function testGetOwningTraitFqn()
419
    {
420
        $traitBase = '\\TemplateNamespace\\Entity\Relations\\TemplateEntity\\Traits';
421
        $expected  = [
422
            'OwningOneToOne'          => $traitBase.'\\HasTemplateEntity\\HasTemplateEntityOwningOneToOne',
423
            'InverseOneToOne'         => $traitBase.'\\HasTemplateEntity\\HasTemplateEntityInverseOneToOne',
424
            'UnidirectionalOneToOne'  => $traitBase.'\\HasTemplateEntity\\HasTemplateEntityUnidirectionalOneToOne',
425
            'OneToMany'               => $traitBase.'\\HasTemplateEntities\\HasTemplateEntitiesOneToMany',
426
            'UnidirectionalOneToMany' => $traitBase.'\\HasTemplateEntities\\HasTemplateEntitiesUnidirectionalOneToMany',
427
            'ManyToOne'               => $traitBase.'\\HasTemplateEntity\\HasTemplateEntityManyToOne',
428
            'UnidirectionalManyToOne' => $traitBase.'\\HasTemplateEntity\\HasTemplateEntityUnidirectionalManyToOne',
429
            'OwningManyToMany'        => $traitBase.'\\HasTemplateEntities\\HasTemplateEntitiesOwningManyToMany',
430
            'InverseManyToMany'       => $traitBase.'\\HasTemplateEntities\\HasTemplateEntitiesInverseManyToMany',
431
        ];
432
        $actual    = [];
433
        foreach (RelationsGenerator::HAS_TYPES as $hasType) {
434
            $actual[$hasType] = $this->helper->getOwningTraitFqn(
435
                $hasType,
436
                "\\TemplateNamespace\\Entities\\TemplateEntity",
437
                "\\TemplateNamespace"
438
            );
439
        }
440
        $this->assertSame(
441
            $expected,
442
            $actual,
443
            "\nExpected:\n".var_export($actual, true)
444
            ."\nActual:\n".var_export($actual, true)."\n"
445
        );
446
    }
447
448
    /**
449
     */
450
    public function testGetOwningInterfaceFqn()
451
    {
452
        $intBase  = '\\TemplateNamespace\\Entity\Relations\\TemplateEntity\\Interfaces';
453
        $expected = [
454
            'OwningOneToOne'          => $intBase.'\\HasTemplateEntityInterface',
455
            'InverseOneToOne'         => $intBase.'\\HasTemplateEntityInterface',
456
            'UnidirectionalOneToOne'  => $intBase.'\\HasTemplateEntityInterface',
457
            'OneToMany'               => $intBase.'\\HasTemplateEntitiesInterface',
458
            'UnidirectionalOneToMany' => $intBase.'\\HasTemplateEntitiesInterface',
459
            'ManyToOne'               => $intBase.'\\HasTemplateEntityInterface',
460
            'UnidirectionalManyToOne' => $intBase.'\\HasTemplateEntityInterface',
461
            'OwningManyToMany'        => $intBase.'\\HasTemplateEntitiesInterface',
462
            'InverseManyToMany'       => $intBase.'\\HasTemplateEntitiesInterface',
463
        ];
464
        $actual   = [];
465
        foreach (RelationsGenerator::HAS_TYPES as $hasType) {
466
            $actual[$hasType] = $this->helper->getOwningInterfaceFqn(
467
                $hasType,
468
                "\\TemplateNamespace\\Entities\\TemplateEntity",
469
                "\\TemplateNamespace"
470
            );
471
        }
472
        $this->assertSame(
473
            $expected,
474
            $actual,
475
            "\nExpected:\n".var_export($actual, true)
476
            ."\nActual:\n".var_export($actual, true)."\n"
477
        );
478
    }
479
}
480