Passed
Pull Request — main (#11)
by Angel
02:48
created

GenerateFilesTest::defaultStubsConfigProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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