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 ( cc354f...2af0ad )
by joseph
12s
created

UnusedRelationsRemoverTest::setupCopiedWorkDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
use Symfony\Component\Finder\Finder;
9
10
class UnusedRelationsRemoverTest extends AbstractIntegrationTest
11
{
12
    public const WORK_DIR = AbstractIntegrationTest::VAR_PATH.'/'.self::TEST_TYPE.'/UnusedRelationsRemoverTest';
13
14
    public const TEST_ENTITY_FQN_BASE = self::TEST_PROJECT_ROOT_NAMESPACE.'\\'.AbstractGenerator::ENTITIES_FOLDER_NAME;
15
16
    public const TEST_ENTITIES = [
17
        self::TEST_ENTITY_FQN_BASE.'\\Blah\\Foo',
18
        self::TEST_ENTITY_FQN_BASE.'\\Bar\\Baz',
19
        self::TEST_ENTITY_FQN_BASE.'\\No\\Relative',
20
        self::TEST_ENTITY_FQN_BASE.'\\Meh',
21
        self::TEST_ENTITY_FQN_BASE.'\\Nested\\Something\\Ho\\Hum',
22
    ];
23
    /**
24
     * @var UnusedRelationsRemover
25
     */
26
    private $remover;
27
    /**
28
     * @var RelationsGenerator
29
     */
30
    private $relationsGenerator;
31
32
    /**
33
     * @var bool
34
     */
35
    private $built = false;
36
37
    public function setup()
38
    {
39
        parent::setup();
40
        if (!$this->built) {
41
            $this->relationsGenerator = $this->getRelationsGenerator();
42
            $entityGenerator          = $this->getEntityGenerator();
43
            foreach (self::TEST_ENTITIES as $fqn) {
44
                $entityGenerator->generateEntity($fqn);
45
                $this->relationsGenerator->generateRelationCodeForEntity($fqn);
46
            }
47
            $this->built = true;
48
        }
49
    }
50
51
    protected function setupCopiedWorkDir(): string
52
    {
53
        $return        = parent::setupCopiedWorkDir();
54
        $this->remover = new UnusedRelationsRemover();
55
        $this->relationsGenerator->setPathToProjectRoot($this->copiedWorkDir);
0 ignored issues
show
Bug introduced by
It seems like $this->copiedWorkDir can also be of type null; however, parameter $pathToProjectRoot of EdmondsCommerce\Doctrine...:setPathToProjectRoot() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

55
        $this->relationsGenerator->setPathToProjectRoot(/** @scrutinizer ignore-type */ $this->copiedWorkDir);
Loading history...
56
57
        return $return;
58
    }
59
60
    private function finder(): Finder
61
    {
62
        return new Finder();
63
    }
64
65
    private function finderToArrayOfPaths(Finder $finder): array
66
    {
67
        $return = [];
68
        foreach ($finder as $fileInfo) {
69
            $return[] = $fileInfo->getRealPath();
70
        }
71
72
        return $return;
73
    }
74
75
    /**
76
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
77
     * @throws \ReflectionException
78
     */
79
    public function testItShouldRemoveAllRelationsIfNoneAreUsed(): void
80
    {
81
        $this->setupCopiedWorkDir();
82
        $expectedFilesRemovedCount = 75;
83
        $actualFilesRemoved        = $this->remover->run($this->copiedWorkDir, $this->getCopiedNamespaceRoot());
84
        self::assertCount($expectedFilesRemovedCount, $actualFilesRemoved);
85
        $expectedFilesFound = [];
86
        $actualFilesFound   = $this->finderToArrayOfPaths(
87
            $this->finder()->files()->in($this->copiedWorkDir.'/src/Entity/Relations/')
88
        );
89
        self::assertSame($expectedFilesFound, $actualFilesFound);
90
    }
91
92
    /**
93
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
94
     * @throws \ReflectionException
95
     */
96
    public function testItShouldNotRemoveUsedRelations(): void
97
    {
98
        $this->relationsGenerator->setEntityHasRelationToEntity(
99
            self::TEST_ENTITIES[0], //Blah\Foo
100
            RelationsGenerator::HAS_ONE_TO_MANY,
101
            self::TEST_ENTITIES[1] //Bar\Baz
102
        );
103
        $this->setupCopiedWorkDir();
104
105
        $actualFilesRemoved          = $this->remover->run($this->copiedWorkDir, $this->getCopiedNamespaceRoot());
106
        $actualFilesRemovedBasenames = array_map('basename', $actualFilesRemoved);
107
        self::assertNotContains('HasBlahFooManyToOne.php', $actualFilesRemovedBasenames);
108
        self::assertNotContains('HasBarBazsOneToMany.php', $actualFilesRemovedBasenames);
109
110
        $expectedFilesRemovedCount = 65;
111
        self::assertCount($expectedFilesRemovedCount, $actualFilesRemoved);
112
        $expectedFilesLeftCount = 10;
113
        $actualFilesLeft        = $this->finderToArrayOfPaths(
114
            $this->finder()->files()->in($this->copiedWorkDir.'/src/Entity/Relations/')
115
        );
116
        self::assertCount($expectedFilesLeftCount, $actualFilesLeft);
117
    }
118
}
119