|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 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 | +declare(strict_types=1); |
| 13 | + |
| 14 | +use Behat\Gherkin\Node\PyStringNode; |
| 15 | +use Behat\Gherkin\Node\TableNode; |
| 16 | +use Behat\Symfony2Extension\Context\KernelAwareContext; |
| 17 | +use PHPUnit\Framework\Assert; |
| 18 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 19 | +use Symfony\Component\Console\Command\Command; |
| 20 | +use Symfony\Component\Console\Tester\CommandTester; |
| 21 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * Context for Symfony commands. |
| 25 | + * |
| 26 | + * @author Alan Poulain <contact@alanpoulain.eu> |
| 27 | + */ |
| 28 | +final class CommandContext implements KernelAwareContext |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var KernelInterface |
| 32 | + */ |
| 33 | + private $kernel; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Application |
| 37 | + */ |
| 38 | + private $application; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var CommandTester |
| 42 | + */ |
| 43 | + private $commandTester; |
| 44 | + |
| 45 | + /** |
| 46 | + * @When I run the command :command |
| 47 | + */ |
| 48 | + public function iRunTheCommand(string $command): void |
| 49 | + { |
| 50 | + $command = $this->getApplication()->find($command); |
| 51 | + |
| 52 | + $this->getCommandTester($command)->execute([]); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @When I run the command :command with options: |
| 57 | + */ |
| 58 | + public function iRunTheCommandWithOptions(string $command, TableNode $options): void |
| 59 | + { |
| 60 | + $command = $this->getApplication()->find($command); |
| 61 | + |
| 62 | + $this->getCommandTester($command)->execute($options->getRowsHash()); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @Then the command output should be: |
| 67 | + */ |
| 68 | + public function theCommandOutputShouldBe(PyStringNode $expectedOutput): void |
| 69 | + { |
| 70 | + Assert::assertEquals($expectedOutput->getRaw(), $this->commandTester->getDisplay()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @Then the command output should contain: |
| 75 | + */ |
| 76 | + public function theCommandOutputShouldContain(PyStringNode $expectedOutput): void |
| 77 | + { |
| 78 | + $expectedOutput = str_replace('###', '"""', $expectedOutput->getRaw()); |
| 79 | + |
| 80 | + Assert::assertContains($expectedOutput, $this->commandTester->getDisplay()); |
| 81 | + } |
| 82 | + |
| 83 | + public function setKernel(KernelInterface $kernel): void |
| 84 | + { |
| 85 | + $this->kernel = $kernel; |
| 86 | + } |
| 87 | + |
| 88 | + public function getApplication(): Application |
| 89 | + { |
| 90 | + if (null !== $this->application) { |
| 91 | + return $this->application; |
| 92 | + } |
| 93 | + |
| 94 | + $this->application = new Application($this->kernel); |
| 95 | + |
| 96 | + return $this->application; |
| 97 | + } |
| 98 | + |
| 99 | + private function getCommandTester(Command $command): CommandTester |
| 100 | + { |
| 101 | + $this->commandTester = new CommandTester($command); |
| 102 | + |
| 103 | + return $this->commandTester; |
| 104 | + } |
| 105 | +} |
0 commit comments