2015-06-05 15 views
7

MonologBundle varsayılanı kullanılarak Symfony2 içinde Monolog kullanıyorum. Testlerimin içinde, bir satırın kaydedildiğini iddia etmeye çalışıyorum. Ben bu yapılandırılmış ettik benim config_test.yml:Symfony2 içinde Monolog kullanılarak bir satırın günlüğe kaydedileceği bildiriliyor

monolog: 
    handlers: 
     main: 
      type: test 
      level: debug 

nasıl Monolog en TestHandler (Symfony2'nin en WebTestCase devralan) benim testlerinde sonuçlarına alabilirim?

monolog hizmetinden tüm işleyicileri alın ve test işleyicisi arama: çözüm olarak

cevap

5

. Komut sınıfta

foreach ($this->container->get('monolog')->getHandlers() as $handler) { 
    if ($handler instanceof TestHandler) { 
    $testHandler = $handler; 
    break; 
    } 
} 

if (!$testHandler) { 
    throw new \RuntimeException('Oops, not exist "test" handler in monolog.'); 
} 

$this->assertFalse($testHandler->hasCritical()); // Or another assertions 
0

, 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.

+0

Bir komutta günlük satırlarını denemeye çalışmıyorum. Daha genel, genel bir çözüm takdir edilecektir. – hvtilborg

+0

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

İlgili konular