Skip to content

Commit

Permalink
php >= 8.0 compatibility (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
8ctopus authored Sep 6, 2022
1 parent da9c226 commit e561845
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 145 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ jobs:
strategy:
matrix:
php-version:
- "5.3"
- "8.0"
- "8.1"

dependencies:
- "highest"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ composer.phar
/build/
phpunit.xml
TODO
.phpunit.result.cache
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
```

Expand All @@ -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
Expand All @@ -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');

Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
49 changes: 17 additions & 32 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Apix Log Unit Tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>build</directory>
<directory>tests</directory>
<directory>vendor</directory>
</exclude>
</whitelist>
</filter>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>build</directory>
<directory>tests</directory>
<directory>vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Apix Log Unit Tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>

<!-- vim: set tabstop=4 shiftwidth=4 expandtab: -->
<!-- vim: set tabstop=4 shiftwidth=4 expandtab: -->
2 changes: 1 addition & 1 deletion src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected function sortBuckets()
{
return usort(
$this->buckets, function ($a, $b) {
return $a->getMinLevel() > $b->getMinLevel();
return $a->getMinLevel() - $b->getMinLevel();
}
);
}
Expand Down
21 changes: 20 additions & 1 deletion src/Logger/AbstractLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Psr\Log\AbstractLogger as PsrAbstractLogger;
use Psr\Log\InvalidArgumentException;
use Stringable;
use Apix\Log\LogEntry;
use Apix\Log\LogFormatter;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down
5 changes: 2 additions & 3 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use Apix\Log\Logger;

class ReadmeTest extends \PHPUnit_Framework_TestCase
class ReadmeTest extends \PHPUnit\Framework\TestCase
{

/**
Expand Down Expand Up @@ -119,5 +119,4 @@ public function testUsages()
$this->getLogs($debug_logger)
);
}

}
}
8 changes: 4 additions & 4 deletions tests/InterfacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -83,4 +83,4 @@ public function testLogFormatterInterfaceExample()
'@\{"timestamp":.*\,"name":"error"\,"level_code":3\,"message":"hello world","context":\{"who":"world"\}\,"formatter":\{"separator":"~"\}\}@'
);
}
}
}
24 changes: 13 additions & 11 deletions tests/Logger/ErrorLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
38 changes: 16 additions & 22 deletions tests/Logger/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
23 changes: 10 additions & 13 deletions tests/Logger/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Loading

0 comments on commit e561845

Please sign in to comment.