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 (#101)
by joseph
18:59
created

UnusedRelationsRemoverTest::finderToArrayOfPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Large\CodeGeneration;
4
5
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\UnusedRelationsRemover;
8
use EdmondsCommerce\DoctrineStaticMeta\Tests\Assets\AbstractTest;
9
use Symfony\Component\Finder\Finder;
10
11
/**
12
 * @coversDefaultClass \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\UnusedRelationsRemover
13
 * @uses \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper
14
 * @uses \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator
15
 */
16
class UnusedRelationsRemoverTest extends AbstractTest
17
{
18
    public const WORK_DIR = AbstractTest::VAR_PATH . '/' . self::TEST_TYPE_LARGE . '/UnusedRelationsRemoverTest';
19
20
    public const TEST_ENTITY_FQN_BASE = self::TEST_PROJECT_ROOT_NAMESPACE .
21
                                        '\\' .
22
                                        AbstractGenerator::ENTITIES_FOLDER_NAME;
23
24
    public const TEST_ENTITIES = [
25
        self::TEST_ENTITY_FQN_BASE . '\\Blah\\Foo',
26
        self::TEST_ENTITY_FQN_BASE . '\\Bar\\Baz',
27
        self::TEST_ENTITY_FQN_BASE . '\\No\\Relative',
28
        self::TEST_ENTITY_FQN_BASE . '\\Meh',
29
        self::TEST_ENTITY_FQN_BASE . '\\Nested\\Something\\Ho\\Hum',
30
    ];
31
32
    /**
33
     * @var UnusedRelationsRemover
34
     */
35
    private $remover;
36
    /**
37
     * @var RelationsGenerator
38
     */
39
    private $relationsGenerator;
40
41
    /**
42
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
43
     * @coversNothing
44
     */
45
    public function setup()
46
    {
47
        parent::setup();
48
        $this->relationsGenerator = $this->getRelationsGenerator();
49
        $entityGenerator          = $this->getEntityGenerator();
50
        foreach (self::TEST_ENTITIES as $fqn) {
51
            $entityGenerator->generateEntity($fqn);
52
            $this->relationsGenerator->generateRelationCodeForEntity($fqn);
53
        }
54
    }
55
56
    /**
57
     * @test
58
     * @large
59
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
60
     * @throws \ReflectionException
61
     * @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\UnusedRelationsRemover
62
     */
63
    public function itShouldRemoveAllRelationsIfNoneAreUsed(): void
64
    {
65
        $this->setupCopiedWorkDir();
66
        $expectedFilesRemovedCount = 75;
67
        $actualFilesRemoved        = $this->remover->run($this->copiedWorkDir, $this->getCopiedNamespaceRoot());
68
        self::assertCount($expectedFilesRemovedCount, $actualFilesRemoved);
69
        $expectedFilesFound = [];
70
        $actualFilesFound   = $this->finderToArrayOfPaths(
71
            $this->finder()->files()->in($this->copiedWorkDir . '/src/Entity/Relations/')
72
        );
73
        self::assertSame($expectedFilesFound, $actualFilesFound);
74
    }
75
76
    protected function setupCopiedWorkDir(): string
77
    {
78
        $return        = parent::setupCopiedWorkDir();
79
        $this->remover = new UnusedRelationsRemover();
80
        $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

80
        $this->relationsGenerator->setPathToProjectRoot(/** @scrutinizer ignore-type */ $this->copiedWorkDir);
Loading history...
81
82
        return $return;
83
    }
84
85
    private function finderToArrayOfPaths(Finder $finder): array
86
    {
87
        $return = [];
88
        foreach ($finder as $fileInfo) {
89
            $return[] = $fileInfo->getRealPath();
90
        }
91
92
        return $return;
93
    }
94
95
    private function finder(): Finder
96
    {
97
        return new Finder();
98
    }
99
100
    /**
101
     * @test
102
     * @large
103
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
104
     * @throws \ReflectionException
105
     * @covers \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\UnusedRelationsRemover
106
     */
107
    public function itShouldNotRemoveUsedRelations(): void
108
    {
109
        $this->relationsGenerator->setEntityHasRelationToEntity(
110
            self::TEST_ENTITIES[0], //Blah\Foo
111
            RelationsGenerator::HAS_ONE_TO_MANY,
112
            self::TEST_ENTITIES[1] //Bar\Baz
113
        );
114
        $this->setupCopiedWorkDir();
115
116
        $actualFilesRemoved          = $this->remover->run($this->copiedWorkDir, $this->getCopiedNamespaceRoot());
117
        $actualFilesRemovedBasenames = array_map('basename', $actualFilesRemoved);
118
        self::assertNotContains('HasBlahFooManyToOne.php', $actualFilesRemovedBasenames);
119
        self::assertNotContains('HasBarBazsOneToMany.php', $actualFilesRemovedBasenames);
120
121
        $expectedFilesRemovedCount = 65;
122
        self::assertCount($expectedFilesRemovedCount, $actualFilesRemoved);
123
        $expectedFilesLeftCount = 10;
124
        $actualFilesLeft        = $this->finderToArrayOfPaths(
125
            $this->finder()->files()->in($this->copiedWorkDir . '/src/Entity/Relations/')
126
        );
127
        self::assertCount($expectedFilesLeftCount, $actualFilesLeft);
128
    }
129
}
130