Skip to content

Commit 3a89f6f

Browse files
authored
[9.x] Return exit status instead of booleans from GeneratorCommand::handle() (#43915)
* test it should fail if name argument is a reserved word * return exit status instead of bool values - Failure: `1` - Success: `0` * replace array access with make calls - makes it easier to test * test it should generate the requested class * fix styling * fix typo
1 parent 45cd804 commit 3a89f6f

File tree

2 files changed

+125
-5
lines changed

2 files changed

+125
-5
lines changed

src/Illuminate/Console/GeneratorCommand.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Console;
44

55
use Illuminate\Console\Concerns\CreatesMatchingTest;
6+
use Illuminate\Contracts\Container\BindingResolutionException;
67
use Illuminate\Filesystem\Filesystem;
78
use Illuminate\Support\Str;
89
use Symfony\Component\Console\Input\InputArgument;
@@ -137,7 +138,7 @@ abstract protected function getStub();
137138
/**
138139
* Execute the console command.
139140
*
140-
* @return bool|null
141+
* @return int|void
141142
*
142143
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
143144
*/
@@ -149,7 +150,7 @@ public function handle()
149150
if ($this->isReservedName($this->getNameInput())) {
150151
$this->components->error('The name "'.$this->getNameInput().'" is reserved by PHP.');
151152

152-
return false;
153+
return self::FAILURE;
153154
}
154155

155156
$name = $this->qualifyClass($this->getNameInput());
@@ -164,7 +165,7 @@ public function handle()
164165
$this->alreadyExists($this->getNameInput())) {
165166
$this->components->error($this->type.' already exists.');
166167

167-
return false;
168+
return self::FAILURE;
168169
}
169170

170171
// Next, we will generate the path to the location where this class' file should get
@@ -183,6 +184,8 @@ public function handle()
183184
}
184185

185186
$this->components->info($info.' created successfully.');
187+
188+
return self::SUCCESS;
186189
}
187190

188191
/**
@@ -258,12 +261,14 @@ protected function alreadyExists($rawName)
258261
*
259262
* @param string $name
260263
* @return string
264+
*
265+
* @throws BindingResolutionException
261266
*/
262267
protected function getPath($name)
263268
{
264269
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
265270

266-
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
271+
return $this->laravel->make('path').'/'.str_replace('\\', '/', $name).'.php';
267272
}
268273

269274
/**
@@ -390,10 +395,12 @@ protected function rootNamespace()
390395
* Get the model for the default guard's user provider.
391396
*
392397
* @return string|null
398+
*
399+
* @throws BindingResolutionException
393400
*/
394401
protected function userProviderModel()
395402
{
396-
$config = $this->laravel['config'];
403+
$config = $this->laravel->make('config');
397404

398405
$provider = $config->get('auth.guards.'.$config->get('auth.defaults.guard').'.provider');
399406

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Console;
4+
5+
use Illuminate\Config\Repository;
6+
use Illuminate\Console\GeneratorCommand;
7+
use Illuminate\Console\OutputStyle;
8+
use Illuminate\Console\View\Components\Factory;
9+
use Illuminate\Contracts\Filesystem\FileNotFoundException;
10+
use Illuminate\Contracts\Foundation\Application;
11+
use Illuminate\Filesystem\Filesystem;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Console\Command\Command;
14+
use Symfony\Component\Console\Exception\RuntimeException;
15+
use Symfony\Component\Console\Input\ArrayInput;
16+
use Symfony\Component\Console\Output\NullOutput;
17+
18+
class GeneratorCommandTest extends TestCase
19+
{
20+
/**
21+
* @test
22+
*/
23+
public function itShouldThrowIfNameIsNotPassedAsArgument(): void
24+
{
25+
$this->expectException(RuntimeException::class);
26+
$this->expectExceptionMessage('Not enough arguments (missing: "name").');
27+
28+
$sut = $this->getMockForAbstractClass(
29+
GeneratorCommand::class,
30+
[$this->createStub(Filesystem::class)],
31+
'FooMakeCommand'
32+
);
33+
$sut->setLaravel(app());
34+
35+
$input = new ArrayInput([]);
36+
$output = new NullOutput();
37+
38+
$sut->run($input, $output);
39+
}
40+
41+
/**
42+
* @test
43+
*/
44+
public function itShouldFailIfNameArgumentIsReservedName(): void
45+
{
46+
$sut = $this->getMockForAbstractClass(
47+
GeneratorCommand::class,
48+
[$this->createStub(Filesystem::class)],
49+
'FooMakeCommand'
50+
);
51+
$sut->setLaravel(app());
52+
53+
$input = new ArrayInput(['name' => 'class']);
54+
$output = new NullOutput();
55+
56+
$this->assertSame(Command::FAILURE, $sut->run($input, $output));
57+
}
58+
59+
/**
60+
* @test
61+
*
62+
* @throws FileNotFoundException
63+
*/
64+
public function itShouldGenerateTheRequestedClass(): void
65+
{
66+
$appPath = '/path/to/app';
67+
$nameArgument = 'MyFoo';
68+
69+
$fileSystem = $this->createStub(Filesystem::class);
70+
$stub = '<?php namespace DummyNamespace; class DummyClass {}';
71+
$fileSystem->method('get')->willReturn($stub);
72+
73+
// @phpstan-ignore-next-line
74+
$fileSystem->expects($this->once())->method('put')->with(
75+
sprintf('%s//%s.php', $appPath, $nameArgument),
76+
'<?php namespace App; class MyFoo {}'
77+
)->willReturn(0);
78+
79+
$sut = $this->getMockForAbstractClass(
80+
GeneratorCommand::class,
81+
[$fileSystem],
82+
'FooMakeCommand'
83+
);
84+
85+
$laravel = $this->createStub(Application::class);
86+
$config = new Repository();
87+
$config->set('auth', [
88+
'defaults'=> ['guard' => 'web'],
89+
'guards' => ['web' => ['provider' => 'users']],
90+
'providers' => ['users' => ['model' => 'App\User']],
91+
]);
92+
93+
$laravel->method('make')->willReturnOnConsecutiveCalls(
94+
$this->createStub(OutputStyle::class),
95+
$this->createStub(Factory::class),
96+
$appPath,
97+
null,
98+
$config,
99+
$config,
100+
$config,
101+
);
102+
$laravel->method('getNamespace')->willReturn('App');
103+
104+
$sut->setLaravel($laravel);
105+
106+
$input = new ArrayInput(['name' => 'MyFoo']);
107+
$output = new NullOutput();
108+
109+
$sut->run($input, $output);
110+
111+
$this->assertSame(Command::SUCCESS, $sut->handle());
112+
}
113+
}

0 commit comments

Comments
 (0)