Throw an exception when the scanner directories all does not exist. (#2043)

* Optimization when the Scanner directory all does not exist

* Optimized test cases.

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
Millyn 2020-07-23 17:37:04 +08:00 committed by GitHub
parent 3f0d876bf1
commit c954e949c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 0 deletions

View File

@ -11,6 +11,10 @@
- [#2159](https://github.com/hyperf/hyperf/pull/2159) Fixed fatal exception caused by exist file when using `gen:migration`.
## Optimized
- [#2043](https://github.com/hyperf/hyperf/pull/2043) Throw an exception when the scanner directories all does not exist.
# v2.0.3 - 2020-07-20
## Added

View File

@ -14,6 +14,7 @@ namespace Hyperf\Di\Annotation;
use Hyperf\Config\ProviderConfig;
use Hyperf\Di\BetterReflectionManager;
use Hyperf\Di\ClassLoader;
use Hyperf\Di\Exception\DirectoryNotExistException;
use Hyperf\Di\MetadataCollector;
use Hyperf\Utils\Filesystem\Filesystem;
use ReflectionProperty;
@ -162,6 +163,7 @@ class Scanner
/**
* Normalizes given directory names by removing directory not exist.
* @throws DirectoryNotExistException
*/
public function normalizeDir(array $paths): array
{
@ -172,6 +174,10 @@ class Scanner
}
}
if ($paths && ! $result) {
throw new DirectoryNotExistException('The scanned directory does not exist');
}
return $result;
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di\Exception;
class DirectoryNotExistException extends Exception
{
}

View File

@ -16,6 +16,7 @@ use Hyperf\Di\Annotation\ScanConfig;
use Hyperf\Di\Annotation\Scanner;
use Hyperf\Di\BetterReflectionManager;
use Hyperf\Di\ClassLoader;
use Hyperf\Di\Exception\DirectoryNotExistException;
use HyperfTest\Di\Stub\AnnotationCollector;
use HyperfTest\Di\Stub\Ignore;
use HyperfTest\Di\Stub\IgnoreDemoAnnotation;
@ -51,4 +52,25 @@ class AnnotationTest extends TestCase
$annotations = AnnotationCollector::get(Ignore::class . '._c');
$this->assertNull($annotations);
}
public function testScanAnnotationsDirectoryNotExist()
{
$scanner = new Scanner($loader = Mockery::mock(ClassLoader::class), new ScanConfig(false, '/'));
$ref = new \ReflectionClass($scanner);
$method = $ref->getMethod('normalizeDir');
$method->setAccessible(true);
$this->expectException(DirectoryNotExistException::class);
$method->invokeArgs($scanner, [['/not_exists']]);
}
public function testScanAnnotationsDirectoryEmpty()
{
$scanner = new Scanner($loader = Mockery::mock(ClassLoader::class), new ScanConfig(false, '/'));
$ref = new \ReflectionClass($scanner);
$method = $ref->getMethod('normalizeDir');
$method->setAccessible(true);
$this->assertSame([], $method->invokeArgs($scanner, [[]]));
}
}