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

orchestral / canvas / 6224137920

18 Sep 2023 02:42PM UTC coverage: 90.984% (+1.0%) from 89.945%
6224137920

push

github

crynobone
wip

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

603 of 603 new or added lines in 34 files covered. (100.0%)

555 of 610 relevant lines covered (90.98%)

24.54 hits per line

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

91.49
/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
#[AsCommand(name: 'make:view', description: 'Create a new view')]
14
class ViewMakeCommand extends \Illuminate\Foundation\Console\ViewMakeCommand
15
{
16
    use CodeGenerator;
17
    use TestGenerator {
18
        handleTestCreationUsingCanvas as protected handleTestCreationUsingCanvasFromTrait;
19
    }
20
    use UsesGeneratorOverrides;
21

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

31
        $this->addGeneratorPresetOptions();
2✔
32
    }
33

34
    /**
35
     * Execute the console command.
36
     *
37
     * @return bool|null
38
     *
39
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
40
     */
41
    public function handle()
42
    {
43
        return $this->generateCode() ? self::SUCCESS : self::FAILURE;
2✔
44
    }
45

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

56
        if (! $preset instanceof GeneratorPreset) {
2✔
57
            return parent::resolveStubPath($stub);
×
58
        }
59

60
        return $preset->hasCustomStubPath() && file_exists($customPath = implode('/', [$preset->basePath(), trim($stub, '/')]))
2✔
61
            ? $customPath
×
62
            : $this->resolveDefaultStubPath($stub);
2✔
63
    }
64

65
    /**
66
     * Resolve the default fully-qualified path to the stub.
67
     *
68
     * @param  string  $stub
69
     * @return string
70
     */
71
    protected function resolveDefaultStubPath($stub)
72
    {
73
        return __DIR__.$stub;
2✔
74
    }
75

76
    /**
77
     * Get the destination view path.
78
     *
79
     * @param  string  $name
80
     * @return string
81
     */
82
    protected function getPath($name)
83
    {
84
        /** @var string $extension */
85
        $extension = transform($this->option('extension'), fn (string $extension) => trim($extension));
2✔
86

87
        return $this->viewPath(
2✔
88
            $this->getNameInput().'.'.$extension,
2✔
89
        );
2✔
90
    }
91

92
    /**
93
     * Get the desired view name from the input.
94
     *
95
     * @return string
96
     */
97
    protected function getNameInput()
98
    {
99
        return transform($this->argument('name'), function (string $name) {
2✔
100
            return str_replace(['\\', '.'], '/', trim($name));
2✔
101
        });
2✔
102
    }
103

104
    /**
105
     * Create the matching test case if requested.
106
     *
107
     * @param  string  $path
108
     */
109
    protected function handleTestCreationUsingCanvas($path): bool
110
    {
111
        if (! $this->option('test') && ! $this->option('pest')) {
2✔
112
            return false;
1✔
113
        }
114

115
        $preset = $this->generatorPreset();
1✔
116

117
        if (! $preset instanceof GeneratorPreset) {
1✔
118
            return $this->handleTestCreationUsingCanvasFromTrait($path);
×
119
        }
120

121
        $namespaceTestCase = $testCase = $preset->canvas()->config(
1✔
122
            'testing.extends.feature',
1✔
123
            $preset->canvas()->is('laravel') ? 'Tests\TestCase' : 'Orchestra\Testbench\TestCase'
1✔
124
        );
1✔
125

126
        $stub = $this->files->get($this->getTestStub());
1✔
127

128
        if (Str::startsWith($testCase, '\\')) {
1✔
129
            $stub = str_replace('NamespacedDummyTestCase', trim($testCase, '\\'), $stub);
×
130
        } else {
131
            $stub = str_replace('NamespacedDummyTestCase', $namespaceTestCase, $stub);
1✔
132
        }
133

134
        $contents = str_replace(
1✔
135
            ['{{ namespace }}', '{{ class }}', '{{ name }}', 'DummyTestCase'],
1✔
136
            [$this->testNamespace(), $this->testClassName(), $this->testViewName(), class_basename(trim($testCase, '\\'))],
1✔
137
            $stub,
1✔
138
        );
1✔
139

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

142
        return $this->files->put($this->getTestPath(), $contents) !== false;
1✔
143
    }
144

145
    /**
146
     * Get the root namespace for the class.
147
     *
148
     * @return string
149
     */
150
    protected function rootNamespace()
151
    {
152
        return $this->rootNamespaceUsingCanvas();
2✔
153
    }
154

155
    /**
156
     * Get the destination test case path.
157
     *
158
     * @return string
159
     */
160
    protected function getTestPath()
161
    {
162
        $preset = $this->generatorPreset();
1✔
163

164
        $testPath = Str::of($this->testClassFullyQualifiedName())
1✔
165
            ->replace('\\', '/')
1✔
166
            ->replaceFirst('Tests/Feature', str_replace($preset->basePath(), '', $preset->testingPath()).'/Feature')
1✔
167
            ->append('Test.php')
1✔
168
            ->value();
1✔
169

170
        return $preset->basePath().$testPath;
1✔
171
    }
172

173
    /**
174
     * Get the test stub file for the generator.
175
     *
176
     * @return string
177
     */
178
    protected function getTestStub()
179
    {
180
        $stubName = 'view.'.($this->option('pest') ? 'pest' : 'test').'.stub';
1✔
181

182
        return $this->resolveStubPath("/stubs/{$stubName}");
1✔
183
    }
184
}
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

© 2025 Coveralls, Inc