Skip to content

Commit

Permalink
Fixed aop zero bug
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Sep 13, 2019
1 parent a367173 commit 85bc1b1
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/aop/src/AspectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Swoft\Aop\Point\JoinPoint;
use Swoft\Aop\Point\ProceedingJoinPoint;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Stdlib\Reflections;
use Throwable;
use function array_shift;
Expand Down Expand Up @@ -77,7 +76,6 @@ class AspectHandler
*
* @return mixed
* @throws ReflectionException
* @throws ContainerException
* @throws Throwable
*/
public function invokeAspect()
Expand Down Expand Up @@ -214,7 +212,6 @@ public function setArgsMap(array $argsMap): void
*
* @return mixed
* @throws ReflectionException
* @throws ContainerException
*/
private function invokeAdvice(array $aspectAry, Throwable $catch = null, $return = null)
{
Expand Down Expand Up @@ -275,7 +272,8 @@ private function getProceedingJoinPoint(Throwable $catch = null, $return = null)
$pgp->setCatch($catch);
}

if ($return) {
// Must use all equal to fixed `0` bug
if ($return !== null) {
$pgp->setReturn($return);
}

Expand All @@ -297,7 +295,8 @@ private function getJoinPoint(Throwable $catch = null, $return = null): JoinPoin
$joinPoint->setCatch($catch);
}

if ($return) {
// Must use all equal to fixed `0` bug
if ($return !== null) {
$joinPoint->setReturn($return);
}

Expand Down
40 changes: 40 additions & 0 deletions test/testing/Aop/Aspect/Zero2Aspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);


namespace SwoftTest\Component\Testing\Aop\Aspect;


use Swoft\Aop\Annotation\Mapping\AfterReturning;
use Swoft\Aop\Annotation\Mapping\Aspect;
use Swoft\Aop\Annotation\Mapping\PointExecution;
use Swoft\Aop\Point\JoinPoint;
use Throwable;

/**
* Class Zero2Aspect
*
* @since 2.0
*
*
* @Aspect()
* @PointExecution(
* include={
* "SwoftTest\Component\Testing\Aop\ZeroAop::afterZero"
* }
* ))
*/
class Zero2Aspect
{
/**
* @AfterReturning()
*
* @param JoinPoint $joinPoint
*
* @return mixed
* @throws Throwable
*/
public function afterReturn(JoinPoint $joinPoint)
{
return 0;
}
}
40 changes: 40 additions & 0 deletions test/testing/Aop/Aspect/ZeroAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);


namespace SwoftTest\Component\Testing\Aop\Aspect;

use Swoft\Aop\Annotation\Mapping\AfterReturning;
use Swoft\Aop\Annotation\Mapping\Aspect;
use Swoft\Aop\Annotation\Mapping\PointExecution;
use Swoft\Aop\Point\JoinPoint;
use Throwable;

/**
* Class ZeroAspect
*
* @since 2.0
*
* @Aspect()
* @PointExecution(
* include={
* "SwoftTest\Component\Testing\Aop\ZeroAop::returnZero"
* }
* )
*/
class ZeroAspect
{

/**
* @AfterReturning()
*
* @param JoinPoint $joinPoint
*
* @return mixed
* @throws Throwable
*/
public function afterReturn(JoinPoint $joinPoint)
{
$result = $joinPoint->getReturn();
return $result;
}
}
32 changes: 32 additions & 0 deletions test/testing/Aop/ZeroAop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);


namespace SwoftTest\Component\Testing\Aop;

use Swoft\Bean\Annotation\Mapping\Bean;

/**
* Class ZeroAop
*
* @since 2.0
*
* @Bean()
*/
class ZeroAop
{
/**
* @return int
*/
public function returnZero(): int
{
return 0;
}

/**
* @return int
*/
public function afterZero(): int
{
return 1;
}
}
28 changes: 28 additions & 0 deletions test/unit/ZeroAopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);


namespace SwoftTest\Component\Unit;


use PHPUnit\Framework\TestCase;
use Swoft\Bean\BF;
use SwoftTest\Component\Testing\Aop\ZeroAop;

class ZeroAopTest extends TestCase
{
public function testZero(): void
{
/* @var ZeroAop $bean */
$bean = BF::getBean(ZeroAop::class);
$result = $bean->returnZero();
$this->assertTrue($result === 0);
}

public function testZero2(): void
{
/* @var ZeroAop $bean */
$bean = BF::getBean(ZeroAop::class);
$result = $bean->afterZero();
$this->assertTrue($result === 0);
}
}

0 comments on commit 85bc1b1

Please sign in to comment.