This commit is contained in:
icefox 2025-12-22 17:54:16 -03:00
commit a5ce423afe
No known key found for this signature in database
30 changed files with 1807 additions and 0 deletions

View file

@ -0,0 +1,58 @@
<?php
use IceFox\Aspect\AspectException;
use PHPUnit\Framework\TestCase;
use Tests\Aspects\ThrowingAspect;
use Tests\Classes\ThrowingClass;
final class AspectExceptionTest extends TestCase
{
protected function setUp(): void
{
ThrowingAspect::reset();
}
public function testExceptionInBeforeIsWrappedInAspectException(): void
{
ThrowingAspect::$throwInBefore = true;
$instance = new ThrowingClass();
try {
$instance->methodWithAspect();
$this->fail('Expected AspectException to be thrown');
} catch (AspectException $e) {
$this->assertEquals('Exception in before() method of aspect', $e->getMessage());
$this->assertEquals(ThrowingAspect::class, $e->aspectClass);
$this->assertEquals('before', $e->method);
$this->assertInstanceOf(\RuntimeException::class, $e->getPrevious());
$this->assertEquals('Exception thrown in before()', $e->getPrevious()->getMessage());
}
}
public function testExceptionInAfterIsWrappedInAspectException(): void
{
ThrowingAspect::$throwInAfter = true;
$instance = new ThrowingClass();
try {
$instance->methodWithAspect();
$this->fail('Expected AspectException to be thrown');
} catch (AspectException $e) {
$this->assertEquals('Exception in after() method of aspect', $e->getMessage());
$this->assertEquals(ThrowingAspect::class, $e->aspectClass);
$this->assertEquals('after', $e->method);
$this->assertInstanceOf(\RuntimeException::class, $e->getPrevious());
$this->assertEquals('Exception thrown in after()', $e->getPrevious()->getMessage());
}
}
public function testMethodExecutesSuccessfullyWhenNoExceptions(): void
{
$instance = new ThrowingClass();
$result = $instance->methodWithAspect();
$this->assertEquals('success', $result);
}
}