|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of Hyperf. |
| 6 | + * |
| 7 | + * @link https://www.hyperf.io |
| 8 | + * @document https://hyperf.wiki |
| 9 | + * @contact group@hyperf.io |
| 10 | + * @license https://github.com/hyperf/hyperf/blob/master/LICENSE |
| 11 | + */ |
| 12 | + |
| 13 | +namespace HyperfTest\Command; |
| 14 | + |
| 15 | +use Hyperf\Command\AsCommand; |
| 16 | +use Hyperf\Command\ClosureCommand; |
| 17 | +use Hyperf\Command\Console; |
| 18 | +use Hyperf\Command\Listener\RegisterCommandListener; |
| 19 | +use Hyperf\Command\ParameterParser; |
| 20 | +use Hyperf\Config\Config; |
| 21 | +use Hyperf\Context\ApplicationContext; |
| 22 | +use Hyperf\Contract\ConfigInterface; |
| 23 | +use Hyperf\Contract\ContainerInterface; |
| 24 | +use Hyperf\Contract\NormalizerInterface; |
| 25 | +use Hyperf\Contract\StdoutLoggerInterface; |
| 26 | +use Hyperf\Di\Annotation\AnnotationReader; |
| 27 | +use Hyperf\Di\Annotation\ScanConfig; |
| 28 | +use Hyperf\Di\Annotation\Scanner; |
| 29 | +use Hyperf\Di\ClosureDefinitionCollector; |
| 30 | +use Hyperf\Di\ClosureDefinitionCollectorInterface; |
| 31 | +use Hyperf\Di\MethodDefinitionCollector; |
| 32 | +use Hyperf\Di\MethodDefinitionCollectorInterface; |
| 33 | +use Hyperf\Di\ReflectionManager; |
| 34 | +use Hyperf\Di\ScanHandler\NullScanHandler; |
| 35 | +use Hyperf\Serializer\SerializerFactory; |
| 36 | +use Hyperf\Serializer\SymfonyNormalizer; |
| 37 | +use HyperfTest\Command\Command\Annotation\TestAsCommand; |
| 38 | +use Mockery; |
| 39 | +use PHPUnit\Framework\Attributes\CoversNothing; |
| 40 | +use PHPUnit\Framework\TestCase; |
| 41 | + |
| 42 | +/** |
| 43 | + * @internal |
| 44 | + * @coversNothing |
| 45 | + * @method void registerAnnotationCommands() |
| 46 | + * @method void registerClosureCommands() |
| 47 | + * @property string $signature |
| 48 | + */ |
| 49 | +#[CoversNothing] |
| 50 | +class AsCommandAndClosureCommandTest extends TestCase |
| 51 | +{ |
| 52 | + /** |
| 53 | + * @var AsCommand[] |
| 54 | + */ |
| 55 | + protected array $containerSet = []; |
| 56 | + |
| 57 | + protected ContainerInterface $container; |
| 58 | + |
| 59 | + protected function setUp(): void |
| 60 | + { |
| 61 | + parent::setUp(); |
| 62 | + |
| 63 | + if (! empty($this->containerSet)) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $scanner = new Scanner(new ScanConfig(false, '/'), new NullScanHandler()); |
| 68 | + $reader = new AnnotationReader(); |
| 69 | + $scanner->collect($reader, ReflectionManager::reflectClass(TestAsCommand::class)); |
| 70 | + |
| 71 | + $this->container = $container = $this->getContainer(); |
| 72 | + } |
| 73 | + |
| 74 | + protected function tearDown(): void |
| 75 | + { |
| 76 | + Mockery::close(); |
| 77 | + $this->containerSet = []; |
| 78 | + } |
| 79 | + |
| 80 | + public function testRegisterAsCommand() |
| 81 | + { |
| 82 | + $container = $this->container; |
| 83 | + (fn () => $this->registerAnnotationCommands())->call( |
| 84 | + new RegisterCommandListener($container, $container->get(ConfigInterface::class), $container->get(StdoutLoggerInterface::class)) |
| 85 | + ); |
| 86 | + |
| 87 | + $commands = array_values($this->containerSet); |
| 88 | + $this->assertCount(3, $commands); |
| 89 | + |
| 90 | + $runCommand = $commands[0]; |
| 91 | + $runCommandDefinition = $runCommand->getDefinition(); |
| 92 | + $this->assertEquals($this->getSignature($runCommand), 'command:as-command:run'); |
| 93 | + $this->assertEquals(count($runCommandDefinition->getOptions()), 1); |
| 94 | + $this->assertEquals(count($runCommandDefinition->getArguments()), 0); |
| 95 | + $this->assertNotNull($runCommandDefinition->getOption('disable-event-dispatcher')); |
| 96 | + |
| 97 | + $runWithDefinedOptionsCommand = $commands[1]; |
| 98 | + $runWithDefinedOptionsCommandDefinition = $runWithDefinedOptionsCommand->getDefinition(); |
| 99 | + $this->assertEquals($this->getSignature($runWithDefinedOptionsCommand), 'command:as-command:runWithDefinedOptions {--name=}'); |
| 100 | + $this->assertEquals(count($runWithDefinedOptionsCommandDefinition->getOptions()), 2); |
| 101 | + $this->assertEquals(count($runWithDefinedOptionsCommandDefinition->getArguments()), 0); |
| 102 | + $this->assertNotNull($runCommandDefinition->getOption('disable-event-dispatcher')); |
| 103 | + $this->assertNotNull($runWithDefinedOptionsCommandDefinition->getOption('name')); |
| 104 | + |
| 105 | + $runWithoutOptionsCommand = $commands[2]; |
| 106 | + $runWithoutOptionsCommandDefinition = $runWithoutOptionsCommand->getDefinition(); |
| 107 | + $this->assertEquals($this->getSignature($runWithoutOptionsCommand), 'command:as-command:runWithoutOptions'); |
| 108 | + $this->assertEquals(count($runWithoutOptionsCommandDefinition->getOptions()), 4); |
| 109 | + $this->assertEquals(count($runWithoutOptionsCommandDefinition->getArguments()), 0); |
| 110 | + $this->assertNotNull($runCommandDefinition->getOption('disable-event-dispatcher')); |
| 111 | + $this->assertNotNull($runWithoutOptionsCommandDefinition->getOption('name')); |
| 112 | + $this->assertNotNull($runWithoutOptionsCommandDefinition->getOption('age')); |
| 113 | + $this->assertNotNull($runWithoutOptionsCommandDefinition->getOption('testBool')); |
| 114 | + } |
| 115 | + |
| 116 | + public function testRegisterClosureCommand() |
| 117 | + { |
| 118 | + $runCommand = Console::command('command:closure:run', function () { |
| 119 | + return 'closure'; |
| 120 | + }); |
| 121 | + $runCommandDefinition = $runCommand->getDefinition(); |
| 122 | + $this->assertEquals($this->getSignature($runCommand), 'command:closure:run'); |
| 123 | + $this->assertEquals(count($runCommandDefinition->getOptions()), 1); |
| 124 | + $this->assertEquals(count($runCommandDefinition->getArguments()), 0); |
| 125 | + $this->assertNotNull($runCommandDefinition->getOption('disable-event-dispatcher')); |
| 126 | + |
| 127 | + $runWithDefinedOptionsCommand = Console::command('command:closure:withDefineOptions {--name=}', function (string $name) { |
| 128 | + return 'with define options'; |
| 129 | + }); |
| 130 | + $runWithDefinedOptionsCommandDefinition = $runWithDefinedOptionsCommand->getDefinition(); |
| 131 | + $this->assertEquals($this->getSignature($runWithDefinedOptionsCommand), 'command:closure:withDefineOptions {--name=}'); |
| 132 | + $this->assertEquals(count($runWithDefinedOptionsCommandDefinition->getOptions()), 2); |
| 133 | + $this->assertEquals(count($runWithDefinedOptionsCommandDefinition->getArguments()), 0); |
| 134 | + $this->assertNotNull($runCommandDefinition->getOption('disable-event-dispatcher')); |
| 135 | + $this->assertNotNull($runWithDefinedOptionsCommandDefinition->getOption('name')); |
| 136 | + |
| 137 | + $runWithoutOptionsCommand = Console::command('command:closure:withoutDefineOptions', function (string $name, int $age = 9, bool $testBool = false) { |
| 138 | + return 'with define options'; |
| 139 | + }); |
| 140 | + $runWithoutOptionsCommandDefinition = $runWithoutOptionsCommand->getDefinition(); |
| 141 | + $this->assertEquals($this->getSignature($runWithoutOptionsCommand), 'command:closure:withoutDefineOptions'); |
| 142 | + $this->assertEquals(count($runWithoutOptionsCommandDefinition->getOptions()), 4); |
| 143 | + $this->assertEquals(count($runWithoutOptionsCommandDefinition->getArguments()), 0); |
| 144 | + $this->assertNotNull($runCommandDefinition->getOption('disable-event-dispatcher')); |
| 145 | + $this->assertNotNull($runWithoutOptionsCommandDefinition->getOption('name')); |
| 146 | + $this->assertNotNull($runWithoutOptionsCommandDefinition->getOption('age')); |
| 147 | + $this->assertNotNull($runWithoutOptionsCommandDefinition->getOption('testBool')); |
| 148 | + } |
| 149 | + |
| 150 | + public function testParameterParser() |
| 151 | + { |
| 152 | + $container = $this->container; |
| 153 | + $parameterParser = $container->get(ParameterParser::class); |
| 154 | + |
| 155 | + $class = TestAsCommand::class; |
| 156 | + $method = 'runWithoutOptions'; |
| 157 | + $arguments = [ |
| 158 | + 'name' => 'Hyperf', |
| 159 | + 'test-bool' => '123', // snake case |
| 160 | + ]; |
| 161 | + |
| 162 | + $result = $parameterParser->parseMethodParameters($class, $method, $arguments); |
| 163 | + $this->assertEquals([ |
| 164 | + 'Hyperf', |
| 165 | + 9, |
| 166 | + true, |
| 167 | + ], $result); |
| 168 | + } |
| 169 | + |
| 170 | + protected function getSignature(AsCommand|ClosureCommand $asCommand): string |
| 171 | + { |
| 172 | + return (fn () => $this->signature)->call($asCommand); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @return ContainerInterface |
| 177 | + */ |
| 178 | + protected function getContainer() |
| 179 | + { |
| 180 | + $container = Mockery::mock(ContainerInterface::class); |
| 181 | + ApplicationContext::setContainer($container); |
| 182 | + |
| 183 | + $container->shouldReceive('has')->with(ConfigInterface::class)->andReturnTrue(); |
| 184 | + $container->shouldReceive('get')->with(ConfigInterface::class)->andReturnUsing(function () { |
| 185 | + return new Config([ |
| 186 | + ]); |
| 187 | + }); |
| 188 | + |
| 189 | + $container->shouldReceive('get')->with(StdoutLoggerInterface::class)->andReturnUsing(function () { |
| 190 | + $logger = Mockery::mock(StdoutLoggerInterface::class); |
| 191 | + $logger->shouldReceive('debug')->andReturn(null); |
| 192 | + $logger->shouldReceive('log')->andReturn(null); |
| 193 | + return $logger; |
| 194 | + }); |
| 195 | + |
| 196 | + $container->shouldReceive('get')->with(NormalizerInterface::class)->andReturn(new SymfonyNormalizer((new SerializerFactory())->__invoke())); |
| 197 | + $container->shouldReceive('has')->with(NormalizerInterface::class)->andReturn(true); |
| 198 | + $container->shouldReceive('get')->with(MethodDefinitionCollectorInterface::class)->andReturn(new MethodDefinitionCollector()); |
| 199 | + $container->shouldReceive('has')->with(MethodDefinitionCollectorInterface::class)->andReturn(true); |
| 200 | + $container->shouldReceive('has')->with(ClosureDefinitionCollectorInterface::class)->andReturn(true); |
| 201 | + $container->shouldReceive('get')->with(ClosureDefinitionCollectorInterface::class)->andReturn(new ClosureDefinitionCollector()); |
| 202 | + |
| 203 | + $container->shouldReceive('get')->with(ParameterParser::class)->andReturn(new ParameterParser($container)); |
| 204 | + $container->shouldReceive('get')->with(TestAsCommand::class)->andReturn(new TestAsCommand()); |
| 205 | + |
| 206 | + $container->shouldReceive('set')->withAnyArgs()->andReturnUsing(function ($key, $value) { |
| 207 | + $this->containerSet[$key] = $value; |
| 208 | + }); |
| 209 | + |
| 210 | + $container->shouldReceive('get')->with(ClosureCommand::class)->andReturn(ClosureCommand::class); |
| 211 | + |
| 212 | + // closure command |
| 213 | + $container->shouldReceive('make')->with(ClosureCommand::class, Mockery::any()) |
| 214 | + ->andReturnUsing(function ($class, $arguments) { |
| 215 | + return new ClosureCommand($this->container, $arguments['signature'], $arguments['closure']); |
| 216 | + }); |
| 217 | + |
| 218 | + return $container; |
| 219 | + } |
| 220 | +} |
0 commit comments