diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index d4bac74..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,63 +0,0 @@ -in(__DIR__.'/tests') - ->in(__DIR__.'/src') -; - -return (new PhpCsFixer\Config()) - ->setRules([ - '@PSR1' => true, - '@PSR2' => true, - '@PhpCsFixer' => true, - '@Symfony' => true, - '@DoctrineAnnotation' => true, - '@PHP70Migration' => true, - '@PHP71Migration' => true, - 'strict_param' => true, - 'strict_comparison' => true, - 'array_syntax' => ['syntax' => 'short'], - 'array_indentation' => true, - 'ordered_imports' => true, - 'protected_to_private' => true, - 'declare_strict_types' => true, - 'native_function_invocation' => [ - 'include' => ['@compiler_optimized'], - 'scope' => 'namespaced', - 'strict' => true, - ], - 'mb_str_functions' => true, - 'linebreak_after_opening_tag' => true, - 'combine_consecutive_issets' => true, - 'combine_consecutive_unsets' => true, - 'compact_nullable_typehint' => true, - 'no_superfluous_elseif' => true, - 'phpdoc_trim_consecutive_blank_line_separation' => true, - 'phpdoc_order' => true, - 'pow_to_exponentiation' => true, - 'simplified_null_return' => true, - 'header_comment' => [ - 'header' => $header, - ], - 'align_multiline_comment' => [ - 'comment_type' => 'all_multiline', - ], - 'php_unit_test_annotation' => [ - 'style' => 'annotation', - ], - 'php_unit_test_case_static_method_calls' => true, - 'method_chaining_indentation' => false, - 'php_unit_expectation' => true, - 'php_unit_test_class_requires_covers' => false, - 'global_namespace_import' => [ - 'import_classes' => true, - 'import_constants' => true, - 'import_functions' => true, - ], - ]) - ->setRiskyAllowed(true) - ->setUsingCache(true) - ->setFinder($finder) - ; diff --git a/tests/Library/Functional/Payload/AES128GCMTest.php b/tests/Library/Functional/Payload/AES128GCMTest.php index 9fe5ee0..d142873 100644 --- a/tests/Library/Functional/Payload/AES128GCMTest.php +++ b/tests/Library/Functional/Payload/AES128GCMTest.php @@ -16,7 +16,6 @@ use Psr\Cache\CacheItemPoolInterface; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; -use Psr\Log\Test\TestLogger; use Symfony\Component\Cache\Adapter\ArrayAdapter; use Symfony\Component\Cache\Adapter\NullAdapter; use function unpack; @@ -24,6 +23,7 @@ use WebPush\Payload\AES128GCM; use WebPush\Payload\ServerKey; use WebPush\Subscription; +use WebPush\Tests\TestLogger; use WebPush\Utils; /** diff --git a/tests/Library/Functional/VAPID/LcobucciProviderTest.php b/tests/Library/Functional/VAPID/LcobucciProviderTest.php index a75e1ad..17277c0 100644 --- a/tests/Library/Functional/VAPID/LcobucciProviderTest.php +++ b/tests/Library/Functional/VAPID/LcobucciProviderTest.php @@ -7,9 +7,9 @@ use DateTimeImmutable; use InvalidArgumentException; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use const STR_PAD_RIGHT; use WebPush\Base64Url; +use WebPush\Tests\TestLogger; use WebPush\VAPID\LcobucciProvider; /** diff --git a/tests/Library/Functional/VAPID/VAPIDTest.php b/tests/Library/Functional/VAPID/VAPIDTest.php index 70ebbed..59359b0 100644 --- a/tests/Library/Functional/VAPID/VAPIDTest.php +++ b/tests/Library/Functional/VAPID/VAPIDTest.php @@ -7,10 +7,10 @@ use function json_decode; use Nyholm\Psr7\Request; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use WebPush\Base64Url; use WebPush\Notification; use WebPush\Subscription; +use WebPush\Tests\TestLogger; use WebPush\VAPID\LcobucciProvider; use WebPush\VAPID\VAPIDExtension; diff --git a/tests/Library/Functional/VAPID/WebTokenProviderTest.php b/tests/Library/Functional/VAPID/WebTokenProviderTest.php index 0a38d70..62e758e 100644 --- a/tests/Library/Functional/VAPID/WebTokenProviderTest.php +++ b/tests/Library/Functional/VAPID/WebTokenProviderTest.php @@ -7,9 +7,9 @@ use DateTimeImmutable; use InvalidArgumentException; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use const STR_PAD_RIGHT; use WebPush\Base64Url; +use WebPush\Tests\TestLogger; use WebPush\VAPID\WebTokenProvider; /** diff --git a/tests/Library/Unit/ExtensionManagerTest.php b/tests/Library/Unit/ExtensionManagerTest.php index aea623f..9acae0e 100644 --- a/tests/Library/Unit/ExtensionManagerTest.php +++ b/tests/Library/Unit/ExtensionManagerTest.php @@ -6,11 +6,11 @@ use Nyholm\Psr7\Request; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use WebPush\ExtensionManager; use WebPush\Notification; use WebPush\PreferAsyncExtension; use WebPush\Subscription; +use WebPush\Tests\TestLogger; use WebPush\TTLExtension; /** diff --git a/tests/Library/Unit/PayloadExtensionTest.php b/tests/Library/Unit/PayloadExtensionTest.php index d71d49d..0c27cf5 100644 --- a/tests/Library/Unit/PayloadExtensionTest.php +++ b/tests/Library/Unit/PayloadExtensionTest.php @@ -7,11 +7,11 @@ use InvalidArgumentException; use Nyholm\Psr7\Request; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use WebPush\Notification; use WebPush\Payload\AESGCM; use WebPush\Payload\PayloadExtension; use WebPush\Subscription; +use WebPush\Tests\TestLogger; /** * @internal diff --git a/tests/Library/Unit/SyncExtensionTest.php b/tests/Library/Unit/SyncExtensionTest.php index 27cfcd5..0b22991 100644 --- a/tests/Library/Unit/SyncExtensionTest.php +++ b/tests/Library/Unit/SyncExtensionTest.php @@ -6,10 +6,10 @@ use Nyholm\Psr7\Request; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use WebPush\Notification; use WebPush\PreferAsyncExtension; use WebPush\Subscription; +use WebPush\Tests\TestLogger; /** * @internal diff --git a/tests/Library/Unit/TTLExtensionTest.php b/tests/Library/Unit/TTLExtensionTest.php index 83f7f7d..475ed29 100644 --- a/tests/Library/Unit/TTLExtensionTest.php +++ b/tests/Library/Unit/TTLExtensionTest.php @@ -6,9 +6,9 @@ use Nyholm\Psr7\Request; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use WebPush\Notification; use WebPush\Subscription; +use WebPush\Tests\TestLogger; use WebPush\TTLExtension; /** diff --git a/tests/Library/Unit/TopicExtensionTest.php b/tests/Library/Unit/TopicExtensionTest.php index 43bc4b8..4490d5a 100644 --- a/tests/Library/Unit/TopicExtensionTest.php +++ b/tests/Library/Unit/TopicExtensionTest.php @@ -6,9 +6,9 @@ use Nyholm\Psr7\Request; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use WebPush\Notification; use WebPush\Subscription; +use WebPush\Tests\TestLogger; use WebPush\TopicExtension; /** diff --git a/tests/Library/Unit/UrgencyExtensionTest.php b/tests/Library/Unit/UrgencyExtensionTest.php index 8a7cca9..5d50713 100644 --- a/tests/Library/Unit/UrgencyExtensionTest.php +++ b/tests/Library/Unit/UrgencyExtensionTest.php @@ -6,9 +6,9 @@ use Nyholm\Psr7\Request; use PHPUnit\Framework\TestCase; -use Psr\Log\Test\TestLogger; use WebPush\Notification; use WebPush\Subscription; +use WebPush\Tests\TestLogger; use WebPush\UrgencyExtension; /** diff --git a/tests/Library/Unit/WebPushTest.php b/tests/Library/Unit/WebPushTest.php index 2e63516..b945dc8 100644 --- a/tests/Library/Unit/WebPushTest.php +++ b/tests/Library/Unit/WebPushTest.php @@ -10,10 +10,10 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; -use Psr\Log\Test\TestLogger; use WebPush\ExtensionManager; use WebPush\Notification; use WebPush\Subscription; +use WebPush\Tests\TestLogger; use WebPush\WebPush; /** diff --git a/tests/TestLogger.php b/tests/TestLogger.php new file mode 100644 index 0000000..f31ca31 --- /dev/null +++ b/tests/TestLogger.php @@ -0,0 +1,227 @@ + 0) { + $genericMethod = $matches[1] . ($matches[3] !== 'Records' ? 'Record' : '') . $matches[3]; + $level = mb_strtolower($matches[2]); + if (method_exists($this, $genericMethod)) { + $args[] = $level; + + return call_user_func_array([$this, $genericMethod], $args); + } + } + throw new BadMethodCallException('Call to undefined method ' . static::class . '::' . $method . '()'); + } + + /** + * @inheritdoc + */ + public function log($level, $message, array $context = []): void + { + $record = [ + 'level' => $level, + 'message' => $message, + 'context' => $context, + ]; + + $this->recordsByLevel[$record['level']][] = $record; + $this->records[] = $record; + } + + public function hasRecords($level): bool + { + return isset($this->recordsByLevel[$level]); + } + + public function hasRecord($record, $level): bool + { + if (is_string($record)) { + $record = [ + 'message' => $record, + ]; + } + + return $this->hasRecordThatPasses(function ($rec) use ($record) { + if ($rec['message'] !== $record['message']) { + return false; + } + if (isset($record['context']) && $rec['context'] !== $record['context']) { + return false; + } + + return true; + }, $level); + } + + public function hasRecordThatPasses(callable $predicate, $level): bool + { + if (! isset($this->recordsByLevel[$level])) { + return false; + } + foreach ($this->recordsByLevel[$level] as $i => $rec) { + if (call_user_func($predicate, $rec, $i)) { + return true; + } + } + + return false; + } + + public function reset(): void + { + $this->records = []; + $this->recordsByLevel = []; + } + + /** + * System is unusable. + * + * @param mixed[] $context + */ + public function emergency($message, array $context = []): void + { + $this->log(LogLevel::EMERGENCY, $message, $context); + } + + /** + * Action must be taken immediately. + * + * Example: Entire website down, database unavailable, etc. This should trigger the SMS alerts and wake you up. + * + * @param mixed[] $context + */ + public function alert($message, array $context = []): void + { + $this->log(LogLevel::ALERT, $message, $context); + } + + /** + * Critical conditions. + * + * Example: Application component unavailable, unexpected exception. + * + * @param mixed[] $context + */ + public function critical($message, array $context = []): void + { + $this->log(LogLevel::CRITICAL, $message, $context); + } + + /** + * Runtime errors that do not require immediate action but should typically be logged and monitored. + * + * @param mixed[] $context + */ + public function error($message, array $context = []): void + { + $this->log(LogLevel::ERROR, $message, $context); + } + + /** + * Exceptional occurrences that are not errors. + * + * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not necessarily wrong. + * + * @param mixed[] $context + */ + public function warning($message, array $context = []): void + { + $this->log(LogLevel::WARNING, $message, $context); + } + + /** + * Normal but significant events. + * + * @param mixed[] $context + */ + public function notice($message, array $context = []): void + { + $this->log(LogLevel::NOTICE, $message, $context); + } + + /** + * Interesting events. + * + * Example: User logs in, SQL logs. + * + * @param mixed[] $context + */ + public function info($message, array $context = []): void + { + $this->log(LogLevel::INFO, $message, $context); + } + + /** + * Detailed debug information. + * + * @param mixed[] $context + */ + public function debug($message, array $context = []): void + { + $this->log(LogLevel::DEBUG, $message, $context); + } +}