GenerateFilesTest::testGenerate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 10
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
    private array $filesToGenerate = ['Controller', 'Repository', 'Model', 'Request', 'Resource', 'Collection', 'Policy', 'Test'];
13
14
    /**
15
     * @param string $dataName
16
     */
17
    public function __construct(?string $name = null, array $data = [], $dataName = '')
18
    {
19
        parent::__construct($name, $data, $dataName);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of PHPUnit\Framework\TestCase::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

19
        parent::__construct(/** @scrutinizer ignore-type */ $name, $data, $dataName);
Loading history...
Unused Code introduced by
The call to PHPUnit\Framework\TestCase::__construct() has too many arguments starting with $data. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        parent::/** @scrutinizer ignore-call */ 
20
                __construct($name, $data, $dataName);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
20
    }
21
22
    public function testGenerate()
23
    {
24
        $this->artisan("generate:repository $this->userPath/$this->modelName -f");
25
26
        foreach ($this->filesToGenerate as $type) {
27
            $absolutePath = $this->generator->absolutePath(
28
                $this->pathResolver->outputPath($type)
29
            );
30
31
            $this->assertFileExists($absolutePath);
32
        }
33
    }
34
35
    public function testGenerateMCROnly()
36
    {
37
        $this->artisan("generate:repository $this->userPath/$this->modelName -f -mrc");
38
39
        $filesToGenerate = ['Controller', 'Repository', 'Model'];
40
        foreach ($filesToGenerate as $type) {
41
            $absolutePath = $this->generator->absolutePath(
42
                $this->pathResolver->outputPath($type)
43
            );
44
45
            $this->assertFileExists($absolutePath);
46
        }
47
    }
48
49
    public static function defaultStubsConfigProvider(): array
50
    {
51
        return [
52
            // run 1
53
            [
54
                [
55
                    Generator::RESOURCE_OPTION
56
                ],
57
                [
58
                    'Resource',
59
                ]
60
            ],
61
            // run 2
62
            [
63
                [
64
                    Generator::MODEL_OPTION,
65
                    Generator::CONTROLLER_OPTION,
66
                ],
67
                [
68
                    'Model',
69
                    'Controller',
70
                ],
71
            ],
72
            // running Request option should only generate Request
73
            [
74
                [
75
                    Generator::REQUEST_OPTION,
76
                ],
77
                [
78
                    'Request',
79
                ]
80
            ],
81
            // running Collection option should only generate Collection
82
            [
83
                [
84
                    Generator::COLLECTION_OPTION,
85
                ],
86
                [
87
                    'Collection',
88
                ]
89
            ],
90
        ];
91
    }
92
93
    /** @dataProvider defaultStubsConfigProvider */
94
    public function testDefaultStubsConfig(array $config, array $generatedNames)
95
    {
96
        Config::set('repository.default_generated_files', $config);
97
        $this->artisan("generate:repository $this->userPath/$this->modelName -f");
98
99
        foreach ($generatedNames as $generatedName) {
100
            $this->assertFileExists($this->generator->absolutePath(
101
                $this->pathResolver->outputPath($generatedName)
102
            ));
103
        }
104
105
        $allGeneratedStubs = array_keys(PathResolver::$configTypePathMap);
106
        $filesNotGenerated = array_diff($allGeneratedStubs, $generatedNames);
107
108
        foreach ($filesNotGenerated as $fileNotGenerated) {
109
            $this->assertFileDoesNotExist($this->generator->absolutePath(
110
                $this->pathResolver->outputPath($fileNotGenerated)
111
            ));
112
        }
113
    }
114
}
115