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 (#51)
by joseph
102:38 queued 99:24
created

FieldGeneratorTest::buildAndCheck()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 51
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
c 0
b 0
f 0
rs 8.8981
cc 4
eloc 34
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\AbstractTest;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper;
9
10
class FieldGeneratorTest extends AbstractTest
11
{
12
    public const WORK_DIR = AbstractTest::VAR_PATH.'/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
177
        $interfacePath = $basePath.'Interfaces/'.Inflector::classify($basename).'FieldInterface.php';
178
        $this->assertNoMissedReplacements($interfacePath);
179
180
        $traitPath = $basePath.'Traits/'.Inflector::classify($basename).'FieldTrait.php';
181
        $this->assertNoMissedReplacements($traitPath);
182
183
        $interfaceContents = file_get_contents($interfacePath);
184
        $traitContents     = file_get_contents($traitPath);
185
186
        if (!\in_array($type, [MappingHelper::TYPE_TEXT, MappingHelper::TYPE_STRING], true)) {
187
            $this->assertNotContains(': string', $interfaceContents);
188
            $this->assertNotContains('(string', $interfaceContents);
189
            $this->assertNotContains(': string', $traitContents);
190
            $this->assertNotContains('(string', $traitContents);
191
            $phpType = MappingHelper::COMMON_TYPES_TO_PHP_TYPES[$type];
192
            if (null === $default) {
193
                $phpType = "?$phpType";
194
            }
195
            $this->assertContains(': '.$phpType, $interfaceContents);
196
            $this->assertContains('('.$phpType, $interfaceContents);
197
            $this->assertContains(': '.$phpType, $traitContents);
198
            $this->assertContains('('.$phpType, $traitContents);
199
        }
200
201
        if ($type === MappingHelper::TYPE_BOOLEAN) {
202
            $this->assertNotContains('public function get', $interfaceContents);
203
            $this->assertNotContains('public function isIs', $interfaceContents, '', true);
204
            $this->assertNotContains('public function get', $traitContents);
205
            $this->assertNotContains('public function isIs', $traitContents, '', true);
206
        }
207
208
        return $fieldTraitFqn;
209
    }
210
211
    public function testBuildFieldsAndSetToEntity()
212
    {
213
        foreach (self::CAR_FIELDS_TO_TYPES as $args) {
214
            $fieldFqn = $this->buildAndCheck($args[0], $args[1], null);
215
            $this->fieldGenerator->setEntityHasField(self::TEST_ENTITY_CAR, $fieldFqn);
216
        }
217
        $this->qaGeneratedCode();
218
    }
219
220
    public function testBuildNullableFieldsAndSetToEntity()
221
    {
222
        foreach (self::CAR_FIELDS_TO_TYPES as $args) {
223
            $fieldFqn = $this->buildAndCheck($args[0], $args[1], null);
224
            $this->fieldGenerator->setEntityHasField(self::TEST_ENTITY_CAR, $fieldFqn);
225
        }
226
        $this->qaGeneratedCode();
227
    }
228
229
    public function testBuildUniqueFieldsAndSetToEntity()
230
    {
231
        foreach (self::UNIQUE_FIELDS_TO_TYPES as $args) {
232
            $fieldFqn = $this->buildAndCheck($args[0], $args[1], null, true);
233
            $this->fieldGenerator->setEntityHasField(self::TEST_ENTITY_CAR, $fieldFqn);
234
        }
235
        $this->qaGeneratedCode();
236
    }
237
}
238