From b20f27cece61a38a2770a849c35572bac827135e Mon Sep 17 00:00:00 2001 From: Nikolaos Dimopoulos Date: Wed, 5 Dec 2018 22:12:22 -0500 Subject: [PATCH] [#13438] - Added more tests for formatters --- tests/unit/Logger/Adapter/File/AddCest.php | 37 +++++++++ .../unit/Logger/Formatter/Json/FormatCest.php | 8 +- .../Logger/Formatter/Json/InterpolateCest.php | 78 +++++++++++++++++++ .../unit/Logger/Formatter/Line/FormatCest.php | 8 +- .../Logger/Formatter/Line/InterpolateCest.php | 40 +++++++++- .../Logger/Formatter/Syslog/FormatCest.php | 47 +++++++++++ .../Formatter/Syslog/InterpolateCest.php | 75 ++++++++++++++++++ 7 files changed, 284 insertions(+), 9 deletions(-) create mode 100644 tests/unit/Logger/Adapter/File/AddCest.php create mode 100644 tests/unit/Logger/Formatter/Json/InterpolateCest.php create mode 100644 tests/unit/Logger/Formatter/Syslog/FormatCest.php create mode 100644 tests/unit/Logger/Formatter/Syslog/InterpolateCest.php diff --git a/tests/unit/Logger/Adapter/File/AddCest.php b/tests/unit/Logger/Adapter/File/AddCest.php new file mode 100644 index 00000000000..2f0c46874b1 --- /dev/null +++ b/tests/unit/Logger/Adapter/File/AddCest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Logger\Adapter\File; + +use UnitTester; + +/** + * Class AddCest + * + * @package Phalcon\Test\Unit\Logger + */ +class AddCest +{ + /** + * Tests Phalcon\Logger\Adapter\File :: add() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function loggerAdapterFileAdd(UnitTester $I) + { + $I->wantToTest("Logger\Adapter\File - add()"); + $I->skipTest("Need implementation"); + } +} diff --git a/tests/unit/Logger/Formatter/Json/FormatCest.php b/tests/unit/Logger/Formatter/Json/FormatCest.php index 6f05fd89f62..3752e01b66a 100644 --- a/tests/unit/Logger/Formatter/Json/FormatCest.php +++ b/tests/unit/Logger/Formatter/Json/FormatCest.php @@ -35,7 +35,7 @@ class FormatCest public function loggerFormatterJsonFormat(UnitTester $I) { $I->wantToTest("Logger\Formatter\Json - format()"); - $line = new Json(); + $formatter = new Json(); $time = time(); $item = new Item('log message', 'debug', Logger::DEBUG, $time); @@ -44,7 +44,7 @@ public function loggerFormatterJsonFormat(UnitTester $I) '{"type":"debug","message":"log message","timestamp":"%s"}', date('D, d M y H:i:s O', $time) ); - $actual = $line->format($item); + $actual = $formatter->format($item); $I->assertEquals($expected, $actual); } @@ -59,7 +59,7 @@ public function loggerFormatterJsonFormat(UnitTester $I) public function loggerFormatterJsonFormatCustom(UnitTester $I) { $I->wantToTest("Logger\Formatter\Json - format() - custom"); - $line = new Json('YmdHis'); + $formatter = new Json('YmdHis'); $time = time(); $item = new Item('log message', 'debug', Logger::DEBUG, $time); @@ -68,7 +68,7 @@ public function loggerFormatterJsonFormatCustom(UnitTester $I) '{"type":"debug","message":"log message","timestamp":"%s"}', date('YmdHis', $time) ); - $actual = $line->format($item); + $actual = $formatter->format($item); $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Logger/Formatter/Json/InterpolateCest.php b/tests/unit/Logger/Formatter/Json/InterpolateCest.php new file mode 100644 index 00000000000..38baae84eb6 --- /dev/null +++ b/tests/unit/Logger/Formatter/Json/InterpolateCest.php @@ -0,0 +1,78 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Logger\Formatter\Json; + +use Phalcon\Logger; +use Phalcon\Logger\Formatter\Json; +use Phalcon\Logger\Item; +use UnitTester; + +/** + * Class InterpolateCest + * + * @package Phalcon\Test\Unit\Logger + */ +class InterpolateCest +{ + /** + * Tests Phalcon\Logger\Formatter\Json :: interpolate() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function loggerFormatterJsonInterpolate(UnitTester $I) + { + $I->wantToTest("Logger\Formatter\Json - interpolate()"); + $formatter = new Json(); + + $message = 'The sky is {color}'; + $context = [ + 'color' => 'blue', + ]; + + $expected = 'The sky is blue'; + $actual = $formatter->interpolate($message, $context); + $I->assertEquals($expected, $actual); + } + + /** + * Tests Phalcon\Logger\Formatter\Json :: interpolate() - format + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function loggerFormatterJsonInterpolateFormat(UnitTester $I) + { + $I->wantToTest("Logger\Formatter\Json - interpolate() - format()"); + $formatter = new Json(); + + $message = 'The sky is {color}'; + $context = [ + 'color' => 'blue', + ]; + + $time = time(); + $item = new Item($message, 'debug', Logger::DEBUG, $time, $context); + + $expected = sprintf( + '{"type":"debug","message":"The sky is blue","timestamp":"%s"}', + date('D, d M y H:i:s O', $time) + ); + $actual = $formatter->format($item); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/unit/Logger/Formatter/Line/FormatCest.php b/tests/unit/Logger/Formatter/Line/FormatCest.php index 9eb20553eb4..9670cfc4c30 100644 --- a/tests/unit/Logger/Formatter/Line/FormatCest.php +++ b/tests/unit/Logger/Formatter/Line/FormatCest.php @@ -36,13 +36,13 @@ class FormatCest public function loggerFormatterLineFormat(UnitTester $I) { $I->wantToTest("Logger\Formatter\Line - format()"); - $line = new Line(); + $formatter = new Line(); $time = time(); $item = new Item('log message', 'debug', Logger::DEBUG, $time); $expected = sprintf('[%s][debug] log message', date('D, d M y H:i:s O', $time)) . PHP_EOL; - $actual = $line->format($item); + $actual = $formatter->format($item); $I->assertEquals($expected, $actual); } @@ -57,13 +57,13 @@ public function loggerFormatterLineFormat(UnitTester $I) public function loggerFormatterLineFormatCustom(UnitTester $I) { $I->wantToTest("Logger\Formatter\Line - format() - custom"); - $line = new Line('%message%-[%type%]-%date%'); + $formatter = new Line('%message%-[%type%]-%date%'); $time = time(); $item = new Item('log message', 'debug', Logger::DEBUG, $time); $expected = sprintf('log message-[debug]-%s', date('D, d M y H:i:s O', $time)) . PHP_EOL; - $actual = $line->format($item); + $actual = $formatter->format($item); $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Logger/Formatter/Line/InterpolateCest.php b/tests/unit/Logger/Formatter/Line/InterpolateCest.php index 07aa4ce92a8..d017a608021 100644 --- a/tests/unit/Logger/Formatter/Line/InterpolateCest.php +++ b/tests/unit/Logger/Formatter/Line/InterpolateCest.php @@ -12,6 +12,9 @@ namespace Phalcon\Test\Unit\Logger\Formatter\Line; +use Phalcon\Logger; +use Phalcon\Logger\Formatter\Line; +use Phalcon\Logger\Item; use UnitTester; /** @@ -32,6 +35,41 @@ class InterpolateCest public function loggerFormatterLineInterpolate(UnitTester $I) { $I->wantToTest("Logger\Formatter\Line - interpolate()"); - $I->skipTest("Need implementation"); + $formatter = new Line(); + + $message = 'The sky is {color}'; + $context = [ + 'color' => 'blue', + ]; + + $expected = 'The sky is blue'; + $actual = $formatter->interpolate($message, $context); + $I->assertEquals($expected, $actual); + } + + /** + * Tests Phalcon\Logger\Formatter\Line :: interpolate() - format + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function loggerFormatterLineInterpolateFormat(UnitTester $I) + { + $I->wantToTest("Logger\Formatter\Line - interpolate() - format()"); + $formatter = new Line(); + + $message = 'The sky is {color}'; + $context = [ + 'color' => 'blue', + ]; + + $time = time(); + $item = new Item($message, 'debug', Logger::DEBUG, $time, $context); + + $expected = sprintf('[%s][debug] The sky is blue', date('D, d M y H:i:s O', $time)) . PHP_EOL; + $actual = $formatter->format($item); + $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Logger/Formatter/Syslog/FormatCest.php b/tests/unit/Logger/Formatter/Syslog/FormatCest.php new file mode 100644 index 00000000000..973b9ba1f69 --- /dev/null +++ b/tests/unit/Logger/Formatter/Syslog/FormatCest.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Logger\Formatter\Syslog; + +use Phalcon\Logger; +use Phalcon\Logger\Formatter\Syslog; +use Phalcon\Logger\Item; +use UnitTester; + +/** + * Class FormatCest + * + * @package Phalcon\Test\Unit\Logger + */ +class FormatCest +{ + /** + * Tests Phalcon\Logger\Formatter\Syslog :: format() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function loggerFormatterSyslogFormat(UnitTester $I) + { + $I->wantToTest("Logger\Formatter\Syslog - format()"); + $formatter = new Syslog(); + + $time = time(); + $item = new Item('log message', 'debug', Logger::DEBUG, $time); + + $expected = [Logger::DEBUG, 'log message']; + $actual = $formatter->format($item); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/unit/Logger/Formatter/Syslog/InterpolateCest.php b/tests/unit/Logger/Formatter/Syslog/InterpolateCest.php new file mode 100644 index 00000000000..7b622f85e54 --- /dev/null +++ b/tests/unit/Logger/Formatter/Syslog/InterpolateCest.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Logger\Formatter\Syslog; + +use Phalcon\Logger; +use Phalcon\Logger\Formatter\Syslog; +use Phalcon\Logger\Item; +use UnitTester; + +/** + * Class InterpolateCest + * + * @package Phalcon\Test\Unit\Logger + */ +class InterpolateCest +{ + /** + * Tests Phalcon\Logger\Formatter\Syslog :: interpolate() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function loggerFormatterSyslogInterpolate(UnitTester $I) + { + $I->wantToTest("Logger\Formatter\Syslog - interpolate()"); + $formatter = new Syslog(); + + $message = 'The sky is {color}'; + $context = [ + 'color' => 'blue', + ]; + + $expected = 'The sky is blue'; + $actual = $formatter->interpolate($message, $context); + $I->assertEquals($expected, $actual); + } + + /** + * Tests Phalcon\Logger\Formatter\Syslog :: interpolate() - format + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function loggerFormatterSyslogInterpolateFormat(UnitTester $I) + { + $I->wantToTest("Logger\Formatter\Syslog - interpolate() - format()"); + $formatter = new Syslog(); + + $message = 'The sky is {color}'; + $context = [ + 'color' => 'blue', + ]; + + $time = time(); + $item = new Item($message, 'debug', Logger::DEBUG, $time, $context); + + $expected = [Logger::DEBUG, 'The sky is blue']; + $actual = $formatter->format($item); + $I->assertEquals($expected, $actual); + } +}