Skip to content

Commit

Permalink
Merge pull request #546 from alexislefebvre/add-test-of-command-input
Browse files Browse the repository at this point in the history
Add test of command input
  • Loading branch information
alexislefebvre authored Sep 6, 2019
2 parents af9630d + 563b59e commit a47557f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
60 changes: 60 additions & 0 deletions tests/App/Command/TestInteractiveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Liip/FunctionalTestBundle
*
* (c) Lukas Kahwe Smith <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Liip\Acme\Tests\App\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class TestInteractiveCommand extends ContainerAwareCommand
{
private $container;

protected function configure(): void
{
parent::configure();

$this
->setName('liipfunctionaltestbundle:test:interactive')
->setDescription('Interactive test command')
;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);

$this->container = $this->getContainer();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$helper = $this->getHelper('question');
$question = new Question('Please enter the input', 'AcmeDemoBundle');

$answer = $helper->ask($input, $output, $question);

$output->writeln(PHP_EOL);
$output->writeln(sprintf('Value of answer: %s', $answer));
}
}
2 changes: 2 additions & 0 deletions tests/App/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,7 @@ services:
autoconfigure: true
Liip\Acme\Tests\App\Command\TestCommand:
tags: ['console.command']
Liip\Acme\Tests\App\Command\TestInteractiveCommand:
tags: ['console.command']
Liip\Acme\Tests\App\Command\TestStatusCodeCommand:
tags: ['console.command']
13 changes: 12 additions & 1 deletion tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,21 @@ public function testRunCommandWithInputs(): void
$this->setInputs(['foo']);
$this->assertSame(['foo'], $this->getInputs());

$this->commandTester = $this->runCommand('liipfunctionaltestbundle:test');
$this->commandTester = $this->runCommand('liipfunctionaltestbundle:test:interactive');

$this->assertNull($this->getInputs());
$this->assertTrue($this->commandTester->getInput()->isInteractive());
$this->assertContains('Value of answer: foo', $this->commandTester->getDisplay());

// Run command again
$this->assertNull($this->getInputs());

$this->commandTester = $this->runCommand('liipfunctionaltestbundle:test:interactive');

$this->assertNull($this->getInputs());
$this->assertFalse($this->commandTester->getInput()->isInteractive());
// The default value is shown
$this->assertContains('Value of answer: AcmeDemoBundle', $this->commandTester->getDisplay());
}

public function testRunCommandWithoutOptionsAndNotReuseKernel(): void
Expand Down

0 comments on commit a47557f

Please sign in to comment.