|
| 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