From e561845b944f45a05658eebdd2fe6407b2620a82 Mon Sep 17 00:00:00 2001 From: 8ctopus Date: Tue, 6 Sep 2022 22:09:44 +0500 Subject: [PATCH] php >= 8.0 compatibility (#7) * Update composer minimum php >= 8.0 Update composer phpunit versions Update composer psr log to v2 or v3 Fix usort() returning bool from comparison function is deprecated Fix phpunit 8 tests Fix phpunit 8 deprecated tests Fix phpunit 8 the attribute 'syntaxCheck' is not allowed Fix tests need build dir Fix readme examples * Add github workflow * Fix build dir must be present for tests to succeed * Updated php versions for automated testing * Fix tests --- .github/workflows/ci.yaml | 3 ++- .gitignore | 1 + README.md | 9 +++---- composer.json | 7 +++-- phpunit.xml.dist | 49 ++++++++++++----------------------- src/Logger.php | 2 +- src/Logger/AbstractLogger.php | 21 ++++++++++++++- tests/FunctionalTest.php | 5 ++-- tests/InterfacesTest.php | 8 +++--- tests/Logger/ErrorLogTest.php | 24 +++++++++-------- tests/Logger/FileTest.php | 38 ++++++++++++--------------- tests/Logger/MailTest.php | 23 +++++++--------- tests/Logger/RuntimeTest.php | 10 +++---- tests/Logger/StreamTest.php | 21 +++++++-------- tests/Logger/TestCase.php | 4 +-- tests/LoggerTest.php | 47 +++++++++++++-------------------- 16 files changed, 127 insertions(+), 145 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1dd12a2..525507f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,7 +16,8 @@ jobs: strategy: matrix: php-version: - - "5.3" + - "8.0" + - "8.1" dependencies: - "highest" diff --git a/.gitignore b/.gitignore index cbf3145..40da81c 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ composer.phar /build/ phpunit.xml TODO +.phpunit.result.cache diff --git a/README.md b/README.md index f3be58c..6feff9d 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,8 @@ Feel free to comment, send pull requests and patches... Basic usage ~ *standalone* ----------- ```php -use Apix\Log; -$urgent_logger = new Logger\Mail('franck@foo.bar'); +$urgent_logger = new Apix\Log\Logger\Mail('franck@foo.bar'); $urgent_logger->setMinLevel('critical'); // catch logs >= to `critical` ``` @@ -49,7 +48,7 @@ Advanced usage ~ *multi-logs dispatcher* -------------- Lets create an additional logger with purpose of catching log entries that have a severity level of `warning` or more -- see the [log levels](#log-levels) for the order. ```php -$app_logger = new Logger\File('/var/log/apix_app.log'); +$app_logger = new Apix\Log\Logger\File('/var/log/apix_app.log'); $app_logger->setMinLevel('warning') // intercept logs that are >= `warning` ->setCascading(false) // don't propagate to further buckets ->setDeferred(true); // postpone/accumulate logs processing @@ -59,13 +58,13 @@ $app_logger->setMinLevel('warning') // intercept logs that are >= `warning` Now, lets create a main logger object and inject the two previous loggers. ```php // The main logger object (injecting an array of loggers) -$logger = new Logger( array($urgent_logger, $app_logger) ); +$logger = new Apix\Log\Logger( array($urgent_logger, $app_logger) ); ``` Lets create an additional logger -- just for development/debug purposes. ```php if(DEBUG) { // Bucket for the remaining logs -- i.e. `notice`, `info` and `debug` - $dev_logger = new Logger\Stream(); // default to screen without output buffer + $dev_logger = new Apix\Log\Logger\Stream(); // default to screen without output buffer // $dev_logger = new Logger\File('/tmp/apix_debug.log'); $dev_logger->setMinLevel('debug'); diff --git a/composer.json b/composer.json index f2465be..ac03dcf 100644 --- a/composer.json +++ b/composer.json @@ -19,12 +19,11 @@ "irc": "irc://irc.freenode.org/ouarz" }, "require": { - "php": ">=5.3", - "psr/log": "~1.0" + "php": ">=8.0", + "psr/log": "^2.0 || ^3.0" }, "require-dev": { - "phpunit/phpunit": "^4.0|^5.0", - "satooshi/php-coveralls": "~0.7.1" + "phpunit/phpunit": "^8.0|^9.0" }, "suggest": { "PHPMailer/apix-log-phpmailer": "Allow sending log messages via PHPMailer", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 5ac58ed..70be2bf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,34 +1,19 @@ - - - - - tests - - - - - - ./ - - build - tests - vendor - - - - + + + + ./ + + + build + tests + vendor + + + + + tests + + - - \ No newline at end of file + diff --git a/src/Logger.php b/src/Logger.php index 4ac31dd..14fd748 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -138,7 +138,7 @@ protected function sortBuckets() { return usort( $this->buckets, function ($a, $b) { - return $a->getMinLevel() > $b->getMinLevel(); + return $a->getMinLevel() - $b->getMinLevel(); } ); } diff --git a/src/Logger/AbstractLogger.php b/src/Logger/AbstractLogger.php index cf058c6..40a25dc 100644 --- a/src/Logger/AbstractLogger.php +++ b/src/Logger/AbstractLogger.php @@ -12,6 +12,7 @@ use Psr\Log\AbstractLogger as PsrAbstractLogger; use Psr\Log\InvalidArgumentException; +use Stringable; use Apix\Log\LogEntry; use Apix\Log\LogFormatter; @@ -95,7 +96,7 @@ public static function getLevelCode($level_name) /** * {@inheritdoc} */ - public function log($level, $message, array $context = array()) + public function log($level, Stringable|string $message, array $context = array()) : void { $entry = new LogEntry($level, $message, $context); $entry->setFormatter($this->getLogFormatter()); @@ -180,6 +181,15 @@ public function setCascading($bool) return $this; } + /** + * Get cascading property + * @return bool + */ + public function cascading() + { + return $this->cascading; + } + /** * Sets wether to enable/disable log deferring. * @@ -193,6 +203,15 @@ public function setDeferred($bool) return $this; } + /** + * Get deferred property + * @return bool + */ + public function deferred() + { + return $this->deferred; + } + /** * Returns all the deferred logs. * diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index caad9da..51a9bc0 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -14,7 +14,7 @@ use Apix\Log\Logger; -class ReadmeTest extends \PHPUnit_Framework_TestCase +class ReadmeTest extends \PHPUnit\Framework\TestCase { /** @@ -119,5 +119,4 @@ public function testUsages() $this->getLogs($debug_logger) ); } - -} \ No newline at end of file +} diff --git a/tests/InterfacesTest.php b/tests/InterfacesTest.php index 80f57f5..30f27f1 100644 --- a/tests/InterfacesTest.php +++ b/tests/InterfacesTest.php @@ -44,16 +44,16 @@ public function format(LogEntry $log) } } -class InterfacesTest extends \PHPUnit_Framework_TestCase +class InterfacesTest extends \PHPUnit\Framework\TestCase { protected $logger; - protected function setUp() + protected function setUp() : void { $this->logger = new StandardOutput(); } - protected function tearDown() + protected function tearDown() : void { unset($this->logger); } @@ -83,4 +83,4 @@ public function testLogFormatterInterfaceExample() '@\{"timestamp":.*\,"name":"error"\,"level_code":3\,"message":"hello world","context":\{"who":"world"\}\,"formatter":\{"separator":"~"\}\}@' ); } -} \ No newline at end of file +} diff --git a/tests/Logger/ErrorLogTest.php b/tests/Logger/ErrorLogTest.php index 7745867..00d6697 100644 --- a/tests/Logger/ErrorLogTest.php +++ b/tests/Logger/ErrorLogTest.php @@ -12,12 +12,11 @@ use Apix\Log\Logger; -class ErrorLogTest extends TestCase +class ErrorLogTest extends \PHPUnit\Framework\TestCase { + protected $dest = 'test'; - // protected $dest = '/dev/stdout'; - - protected function setUp() + protected function setUp() : void { // HHVM support // @see: https://github.com/facebook/hhvm/issues/3558 @@ -29,19 +28,22 @@ protected function setUp() ini_set('error_log', $this->dest); } - protected function tearDown() + protected function tearDown() : void { if (file_exists($this->dest)) { unlink($this->dest); } } - /** - * {@inheritDoc} - */ - public function getLogger() + public function testWrite() { - return new Logger\ErrorLog(); - } + $logger = new Logger\ErrorLog(); + + $message = 'test log'; + $logger->debug($message); + $content = file_get_contents($this->dest); + + $this->assertStringContainsString($message, $content); + } } diff --git a/tests/Logger/FileTest.php b/tests/Logger/FileTest.php index 9a8e6ee..e53f124 100644 --- a/tests/Logger/FileTest.php +++ b/tests/Logger/FileTest.php @@ -11,43 +11,37 @@ namespace Apix\Log\tests\Logger; use Apix\Log\Logger; +use Psr\Log\InvalidArgumentException; -class FileTest extends TestCase +class FileTest extends \PHPUnit\Framework\TestCase { + protected $dest = 'test'; - protected function tearDown() + protected function tearDown() : void { if (file_exists($this->dest)) { + chmod($this->dest, 0777); unlink($this->dest); } } - /** - * {@inheritDoc} - */ - public function getLogger() - { - return new Logger\File($this->dest); - } - - /** - * @expectedException Psr\Log\InvalidArgumentException - * @expectedExceptionMessage Log file "" cannot be created - * @expectedExceptionCode 1 - */ public function testThrowsInvalidArgumentExceptionWhenFileCannotBeCreated() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Log file "" cannot be created'); + $this->expectExceptionCode(1); new Logger\File(null); } - /** - * @expectedException Psr\Log\InvalidArgumentException - * @expectedExceptionMessage Log file "/" is not writable - * @expectedExceptionCode 2 - */ public function testThrowsInvalidArgumentExceptionWhenNotWritable() { - new Logger\File('/'); - } + touch($this->dest); + chmod($this->dest, 0000); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Log file \"{$this->dest}\" is not writable"); + $this->expectExceptionCode(2); + new Logger\File($this->dest); + } } diff --git a/tests/Logger/MailTest.php b/tests/Logger/MailTest.php index da7efe1..8148d4a 100644 --- a/tests/Logger/MailTest.php +++ b/tests/Logger/MailTest.php @@ -8,34 +8,31 @@ * @license http://opensource.org/licenses/BSD-3-Clause New BSD License */ -namespace Apix\Log\tests\Logger; +namespace Apix\Log; -use Apix\Log\Logger; +use Apix\Log; +use Psr\Log\InvalidArgumentException; +use PHPUnit\Framework\Assert; -class MailTest extends \PHPUnit_Framework_TestCase +class MailTest extends \PHPUnit\Framework\TestCase { - - /** - * @expectedException Psr\Log\InvalidArgumentException - * @expectedExceptionMessage "" is an invalid email address - */ public function testThrowsInvalidArgumentExceptionWhenNull() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('"" is an invalid email address'); new Logger\Mail(null); } - /** - * @expectedException Psr\Log\InvalidArgumentException - * @expectedExceptionMessage "foo" is an invalid email address - */ public function testThrowsInvalidArgumentException() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('"foo" is an invalid email address'); new Logger\Mail('foo'); } public function testConstructor() { new Logger\Mail('foo@bar.com', 'CC: some@somewhere.com'); + $this->assertTrue(true); } - } diff --git a/tests/Logger/RuntimeTest.php b/tests/Logger/RuntimeTest.php index f80016c..a1e6cf7 100644 --- a/tests/Logger/RuntimeTest.php +++ b/tests/Logger/RuntimeTest.php @@ -9,18 +9,19 @@ namespace Apix\Log\tests\Logger; +use Apix\Log\tests\Logger\TestCase; use Apix\Log\Logger; -class RuntimeTest extends TestCase +class RuntimeTest extends \PHPUnit\Framework\TestCase { protected $logger; - protected function setUp() + protected function setUp() : void { $this->logger = new Logger\Runtime(); } - protected function tearDown() + protected function tearDown() : void { unset($this->logger); } @@ -30,7 +31,7 @@ protected function tearDown() */ public function getLogs() { - return self::normalizeLogs($this->logger->getItems()); + return TestCase::normalizeLogs($this->logger->getItems()); } /** @@ -52,5 +53,4 @@ public function testAbstractLogger() $this->getLogs() ); } - } diff --git a/tests/Logger/StreamTest.php b/tests/Logger/StreamTest.php index a0ce65a..edfbdb2 100644 --- a/tests/Logger/StreamTest.php +++ b/tests/Logger/StreamTest.php @@ -11,19 +11,20 @@ namespace Apix\Log\tests\Logger; use Apix\Log\Logger; +use Psr\Log\InvalidArgumentException; -class StreamTest extends TestCase +class StreamTest extends \PHPUnit\Framework\TestCase { protected $dest = 'php://memory'; protected $stream, $logger; - protected function setUp() + protected function setUp() : void { $this->stream = fopen($this->dest, 'a'); $this->logger = new Logger\Stream($this->stream); } - protected function tearDown() + protected function tearDown() : void { unset($this->logger, $this->stream); } @@ -57,24 +58,20 @@ public function getLogger() // $this->assertEquals($this->logger, $logger); // } - /** - * @expectedException Psr\Log\InvalidArgumentException - * @expectedExceptionMessage The stream "" cannot be created or opened - */ public function testThrowsInvalidArgumentExceptionWhenFileCannotBeCreated() { + $this->expectException(\ValueError::class); + $this->expectExceptionMessage('Path cannot be empty'); new Logger\Stream(null); } - /** - * @expectedException \LogicException - * @expectedExceptionMessage The stream resource has been __destruct() too early - */ public function testThrowsLogicException() { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('The stream resource has been __destruct() too early'); + $logger = new Logger\Stream; $logger->__destruct(); $logger->debug('foo'); } - } diff --git a/tests/Logger/TestCase.php b/tests/Logger/TestCase.php index 3b49226..07a9120 100644 --- a/tests/Logger/TestCase.php +++ b/tests/Logger/TestCase.php @@ -10,9 +10,9 @@ namespace Apix\Log\tests\Logger; -use Psr\Log\Test\LoggerInterfaceTest; +use Psr\Log\LoggerInterface; -abstract class TestCase extends LoggerInterfaceTest +abstract class TestCase implements LoggerInterface { protected $dest = 'build/apix-unit-test-logger.log'; diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 73be712..07c5651 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -10,27 +10,24 @@ namespace Apix\Log; +use Psr\Log\InvalidArgumentException; use Psr\Log\LogLevel; +use PHPUnit\Framework\Assert; -class LoggerTest extends \PHPUnit_Framework_TestCase +class LoggerTest extends \PHPUnit\Framework\TestCase { protected $logger; - protected function setUp() + protected function setUp() : void { $this->logger = new Logger(); } - protected function tearDown() + protected function tearDown() : void { unset($this->logger); } - protected function _getPrivateAttribute($prop) - { - return \PHPUnit_Framework_Assert::readAttribute($this->logger, $prop); - } - /** * @see http://tools.ietf.org/html/rfc5424#section-6.2.1 */ @@ -40,12 +37,10 @@ public function testGetLevelCodeSameOrderAsRfc5424() $this->assertSame(3, Logger::getLevelCode('error')); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage "stdClass" must interface "Apix\Log\Logger\LoggerInterface" - */ public function testConstructorThrowsInvalidArgumentException() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('"stdClass" must interface "Apix\Log\Logger\LoggerInterface"'); new Logger(array( new \StdClass() )); } @@ -66,11 +61,9 @@ public function testConstructor() $this->logger->critical('test crit'); } - /** - * @expectedException InvalidArgumentException - */ public function testGetLevelCodeThrows() { + $this->expectException(InvalidArgumentException::class); Logger::getLevelCode('non-existant'); } @@ -79,11 +72,9 @@ public function testgGtPsrLevelName() $this->assertEquals('error', Logger::getPsrLevelName(LogLevel::ERROR)); } - /** - * @expectedException InvalidArgumentException - */ public function testGetPsrLevelNameWillThrows() { + $this->expectException(InvalidArgumentException::class); Logger::getPsrLevelName('non-existant'); } @@ -210,12 +201,11 @@ public function testLogEntriesAreNotCascasding() public function testSetCascading() { - $this->assertTrue( - $this->_getPrivateAttribute("cascading"), - "The 'cascading' propertie should be True by default" + $this->assertTrue($this->logger->cascading(), + "The 'cascading' property should be True by default" ); $this->logger->setCascading(false); - $this->assertFalse($this->_getPrivateAttribute("cascading")); + $this->assertFalse($this->logger->cascading()); } public function testCascading() @@ -244,11 +234,11 @@ public function testCascading() public function testSetDeferred() { $this->assertFalse( - $this->_getPrivateAttribute('deferred'), - "The 'deferred' propertie should be False by default" + $this->logger->deferred(), + "The 'deferred' property should be False by default" ); $this->logger->setDeferred(true); - $this->assertTrue($this->_getPrivateAttribute('deferred')); + $this->assertTrue($this->logger->deferred()); } public function testDeferring() @@ -289,11 +279,10 @@ public function testInterceptAtAliasSetMinLevel() $this->logger->setMinLevel('alert', true); $this->assertEquals(1, $this->logger->getMinLevel()); - $this->assertTrue($this->_getPrivateAttribute("cascading")); + $this->assertTrue($this->logger->cascading()); $this->logger->interceptAt('warning', true); $this->assertEquals(4, $this->logger->getMinLevel()); - $this->assertFalse($this->_getPrivateAttribute("cascading")); + $this->assertFalse($this->logger->cascading()); } - -} \ No newline at end of file +}