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

orchestral / canvas / 6168648387

13 Sep 2023 06:10AM UTC coverage: 89.945% (-4.2%) from 94.11%
6168648387

push

github

crynobone
Merge branch '8.x'

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

128 of 128 new or added lines in 8 files covered. (100.0%)

814 of 905 relevant lines covered (89.94%)

23.14 hits per line

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

98.63
/src/Commands/View.php
1
<?php
2

3
namespace Orchestra\Canvas\Commands;
4

5
use Illuminate\Console\Concerns\CreatesMatchingTest;
6
use Illuminate\Foundation\Inspiring;
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\Str;
9
use Orchestra\Canvas\Processors\GeneratesViewCode;
10
use Symfony\Component\Console\Attribute\AsCommand;
11
use Symfony\Component\Console\Input\InputOption;
12

13
/**
14
 * @see https://github.com/laravel/framework/blob/10.x/src/Illuminate/Foundation/Console/ViewMakeCommand.php
15
 */
16
#[AsCommand(name: 'make:view', description: 'Create a new view')]
17
class View extends Generator
18
{
19
    use CreatesMatchingTest;
20

21
    /**
22
     * The type of file being generated.
23
     *
24
     * @var string
25
     */
26
    protected string $type = 'View';
27

28
    /**
29
     * Generator processor.
30
     *
31
     * @var class-string<\Orchestra\Canvas\Core\GeneratesCode>
32
     */
33
    protected string $processor = GeneratesViewCode::class;
34

35
    /**
36
     * Handle generating code.
37
     */
38
    public function generatingCode(string $stub, string $name): string
39
    {
40
        $stub = parent::generatingCode($stub, $name);
2✔
41

42
        return str_replace(
2✔
43
            '{{ quote }}',
2✔
44
            Inspiring::quotes()->random(),
2✔
45
            $stub,
2✔
46
        );
2✔
47
    }
48

49
    /**
50
     * Get the stub file name for the generator.
51
     */
52
    public function getStubFileName(): string
53
    {
54
        return 'view.stub';
2✔
55
    }
56

57
    /**
58
     * Generator options.
59
     *
60
     * @return array<string, mixed>
61
     */
62
    public function generatorOptions(): array
63
    {
64
        return array_merge(parent::generatorOptions(), [
2✔
65
            'extension' => $this->option('extension'),
2✔
66
            'force' => $this->option('force'),
2✔
67
        ]);
2✔
68
    }
69

70
    /**
71
     * Get the destination test case path.
72
     *
73
     * @return string
74
     */
75
    protected function getTestPath()
76
    {
77
        return sprintf(
1✔
78
            '%s/%s',
1✔
79
            $this->preset->basePath(),
1✔
80
            Str::of($this->testClassFullyQualifiedName())
1✔
81
                ->replace('\\', '/')
1✔
82
                ->replaceFirst('Tests/Feature', 'tests/Feature')
1✔
83
                ->append('Test.php')
1✔
84
                ->value()
1✔
85
        );
1✔
86
    }
87

88
    /**
89
     * Create the matching test case if requested.
90
     *
91
     * @param  string  $path
92
     */
93
    protected function handleTestCreationUsingCanvas(string $path): bool
94
    {
95
        if (! $this->option('test') && ! $this->option('pest')) {
2✔
96
            return false;
1✔
97
        }
98

99
        $namespaceTestCase = $testCase = $this->preset->config(
1✔
100
            'testing.extends.feature',
1✔
101
            $this->preset->is('laravel') ? 'Tests\TestCase' : 'Orchestra\Testbench\TestCase'
1✔
102
        );
1✔
103

104
        $stub = $this->files->get($this->getTestStub());
1✔
105

106
        if (Str::startsWith($testCase, '\\')) {
1✔
107
            $stub = str_replace('NamespacedDummyTestCase', trim($testCase, '\\'), $stub);
×
108
        } else {
109
            $stub = str_replace('NamespacedDummyTestCase', $namespaceTestCase, $stub);
1✔
110
        }
111

112
        $contents = str_replace(
1✔
113
            ['{{ namespace }}', '{{ class }}', '{{ name }}', 'DummyTestCase'],
1✔
114
            [$this->testNamespace(), $this->testClassName(), $this->testViewName(), class_basename(trim($testCase, '\\'))],
1✔
115
            $stub,
1✔
116
        );
1✔
117

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

120
        return $this->files->put($this->getTestPath(), $contents) !== false;
1✔
121
    }
122

123
    /**
124
     * Get the namespace for the test.
125
     *
126
     * @return string
127
     */
128
    protected function testNamespace(): string
129
    {
130
        return Str::of($this->testClassFullyQualifiedName())
1✔
131
            ->beforeLast('\\')
1✔
132
            ->value();
1✔
133
    }
134

135
    /**
136
     * Get the class name for the test.
137
     *
138
     * @return string
139
     */
140
    protected function testClassName(): string
141
    {
142
        return Str::of($this->testClassFullyQualifiedName())
1✔
143
            ->afterLast('\\')
1✔
144
            ->append('Test')
1✔
145
            ->value();
1✔
146
    }
147

148
    /**
149
     * Get the class fully qualified name for the test.
150
     *
151
     * @return string
152
     */
153
    protected function testClassFullyQualifiedName(): string
154
    {
155
        /** @var string $extension */
156
        $extension = $this->option('extension');
1✔
157

158
        $name = Str::of(Str::lower($this->generatorName()))->replace('.'.$extension, '');
1✔
159

160
        $namespacedName = Str::of(
1✔
161
            Str::of($name)
1✔
162
                ->replace('/', ' ')
1✔
163
                ->explode(' ')
1✔
164
                ->map(fn ($part) => Str::of($part)->ucfirst())
1✔
165
                ->implode('\\')
1✔
166
        )
1✔
167
            ->replace(['-', '_'], ' ')
1✔
168
            ->explode(' ')
1✔
169
            ->map(fn ($part) => Str::of($part)->ucfirst())
1✔
170
            ->implode('');
1✔
171

172
        return sprintf(
1✔
173
            '%s\\Feature\\View\\%s',
1✔
174
            $this->preset->testingNamespace(),
1✔
175
            $namespacedName
1✔
176
        );
1✔
177
    }
178

179
    /**
180
     * Get the test stub file for the generator.
181
     *
182
     * @return string
183
     */
184
    protected function getTestStub(): string
185
    {
186
        return $this->getStubFileFromPresetStorage(
1✔
187
            $this->preset, 'view.'.($this->option('pest') ? 'pest' : 'test').'.stub'
1✔
188
        );
1✔
189
    }
190

191
    /**
192
     * Get the view name for the test.
193
     *
194
     * @return string
195
     */
196
    protected function testViewName(): string
197
    {
198
        return Str::of($this->generatorName())
1✔
199
            ->replace('/', '.')
1✔
200
            ->lower()
1✔
201
            ->value();
1✔
202
    }
203

204
    /**
205
     * Get the console command arguments.
206
     *
207
     * @return array
208
     */
209
    protected function getOptions()
210
    {
211
        return [
100✔
212
            ['extension', null, InputOption::VALUE_OPTIONAL, 'The extension of the generated view', 'blade.php'],
100✔
213
            ['force', 'f', InputOption::VALUE_NONE, 'Create the view even if the view already exists'],
100✔
214
        ];
100✔
215
    }
216
}
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