Added test cases.

This commit is contained in:
李铭昕 2021-09-05 00:00:43 +08:00 committed by GitHub
parent 00c94e5cae
commit 705370fb1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 5 deletions

View File

@ -11,7 +11,8 @@
## Added
- [#4002](https://github.com/hyperf/hyperf/pull/4002) Support method `FormRequest::scene()` which used to rewrite different rules according to different scenes.
- [#4002](https://github.com/hyperf/hyperf/pull/4002) [#4012](https://github.com/hyperf/hyperf/pull/4012) Support method `FormRequest::scene()` which used to rewrite different rules according to different scenes.
- [#4011](https://github.com/hyperf/hyperf/pull/4011) Added some methods for `Hyperf\Utils\Str`.
# v2.2.6 - 2021-08-30

View File

@ -64,7 +64,7 @@
"php-amqplib/php-amqplib": "^3.0",
"php-di/phpdoc-reader": "^2.2",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "0.12.94",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^9.5",
"predis/predis": "^1.1",
"promphp/prometheus_client_php": "^2.2",

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
*/
namespace HyperfTest\Validation\Cases;
use Hyperf\HttpMessage\Server\Response;
use Hyperf\HttpMessage\Upload\UploadedFile;
use Hyperf\Translation\ArrayLoader;
use Hyperf\Translation\Translator;
@ -18,11 +19,13 @@ use Hyperf\Utils\Context;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Hyperf\Validation\ValidationException;
use Hyperf\Validation\ValidatorFactory;
use HyperfTest\Validation\Cases\Stub\BarSceneRequest;
use HyperfTest\Validation\Cases\Stub\DemoRequest;
use HyperfTest\Validation\Cases\Stub\FooSceneRequest;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
@ -74,6 +77,35 @@ class FormRequestTest extends TestCase
$this->assertEquals(['file' => ['Invalid File.', $file]], $request->getValidationData());
}
public function testRewriteGetRules()
{
$psrRequest = Mockery::mock(ServerRequestInterface::class);
$psrRequest->shouldReceive('getQueryParams')->andReturn([]);
$psrRequest->shouldReceive('getUploadedFiles')->andReturn([]);
$psrRequest->shouldReceive('getParsedBody')->andReturn([
'name' => 'xxx',
]);
Context::set(ServerRequestInterface::class, $psrRequest);
Context::set(ResponseInterface::class, new Response());
$container = Mockery::mock(ContainerInterface::class);
$translator = new Translator(new ArrayLoader(), 'en');
$container->shouldReceive('get')->with(ValidatorFactoryInterface::class)->andReturn(new ValidatorFactory($translator));
$request = new BarSceneRequest($container);
$res = $request->scene('required')->validated();
$this->assertSame(['name' => 'xxx'], $res);
try {
$request = new BarSceneRequest($container);
$request->validateResolved();
$this->assertTrue(false);
} catch (\Throwable $exception) {
$this->assertInstanceOf(ValidationException::class, $exception);
$this->assertSame('validation.integer', $exception->validator->errors()->first());
}
}
public function testSceneForFormRequest()
{
$psrRequest = Mockery::mock(ServerRequestInterface::class);
@ -81,10 +113,10 @@ class FormRequestTest extends TestCase
$psrRequest->shouldReceive('getUploadedFiles')->andReturn([]);
$psrRequest->shouldReceive('getParsedBody')->andReturn([
'mobile' => '12345',
'name' => '',
]);
Context::set(ServerRequestInterface::class, $psrRequest);
Context::set(ResponseInterface::class, new Response());
$container = Mockery::mock(ContainerInterface::class);
$translator = new Translator(new ArrayLoader(), 'en');
$container->shouldReceive('get')->with(ValidatorFactoryInterface::class)->andReturn(new ValidatorFactory($translator));
@ -95,8 +127,9 @@ class FormRequestTest extends TestCase
wait(function () use ($request, $psrRequest) {
Context::set(ServerRequestInterface::class, $psrRequest);
Context::set(ResponseInterface::class, new Response());
try {
$request->validated();
$request->validateResolved();
$this->assertTrue(false);
} catch (\Throwable $exception) {
$this->assertInstanceOf(ValidationException::class, $exception);
@ -105,7 +138,7 @@ class FormRequestTest extends TestCase
try {
$request = new FooSceneRequest($container);
$request->validated();
$request->validateResolved();
$this->assertTrue(false);
} catch (\Throwable $exception) {
$this->assertInstanceOf(ValidationException::class, $exception);

View File

@ -0,0 +1,38 @@
<?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 HyperfTest\Validation\Cases\Stub;
use Hyperf\Validation\Request\FormRequest;
class BarSceneRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
$scene = $this->getScene();
switch ($scene) {
case 'required':
return [
'name' => 'required',
];
default:
return [
'name' => 'required|integer',
];
}
}
}

View File

@ -266,6 +266,7 @@ class Finder implements FinderInterface
*/
protected function parseNamespaceSegments($name)
{
/* @phpstan-ignore-next-line */
$segments = explode(static::HINT_PATH_DELIMITER, $name);
if (count($segments) !== 2) {