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 (#52)
by joseph
38:21 queued 34:31
created

FieldGeneratorIntegrationTest::buildAndCheck()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 52
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 8.9408
c 0
b 0
f 0
cc 4
eloc 35
nc 6
nop 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator;
4
5
use Doctrine\Common\Util\Inflector;
6
use EdmondsCommerce\DoctrineStaticMeta\AbstractIntegrationTest;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
9
10
class FieldGeneratorIntegrationTest extends AbstractIntegrationTest
11
{
12
    public const WORK_DIR = AbstractIntegrationTest::VAR_PATH.'/'.self::TEST_TYPE.'/FieldGeneratorTest/';
13
14
    private const TEST_ENTITY_CAR = self::TEST_PROJECT_ROOT_NAMESPACE.'\\'
15
                                    .AbstractGenerator::ENTITIES_FOLDER_NAME.'\\Car';
16
17
    private const TEST_FIELD_NAMESPACE = self::TEST_PROJECT_ROOT_NAMESPACE.'\\'
18
                                         .AbstractGenerator::ENTITY_FIELD_TRAIT_NAMESPACE;
19
20
    private const CAR_FIELDS_TO_TYPES = [
21
        [self::TEST_FIELD_NAMESPACE.'\\Brand', MappingHelper::TYPE_STRING],
22
        [self::TEST_FIELD_NAMESPACE.'\\EngineCC', MappingHelper::TYPE_INTEGER],
23
        [self::TEST_FIELD_NAMESPACE.'\\Manufactured', MappingHelper::TYPE_DATETIME],
24
        [self::TEST_FIELD_NAMESPACE.'\\Mpg', MappingHelper::TYPE_FLOAT],
25
        [self::TEST_FIELD_NAMESPACE.'\\Description', MappingHelper::TYPE_TEXT],
26
        [self::TEST_FIELD_NAMESPACE.'\\IsCar', MappingHelper::TYPE_BOOLEAN],
27
    ];
28
29
    private const UNIQUE_FIELDS_TO_TYPES = [
30
        [self::TEST_FIELD_NAMESPACE.'\\UniqueString', MappingHelper::TYPE_STRING],
31
        [self::TEST_FIELD_NAMESPACE.'\\UniqueInt', MappingHelper::TYPE_INTEGER],
32
    ];
33
34
    /**
35
     * @var FieldGenerator
36
     */
37
    private $fieldGenerator;
38
39
    public function setup()
40
    {
41
        parent::setup();
42
        $this->getEntityGenerator()->generateEntity(self::TEST_ENTITY_CAR);
43
        $this->fieldGenerator = $this->getFieldGenerator();
44
    }
45
46
    public function testFieldMustContainEntityNamespace()
47
    {
48
        $this->expectException(\InvalidArgumentException::class);
49
        $this->fieldGenerator->generateField(
50
            '\\Blah\\Foop',
51
            MappingHelper::TYPE_STRING,
52
            null,
53
            null,
54
            true
55
        );
56
    }
57
58
    public function testFieldTypeMustBeValid()
59
    {
60
        $this->expectException(\InvalidArgumentException::class);
61
        $this->fieldGenerator->generateField(
62
            self::CAR_FIELDS_TO_TYPES[0][0],
63
            'invalid',
64
            null,
65
            null,
66
            true
67
        );
68
    }
69
70
    public function testPHPTypeMustBeValid()
71
    {
72
        $this->expectException(\InvalidArgumentException::class);
73
        $this->fieldGenerator->generateField(
74
            self::CAR_FIELDS_TO_TYPES[0][0],
75
            MappingHelper::PHP_TYPE_FLOAT,
76
            'invalid',
77
            null,
78
            true
79
        );
80
    }
81
82
    public function testDefaultTypeMustBeValid()
83
    {
84
        $this->expectException(\InvalidArgumentException::class);
85
        $this->fieldGenerator->generateField(
86
            self::CAR_FIELDS_TO_TYPES[0][0],
87
            MappingHelper::PHP_TYPE_FLOAT,
88
            'invalid',
89
            'clearly not a float',
90
            true
91
        );
92
    }
93
94
    /**
95
     * Default values passed in by CLI could come through quite dirty and need to be normalised     *
96
     */
97
    public function testDefaultValueIsNormalised()
98
    {
99
        $defaultValuesToTypes = [
100
            MappingHelper::TYPE_INTEGER => [
101
                1,
102
                '1',
103
                ' 1',
104
                ' 1 ',
105
            ],
106
            MappingHelper::TYPE_FLOAT   => [
107
                1,
108
                1.0,
109
                '1',
110
                '1.1',
111
                ' 1.1 ',
112
                ' 1.1 ',
113
            ],
114
            MappingHelper::TYPE_BOOLEAN => [
115
                'true',
116
                'false',
117
                'TRUE',
118
                'FALSE',
119
                ' TRue ',
120
                ' FaLse ',
121
            ],
122
        ];
123
        $errors               = [];
124
        foreach ($defaultValuesToTypes as $type => $defaultValues) {
125
            foreach ($defaultValues as $key => $defaultValue) {
126
                try {
127
                    $this->buildAndCheck(
128
                        self::TEST_FIELD_NAMESPACE.'\\normalisedDefault'.$type.$key,
129
                        $type,
130
                        $defaultValue
131
                    );
132
                } catch (\Throwable $e) {
133
                    $errors[] = [
134
                        'type'    => $type,
135
                        'default' => $defaultValue,
136
                        'error'   => $e->getMessage(),
137
                    ];
138
                }
139
            }
140
        }
141
        $this->assertSame([], $errors, print_r($errors, true));
142
    }
143
144
    /**
145
     * Build and then test a field
146
     *
147
     * @param string $name
148
     * @param string $type
149
     *
150
     * @param mixed  $default
151
     * @param bool   $isUnique
152
     *
153
     * @return string
154
     * @throws \EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException
155
     * @SuppressWarnings(PHPMD.StaticAccess)
156
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
157
     */
158
    protected function buildAndCheck(
159
        string $name,
160
        string $type,
161
        $default = null,
162
        bool $isUnique = false
163
    ): string {
164
        $fieldTraitFqn = $this->fieldGenerator->generateField(
165
            $name,
166
            $type,
167
            null,
168
            $default,
169
            $isUnique
170
        );
171
172
        $this->qaGeneratedCode();
173
        $basePath        = self::WORK_DIR.'src/Entity/Fields/';
174
        $namespaceHelper = new NamespaceHelper();
175
        $basename        = $namespaceHelper->basename($name);
176
        $basename        = \str_replace(FieldGenerator::FIELD_TRAIT_SUFFIX, '', $basename);
177
178
        $interfacePath = $basePath.'Interfaces/'.Inflector::classify($basename).'FieldInterface.php';
179
        $this->assertNoMissedReplacements($interfacePath);
180
181
        $traitPath = $basePath.'Traits/'.Inflector::classify($basename).'FieldTrait.php';
182
        $this->assertNoMissedReplacements($traitPath);
183
184
        $interfaceContents = file_get_contents($interfacePath);
185
        $traitContents     = file_get_contents($traitPath);
186
187
        if (!\in_array($type, [MappingHelper::TYPE_TEXT, MappingHelper::TYPE_STRING], true)) {
188
            $this->assertNotContains(': string', $interfaceContents);
189
            $this->assertNotContains('(string', $interfaceContents);
190
            $this->assertNotContains(': string', $traitContents);
191
            $this->assertNotContains('(string', $traitContents);
192
            $phpType = MappingHelper::COMMON_TYPES_TO_PHP_TYPES[$type];
193
            if (null === $default) {
194
                $phpType = "?$phpType";
195
            }
196
            $this->assertContains(': '.$phpType, $interfaceContents);
197
            $this->assertContains('('.$phpType, $interfaceContents);
198
            $this->assertContains(': '.$phpType, $traitContents);
199
            $this->assertContains('('.$phpType, $traitContents);
200
        }
201
202
        if ($type === MappingHelper::TYPE_BOOLEAN) {
203
            $this->assertNotContains('public function get', $interfaceContents);
204
            $this->assertNotContains('public function isIs', $interfaceContents, '', true);
205
            $this->assertNotContains('public function get', $traitContents);
206
            $this->assertNotContains('public function isIs', $traitContents, '', true);
207
        }
208
209
        return $fieldTraitFqn;
210
    }
211
212
    public function testBuildFieldsAndSetToEntity()
213
    {
214
        foreach (self::CAR_FIELDS_TO_TYPES as $args) {
215
            $fieldFqn = $this->buildAndCheck($args[0], $args[1], null);
216
            $this->fieldGenerator->setEntityHasField(self::TEST_ENTITY_CAR, $fieldFqn);
217
        }
218
        $this->qaGeneratedCode();
219
    }
220
221
    public function testBuildFieldsWithSuffixAndSetToEntity()
222
    {
223
        foreach (self::CAR_FIELDS_TO_TYPES as $args) {
224
            $fieldFqn = $this->buildAndCheck($args[0].FieldGenerator::FIELD_TRAIT_SUFFIX, $args[1], null);
225
            $this->fieldGenerator->setEntityHasField(self::TEST_ENTITY_CAR, $fieldFqn);
226
        }
227
        $this->qaGeneratedCode();
228
    }
229
230
    public function testBuildNullableFieldsAndSetToEntity()
231
    {
232
        foreach (self::CAR_FIELDS_TO_TYPES as $args) {
233
            $fieldFqn = $this->buildAndCheck($args[0], $args[1], null);
234
            $this->fieldGenerator->setEntityHasField(self::TEST_ENTITY_CAR, $fieldFqn);
235
        }
236
        $this->qaGeneratedCode();
237
    }
238
239
    public function testBuildUniqueFieldsAndSetToEntity()
240
    {
241
        foreach (self::UNIQUE_FIELDS_TO_TYPES as $args) {
242
            $fieldFqn = $this->buildAndCheck($args[0], $args[1], null, true);
243
            $this->fieldGenerator->setEntityHasField(self::TEST_ENTITY_CAR, $fieldFqn);
244
        }
245
        $this->qaGeneratedCode();
246
    }
247
}
248