diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index 92aaaeb13..fd46cd309 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -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 diff --git a/src/di/tests/Annotation/ScannerTest.php b/src/di/tests/Annotation/ScannerTest.php new file mode 100644 index 000000000..994e043a4 --- /dev/null +++ b/src/di/tests/Annotation/ScannerTest.php @@ -0,0 +1,103 @@ +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; + } +} diff --git a/src/di/tests/Stub/Aspect/Debug1Aspect.php b/src/di/tests/Stub/Aspect/Debug1Aspect.php new file mode 100644 index 000000000..c3999465f --- /dev/null +++ b/src/di/tests/Stub/Aspect/Debug1Aspect.php @@ -0,0 +1,27 @@ +process(); + } +} diff --git a/src/di/tests/Stub/Aspect/Debug2Aspect.php b/src/di/tests/Stub/Aspect/Debug2Aspect.php new file mode 100644 index 000000000..159a4f06c --- /dev/null +++ b/src/di/tests/Stub/Aspect/Debug2Aspect.php @@ -0,0 +1,31 @@ +process(); + } +} diff --git a/src/di/tests/Stub/Aspect/Debug3Aspect.php b/src/di/tests/Stub/Aspect/Debug3Aspect.php new file mode 100644 index 000000000..36d73ea7d --- /dev/null +++ b/src/di/tests/Stub/Aspect/Debug3Aspect.php @@ -0,0 +1,27 @@ +process(); + } +}