From c57dfc631caf98df46ac014812f962863f0bfa61 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 19:50:59 +0200 Subject: engelsystem_provider should not be loaded in phpunit, changed dir structure --- tests/Feature/Logger/EngelsystemLoggerTest.php | 136 +++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/Feature/Logger/EngelsystemLoggerTest.php (limited to 'tests/Feature/Logger/EngelsystemLoggerTest.php') diff --git a/tests/Feature/Logger/EngelsystemLoggerTest.php b/tests/Feature/Logger/EngelsystemLoggerTest.php new file mode 100644 index 00000000..3b6572f5 --- /dev/null +++ b/tests/Feature/Logger/EngelsystemLoggerTest.php @@ -0,0 +1,136 @@ +assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); + } + + /** + * @dataProvider provideLogLevels + * @param string $level + */ + public function testAllLevels($level) + { + $logger = $this->getLogger(); + + LogEntries_clear_all(); + + $logger->log($level, 'First log message'); + $logger->{$level}('Second log message'); + + $entries = LogEntries(); + $this->assertCount(2, $entries); + } + + /** + * @return string[] + */ + public function provideLogLevels() + { + return [ + [LogLevel::ALERT], + [LogLevel::CRITICAL], + [LogLevel::DEBUG], + [LogLevel::EMERGENCY], + [LogLevel::ERROR], + [LogLevel::INFO], + [LogLevel::NOTICE], + [LogLevel::WARNING], + ]; + } + + public function testContextReplacement() + { + $logger = $this->getLogger(); + LogEntries_clear_all(); + + $logger->log(LogLevel::INFO, 'My username is {username}', ['username' => 'Foo']); + + $entry = $this->getLastEntry(); + $this->assertEquals('My username is Foo', $entry['message']); + $this->assertEquals(LogLevel::INFO, $entry['level']); + + foreach ( + [ + ['Data and {context}', []], + ['Data and ', ['context' => null]], + ['Data and {context}', ['context' => new \stdClass()]], + ] as $data + ) { + list($result, $context) = $data; + + $logger->log(LogLevel::INFO, 'Data and {context}', $context); + + $entry = $this->getLastEntry(); + $this->assertEquals($result, $entry['message']); + } + } + + public function testContextToString() + { + $logger = $this->getLogger(); + LogEntries_clear_all(); + + $mock = $this->getMockBuilder('someDataProvider') + ->setMethods(['__toString']) + ->getMock(); + + $mock->expects($this->atLeastOnce()) + ->method('__toString') + ->will($this->returnValue('FooBar')); + + $logger->log(LogLevel::INFO, 'Some data and {context}', ['context' => $mock]); + + $entry = $this->getLastEntry(); + $this->assertEquals('Some data and FooBar', $entry['message']); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testThrowExceptionOnInvalidLevel() + { + $logger = $this->getLogger(); + + $logger->log('This log level should never be defined', 'Some message'); + } + + /** + * @return array + */ + public function getLastEntry() + { + $entries = LogEntries(); + $entry = array_pop($entries); + + return $entry; + } + + public function tearDown() + { + LogEntries_clear_all(); + } +} -- cgit v1.2.3-54-g00ecf From c35c4ccbf39a5910530d6cf9b4f5dba45dbf82b0 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 19 Sep 2017 20:40:48 +0200 Subject: Refactoring: Logger test cleanup --- tests/Feature/Logger/EngelsystemLoggerTest.php | 72 +++++++++++++++----------- 1 file changed, 43 insertions(+), 29 deletions(-) (limited to 'tests/Feature/Logger/EngelsystemLoggerTest.php') diff --git a/tests/Feature/Logger/EngelsystemLoggerTest.php b/tests/Feature/Logger/EngelsystemLoggerTest.php index 3b6572f5..63a01318 100644 --- a/tests/Feature/Logger/EngelsystemLoggerTest.php +++ b/tests/Feature/Logger/EngelsystemLoggerTest.php @@ -28,23 +28,6 @@ class EngelsystemLoggerTest extends TestCase $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger()); } - /** - * @dataProvider provideLogLevels - * @param string $level - */ - public function testAllLevels($level) - { - $logger = $this->getLogger(); - - LogEntries_clear_all(); - - $logger->log($level, 'First log message'); - $logger->{$level}('Second log message'); - - $entries = LogEntries(); - $this->assertCount(2, $entries); - } - /** * @return string[] */ @@ -62,6 +45,23 @@ class EngelsystemLoggerTest extends TestCase ]; } + /** + * @dataProvider provideLogLevels + * @param string $level + */ + public function testAllLevels($level) + { + $logger = $this->getLogger(); + + LogEntries_clear_all(); + + $logger->log($level, 'First log message'); + $logger->{$level}('Second log message'); + + $entries = LogEntries(); + $this->assertCount(2, $entries); + } + public function testContextReplacement() { $logger = $this->getLogger(); @@ -72,21 +72,35 @@ class EngelsystemLoggerTest extends TestCase $entry = $this->getLastEntry(); $this->assertEquals('My username is Foo', $entry['message']); $this->assertEquals(LogLevel::INFO, $entry['level']); + } - foreach ( - [ - ['Data and {context}', []], - ['Data and ', ['context' => null]], - ['Data and {context}', ['context' => new \stdClass()]], - ] as $data - ) { - list($result, $context) = $data; + /** + * @return string[] + */ + public function provideContextReplaceValues() + { + return [ + ['Data and {context}', [], 'Data and {context}'], + ['Data and {context}', ['context' => null], 'Data and '], + ['Data and {context}', ['context' => new \stdClass()], 'Data and {context}'], + ['Some user asked: {question}', ['question' => 'Foo?'], 'Some user asked: Foo?'], + ]; + } - $logger->log(LogLevel::INFO, 'Data and {context}', $context); + /** + * @dataProvider provideContextReplaceValues + * + * @param string $message + * @param string[] $context + * @param string $expected + */ + public function testContextReplaceValues($message, $context, $expected) + { + $logger = $this->getLogger(); + $logger->log(LogLevel::INFO, $message, $context); - $entry = $this->getLastEntry(); - $this->assertEquals($result, $entry['message']); - } + $entry = $this->getLastEntry(); + $this->assertEquals($expected, $entry['message']); } public function testContextToString() -- cgit v1.2.3-54-g00ecf