Completed
Push — main ( 44d9a4...6d0af8 )
by Mohammad
16s queued 13s
created

GenerateFilesTest::testDefaultStubsConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Shamaseen\Repository\Tests\Feature;
4
5
use Illuminate\Support\Facades\Config;
6
use Shamaseen\Repository\Commands\Generator;
7
use Shamaseen\Repository\PathResolver;
8
use Shamaseen\Repository\Tests\TestCase;
9
10
class GenerateFilesTest extends TestCase
11
{
12
    /**
13
     * @var mixed
14
     */
15
    private PathResolver $pathResolver;
16
17
    private array $filesToGenerate = ['Controller', 'Repository', 'Model', 'Request', 'Resource', 'Collection', 'Policy', 'Test'];
18
19
    /**
20
     * @param string $dataName
21
     */
22
    public function __construct(?string $name = null, array $data = [], $dataName = '')
23
    {
24
        parent::__construct($name, $data, $dataName);
25
    }
26
27
    public function setUp(): void
28
    {
29
        parent::setUp();
30
        $this->pathResolver = new PathResolver($this->modelName, $this->userPath, config('repository.base_path'));
31
    }
32
33
    public function testGenerate()
34
    {
35
        $this->artisan("generate:repository $this->userPath/$this->modelName -f");
36
37
        foreach ($this->filesToGenerate as $type) {
38
            $absolutePath = $this->pathResolver->absolutePath($type);
39
40
            $this->assertFileExists($absolutePath);
41
        }
42
    }
43
44
    public function testGenerateMCROnly()
45
    {
46
        $this->artisan("generate:repository $this->userPath/$this->modelName -f -mrc");
47
48
        $filesToGenerate = ['Controller', 'Repository', 'Model'];
49
        foreach ($filesToGenerate as $type) {
50
            $absolutePath = $this->pathResolver->absolutePath($type);
51
52
            $this->assertFileExists($absolutePath);
53
        }
54
    }
55
56
    public function defaultStubsConfigProvider(): array
57
    {
58
        return [
59
            // run 1
60
            [
61
                [
62
                    Generator::RESOURCE_OPTION
63
                ],
64
                [
65
                    'Resource',
66
                ]
67
            ],
68
            // run 2
69
            [
70
                [
71
                    Generator::MODEL_OPTION,
72
                    Generator::CONTROLLER_OPTION,
73
                ],
74
                [
75
                    'Model',
76
                    'Controller',
77
                ],
78
            ],
79
            // running Request option should only generate Request
80
            [
81
                [
82
                    Generator::REQUEST_OPTION,
83
                ],
84
                [
85
                    'Request',
86
                ]
87
            ],
88
            // running Collection option should only generate Collection
89
            [
90
                [
91
                    Generator::COLLECTION_OPTION,
92
                ],
93
                [
94
                    'Collection',
95
                ]
96
            ],
97
        ];
98
    }
99
100
    /** @dataProvider defaultStubsConfigProvider */
101
    public function testDefaultStubsConfig(array $config, array $generatedNames)
102
    {
103
        Config::set('repository.default_generated_files', $config);
104
        $this->artisan("generate:repository $this->userPath/$this->modelName -f");
105
106
        foreach ($generatedNames as $generatedName) {
107
            $this->assertFileExists($this->pathResolver->absolutePath($generatedName));
108
        }
109
110
        $allGeneratedStubs = array_keys(PathResolver::$configTypePathMap);
111
        $filesNotGenerated = array_diff($allGeneratedStubs, $generatedNames);
112
113
        foreach ($filesNotGenerated as $fileNotGenerated) {
114
            $this->assertFileDoesNotExist($this->pathResolver->absolutePath($fileNotGenerated));
115
        }
116
    }
117
}
118