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 (#29)
by Ross
03:51
created

AbstractTest.php$0 ➔ clearWorkDir()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta;
4
5
use Composer\Autoload\ClassLoader;
6
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\CodeHelper;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Command\AbstractCommand;
8
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\AbstractGenerator;
9
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\EntityGenerator;
10
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FieldGenerator;
11
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\RelationsGenerator;
12
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database;
13
use EdmondsCommerce\DoctrineStaticMeta\Schema\Schema;
14
use EdmondsCommerce\PHPQA\Constants;
15
use Overtrue\PHPLint\Linter;
16
use PHPUnit\Framework\TestCase;
17
use Symfony\Component\Filesystem\Filesystem;
18
19
/**
20
 * Class AbstractTest
21
 *
22
 * @package EdmondsCommerce\DoctrineStaticMeta
23
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
24
 */
25
abstract class AbstractTest extends TestCase
26
{
27
    public const VAR_PATH                    = __DIR__.'/../var';
28
    public const WORK_DIR                    = 'override me';
29
    public const TEST_PROJECT_ROOT_NAMESPACE = 'My\\Test\\Project';
30
31
    /**
32
     * The absolute path to the Entities folder, eg:
33
     * /var/www/vhosts/doctrine-static-meta/var/{testWorkDir}/Entities
34
     *
35
     * @var string
36
     */
37
    protected $entitiesPath = '';
38
39
    /**
40
     * The absolute path to the EntityRelations folder, eg:
41
     * /var/www/vhosts/doctrine-static-meta/var/{testWorkDir}/Entity/Relations
42
     *
43
     * @var string
44
     */
45
    protected $entityRelationsPath = '';
46
47
    /**
48
     * @var Container
49
     */
50
    protected $container;
51
52
    /**
53
     * @var Filesystem
54
     */
55
    protected $filesystem;
56
57
58
    /**
59
     * Prepare working directory, ensure its empty, create entities folder and set up env variables
60
     *
61
     * The order of these actions is critical
62
     */
63
    public function setup()
64
    {
65
        $this->entitiesPath = static::WORK_DIR
66
                              .'/'.AbstractCommand::DEFAULT_SRC_SUBFOLDER
67
                              .'/'.AbstractGenerator::ENTITIES_FOLDER_NAME;
68
        $this->getFileSystem()->mkdir($this->entitiesPath);
69
        $this->entitiesPath        = realpath($this->entitiesPath);
70
        $this->entityRelationsPath = static::WORK_DIR
71
                                     .'/'.AbstractCommand::DEFAULT_SRC_SUBFOLDER
72
                                     .'/'.AbstractGenerator::ENTITY_RELATIONS_FOLDER_NAME;
73
        $this->getFileSystem()->mkdir($this->entityRelationsPath);
74
        $this->entityRelationsPath = realpath($this->entityRelationsPath);
75
        $this->setupContainer();
76
        $this->clearWorkDir();
77
        $this->extendAutoloader();
78
    }
79
80
    /**
81
     * @throws Exception\ConfigException
82
     * @throws Exception\DoctrineStaticMetaException
83
     * @SuppressWarnings(PHPMD.Superglobals)
84
     * @SuppressWarnings(PHPMD.StaticAccess)
85
     */
86
    protected function setupContainer()
87
    {
88
        SimpleEnv::setEnv(Config::getProjectRootDirectory().'/.env');
89
        $testConfig                                       = $_SERVER;
90
        $testConfig[ConfigInterface::PARAM_ENTITIES_PATH] = $this->entitiesPath;
91
        $testConfig[ConfigInterface::PARAM_DB_NAME]       .= '_test';
92
        $testConfig[ConfigInterface::PARAM_DEVMODE]       = true;
93
        $this->container                                  = new Container();
94
        $this->container->buildSymfonyContainer($testConfig);
95
        $this->container->get(Database::class)->drop(true)->create(true);
96
    }
97
98
99
    /**
100
     * Accesses the standard Composer autoloader
101
     *
102
     * Extends the class and also providing an entry point for xdebugging
103
     *
104
     * Extends this with a PSR4 root for our WORK_DIR
105
     **/
106
    protected function extendAutoloader()
107
    {
108
        $loader = new class extends ClassLoader
109
        {
110
            public function loadClass($class)
111
            {
112
                if (false === strpos($class, AbstractTest::TEST_PROJECT_ROOT_NAMESPACE)) {
113
                    return false;
114
                }
115
                $found = parent::loadClass($class);
116
                if (\in_array(gettype($found), ['boolean', 'NULL'], true)) {
117
                    //good spot to set a break point ;)
118
                    return false;
119
                }
120
121
                return true;
122
            }
123
        };
124
        $loader->addPsr4(
125
            static::TEST_PROJECT_ROOT_NAMESPACE.'\\',
126
            static::WORK_DIR.'/'.AbstractCommand::DEFAULT_SRC_SUBFOLDER
127
        );
128
        $loader->register();
129
    }
130
131
    protected function clearWorkDir()
132
    {
133
        if (static::WORK_DIR === self::WORK_DIR) {
0 ignored issues
show
introduced by
The condition static::WORK_DIR === self::WORK_DIR is always true.
Loading history...
134
            throw new \RuntimeException(
135
                "You must set a `const WORK_DIR=AbstractTest::VAR_PATH.'/folderName/';` in your test class"
136
            );
137
        }
138
        $this->getFileSystem()->mkdir(static::WORK_DIR);
139
        $this->emptyDirectory(static::WORK_DIR);
140
        if (empty($this->entitiesPath)) {
141
            throw new \RuntimeException('$this->entitiesPath path is empty');
142
        }
143
        $this->getFileSystem()->mkdir($this->entitiesPath);
144
    }
145
146
    protected function getFileSystem(): Filesystem
147
    {
148
        if (null === $this->filesystem) {
149
            $this->filesystem = (null !== $this->container)
150
                ? $this->container->get(Filesystem::class)
151
                : new Filesystem();
152
        }
153
154
        return $this->filesystem;
155
    }
156
157
    protected function emptyDirectory(string $path)
158
    {
159
        $fileSystem = $this->getFileSystem();
160
        $fileSystem->remove($path);
161
        $fileSystem->mkdir($path);
162
    }
163
164
    protected function assertNoMissedReplacements(string $createdFile)
165
    {
166
        $createdFile = $this->container->get(CodeHelper::class)->resolvePath($createdFile);
167
        $this->assertFileExists($createdFile);
168
        $contents = file_get_contents($createdFile);
169
        $this->assertNotContains(
170
            'template',
171
            $contents,
172
            'Found the word "template" (case insensitive) in the created file '.$createdFile,
173
            true
174
        );
175
    }
176
177
    protected function assertFileContains(string $createdFile, string $needle)
178
    {
179
        $createdFile = $this->container->get(CodeHelper::class)->resolvePath($createdFile);
180
        $this->assertFileExists($createdFile);
181
        $contents = file_get_contents($createdFile);
182
        $this->assertContains(
183
            $needle,
184
            $contents,
185
            "Missing '$needle' in file '$createdFile'"
186
        );
187
    }
188
189
    protected function getEntityGenerator(): EntityGenerator
190
    {
191
        /**
192
         * @var EntityGenerator $entityGenerator
193
         */
194
        $entityGenerator = $this->container->get(EntityGenerator::class);
195
        $entityGenerator->setPathToProjectRoot(static::WORK_DIR)
196
                        ->setProjectRootNamespace(static::TEST_PROJECT_ROOT_NAMESPACE);
197
198
        return $entityGenerator;
199
    }
200
201
    protected function getRelationsGenerator(): RelationsGenerator
202
    {
203
        /**
204
         * @var RelationsGenerator $relationsGenerator
205
         */
206
        $relationsGenerator = $this->container->get(RelationsGenerator::class);
207
        $relationsGenerator->setPathToProjectRoot(static::WORK_DIR)
208
                           ->setProjectRootNamespace(static::TEST_PROJECT_ROOT_NAMESPACE);
209
210
        return $relationsGenerator;
211
    }
212
213
    protected function getFieldGenerator(): FieldGenerator
214
    {
215
        /**
216
         * @var FieldGenerator $fieldGenerator
217
         */
218
        $fieldGenerator = $this->container->get(FieldGenerator::class);
219
        $fieldGenerator->setPathToProjectRoot(static::WORK_DIR)
220
                       ->setProjectRootNamespace(static::TEST_PROJECT_ROOT_NAMESPACE);
221
222
        return $fieldGenerator;
223
    }
224
225
    protected function getSchema(): Schema
226
    {
227
        return $this->container->get(Schema::class);
228
    }
229
230
    /**
231
     * @SuppressWarnings(PHPMD.Superglobals)
232
     */
233
    public function qaGeneratedCode(): bool
234
    {
235
        if (isset($_SERVER[Constants::QA_QUICK_TESTS_KEY])
236
            && (int)$_SERVER[Constants::QA_QUICK_TESTS_KEY] === Constants::QA_QUICK_TESTS_ENABLED
237
        ) {
238
            return true;
239
        }
240
        //lint
241
        $path       = static::WORK_DIR;
242
        $exclude    = ['vendor'];
243
        $extensions = ['php'];
244
245
        $linter  = new Linter($path, $exclude, $extensions);
246
        $lint    = $linter->lint([], false);
247
        $message = str_replace($path, '', print_r($lint, true));
248
        $this->assertEmpty($lint, "\n\nPHP Syntax Errors in $path\n\n$message\n\n");
249
250
        return true;
251
    }
252
}
253