|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the CleverAge/ProcessBundle package. |
| 7 | + * |
| 8 | + * Copyright (c) Clever-Age |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CleverAge\ProcessBundle\Tests\Task\Process; |
| 15 | + |
| 16 | +use CleverAge\ProcessBundle\Configuration\ProcessConfiguration; |
| 17 | +use CleverAge\ProcessBundle\Configuration\TaskConfiguration; |
| 18 | +use CleverAge\ProcessBundle\Context\ContextualOptionResolver; |
| 19 | +use CleverAge\ProcessBundle\Model\ProcessHistory; |
| 20 | +use CleverAge\ProcessBundle\Model\ProcessState; |
| 21 | +use CleverAge\ProcessBundle\Task\Process\CommandRunnerTask; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 24 | +use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
| 25 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 26 | + |
| 27 | +#[\PHPUnit\Framework\Attributes\CoversClass(CommandRunnerTask::class)] |
| 28 | +#[\PHPUnit\Framework\Attributes\CoversMethod(CommandRunnerTask::class, 'execute')] |
| 29 | +#[\PHPUnit\Framework\Attributes\CoversMethod(CommandRunnerTask::class, 'configureOptions')] |
| 30 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessConfiguration::class)] |
| 31 | +#[\PHPUnit\Framework\Attributes\UsesClass(TaskConfiguration::class)] |
| 32 | +#[\PHPUnit\Framework\Attributes\UsesClass(ContextualOptionResolver::class)] |
| 33 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessHistory::class)] |
| 34 | +#[\PHPUnit\Framework\Attributes\UsesClass(ProcessState::class)] |
| 35 | +class CommandRunnerTaskTest extends TestCase |
| 36 | +{ |
| 37 | + public function testExecuteWithArrayCommandline(): void |
| 38 | + { |
| 39 | + $state = $this->createState(['commandline' => ['echo', 'hello']]); |
| 40 | + |
| 41 | + $this->createTask()->execute($state); |
| 42 | + |
| 43 | + $this->assertSame("hello\n", $state->getOutput()); |
| 44 | + } |
| 45 | + |
| 46 | + public function testExecuteWithStringCommandline(): void |
| 47 | + { |
| 48 | + $state = $this->createState(['commandline' => 'echo "hello world" | tr a-z A-Z']); |
| 49 | + |
| 50 | + $this->createTask()->execute($state); |
| 51 | + |
| 52 | + $this->assertSame("HELLO WORLD\n", $state->getOutput()); |
| 53 | + } |
| 54 | + |
| 55 | + public function testInputIsPassedToCommand(): void |
| 56 | + { |
| 57 | + $state = $this->createState(['commandline' => ['cat']]); |
| 58 | + $state->setInput('from input'); |
| 59 | + |
| 60 | + $this->createTask()->execute($state); |
| 61 | + |
| 62 | + $this->assertSame('from input', $state->getOutput()); |
| 63 | + } |
| 64 | + |
| 65 | + public function testCwdAndEnvOptions(): void |
| 66 | + { |
| 67 | + $state = $this->createState([ |
| 68 | + 'commandline' => 'echo "$FOO" && pwd', |
| 69 | + 'cwd' => sys_get_temp_dir(), |
| 70 | + 'env' => ['FOO' => 'bar'], |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->createTask()->execute($state); |
| 74 | + |
| 75 | + $this->assertSame('bar'.\PHP_EOL.realpath(sys_get_temp_dir()).\PHP_EOL, $state->getOutput()); |
| 76 | + } |
| 77 | + |
| 78 | + public function testProcessOptionsArePassedToProcess(): void |
| 79 | + { |
| 80 | + $state = $this->createState([ |
| 81 | + 'commandline' => ['echo', 'hello'], |
| 82 | + 'options' => ['create_new_console' => false], |
| 83 | + ]); |
| 84 | + |
| 85 | + $this->createTask()->execute($state); |
| 86 | + |
| 87 | + $this->assertSame("hello\n", $state->getOutput()); |
| 88 | + } |
| 89 | + |
| 90 | + public function testInvalidProcessOptionsType(): void |
| 91 | + { |
| 92 | + $state = $this->createState([ |
| 93 | + 'commandline' => ['echo', 'hello'], |
| 94 | + 'options' => 'invalid', |
| 95 | + ]); |
| 96 | + |
| 97 | + $this->expectException(InvalidOptionsException::class); |
| 98 | + $this->createTask()->execute($state); |
| 99 | + } |
| 100 | + |
| 101 | + public function testFailingCommandThrows(): void |
| 102 | + { |
| 103 | + $state = $this->createState(['commandline' => ['false']]); |
| 104 | + |
| 105 | + $this->expectException(ProcessFailedException::class); |
| 106 | + $this->createTask()->execute($state); |
| 107 | + } |
| 108 | + |
| 109 | + private function createTask(): CommandRunnerTask |
| 110 | + { |
| 111 | + $kernel = $this->createStub(KernelInterface::class); |
| 112 | + $kernel->method('getProjectDir')->willReturn(sys_get_temp_dir()); |
| 113 | + |
| 114 | + return new CommandRunnerTask($kernel); |
| 115 | + } |
| 116 | + |
| 117 | + private function createState(array $options): ProcessState |
| 118 | + { |
| 119 | + $processConfiguration = new ProcessConfiguration('test', []); |
| 120 | + $state = new ProcessState($processConfiguration, new ProcessHistory($processConfiguration)); |
| 121 | + $state->setContextualOptionResolver(new ContextualOptionResolver()); |
| 122 | + $state->setContext([]); |
| 123 | + $state->setTaskConfiguration(new TaskConfiguration('command', CommandRunnerTask::class, $options)); |
| 124 | + |
| 125 | + return $state; |
| 126 | + } |
| 127 | +} |
0 commit comments