.
This commit is contained in:
commit
a5ce423afe
30 changed files with 1807 additions and 0 deletions
23
tests/Aspects/BasicAspect.php
Normal file
23
tests/Aspects/BasicAspect.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class BasicAspect
|
||||
{
|
||||
public ?object $object = null;
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
{
|
||||
$this->object = end($args);
|
||||
$this->object->before = true;
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $return): mixed
|
||||
{
|
||||
$this->object->after = true;
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
60
tests/Aspects/ConfigurableAspect.php
Normal file
60
tests/Aspects/ConfigurableAspect.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class ConfigurableAspect
|
||||
{
|
||||
public static array $executionLog = [];
|
||||
|
||||
public function __construct(
|
||||
public readonly string $prefix = 'default',
|
||||
public readonly int $multiplier = 1,
|
||||
public readonly bool $enabled = true,
|
||||
) {
|
||||
}
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
{
|
||||
if ($this->enabled) {
|
||||
self::$executionLog[] = [
|
||||
'event' => 'before',
|
||||
'prefix' => $this->prefix,
|
||||
'multiplier' => $this->multiplier,
|
||||
'args' => $args,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
self::$executionLog[] = [
|
||||
'event' => 'after',
|
||||
'prefix' => $this->prefix,
|
||||
'multiplier' => $this->multiplier,
|
||||
'result' => $result,
|
||||
];
|
||||
|
||||
// Modify result based on constructor parameters
|
||||
if (is_int($result)) {
|
||||
return $result * $this->multiplier;
|
||||
}
|
||||
|
||||
if (is_string($result)) {
|
||||
return $this->prefix . $result;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function reset(): void
|
||||
{
|
||||
self::$executionLog = [];
|
||||
}
|
||||
}
|
||||
27
tests/Aspects/LoggingAspect.php
Normal file
27
tests/Aspects/LoggingAspect.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class LoggingAspect
|
||||
{
|
||||
public static array $logs = [];
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
{
|
||||
self::$logs[] = ['type' => 'logging_before', 'args' => $args];
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
{
|
||||
self::$logs[] = ['type' => 'logging_after', 'result' => $result];
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function clearLogs(): void
|
||||
{
|
||||
self::$logs = [];
|
||||
}
|
||||
}
|
||||
24
tests/Aspects/ModifyingAspect.php
Normal file
24
tests/Aspects/ModifyingAspect.php
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class ModifyingAspect
|
||||
{
|
||||
public static mixed $modifier = null;
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
{
|
||||
if (self::$modifier !== null) {
|
||||
return (self::$modifier)($result);
|
||||
}
|
||||
return $result; // Return original value when not modifying
|
||||
}
|
||||
|
||||
public static function reset(): void
|
||||
{
|
||||
self::$modifier = null;
|
||||
}
|
||||
}
|
||||
34
tests/Aspects/ThrowingAspect.php
Normal file
34
tests/Aspects/ThrowingAspect.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
use RuntimeException;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class ThrowingAspect
|
||||
{
|
||||
public static bool $throwInBefore = false;
|
||||
public static bool $throwInAfter = false;
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
{
|
||||
if (self::$throwInBefore) {
|
||||
throw new RuntimeException('Exception thrown in before()');
|
||||
}
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
{
|
||||
if (self::$throwInAfter) {
|
||||
throw new RuntimeException('Exception thrown in after()');
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function reset(): void
|
||||
{
|
||||
self::$throwInBefore = false;
|
||||
self::$throwInAfter = false;
|
||||
}
|
||||
}
|
||||
47
tests/Aspects/TrackingAspect.php
Normal file
47
tests/Aspects/TrackingAspect.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Aspects;
|
||||
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class TrackingAspect
|
||||
{
|
||||
public static array $calls = [];
|
||||
|
||||
public function before(object|string $target, mixed ...$args): void
|
||||
{
|
||||
self::$calls[] = ['event' => 'before', 'args' => $args];
|
||||
}
|
||||
|
||||
public function after(object|string $target, mixed $result): mixed
|
||||
{
|
||||
self::$calls[] = ['event' => 'after', 'result' => $result];
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function clearCalls(): void
|
||||
{
|
||||
self::$calls = [];
|
||||
}
|
||||
|
||||
public static function getLastBefore(): ?array
|
||||
{
|
||||
foreach (array_reverse(self::$calls) as $call) {
|
||||
if ($call['event'] === 'before') {
|
||||
return $call;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getLastAfter(): ?array
|
||||
{
|
||||
foreach (array_reverse(self::$calls) as $call) {
|
||||
if ($call['event'] === 'after') {
|
||||
return $call;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue