, sadece pushHandler()
ile işleyicisi ayarlamak zorunda: CommandTester
kullanarak testte
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...
}
:
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());
}
}
array('verbosity' => OutputInterface::VERBOSITY_DEBUG)
dikkat edin. Tüm günlükleri ($logger->info('Starting <info>acme:your:command</info>');
seti bu durumda bir INFO) elde edebilirsiniz edeceğiz
Bu şekilde:
[2015-08-13 23:39:22] app.INFO: Starting acme:your:command:
Şimdi Belirli bir satır günlüğe olup olmadığını kontrol etmek için $this->assertRegExp()
kullanabilirsiniz.
Ayrıca bu çözüm found here ve Monolog here belgelerinde açıklanmıştır
explode('\n', $commandTester->getDisplay())
ile array
içinde string
dönüştürebilir.
Monolog and Symfony (Symfony Docu) hakkında daha fazla bilgi.
Monolog Handlers (Monolog Docu) hakkında daha fazla bilgi.
Bir komutta günlük satırlarını denemeye çalışmıyorum. Daha genel, genel bir çözüm takdir edilecektir. – hvtilborg
Neye ulaşmak istediğinizi anlamıyorum. Sorunuzu daha fazla ayrıntıyla güncelleyebilir misiniz? Testin kodunu yayınlayın, böylece nasıl uyguladığınızı görebilir ve size bir çözüm sunabiliriz. – Aerendir