Passed
Pull Request — master (#653)
by Aleksei
07:43
created

ConfigTest::testReverseDefinition()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 69
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 45
nc 4
nop 0
dl 0
loc 69
rs 9.2
c 0
b 0
f 0

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
2
3
/**
4
 * Spiral Framework. Scaffolder
5
 *
6
 * @license MIT
7
 * @author  Valentin V (vvval)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Scaffolder\Command;
13
14
use ReflectionClass;
15
use ReflectionException;
16
use Symfony\Component\Console\Input\StringInput;
17
use Throwable;
18
19
class ConfigTest extends AbstractCommandTest
20
{
21
    private const CLASS_NAME = '\\Spiral\\Tests\\Scaffolder\\App\\Config\\SampleConfig';
22
23
    public function tearDown(): void
24
    {
25
        $this->deleteDeclaration(self::CLASS_NAME);
26
    }
27
28
    /**
29
     * @throws ReflectionException
30
     * @throws Throwable
31
     */
32
    public function testScaffold(): void
33
    {
34
        $this->console()->run('create:config', [
35
            'name'      => 'sample',
36
            '--comment' => 'Sample Config'
37
        ]);
38
39
        clearstatcache();
40
        $this->assertTrue(class_exists(self::CLASS_NAME));
41
42
        $reflection = new ReflectionClass(self::CLASS_NAME);
43
44
        $this->assertStringContainsString('strict_types=1', $this->files()->read($reflection->getFileName()));
45
        $this->assertStringContainsString('Sample Config', $reflection->getDocComment());
46
47
        $this->assertTrue($reflection->hasConstant('CONFIG'));
48
        $this->assertTrue($reflection->hasProperty('config'));
49
50
        $this->assertIsString($reflection->getReflectionConstant('CONFIG')->getValue());
51
        $this->assertEquals([], $reflection->getDefaultProperties()['config']);
52
    }
53
54
    /**
55
     * @throws Throwable
56
     */
57
    public function testReverse(): void
58
    {
59
        $className = '\\Spiral\\Tests\\Scaffolder\\App\\Config\\ReversedConfig';
60
        $this->console()->run(null, new StringInput('create:config reversed -r'));
61
62
        clearstatcache();
63
        $this->assertTrue(class_exists($className));
64
    }
65
66
    /**
67
     * @throws Throwable
68
     */
69
    public function testReverseDefinition(): void
70
    {
71
        $className = '\\Spiral\\Tests\\Scaffolder\\App\\Config\\ReversedConfig';
72
        $this->console()->run('create:config', [
73
            'name'      => 'reversed',
74
            '--comment' => 'Reversed Config',
75
            '--reverse' => true
76
        ]);
77
78
        clearstatcache();
79
        $this->assertTrue(class_exists($className));
80
81
        $reflection = new ReflectionClass($className);
82
83
        $this->assertTrue($reflection->hasConstant('CONFIG'));
84
        $this->assertTrue($reflection->hasProperty('config'));
85
86
        $this->assertIsString($reflection->getReflectionConstant('CONFIG')->getValue());
87
        $this->assertIsArray($reflection->getDefaultProperties()['config']);
88
        $this->assertNotEmpty($reflection->getDefaultProperties()['config']);
89
90
        $methods = [
91
            'getStrParam'   => ['hint' => 'string', 'annotation' => 'string'],
92
            'getIntParam'   => ['hint' => 'int', 'annotation' => 'int'],
93
            'getFloatParam' => ['hint' => 'float', 'annotation' => 'float'],
94
            'getBoolParam'  => ['hint' => 'bool', 'annotation' => 'bool'],
95
            'getNullParam'  => ['hint' => null, 'annotation' => 'null'],
96
97
            'getArrParam' => ['hint' => 'array', 'annotation' => 'array|string[]'],
98
99
            'getMapParam'   => ['hint' => 'array', 'annotation' => 'array|string[]'],
100
            'getMapParamBy' => ['hint' => 'string', 'annotation' => 'string'],
101
102
            'getMixedArrParam' => ['hint' => 'array', 'annotation' => 'array'],
103
            'getParams'        => ['hint' => 'array', 'annotation' => 'array|string[]'],
104
105
            'getParameters' => ['hint' => 'array', 'annotation' => 'array|array[]'],
106
            'getParameter'  => ['hint' => 'array', 'annotation' => 'array'],
107
108
            'getConflicts'  => ['hint' => 'array', 'annotation' => 'array|array[]'],
109
            'getConflict'   => ['hint' => 'string', 'annotation' => 'string'],
110
            'getConflictBy' => ['hint' => 'array', 'annotation' => 'array|int[]'],
111
112
            'getValues'  => ['hint' => 'array', 'annotation' => 'array|array[]'],
113
            'getValue'   => ['hint' => 'string', 'annotation' => 'string'],
114
            'getValueBy' => ['hint' => 'string', 'annotation' => 'string'],
115
        ];
116
117
        $reflectionMethods = [];
118
        foreach ($reflection->getMethods() as $method) {
119
            if ($method->getDeclaringClass()->name !== $reflection->name) {
120
                continue;
121
            }
122
123
            $reflectionMethods[$method->name] = $method;
124
            $this->assertArrayHasKey($method->name, $methods);
125
126
            if (!$method->hasReturnType()) {
127
                $this->assertNull($methods[$method->name]['hint']);
128
            } else {
129
                $this->assertEquals($methods[$method->name]['hint'], $method->getReturnType()->getName());
130
            }
131
132
            $this->assertStringContainsString($methods[$method->name]['annotation'], $method->getDocComment());
133
        }
134
135
        $this->assertCount(count($methods), $reflectionMethods);
136
137
        $this->deleteDeclaration($className);
138
    }
139
140
    /**
141
     * @throws Throwable
142
     */
143
    public function testReverseWeirdKeys(): void
144
    {
145
        $className = '\\Spiral\\Tests\\Scaffolder\\App\\Config\\WeirdConfig';
146
        $this->console()->run('create:config', [
147
            'name'      => 'weird',
148
            '--comment' => 'Weird Config',
149
            '--reverse' => true
150
        ]);
151
152
        clearstatcache();
153
        $this->assertTrue(class_exists($className));
154
155
        $reflection = new ReflectionClass($className);
156
157
        $this->assertTrue($reflection->hasConstant('CONFIG'));
158
        $this->assertTrue($reflection->hasProperty('config'));
159
160
        $this->assertIsString($reflection->getReflectionConstant('CONFIG')->getValue());
161
        $this->assertIsArray($reflection->getDefaultProperties()['config']);
162
        $this->assertNotEmpty($reflection->getDefaultProperties()['config']);
163
164
        $methods = [
165
            'getAthello',
166
            'getWithSpaces',
167
            'getAndOtherChars',
168
            'getWithUnderscoreAndDashes'
169
        ];
170
171
        $reflectionMethods = [];
172
        foreach ($reflection->getMethods() as $method) {
173
            if ($method->getDeclaringClass()->name !== $reflection->name) {
174
                continue;
175
            }
176
            $reflectionMethods[$method->name] = $method;
177
178
            $this->assertContains($method->name, $methods);
179
        }
180
181
        $this->assertCount(count($methods), $reflectionMethods);
182
183
        $this->deleteDeclaration($className);
184
    }
185
186
    /**
187
     * @throws Throwable
188
     */
189
    public function testConfigFile(): void
190
    {
191
        $filename = $this->createConfig('sample', 'Sample Config');
192
        $this->assertStringContainsString('strict_types=1', $this->files()->read($filename));
193
        $this->assertStringContainsString(
194
            '@see \\Spiral\\Tests\\Scaffolder\\App\\Config\\SampleConfig',
195
            $this->files()->read($filename)
196
        );
197
198
        $this->deleteConfigFile($filename);
199
    }
200
201
    /**
202
     * @throws Throwable
203
     */
204
    public function testConfigFileExists(): void
205
    {
206
        $filename = $this->createConfig('sample2', 'Sample2 Config');
207
        $this->files()->append($filename, '//sample comment');
208
209
        $source = $this->files()->read($filename);
210
        $this->assertStringContainsString('//sample comment', $source);
211
212
        $filename = $this->createConfig('sample2', 'Sample2 Config');
213
214
        $source = $this->files()->read($filename);
215
        $this->assertStringContainsString('//sample comment', $source);
216
217
        $this->deleteConfigFile($filename);
218
        $this->deleteDeclaration('\\Spiral\\Tests\\Scaffolder\\App\\Config\\Sample2Config');
219
    }
220
221
    /**
222
     * @param string $filename
223
     * @throws Throwable
224
     */
225
    private function deleteConfigFile(string $filename): void
226
    {
227
        $this->files()->delete($filename);
228
    }
229
230
    /**
231
     * @param string $name
232
     * @param string $comment
233
     * @return string
234
     * @throws Throwable
235
     */
236
    private function createConfig(string $name, string $comment): string
237
    {
238
        $this->console()->run('create:config', [
239
            'name'      => $name,
240
            '--comment' => $comment
241
        ]);
242
243
        clearstatcache();
244
245
        $filename = $this->app->directory('config') . "$name.php";
246
        $this->assertFileExists($filename);
247
248
        return $filename;
249
    }
250
}
251