• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

orchestral / canvas / 9563332959

18 Jun 2024 10:23AM UTC coverage: 94.815%. Remained the same
9563332959

push

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

640 of 675 relevant lines covered (94.81%)

24.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/src/Console/ViewMakeCommand.php
1
<?php
2

3
namespace Orchestra\Canvas\Console;
4

5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Str;
7
use Orchestra\Canvas\Core\Concerns\CodeGenerator;
8
use Orchestra\Canvas\Core\Concerns\TestGenerator;
9
use Orchestra\Canvas\Core\Concerns\UsesGeneratorOverrides;
10
use Orchestra\Canvas\GeneratorPreset;
11
use Symfony\Component\Console\Attribute\AsCommand;
12

13
use function Illuminate\Filesystem\join_paths;
14

15
#[AsCommand(name: 'make:view', description: 'Create a new view')]
16
class ViewMakeCommand extends \Illuminate\Foundation\Console\ViewMakeCommand
17
{
18
    use CodeGenerator;
19
    use TestGenerator {
20
        handleTestCreationUsingCanvas as protected handleTestCreationUsingCanvasFromTrait;
21
    }
22
    use UsesGeneratorOverrides;
23

24
    /**
25
     * Create a new controller creator command instance.
26
     *
27
     * @return void
28
     */
29
    public function __construct(Filesystem $files)
30
    {
31
        parent::__construct($files);
2✔
32

33
        $this->addGeneratorPresetOptions();
2✔
34
    }
35

36
    /**
37
     * Execute the console command.
38
     *
39
     * @return bool|null
40
     *
41
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
42
     */
43
    #[\Override]
44
    public function handle()
45
    {
46
        /** @phpstan-ignore return.type */
47
        return $this->generateCode() ? self::SUCCESS : self::FAILURE;
2✔
48
    }
49

50
    /**
51
     * Resolve the fully-qualified path to the stub.
52
     *
53
     * @param  string  $stub
54
     * @return string
55
     */
56
    protected function resolveStubPath($stub)
57
    {
58
        $preset = $this->generatorPreset();
2✔
59

60
        if (! $preset instanceof GeneratorPreset) {
2✔
61
            return parent::resolveStubPath($stub);
×
62
        }
63

64
        return $preset->hasCustomStubPath() && file_exists($customPath = join_paths($preset->basePath(), $stub))
2✔
65
            ? $customPath
×
66
            : $this->resolveDefaultStubPath($stub);
2✔
67
    }
68

69
    /**
70
     * Resolve the default fully-qualified path to the stub.
71
     *
72
     * @param  string  $stub
73
     * @return string
74
     */
75
    protected function resolveDefaultStubPath($stub)
76
    {
77
        return join_paths(__DIR__, $stub);
2✔
78
    }
79

80
    /**
81
     * Get the destination view path.
82
     *
83
     * @param  string  $name
84
     * @return string
85
     */
86
    #[\Override]
87
    protected function getPath($name)
88
    {
89
        /** @var string $extension */
90
        /** @phpstan-ignore argument.type */
91
        $extension = transform($this->option('extension'), fn (string $extension) => trim($extension));
2✔
92

93
        return $this->viewPath(
2✔
94
            $this->getNameInput().'.'.$extension,
2✔
95
        );
2✔
96
    }
97

98
    /**
99
     * Get the first view directory path from the application configuration.
100
     *
101
     * @param  string  $path
102
     * @return string
103
     */
104
    #[\Override]
105
    protected function viewPath($path = '')
106
    {
107
        return $this->viewPathUsingCanvas($path);
2✔
108
    }
109

110
    /**
111
     * Get the desired view name from the input.
112
     *
113
     * @return string
114
     */
115
    #[\Override]
116
    protected function getNameInput()
117
    {
118
        /** @phpstan-ignore argument.type */
119
        return transform($this->argument('name'), function (string $name) {
2✔
120
            return str_replace(['\\', '.'], '/', trim($name));
2✔
121
        });
2✔
122
    }
123

124
    /**
125
     * Create the matching test case if requested.
126
     *
127
     * @param  string  $path
128
     */
129
    protected function handleTestCreationUsingCanvas($path): bool
130
    {
131
        if (! $this->option('test') && ! $this->option('pest')) {
2✔
132
            return false;
1✔
133
        }
134

135
        $preset = $this->generatorPreset();
1✔
136

137
        if (! $preset instanceof GeneratorPreset) {
1✔
138
            return $this->handleTestCreationUsingCanvasFromTrait($path);
×
139
        }
140

141
        $namespaceTestCase = $testCase = $preset->canvas()->config(
1✔
142
            'testing.extends.feature',
1✔
143
            $preset->canvas()->is('laravel') ? 'Tests\TestCase' : 'Orchestra\Testbench\TestCase'
1✔
144
        );
1✔
145

146
        $stub = $this->files->get($this->getTestStub());
1✔
147

148
        if (Str::startsWith($testCase, '\\')) {
1✔
149
            $stub = str_replace('NamespacedDummyTestCase', trim($testCase, '\\'), $stub);
×
150
        } else {
151
            $stub = str_replace('NamespacedDummyTestCase', $namespaceTestCase, $stub);
1✔
152
        }
153

154
        $contents = str_replace(
1✔
155
            ['{{ namespace }}', '{{ class }}', '{{ name }}', 'DummyTestCase'],
1✔
156
            [$this->testNamespace(), $this->testClassName(), $this->testViewName(), class_basename(trim($testCase, '\\'))],
1✔
157
            $stub,
1✔
158
        );
1✔
159

160
        $this->files->ensureDirectoryExists(\dirname($this->getTestPath()), 0755, true);
1✔
161

162
        return $this->files->put($this->getTestPath(), $contents) !== false;
1✔
163
    }
164

165
    /**
166
     * Get the root namespace for the class.
167
     *
168
     * @return string
169
     */
170
    #[\Override]
171
    protected function rootNamespace()
172
    {
173
        return $this->rootNamespaceUsingCanvas();
2✔
174
    }
175

176
    /**
177
     * Get the destination test case path.
178
     *
179
     * @return string
180
     */
181
    #[\Override]
182
    protected function getTestPath()
183
    {
184
        $preset = $this->generatorPreset();
1✔
185

186
        $testPath = Str::of($this->testClassFullyQualifiedName())
1✔
187
            ->replace('\\', DIRECTORY_SEPARATOR)
1✔
188
            ->replaceFirst(join_paths('Tests', 'Feature'), join_paths(str_replace($preset->basePath(), '', $preset->testingPath()), 'Feature'))
1✔
189
            ->append('Test.php')
1✔
190
            ->value();
1✔
191

192
        return $preset->basePath().$testPath;
1✔
193
    }
194

195
    /**
196
     * Get the test stub file for the generator.
197
     *
198
     * @return string
199
     */
200
    #[\Override]
201
    protected function getTestStub()
202
    {
203
        $stubName = 'view.'.($this->option('pest') ? 'pest' : 'test').'.stub';
1✔
204

205
        return $this->resolveStubPath(join_paths('stubs', $stubName));
1✔
206
    }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc