Passed
Push — master ( 51e9c7...7f3bc9 )
by Ilya
07:38 queued 04:52
created

HandlerMakeCommandTest::test_name_not_specified()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hivokas\LaravelHandlers\Tests;
4
5
use SplFileInfo;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\Artisan;
8
use Symfony\Component\Console\Exception\RuntimeException;
9
10
class HandlerMakeCommandTest extends AbstractTestCase
11
{
12
    protected function setUp()
13
    {
14
        parent::setUp();
15
16
        $this->files->cleanDirectory($this->app->path());
17
    }
18
19
    public function test_name_specified()
20
    {
21
        $filePath = $this->app->path('Http/Handlers/ShowProfile.php');
22
23
        $this->assertFileNotExists($filePath);
24
25
        $this->artisan('make:handler', [
26
            'name' => 'ShowProfile',
27
        ]);
28
29
        $this->assertFileExists($filePath);
30
    }
31
32
    public function test_name_not_specified()
33
    {
34
        $this->expectException(RuntimeException::class);
35
36
        $this->artisan('make:handler');
37
    }
38
39
    public function test_invalid_name_specified()
40
    {
41
        $this->artisan('make:handler', [
42
            'name' => 'ShowProfile%',
43
        ]);
44
45
        $this->assertDirectoryNotExists($this->app->path('Handlers'));
46
47
        $this->assertEquals(Artisan::output(), 'Name can\'t contain any non-word characters.'.PHP_EOL);
48
    }
49
50
    public function test_create_existent_handler_without_force_option()
51
    {
52
        $initialHandlerContent = str_random();
53
        $filePath = $this->app->path('Http/Handlers/ShowProfile.php');
54
55
        $this->forceFilePutContents($filePath, $initialHandlerContent);
56
57
        $this->assertEquals($initialHandlerContent, file_get_contents($filePath));
58
59
        $this->artisan('make:handler', [
60
            'name' => 'ShowProfile',
61
        ]);
62
63
        $this->assertEquals($initialHandlerContent, file_get_contents($filePath));
64
65
        $this->assertEquals(Artisan::output(), 'ShowProfile handler already exists!'.PHP_EOL);
66
    }
67
68
    public function test_create_existent_handler_with_force_option()
69
    {
70
        $initialHandlerContent = str_random();
71
        $filePath = $this->app->path('Http/Handlers/ShowProfile.php');
72
73
        $this->forceFilePutContents($filePath, $initialHandlerContent);
74
75
        $this->assertEquals($initialHandlerContent, file_get_contents($filePath));
76
77
        $this->artisan('make:handler', [
78
            'name' => 'ShowProfile',
79
            '--force' => true,
80
        ]);
81
82
        $this->assertNotEquals($initialHandlerContent, file_get_contents($filePath));
83
    }
84
85
    public function test_namespace_option_with_relative_path()
86
    {
87
        $filePath = $this->app->path('Http/Handlers/Profile/ShowProfile.php');
88
89
        $this->artisan('make:handler', [
90
            'name' => 'ShowProfile',
91
            '--namespace' => 'Profile',
92
        ]);
93
94
        $this->assertFileExists($filePath);
95
    }
96
97
    public function test_namespace_option_with_absolute_path()
98
    {
99
        $filePath = $this->app->path('Custom/ShowProfile.php');
100
101
        $this->artisan('make:handler', [
102
            'name' => 'ShowProfile',
103
            '--namespace' => '\\Custom',
104
        ]);
105
106
        $this->assertFileExists($filePath);
107
    }
108
109
    public function test_invalid_namespace_option()
110
    {
111
        $this->artisan('make:handler', [
112
            'name' => 'ShowProfile',
113
            '--namespace' => 'InvalidNamespace%',
114
        ]);
115
116
        $this->assertDirectoryNotExists($this->app->path('Handlers'));
117
118
        $this->assertEquals(Artisan::output(), '[InvalidNamespace%] is not a valid namespace.'.PHP_EOL);
119
    }
120
121
    public function test_resource_option()
122
    {
123
        $handlersPath = $this->app->path('Http/Handlers');
124
125
        $this->artisan('make:handler', [
126
            'name' => 'Profile',
127
            '--resource' => true,
128
        ]);
129
130
        $this->assertDirectoryExists($handlersPath);
131
132
        $actions = ['index', 'show', 'edit', 'update', 'create', 'store', 'destroy'];
133
134
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $actions);
135
        $actualFiles = $this->getFileNamesByPath($handlersPath);
136
137
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
138
    }
139
140
    public function test_resource_option_with_api_option()
