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

GenerateFilesTest::defaultStubsConfigProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 21
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
    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::RESOURCE_OPTION,
75
                    Generator::CONTROLLER_OPTION,
76
                ],
77
                [
78
                    'Controller',
79
                    'Resource',
80
                ],
81
            ],
82
        ];
83
    }
84
85
    /** @dataProvider defaultStubsConfigProvider */
86
    public function testDefaultStubsConfig(array $config, array $generatedNames)
87
    {
88
        Config::set('repository.default_stubs', $config);
89
        $this->artisan("generate:repository $this->userPath/$this->modelName -f");
90
91
        foreach ($generatedNames as $generatedName) {
92
            $this->assertFileExists($this->pathResolver->absolutePath($generatedName));
93
        }
94
95
        $allGeneratedStubs = array_keys(PathResolver::$configTypePathMap);
96
        $filesNotGenerated = array_diff($allGeneratedStubs, $generatedNames);
97
98
        foreach ($filesNotGenerated as $fileNotGenerated) {
99
            $this->assertFileDoesNotExist($this->pathResolver->absolutePath($fileNotGenerated));
100
        }
101
    }
102
}
103