|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MonologBundle\Tests\Handler; |
| 13 | + |
| 14 | +use Symfony\Bridge\Monolog\Logger; |
| 15 | +use Symfony\Bundle\MonologBundle\Handler\ConsoleHandler; |
| 16 | +use Symfony\Bundle\MonologBundle\Tests\TestCase; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests the ConsoleHandler and also the ConsoleFormatter. |
| 21 | + * |
| 22 | + * @author Tobias Schultze <http://tobion.de> |
| 23 | + */ |
| 24 | +class ConsoleHandlerTest extends TestCase |
| 25 | +{ |
| 26 | + public function testConstructor() |
| 27 | + { |
| 28 | + $handler = new ConsoleHandler(false); |
| 29 | + $this->assertFalse($handler->getBubble(), 'the bubble parameter gets propagated'); |
| 30 | + } |
| 31 | + |
| 32 | + public function testIsHandling() |
| 33 | + { |
| 34 | + $handler = new ConsoleHandler(); |
| 35 | + $this->assertFalse($handler->isHandling(array()), '->isHandling returns false when no output is set'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @dataProvider provideVerbosityMappingTests |
| 40 | + */ |
| 41 | + public function testVerbosityMapping($verbosity, $level, $isHandling) |
| 42 | + { |
| 43 | + $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); |
| 44 | + $output |
| 45 | + ->expects($this->atLeastOnce()) |
| 46 | + ->method('getVerbosity') |
| 47 | + ->will($this->returnValue($verbosity)) |
| 48 | + ; |
| 49 | + $handler = new ConsoleHandler(); |
| 50 | + $handler->setOutput($output); |
| 51 | + $this->assertSame($isHandling, $handler->isHandling(array('level' => $level)), |
| 52 | + '->isHandling returns correct value depending on console verbosity and log level' |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + public function provideVerbosityMappingTests() |
| 57 | + { |
| 58 | + return array( |
| 59 | + array(OutputInterface::VERBOSITY_QUIET, Logger::ERROR, false), |
| 60 | + array(OutputInterface::VERBOSITY_NORMAL, Logger::WARNING, true), |
| 61 | + array(OutputInterface::VERBOSITY_NORMAL, Logger::NOTICE, false), |
| 62 | + array(OutputInterface::VERBOSITY_VERBOSE, Logger::NOTICE, true), |
| 63 | + array(OutputInterface::VERBOSITY_VERBOSE, Logger::INFO, false), |
| 64 | + array(OutputInterface::VERBOSITY_VERY_VERBOSE, Logger::INFO, true), |
| 65 | + array(OutputInterface::VERBOSITY_VERY_VERBOSE, Logger::DEBUG, false), |
| 66 | + array(OutputInterface::VERBOSITY_DEBUG, Logger::DEBUG, true), |
| 67 | + array(OutputInterface::VERBOSITY_DEBUG, Logger::EMERGENCY, true), |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function testVerbosityChanged() |
| 72 | + { |
| 73 | + $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); |
| 74 | + $output |
| 75 | + ->expects($this->at(0)) |
| 76 | + ->method('getVerbosity') |
| 77 | + ->will($this->returnValue(OutputInterface::VERBOSITY_QUIET)) |
| 78 | + ; |
| 79 | + $output |
| 80 | + ->expects($this->at(1)) |
| 81 | + ->method('getVerbosity') |
| 82 | + ->will($this->returnValue(OutputInterface::VERBOSITY_DEBUG)) |
| 83 | + ; |
| 84 | + $handler = new ConsoleHandler(); |
| 85 | + $handler->setOutput($output); |
| 86 | + $this->assertFalse($handler->isHandling(array('level' => Logger::NOTICE)), |
| 87 | + 'when verbosity is set to quiet, the handler does not handle the log' |
| 88 | + ); |
| 89 | + $this->assertTrue($handler->isHandling(array('level' => Logger::NOTICE)), |
| 90 | + 'since the verbosity of the output increased externally, the handler is now handling the log' |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + public function testGetFormatter() |
| 95 | + { |
| 96 | + $handler = new ConsoleHandler(); |
| 97 | + $this->assertInstanceOf('Symfony\Bundle\MonologBundle\Formatter\ConsoleFormatter', $handler->getFormatter(), |
| 98 | + '-getFormatter returns ConsoleFormatter by default' |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function testWritingAndFormatting() |
| 103 | + { |
| 104 | + $output = $this->getMock('Symfony\Component\Console\Output\ConsoleOutputInterface'); |
| 105 | + $output |
| 106 | + ->expects($this->any()) |
| 107 | + ->method('getVerbosity') |
| 108 | + ->will($this->returnValue(OutputInterface::VERBOSITY_DEBUG)) |
| 109 | + ; |
| 110 | + $output |
| 111 | + ->expects($this->once()) |
| 112 | + ->method('write') |
| 113 | + ->with('<info>[2013-05-29 16:21:54] app.INFO:</info> My info message [] []'."\n") |
| 114 | + ; |
| 115 | + |
| 116 | + $errorOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); |
| 117 | + $errorOutput |
| 118 | + ->expects($this->once()) |
| 119 | + ->method('write') |
| 120 | + ->with('<error>[2013-05-29 16:21:54] app.ERROR:</error> My error message [] []'."\n") |
| 121 | + ; |
| 122 | + |
| 123 | + $output |
| 124 | + ->expects($this->any()) |
| 125 | + ->method('getErrorOutput') |
| 126 | + ->will($this->returnValue($errorOutput)) |
| 127 | + ; |
| 128 | + |
| 129 | + $handler = new ConsoleHandler(false); |
| 130 | + $handler->setOutput($output); |
| 131 | + |
| 132 | + $infoRecord = array( |
| 133 | + 'message' => 'My info message', |
| 134 | + 'context' => array(), |
| 135 | + 'level' => Logger::INFO, |
| 136 | + 'level_name' => Logger::getLevelName(Logger::INFO), |
| 137 | + 'channel' => 'app', |
| 138 | + 'datetime' => new \DateTime('2013-05-29 16:21:54'), |
| 139 | + 'extra' => array(), |
| 140 | + ); |
| 141 | + |
| 142 | + $this->assertTrue($handler->handle($infoRecord), 'The handler finished handling the log as bubble is false.'); |
| 143 | + |
| 144 | + $errorRecord = array( |
| 145 | + 'message' => 'My error message', |
| 146 | + 'context' => array(), |
| 147 | + 'level' => Logger::ERROR, |
| 148 | + 'level_name' => Logger::getLevelName(Logger::ERROR), |
| 149 | + 'channel' => 'app', |
| 150 | + 'datetime' => new \DateTime('2013-05-29 16:21:54'), |
| 151 | + 'extra' => array(), |
| 152 | + ); |
| 153 | + |
| 154 | + $this->assertTrue($handler->handle($errorRecord), 'The handler finished handling the log as bubble is false.'); |
| 155 | + } |
| 156 | +} |
0 commit comments