Added test cases.

This commit is contained in:
李铭昕 2020-06-23 13:22:29 +08:00
parent bfce9618fe
commit 07f2996451
5 changed files with 189 additions and 0 deletions

View File

@ -5,6 +5,7 @@
- [#1961](https://github.com/hyperf/hyperf/pull/1961) Fixed start failed when `config/autoload/aspects.php` does not exists.
- [#1964](https://github.com/hyperf/hyperf/pull/1964) Fixed http status code 500 caused by empty body.
- [#1965](https://github.com/hyperf/hyperf/pull/1965) Fixed the wrong http code when `initRequestAndResponse` failed.
- [#1968](https://github.com/hyperf/hyperf/pull/1968) Fixed aspect does not work when edit in `aspects.php`.
## Optimized

View File

@ -0,0 +1,103 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Di\Annotation;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Di\Annotation\AnnotationReader;
use Hyperf\Di\Annotation\ScanConfig;
use Hyperf\Di\Annotation\Scanner;
use Hyperf\Di\BetterReflectionManager;
use Hyperf\Di\ClassLoader;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Filesystem\Filesystem;
use HyperfTest\Di\Stub\AnnotationCollector;
use HyperfTest\Di\Stub\Aspect\Debug1Aspect;
use HyperfTest\Di\Stub\Aspect\Debug2Aspect;
use HyperfTest\Di\Stub\Aspect\Debug3Aspect;
use HyperfTest\Di\Stub\AspectCollector;
use Mockery;
use PHPUnit\Framework\TestCase;
/**
* @internal
* @coversNothing
*/
class ScannerTest extends TestCase
{
protected function tearDown()
{
AspectCollector::clear();
AnnotationCollector::clear();
Mockery::close();
BetterReflectionManager::clear();
}
public function testGetChangedAspects()
{
$this->getContainer();
$scanner = new Scanner($loader = Mockery::mock(ClassLoader::class), new ScanConfig(false, '/'));
$loader->shouldReceive('getComposerClassLoader')->andReturnUsing(function () {
$loader = Mockery::mock(\Composer\Autoload\ClassLoader::class);
$loader->shouldReceive('findFile')->andReturnUsing(function ($class) {
return $class;
});
return $loader;
});
$ref = new \ReflectionClass($scanner);
$property = $ref->getProperty('filesystem');
$property->setAccessible(true);
$property->setValue($scanner, $filesystem = Mockery::mock(Filesystem::class . '[lastModified]'));
$times = [
Debug1Aspect::class => 5,
Debug2Aspect::class => 5,
Debug3Aspect::class => 5,
];
$filesystem->shouldReceive('lastModified')->andReturnUsing(function ($file) use (&$times) {
return $times[$file];
});
$method = $ref->getMethod('getChangedAspects');
$method->setAccessible(true);
$reader = new AnnotationReader();
$scanner->collect($reader, BetterReflectionManager::reflectClass(Debug2Aspect::class));
[$removed, $changed] = $method->invokeArgs($scanner, [[Debug1Aspect::class, Debug2Aspect::class, Debug3Aspect::class], 0]);
$this->assertEmpty($removed);
$this->assertEquals([Debug1Aspect::class, Debug2Aspect::class, Debug3Aspect::class], $changed);
$times[Debug2Aspect::class] = 20;
[$removed, $changed] = $method->invokeArgs($scanner, [[Debug1Aspect::class, Debug3Aspect::class], 10]);
$this->assertEmpty($removed);
$this->assertEmpty($changed);
[$removed, $changed] = $method->invokeArgs($scanner, [[Debug3Aspect::class], 10]);
$this->assertEquals([Debug1Aspect::class], $removed);
$this->assertEmpty($changed);
$times[Debug3Aspect::class] = 20;
[$removed, $changed] = $method->invokeArgs($scanner, [[Debug3Aspect::class], 10]);
$this->assertEmpty($removed);
$this->assertEquals([Debug3Aspect::class], $changed);
}
protected function getContainer()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
BetterReflectionManager::initClassReflector([__DIR__ . '/../Stub']);
return $container;
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Di\Stub\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
class Debug1Aspect extends AbstractAspect
{
public $classes = [
'Debug1AspectFoo',
];
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return $proceedingJoinPoint->process();
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Di\Stub\Aspect;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
/**
* @Aspect
*/
class Debug2Aspect extends AbstractAspect
{
public $classes = [
'Debug2AspectFoo',
];
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return $proceedingJoinPoint->process();
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\Di\Stub\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
class Debug3Aspect extends AbstractAspect
{
public $classes = [
'Debug3AspectFoo',
];
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
return $proceedingJoinPoint->process();
}
}