/** * Use a writer with a passing filter */ public function testPassFilters() { $config = array('mute' => false, 'url' => 'php://memory', 'filters' => array(array('NoFilter' => array()))); $logEvent = LogEvent::create(ILogger::LEVEL_ERROR, 'Hello Streams', array(), 'PHPUnit'); $formatter = new Bunyan(); $writer = new Stream($config); $writer->log($logEvent); $stream = $writer->getStream(); fseek($stream, 0); $this->assertSame($formatter->format($logEvent), stream_get_contents($stream)); }
/** * php://memory doesn't support locking, but as flock() is a PHP core method * I don't really have to unit test it. I pretty much stole a demo wrapper from the PHP documentation * and made lie that it supports locking... */ public function testLogWithLock() { require_once __DIR__ . '/../StreamWrapper/VariableStream.php'; stream_wrapper_register("globalz", "Zalora\\Punyan\\StreamWrapper\\VariableStream"); $config = array('lock' => true, 'mute' => false, 'url' => 'globalz://foobar', 'filters' => array()); $logEvent = LogEvent::create(ILogger::LEVEL_ERROR, 'Hello Streams', array(), 'PHPUnit'); $formatter = new Bunyan(); $writer = new Stream($config); $writer->log($logEvent); $stream = $writer->getStream(); fseek($stream, 0); $this->assertSame($formatter->format($logEvent), stream_get_contents($stream)); }
/** * Here some mandatory fields for the Bunyan format are added. The other fields * which make up the format are tested in the LogEvent test */ public function testFormat() { $logEvent = LogEvent::create(ILogger::LEVEL_WARN, 'Hallo Test', array(), 'PHPUnit'); $formatter = new Bunyan(); $formattedString = $formatter->format($logEvent); // Must be well formed JSON $this->assertJson($formattedString); // Decode it and test if the fields the formatter added are present $logEventArray = json_decode($formattedString, true); $this->assertNotEmpty($logEventArray); $this->assertInternalType('array', $logEventArray); $this->assertArrayHasKey('hostname', $logEventArray); $this->assertArrayHasKey('pid', $logEventArray); $this->assertArrayHasKey('v', $logEventArray); }
/** * Test the fatal method */ public function testFatal() { $logger = $this->getMemoryLogger(); $time = time(); $stream = $this->getCurrentStreamFromLogger($logger); $formatter = new Bunyan(); $logEvent = LogEvent::create(ILogger::LEVEL_FATAL, 'PHPUnit', array('time' => $time), 'PHPUnit'); $logEvent['origin'] = Logger::getLogOrigin(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)); $logger->fatal('PHPUnit', array('time' => $time)); $streamContent = stream_get_contents($stream, -1, 0); $this->assertEquals(json_decode($formatter->format($logEvent), true), json_decode($streamContent, true)); }