Passed
Branch develop (e85239)
by Ilya
06:00 queued 03:25
created

HandlerMakeCommandTest::setUp()   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 Illuminate\Routing\Controller;
6
use Illuminate\Support\Facades\Artisan;
7
use SplFileInfo;
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
117
        $this->assertDirectoryNotExists($this->app->path('Handlers'));
118
119
        $this->assertEquals(Artisan::output(), '[InvalidNamespace%] is not a valid namespace.' . PHP_EOL);
120
    }
121
122
    public function test_resource_option()
123
    {
124
        $handlersPath = $this->app->path('Http/Handlers');
125
126
        $this->artisan('make:handler', [
127
            'name' => 'Profile',
128
            '--resource' => true,
129
        ]);
130
131
        $this->assertDirectoryExists($handlersPath);
132
133
        $actions = ['index', 'show', 'edit', 'update', 'create', 'store', 'destroy'];
134
135
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $actions);
136
        $actualFiles = $this->getFileNamesByPath($handlersPath);
137
138
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
139
    }
140
141
    public function test_resource_option_with_api_option()
142
    {
143
        $handlersPath = $this->app->path('Http/Handlers');
144
145
        $this->artisan('make:handler', [
146
            'name' => 'Profile',
147
            '--resource' => true,
148
            '--api' => true,
149
        ]);
150
151
        $this->assertDirectoryExists($handlersPath);
152
153
        $actions = ['index', 'show', 'update', 'store', 'destroy'];
154
155
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $actions);
156
        $actualFiles = $this->getFileNamesByPath($handlersPath);
157
158
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
159
    }
160
161
    public function test_actions_option()
162
    {
163
        $handlersPath = $this->app->path('Http/Handlers');
164
165
        $actions = ['drop', 'move', 'store'];
166
167
        $this->artisan('make:handler', [
168
            'name' => 'Profile',
169
            '--actions' => implode(',', $actions),
170
        ]);
171
172
        $this->assertDirectoryExists($handlersPath);
173
174
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $actions);
175
        $actualFiles = $this->getFileNamesByPath($handlersPath);
176
177
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
178
    }
179
180
    public function test_invalid_actions_option()
181
    {
182
        $this->artisan('make:handler', [
183
            'name' => 'Profile',
184
            '--actions' => 'show,destroy%',
185
        ]);
186
187
        $this->assertDirectoryNotExists($this->app->path('Handlers'));
188
189
        $this->assertEquals(Artisan::output(), '[destroy%] is not a valid action name.' . PHP_EOL);
190
    }
191
192
    public function test_except_option()
193
    {
194
        $handlersPath = $this->app->path('Http/Handlers');
195
196
        $exceptActions = ['index', 'show', 'destroy'];
197
198
        $this->artisan('make:handler', [
199
            'name' => 'Profile',
200
            '--resource' => true,
201
            '--except' => implode(',', $exceptActions),
202
        ]);
203
204
        $this->assertDirectoryExists($handlersPath);
205
206
        $expectedActions = ['edit', 'update', 'create', 'store'];
207
208
        $expectedFiles = $this->getHandlerFileNamesByNameAndActions('Profile', $expectedActions);
209
        $actualFiles = $this->getFileNamesByPath($handlersPath);
210
211
        $this->assertEquals(array_sort_recursive($expectedFiles), array_sort_recursive($actualFiles));
212
    }
213
214
    public function test_invalid_except_option()
215
    {
216
        $this->artisan('make:handler', [
217
            'name' => 'Profile',
218
            '--resource' => true,
219
            '--except' => 'show,destroy%',
220
        ]);
221
222
        $this->assertDirectoryNotExists($this->app->path('Handlers'));
223
224
        $this->assertEquals(Artisan::output(), '[destroy%] is not a valid action name.' . PHP_EOL);
225
    }
226
227
    public function test_proper_file_content_generation()
228
    {
229
        $this->artisan('make:handler', [
230
            'name' => 'ShowProfile',
231
        ]);
232
233
        $generatedContent = file_get_contents($this->app->path('Http/Handlers/ShowProfile.php'));
234
        $expectedContent = file_get_contents(__DIR__.'/Stubs/ShowProfile.stub');
235
236
        $this->assertEquals($generatedContent, $expectedContent);
237
    }
238
239
    public function test_proper_file_content_generation_with_custom_base_handler()
240
    {
241
        config([
242
            'handlers.base' => Controller::class
243
        ]);
244
245
        $this->artisan('make:handler', [
246
            'name' => 'ShowProfile',
247
        ]);
248
249
        $generatedContent = file_get_contents($this->app->path('Http/Handlers/ShowProfile.php'));
250
        $expectedContent = file_get_contents(__DIR__.'/Stubs/ShowProfileWithCustomBaseHandler.stub');
251
252
        $this->assertEquals($generatedContent, $expectedContent);
253
    }
254
255
    public function test_non_existent_base_class()
256
    {
257
        config([
258
            'handlers.base' => 'Foo\\Bar\\Invalid\\ClassName',
259
        ]);
260
261
        $this->artisan('make:handler', [
262
            'name' => 'ShowProfile',
263
        ]);
264
265
        $this->assertEquals(Artisan::output(), 'The [Foo\\Bar\\Invalid\\ClassName] class specified as the base handler doesn\'t exist.' . PHP_EOL);
266
    }
267
268
    /**
269
     * Get handler file names by name and actions.
270
     *
271
     * @param string $name
272
     * @param array $actions
273
     * @return array
274
     */
275
    protected function getHandlerFileNamesByNameAndActions(string $name, array $actions): array
276
    {
277
        return array_map(function (string $action) use ($name) {
278
            return studly_case($action).studly_case($name).'.php';
279
        }, $actions);
280
    }
281
282
    /**
283
     * Get file names by path.
284
     *
285
     * @param string $path
286
     * @return array
287
     */
288
    protected function getFileNamesByPath(string $path): array
289
    {
290
        return array_map(function (SplFileInfo $file) {
291
            return $file->getFilename();
292
        }, $this->files->files($path));
293
    }
294
}
295