W swojej klasie poleceń, trzeba po prostu ustawić obsługi z pushHandler()
:
namespace AppBundle\Command;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class YourCommand extends ContainerAwareCommand
{
// ...
protected function execute(InputInterface $input, OutputInterface $output)
{
$logger = $this->getContainer()->get('logger');
// PUSH THE OutputInterface OBJECT INTO MONOLOG
$logger->pushHandler(new ConsoleHandler($output));
// Your command logic here...
}
W swoim badaniu, przy użyciu CommandTester
:
namespace AppBundle\Tests\Command;
use AppBundle\Command\YourCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class YourCommandTest extends KernelTestCase
{
public function testExecute()
{
$kernel = $this->createKernel();
$kernel->boot();
// mock the Kernel or create one depending on your needs
$application = new Application($kernel);
$application->add(new YourCommand());
$command = $application->find('acme:your:command');
$commandTester = new CommandTester($command);
$commandTester->execute(
array('command' => $command->getName()),
/**
* Here set the verbosity
*/
array('verbosity' => OutputInterface::VERBOSITY_DEBUG)
);
// die(print_r($commandTester->getDisplay()));
$this->assertRegExp('/.../', $commandTester->getDisplay());
}
}
Przechowywać uwagę array('verbosity' => OutputInterface::VERBOSITY_DEBUG)
.
W ten sposób można uzyskać wszystkie dzienniki (a informacje w tym przypadku, należy ustawić z $logger->info('Starting <info>acme:your:command</info>');
):
[2015-08-13 23:39:22] app.INFO: Starting acme:your:command:
Teraz można użyć $this->assertRegExp()
aby sprawdzić, czy dana linia jest zalogowany czy nie.
Można również przekształcić string
w array
z
explode('\n', $commandTester->getDisplay())
tego roztworu found here i jest opisane w dokumentacji Monolog here.
Więcej o Monolog and Symfony (Symfony Docu).
Więcej o Monolog Handlers (Monolog Docu).
Nie próbowałem potwierdzać wierszy dziennika w komendzie. Rozumie się bardziej ogólne, ogólne rozwiązanie. – hvtilborg
Myślę, że nie rozumiem, co chciałbyś osiągnąć ... Czy możesz zaktualizować swoje pytanie bardziej szczegółowo? Opublikuj kod testu, abyśmy mogli zobaczyć, w jaki sposób go wdrożyłeś i podasz rozwiązanie. – Aerendir