141
    {
142
        $handlersPath = $this->app->path('Http/Handlers');
143
144
        $this->artisan('make:handler', [
145
            'name' => 'Profile',
146
            '--resource' => true,
147
            '--api' => true,
148
        ]);
149
150
        $this->assertDirectoryExists($handlersPath);
151
152
        $actions = ['index', 'show', 'update', 'store', 'destroy'];
153
154
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $actions);
155
        $actualFiles = $this->getFileNamesByPath($handlersPath);
156
157
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
158
    }
159
160
    public function test_actions_option()
161
    {
162
        $handlersPath = $this->app->path('Http/Handlers');
163
164
        $actions = ['drop', 'move', 'store'];
165
166
        $this->artisan('make:handler', [
167
            'name' => 'Profile',
168
            '--actions' => implode(',', $actions),
169
        ]);
170
171
        $this->assertDirectoryExists($handlersPath);
172
173
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $actions);
174
        $actualFiles = $this->getFileNamesByPath($handlersPath);
175
176
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
177
    }
178
179
    public function test_invalid_actions_option()
180
    {
181
        $this->artisan('make:handler', [
182
            'name' => 'Profile',
183
            '--actions' => 'show,destroy%',
184
        ]);
185
186
        $this->assertDirectoryNotExists($this->app->path('Handlers'));
187
188
        $this->assertEquals(Artisan::output(), '[destroy%] is not a valid action name.'.PHP_EOL);
189
    }
190
191
    public function test_except_option()
192
    {
193
        $handlersPath = $this->app->path('Http/Handlers');
194
195
        $exceptActions = ['index', 'show', 'destroy'];
196
197
        $this->artisan('make:handler', [
198
            'name' => 'Profile',
199
            '--resource' => true,
200
            '--except' => implode(',', $exceptActions),
201
        ]);
202
203
        $this->assertDirectoryExists($handlersPath);
204
205
        $expectedActions = ['edit', 'update', 'create', 'store'];
206
207
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $expectedActions);
208
        $actualFiles = $this->getFileNamesByPath($handlersPath);
209
210
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
211
    }
212
213
    public function test_invalid_except_option()
214
    {
215
        $this->artisan('make:handler', [
216
            'name' => 'Profile',
217
            '--resource' => true,
218
            '--except' => 'show,destroy%',
219
        ]);
220
221
        $this->assertDirectoryNotExists($this->app->path('Handlers'));
222
223
        $this->assertEquals(Artisan::output(), '[destroy%] is not a valid action name.'.PHP_EOL);
224
    }
225
226
    public function test_proper_file_content_generation()
227
    {
228
        $this->artisan('make:handler', [
229
            'name' => 'ShowProfile',
230
        ]);
231
232
        $generatedContent = file_get_contents($this->app->path('Http/Handlers/ShowProfile.php'));
233
        $expectedContent = file_get_contents(__DIR__.'/Stubs/ShowProfile.stub');
234
235
        $this->assertEquals($generatedContent, $expectedContent);
236
    }
237
238
    public function test_proper_file_content_generation_with_custom_base_handler()
239
    {
240
        config([
241
            'handlers.base' => Controller::class,
242
        ]);
243
244
        $this->artisan('make:handler', [
245
            'name' => 'ShowProfile',
246
        ]);
247
248
        $generatedContent = file_get_contents($this->app->path('Http/Handlers/ShowProfile.php'));
249
        $expectedContent = file_get_contents(__DIR__.'/Stubs/ShowProfileWithCustomBaseHandler.stub');
250
251
        $this->assertEquals($generatedContent, $expectedContent);
252
    }
253
254
    public function test_non_existent_base_class()
255
    {
256
        config([
257
            'handlers.base' => 'Foo\\Bar\\Invalid\\ClassName',
258
        ]);
259
260
        $this->artisan('make:handler', [
261
            'name' => 'ShowProfile',
262
        ]);
263
264
        $this->assertEquals(Artisan::output(), 'The [Foo\\Bar\\Invalid\\ClassName] class specified as the base handler doesn\'t exist.'.PHP_EOL);
265
    }
266
267
    /**
268
     * Get handler file names by name and actions.
269
     *
270
     * @param string $name
271
     * @param array $actions
272
     * @return array
273
     */
274
    protected function getHandlerFileNamesByNameAndActions(string $name, array $actions): array
275
    {
276
        return array_map(function (string $action) use ($name) {
277
            return studly_case($action).studly_case($name).'.php';
278
        }, $actions);
279
    }
280
281
    /**
282
     * Get file names by path.
283
     *
284
     * @param string $path
285
     * @return array
286
     */
287
    protected function getFileNamesByPath(string $path): array
288
    {
289
        return array_map(function (SplFileInfo $file) {
290
            return $file->getFilename();
291
        }, $this->files->files($path));
292
    }
293
}
294