BaseFormRequestTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Http\Requests\FormRequest;
  4. use Illuminate\Validation\ValidationException;
  5. use Tests\TestCase;
  6. use Illuminate\Foundation\Testing\WithFaker;
  7. use Illuminate\Foundation\Testing\RefreshDatabase;
  8. class BaseFormRequestTest extends TestCase
  9. {
  10. public function testMergeArrayError()
  11. {
  12. // 测试合并
  13. $request = $this->makeRequest(['array' => ['not integer']]);
  14. try {
  15. $request->validateResolved();
  16. } catch (ValidationException $e) {
  17. $this->assertTrue(key_exists('array', $e->validator->errors()->messages()));
  18. }
  19. // 测试不合并
  20. $request = $this->makeRequest(['array' => ['not integer']]);
  21. $request->setMergeArrayError(false);
  22. try {
  23. $request->validateResolved();
  24. } catch (ValidationException $e) {
  25. $this->assertFalse(key_exists('array', $e->validator->errors()->messages()));
  26. }
  27. }
  28. /**
  29. * 指定请求数据, 生成一个 表单请求 实例
  30. *
  31. * @param array $data
  32. *
  33. * @return DummyFormRequest
  34. */
  35. protected function makeRequest($data = [])
  36. {
  37. $request = new DummyFormRequest($data);
  38. app()->call([$request, 'setContainer']);
  39. app()->call([$request, 'setRedirector']);
  40. return $request;
  41. }
  42. }
  43. class DummyFormRequest extends FormRequest
  44. {
  45. public function rules()
  46. {
  47. return [
  48. 'array.*' => 'integer',
  49. ];
  50. }
  51. public function setMergeArrayError($merge)
  52. {
  53. $this->mergeArrayError = $merge;
  54. }
  55. }