mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Added test cases.
This commit is contained in:
parent
00c94e5cae
commit
705370fb1d
@ -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
|
||||
|
||||
|
@ -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",
|
||||
|
@ -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);
|
||||
|
38
src/validation/tests/Cases/Stub/BarSceneRequest.php
Normal file
38
src/validation/tests/Cases/Stub/BarSceneRequest.php
Normal 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',
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user