Permalink
Browse files

Add test suite, exceptions, license

  • Loading branch information...
1 parent 72060c5 commit f5a5360635995046ce2f19b9715b634c8d97f331 @Seldaek Seldaek committed Nov 30, 2012
Showing with 138 additions and 0 deletions.
  1. +13 −0 LICENSE
  2. +7 −0 Psr/Log/Exception.php
  3. +7 −0 Psr/Log/InvalidArgumentException.php
  4. +111 −0 Psr/Log/LoggerInterfaceTest.php
View
13 LICENSE
@@ -0,0 +1,13 @@
+Copyright (c) 2012 PHP Framework Interoperability Group
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
@@ -0,0 +1,7 @@
+<?php
+
+namespace Psr\Log;
+
+interface Exception
+{
+}
@@ -0,0 +1,7 @@
+<?php
+
+namespace Psr\Log;
+
+class InvalidArgumentException extends \InvalidArgumentException implements Exception
+{
+}
@@ -0,0 +1,111 @@
+<?php
+
+namespace Psr\Log;
+
+/**
+ * Provides a base test class for ensuring compliance with the LoggerInterface
+ *
+ * Implementors can extend the class and implement abstract methods to run this as part of their test suite
+ */
+abstract class LoggerInterfaceTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @return LoggerInterface
+ */
+ abstract function getLogger();
+
+ /**
+ * This must return the log messages in order with a simple formatting: "<LOG LEVEL> <MESSAGE>"
+ *
+ * Example ->error('Foo') would yield "error Foo"
+ *
+ * @return string[]
+ */
+ abstract function getLogs();
+
+ public function testImplements()
+ {
+ $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
+ }
+
+ /**
+ * @dataProvider provideLevelsAndMessages
+ */
+ public function testLogsAtAllLevels($level, $message)
+ {
+ $logger = $this->getLogger();
+ $logger->{$level}($message, array('user' => 'Bob'));
+ $logger->log($level, $message, array('user' => 'Bob'));
+
+ $expected = array(
+ $level.' message of level '.$level.' with context: Bob',
+ $level.' message of level '.$level.' with context: Bob',
+ );
+ $this->assertEquals($expected, $this->getLogs());
+ }
+
+ public function provideLevelsAndMessages()
+ {
+ return array(
+ LoggerInterface::EMERGENCY => array(LoggerInterface::EMERGENCY, 'message of level emergency with context: %user%'),
+ LoggerInterface::ALERT => array(LoggerInterface::ALERT, 'message of level alert with context: %user%'),
+ LoggerInterface::CRITICAL => array(LoggerInterface::CRITICAL, 'message of level critical with context: %user%'),
+ LoggerInterface::ERROR => array(LoggerInterface::ERROR, 'message of level error with context: %user%'),
+ LoggerInterface::WARNING => array(LoggerInterface::WARNING, 'message of level warning with context: %user%'),
+ LoggerInterface::NOTICE => array(LoggerInterface::NOTICE, 'message of level notice with context: %user%'),
+ LoggerInterface::INFO => array(LoggerInterface::INFO, 'message of level info with context: %user%'),
+ LoggerInterface::DEBUG => array(LoggerInterface::DEBUG, 'message of level debug with context: %user%'),
+ );
+ }
+
+ public function testThrowsOnInvalidLevel()
@bobthecow

bobthecow Nov 30, 2012

Should expect an exception here?

@Seldaek

Seldaek Nov 30, 2012

Collaborator

Yeah I fixed it locally already, gotta finish up some stuff and push the latest ;)

+ {
+ $logger = $this->getLogger();
+ $logger->log('invalid level', 'Foo');
+ }
+
+ public function testContextReplacement()
+ {
+ $logger = $this->getLogger();
+ $logger->info('Message %nothing% %user% %foo.bar% 100%', array('user' => 'Bob', 'foo.bar' => 'Bar'));
+
+ $expected = array('info Message %nothing% Bob Bar 100%');
+ $this->assertEquals($expected, $this->getLogs());
+ }
+
+ public function testObjectCastToString()
+ {
+ $dummy = $this->getMock('Psr\Log\DummyTest', array('__toString'));
+ $dummy->expects($this->once())
+ ->method('__toString')
+ ->will($this->returnValue('DUMMY'));
+
+ $this->getLogger()->warning($dummy);
+ }
+
+ public function testContextCanContainAnything()
+ {
+ $context = array(
+ 'bool' => true,
+ 'null' => null,
+ 'string' => 'Foo',
+ 'int' => 0,
+ 'float' => 0.5,
+ 'nested' => array('with object' => new DummyTest),
+ 'object' => new \DateTime,
+ 'resource' => fopen('php://memory', 'r'),
+ );
+
+ $this->getLogger()->warning('Crazy context data', $context);
+ }
+
+ public function testContextExceptionKeyCanBeExceptionOrOtherValues()
+ {
+ $this->getLogger()->warning('Random message', array('exception' => 'oops'));
+ $this->getLogger()->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
+ }
+}
+
+class DummyTest
+{
+}

0 comments on commit f5a5360

Please sign in to comment.