58 lines
2 KiB
PHP
58 lines
2 KiB
PHP
<?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);
|
|
}
|
|
}
|