TestMakeCommand::getDefaultNamespace()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 4
nc 2
nop 0
1
<?php
2
3
namespace Rawilk\LaravelModules\Commands\Generators;
4
5
use Rawilk\LaravelModules\Support\Config\GenerateConfigReader;
6
use Rawilk\LaravelModules\Support\Stub;
7
use Rawilk\LaravelModules\Traits\ModuleCommands;
8
9
class TestMakeCommand extends GeneratorCommand
10
{
11
    use ModuleCommands;
12
13
    /** @var string */
14
    protected $argumentName = 'name';
15
16
    /** @var string */
17
    protected $signature = 'module:make-test
18
                            {name : The name of the test}
19
                            {module? : The name of the module to create the test for}
20
                            {--feature : Indicates the test is a feature test}';
21
22
    /** @var string */
23
    protected $description = 'Create a new test class for the specified module.';
24
25
    protected function getDefaultNamespace(): string
26
    {
27
        /** @var \Rawilk\LaravelModules\Contracts\Repository $module */
28
        $module = $this->laravel['modules'];
29
30
        if ($this->option('feature')) {
31
            return $module->config('paths.generator.test-feature.namespace') ?: $module->config('paths.generator.test-feature.path', 'tests/Feature');
32
        }
33
34
        return $module->config('paths.generator.test.namespace') ?: $module->config('paths.generator.test.path', 'tests/Unit');
35
    }
36
37
    protected function getDestinationFilePath(): string
38
    {
39
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
40
41
        if ($this->option('feature')) {
42
            $testPath = GenerateConfigReader::read('test-feature');
43
        } else {
44
            $testPath = GenerateConfigReader::read('test');
45
        }
46
47
        return $path . $testPath->getPath() . '/' . $this->getFileName() . '.php';
48
    }
49
50
    protected function getTemplateContents(): string
51
    {
52
        /** @var \Rawilk\LaravelModules\Module $module */
53
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
54
55
        return (new Stub($this->getStubName(), [
56
            'NAMESPACE' => $this->getClassNamespace($module),
57
            'CLASS'     => $this->getClass(),
58
        ]))->render();
59
    }
60
61
    private function getStubName(): string
62
    {
63
        if ($this->option('feature')) {
64
            return '/feature-test.stub';
65
        }
66
67
        return '/unit-test.stub';
68
    }
69
}
70