|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Silverback API Components Bundle Project |
| 5 | + * |
| 6 | + * (c) Daniel West <daniel@silverback.is> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Silverback\ApiComponentsBundle\Tests\Maker; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Silverback\ApiComponentsBundle\Maker\MakeApiComponent; |
| 16 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 17 | +use Symfony\Bundle\MakerBundle\Generator; |
| 18 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 19 | +use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; |
| 20 | +use Symfony\Component\Console\Application; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\ArrayInput; |
| 23 | +use Symfony\Component\Console\Output\BufferedOutput; |
| 24 | + |
| 25 | +class MakeApiComponentTest extends TestCase |
| 26 | +{ |
| 27 | + private function makeMaker(): MakeApiComponent |
| 28 | + { |
| 29 | + return new MakeApiComponent(); |
| 30 | + } |
| 31 | + |
| 32 | + private function configuredCommand(): Command |
| 33 | + { |
| 34 | + $maker = $this->makeMaker(); |
| 35 | + $command = new Command('make:api-component'); |
| 36 | + $maker->configureCommand($command, new InputConfiguration()); |
| 37 | + |
| 38 | + return $command; |
| 39 | + } |
| 40 | + |
| 41 | + private function boundInput(array $params): ArrayInput |
| 42 | + { |
| 43 | + $command = $this->configuredCommand(); |
| 44 | + $input = new ArrayInput($params, $command->getDefinition()); |
| 45 | + $input->setInteractive(false); |
| 46 | + |
| 47 | + return $input; |
| 48 | + } |
| 49 | + |
| 50 | + private function makeIo(BufferedOutput $output): ConsoleStyle |
| 51 | + { |
| 52 | + return new ConsoleStyle(new ArrayInput([]), $output); |
| 53 | + } |
| 54 | + |
| 55 | + private function makeGenerator(string $expectedClass, array &$capturedVars): Generator |
| 56 | + { |
| 57 | + $generator = $this->createMock(Generator::class); |
| 58 | + |
| 59 | + $generator->method('createClassNameDetails') |
| 60 | + ->willReturn(new ClassNameDetails($expectedClass, 'Entity\\Component\\')); |
| 61 | + |
| 62 | + $generator->expects($this->once()) |
| 63 | + ->method('generateClass') |
| 64 | + ->willReturnCallback(function (string $className, string $template, array $vars) use (&$capturedVars): string { |
| 65 | + $capturedVars = $vars; |
| 66 | + |
| 67 | + return 'src/Entity/Component/'.basename($className).'.php'; |
| 68 | + }); |
| 69 | + |
| 70 | + $generator->expects($this->once())->method('writeChanges'); |
| 71 | + |
| 72 | + return $generator; |
| 73 | + } |
| 74 | + |
| 75 | + public function testCommandName(): void |
| 76 | + { |
| 77 | + $this->assertSame('make:api-component', MakeApiComponent::getCommandName()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testCommandDescription(): void |
| 81 | + { |
| 82 | + $this->assertNotEmpty(MakeApiComponent::getCommandDescription()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testGeneratesMinimalComponent(): void |
| 86 | + { |
| 87 | + $vars = []; |
| 88 | + $generator = $this->makeGenerator('App\\Entity\\Component\\MyComponent', $vars); |
| 89 | + $input = $this->boundInput(['name' => 'MyComponent', '--timestamped' => false, '--publishable' => false, '--uploadable' => false]); |
| 90 | + $output = new BufferedOutput(); |
| 91 | + |
| 92 | + $this->makeMaker()->generate($input, $this->makeIo($output), $generator); |
| 93 | + |
| 94 | + $this->assertFalse($vars['timestamped']); |
| 95 | + $this->assertFalse($vars['publishable']); |
| 96 | + $this->assertFalse($vars['uploadable']); |
| 97 | + } |
| 98 | + |
| 99 | + public function testGeneratesWithTimestamped(): void |
| 100 | + { |
| 101 | + $vars = []; |
| 102 | + $generator = $this->makeGenerator('App\\Entity\\Component\\MyComponent', $vars); |
| 103 | + $input = $this->boundInput(['name' => 'MyComponent', '--timestamped' => true, '--publishable' => false, '--uploadable' => false]); |
| 104 | + $output = new BufferedOutput(); |
| 105 | + |
| 106 | + $this->makeMaker()->generate($input, $this->makeIo($output), $generator); |
| 107 | + |
| 108 | + $this->assertTrue($vars['timestamped']); |
| 109 | + $this->assertFalse($vars['publishable']); |
| 110 | + $this->assertFalse($vars['uploadable']); |
| 111 | + } |
| 112 | + |
| 113 | + public function testGeneratesWithPublishable(): void |
| 114 | + { |
| 115 | + $vars = []; |
| 116 | + $generator = $this->makeGenerator('App\\Entity\\Component\\MyComponent', $vars); |
| 117 | + $input = $this->boundInput(['name' => 'MyComponent', '--timestamped' => false, '--publishable' => true, '--uploadable' => false]); |
| 118 | + $output = new BufferedOutput(); |
| 119 | + |
| 120 | + $this->makeMaker()->generate($input, $this->makeIo($output), $generator); |
| 121 | + |
| 122 | + $this->assertFalse($vars['timestamped']); |
| 123 | + $this->assertTrue($vars['publishable']); |
| 124 | + $this->assertFalse($vars['uploadable']); |
| 125 | + } |
| 126 | + |
| 127 | + public function testGeneratesWithUploadable(): void |
| 128 | + { |
| 129 | + $vars = []; |
| 130 | + $generator = $this->makeGenerator('App\\Entity\\Component\\MyComponent', $vars); |
| 131 | + $input = $this->boundInput(['name' => 'MyComponent', '--timestamped' => false, '--publishable' => false, '--uploadable' => true]); |
| 132 | + $output = new BufferedOutput(); |
| 133 | + |
| 134 | + $this->makeMaker()->generate($input, $this->makeIo($output), $generator); |
| 135 | + |
| 136 | + $this->assertFalse($vars['timestamped']); |
| 137 | + $this->assertFalse($vars['publishable']); |
| 138 | + $this->assertTrue($vars['uploadable']); |
| 139 | + } |
| 140 | + |
| 141 | + public function testGeneratesWithAllAnnotations(): void |
| 142 | + { |
| 143 | + $vars = []; |
| 144 | + $generator = $this->makeGenerator('App\\Entity\\Component\\MyComponent', $vars); |
| 145 | + $input = $this->boundInput(['name' => 'MyComponent', '--timestamped' => true, '--publishable' => true, '--uploadable' => true]); |
| 146 | + $output = new BufferedOutput(); |
| 147 | + |
| 148 | + $this->makeMaker()->generate($input, $this->makeIo($output), $generator); |
| 149 | + |
| 150 | + $this->assertTrue($vars['timestamped']); |
| 151 | + $this->assertTrue($vars['publishable']); |
| 152 | + $this->assertTrue($vars['uploadable']); |
| 153 | + } |
| 154 | + |
| 155 | + public function testOutputIncludesMigrationReminder(): void |
| 156 | + { |
| 157 | + $vars = []; |
| 158 | + $generator = $this->makeGenerator('App\\Entity\\Component\\MyComponent', $vars); |
| 159 | + $input = $this->boundInput(['name' => 'MyComponent', '--timestamped' => false, '--publishable' => false, '--uploadable' => false]); |
| 160 | + $output = new BufferedOutput(); |
| 161 | + |
| 162 | + $this->makeMaker()->generate($input, $this->makeIo($output), $generator); |
| 163 | + |
| 164 | + $text = $output->fetch(); |
| 165 | + $this->assertStringContainsString('make:migration', $text); |
| 166 | + $this->assertStringContainsString('review', $text); |
| 167 | + } |
| 168 | + |
| 169 | + public function testInteractAsksAllThreeQuestions(): void |
| 170 | + { |
| 171 | + $command = $this->configuredCommand(); |
| 172 | + |
| 173 | + $stream = fopen('php://memory', 'r+'); |
| 174 | + fwrite($stream, "yes\nno\nno\n"); |
| 175 | + rewind($stream); |
| 176 | + |
| 177 | + $input = new ArrayInput(['name' => 'MyComponent'], $command->getDefinition()); |
| 178 | + $input->setStream($stream); |
| 179 | + $input->setInteractive(true); |
| 180 | + |
| 181 | + $output = new BufferedOutput(); |
| 182 | + $io = new ConsoleStyle($input, $output); |
| 183 | + |
| 184 | + $this->makeMaker()->interact($input, $io, $command); |
| 185 | + |
| 186 | + $this->assertTrue($input->getOption('timestamped')); |
| 187 | + $this->assertFalse($input->getOption('publishable')); |
| 188 | + $this->assertFalse($input->getOption('uploadable')); |
| 189 | + |
| 190 | + fclose($stream); |
| 191 | + } |
| 192 | +} |
0 commit comments