-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\LogLevel; | ||
|
||
function log_error(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::ERROR, $message, $context); | ||
} | ||
|
||
function log_info(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::INFO, $message, $context); | ||
} | ||
|
||
function log_warning(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::WARNING, $message, $context); | ||
} | ||
|
||
function log_debug(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::DEBUG, $message, $context); | ||
} | ||
|
||
function log_notice(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::NOTICE, $message, $context); | ||
} | ||
|
||
function log_critical(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::CRITICAL, $message, $context); | ||
} | ||
|
||
function log_emergency(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::EMERGENCY, $message, $context); | ||
} | ||
|
||
function log_alert(string|Stringable $message, array $context = []): void | ||
{ | ||
log_message(LogLevel::ALERT, $message, $context); | ||
} | ||
|
||
function log_message( | ||
string $level, | ||
string|stringable $message, | ||
Check failure on line 50 in src/logger.php GitHub Actions / PHP 8.1-ubuntu-latestInvalidClass
|
||
array $context = [], | ||
): void { | ||
/** | ||
* @var LoggerInterface $logger | ||
Check failure on line 54 in src/logger.php GitHub Actions / PHP 8.1-ubuntu-latestUnnecessaryVarAnnotation
Check failure on line 54 in src/logger.php GitHub Actions / PHP 8.2-ubuntu-latestUnnecessaryVarAnnotation
|
||
*/ | ||
$logger = container(LoggerInterface::class); | ||
$logger->log($level, $message, $context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Xepozz\Shortcut\Tests; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
use Stringable; | ||
use Xepozz\Shortcut\State; | ||
use Xepozz\Shortcut\Tests\Support\StringableClass; | ||
use Yiisoft\Test\Support\Container\SimpleContainer; | ||
|
||
final class LoggerTest extends FunctionsTestCase | ||
{ | ||
public function testFunctionExist() | ||
{ | ||
$this->assertTrue(function_exists('log_error')); | ||
$this->assertTrue(function_exists('log_info')); | ||
$this->assertTrue(function_exists('log_warning')); | ||
$this->assertTrue(function_exists('log_debug')); | ||
$this->assertTrue(function_exists('log_notice')); | ||
$this->assertTrue(function_exists('log_critical')); | ||
$this->assertTrue(function_exists('log_emergency')); | ||
$this->assertTrue(function_exists('log_alert')); | ||
$this->assertTrue(function_exists('log_message')); | ||
} | ||
|
||
public function testLogMessage(): void | ||
{ | ||
$this->initEnvironment(); | ||
$this->expectNotToPerformAssertions(); | ||
|
||
log_message('error', 'test message', ['some context']); | ||
} | ||
|
||
#[DataProvider('dataLogFunctions')] | ||
public function testLogFunctions(string $function, string|Stringable $message, array $context): void | ||
{ | ||
$this->initEnvironment(); | ||
$this->expectNotToPerformAssertions(); | ||
|
||
$function($message, $context); | ||
} | ||
|
||
public static function dataLogFunctions(): iterable | ||
{ | ||
yield 'log_error' => ['log_error', 'message', []]; | ||
yield 'log_error' => ['log_error', new StringableClass('message'), ['key' => 'value']]; | ||
|
||
yield 'log_info' => ['log_info', 'message', []]; | ||
yield 'log_info' => ['log_info', new StringableClass('message'), ['key' => 'value']]; | ||
|
||
yield 'log_warning' => ['log_warning', 'message', []]; | ||
yield 'log_warning' => ['log_warning', new StringableClass('message'), ['key' => 'value']]; | ||
|
||
yield 'log_debug' => ['log_debug', 'message', []]; | ||
yield 'log_debug' => ['log_debug', new StringableClass('message'), ['key' => 'value']]; | ||
|
||
yield 'log_notice' => ['log_notice', 'message', []]; | ||
yield 'log_notice' => ['log_notice', new StringableClass('message'), ['key' => 'value']]; | ||
|
||
yield 'log_alert' => ['log_alert', 'message', []]; | ||
yield 'log_alert' => ['log_alert', new StringableClass('message'), ['key' => 'value']]; | ||
|
||
yield 'log_critical' => ['log_critical', 'message', []]; | ||
yield 'log_critical' => ['log_critical', new StringableClass('message'), ['key' => 'value']]; | ||
|
||
yield 'log_emergency' => ['log_emergency', 'message', []]; | ||
yield 'log_emergency' => ['log_emergency', new StringableClass('message'), ['key' => 'value']]; | ||
} | ||
|
||
public function bootstrapFiles(): iterable | ||
{ | ||
yield __DIR__ . '/../src/container.php'; | ||
yield __DIR__ . '/../src/logger.php'; | ||
} | ||
|
||
protected function initEnvironment(): void | ||
{ | ||
State::$container = new SimpleContainer([ | ||
LoggerInterface::class => new NullLogger(), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Xepozz\Shortcut\Tests\Support; | ||
|
||
use Stringable; | ||
|
||
final class StringableClass implements Stringable | ||
{ | ||
public function __construct(private string $string) | ||
{ | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->string; | ||
} | ||
} |