Created
July 26, 2012 09:22
-
-
Save Ziumin/3181180 to your computer and use it in GitHub Desktop.
[Symfony2] Mocking user input when testing commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace XXX\ToolkitBundle\Tests\Command; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
use Symfony\Component\Console\Tester\CommandTester; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use XXX\ToolkitBundle\Command\CommonGenerateCommand; | |
/** | |
* CommonGenerateCommand test case. | |
*/ | |
class CommonGenerateCommandTest extends WebTestCase | |
{ | |
/** | |
* Method for testing generate command | |
*/ | |
public function testExecute() | |
{ | |
$this->createClient(); | |
$application = new Application(self::$kernel); | |
$application->add(new CommonGenerateCommand()); | |
$command = $application->find('comm:generate'); | |
$this->mockCommandDialogHelper($command); | |
// Test command | |
$commandTester = new CommandTester($command); | |
$commandTester->execute(array('command' => $command->getName(), 'type' => 'service')); | |
// Some asserts... | |
} | |
/** | |
* Mocking command input (not to show command prompt) | |
* @param Command $command | |
*/ | |
private function mockCommandDialogHelper(Command $command) | |
{ | |
$dialog = $this->getMock('Symfony\Component\Console\Helper\DialogHelper', array('ask')); | |
// Answers | |
$dialog->expects($this->at(0)) | |
->method('ask') | |
->will($this->returnValue('Val1')); | |
$dialog->expects($this->at(1)) | |
->method('ask') | |
->will($this->returnValue('Val2')); | |
$dialog->expects($this->at(2)) | |
->method('ask') | |
->will($this->returnValue('Val3')); | |
$command->getHelperSet()->set($dialog, 'dialog'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment