2014-12-29 2 views
2

여기에 설명 된대로 CommandTester을 사용하여 심포니 명령을 테스트하고 싶습니다. http://symfony.com/doc/2.3/components/console/introduction.html#testing-commands.symfony 명령 테스트가 작동하지 않습니다.

테스트하려는 명령은 decision이 true로 설정된 레코드를 제거합니다. (아래의 코드에서 자세히) 검사가 실패하더라도

public function testMyCommand() 
{ 
    // ..... 
    // Updating the database 
    $res = $this->client->getContainer()->get('doctrine.orm.entity_manager')->createQueryBuilder() 
     ->update('MyBundle:MyEntity', 'me') 
     ->set('me.decision', true) 
     ->where('me.id = :id') 
     ->setParameter('id', 4); 

    // The command is meant to seek for all entities having *decision* set to true and delete them 
    $application = new Application($this->client->getKernel()); 
    $application->add(new CronQuotidienCommand()); 

    $command = $application->find('myproject:mycmd'); 
    $commandTester = new CommandTester($command); 
    $commandTester->execute(array('command' => $command->getName())); 

    $this->assertEmpty($this->client->getContainer()->get('doctrine')->getRepository('MyBundle:MyEntity')->findBy(array('decision' => true)); 
} 

$commandTester->execute(array('command' => $command->getName())); 어떤 MyEntity와을 찾지 못했음을 의미 true에 열 decision 세트와, 그 ID 4에 해당 한 MyEntity, 거기 decisiontrue (referesh matter?)으로 설정하십시오. 내가

shell_exec('php ' . $this->container->get('kernel')->getRootDir() . '/console myproject:mycmd'); 

이 작업을 수행하여

$application = new Application($this->client->getKernel()); 
    $application->add(new CronQuotidienCommand()); 

    $command = $application->find('myproject:mycmd'); 
    $commandTester = new CommandTester($command); 
    $commandTester->execute(array('command' => $command->getName())); 

를 교체 할 때

놀라운

입니다.

아이디어가 있으십니까? 당신은 시도 들으

+0

1)'$ 입술 = ....-> setParameter를 ('아이디', 4)',하지만 어디 thats 쿼리 실행입니까? 2) 당신은 $ em-> ur 명령에 변경이있을 때 호출되는 플러시입니까? 3) 이러한 기능 테스트는 시간 낭비이며, 단위 테스트를 선호한다. 4) shell_exec은 거의 항상 나쁜 생각이다. – Ziumin

답변

1

이나요 : 대신

use Symfony\Component\Console\Input\ArrayInput; 
use Symfony\Component\Console\Output\ConsoleOutput; 

$arguments = array(
    // ... 
); 
$input = new ArrayInput($arguments); 
$output = new ConsoleOutput(); 
$returnCode = $command->run($input, $output); 

:

$commandTester = new CommandTester($command); 
$commandTester->execute(array('command' => $command->getName())); 
+0

미안하지만 내 게시물에 오타가 있었다. – smarber

+0

내 대답은 여전히 ​​유효하다. – Gnucki

+0

id => 4는 테스트 데이터베이스에있는 유일한 요소이기 때문에 지정할 필요가 없습니다. – smarber

관련 문제