1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace Tests\Unit;
- use App\Http\Requests\FormRequest;
- use Illuminate\Validation\ValidationException;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\WithFaker;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- class BaseFormRequestTest extends TestCase
- {
- public function testMergeArrayError()
- {
- // 测试合并
- $request = $this->makeRequest(['array' => ['not integer']]);
- try {
- $request->validateResolved();
- } catch (ValidationException $e) {
- $this->assertTrue(key_exists('array', $e->validator->errors()->messages()));
- }
- // 测试不合并
- $request = $this->makeRequest(['array' => ['not integer']]);
- $request->setMergeArrayError(false);
- try {
- $request->validateResolved();
- } catch (ValidationException $e) {
- $this->assertFalse(key_exists('array', $e->validator->errors()->messages()));
- }
- }
- /**
- * 指定请求数据, 生成一个 表单请求 实例
- *
- * @param array $data
- *
- * @return DummyFormRequest
- */
- protected function makeRequest($data = [])
- {
- $request = new DummyFormRequest($data);
- app()->call([$request, 'setContainer']);
- app()->call([$request, 'setRedirector']);
- return $request;
- }
- }
- class DummyFormRequest extends FormRequest
- {
- public function rules()
- {
- return [
- 'array.*' => 'integer',
- ];
- }
- public function setMergeArrayError($merge)
- {
- $this->mergeArrayError = $merge;
- }
- }
|