WebauthnRenameRequestTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Tests\Feature\Http\Requests;
  3. use App\Http\Requests\WebauthnRenameRequest;
  4. use Illuminate\Foundation\Testing\WithoutMiddleware;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Support\Facades\Validator;
  8. use PHPUnit\Framework\Attributes\CoversClass;
  9. use PHPUnit\Framework\Attributes\DataProvider;
  10. use PHPUnit\Framework\Attributes\Test;
  11. use Tests\TestCase;
  12. /**
  13. * WebauthnRenameRequestTest test class
  14. */
  15. #[CoversClass(WebauthnRenameRequest::class)]
  16. class WebauthnRenameRequestTest extends TestCase
  17. {
  18. use WithoutMiddleware;
  19. #[Test]
  20. public function test_user_is_authorized()
  21. {
  22. Auth::shouldReceive('check')
  23. ->once()
  24. ->andReturn(true);
  25. Gate::shouldReceive('allows')
  26. ->with('manage-webauthn-credentials')
  27. ->once()
  28. ->andReturn(true);
  29. $request = new WebauthnRenameRequest;
  30. $this->assertTrue($request->authorize());
  31. }
  32. #[Test]
  33. #[DataProvider('provideValidData')]
  34. public function test_valid_data(array $data) : void
  35. {
  36. $request = new WebauthnRenameRequest;
  37. $request->merge($data);
  38. $validator = Validator::make($data, $request->rules());
  39. $this->assertFalse($validator->fails());
  40. }
  41. /**
  42. * Provide Valid data for validation test
  43. */
  44. public static function provideValidData() : array
  45. {
  46. return [
  47. [[
  48. 'name' => 'Yubikey',
  49. ]],
  50. ];
  51. }
  52. #[Test]
  53. #[DataProvider('provideInvalidData')]
  54. public function test_invalid_data(array $data) : void
  55. {
  56. $request = new WebauthnRenameRequest;
  57. $request->merge($data);
  58. $validator = Validator::make($data, $request->rules());
  59. $this->assertTrue($validator->fails());
  60. }
  61. /**
  62. * Provide invalid data for validation test
  63. */
  64. public static function provideInvalidData() : array
  65. {
  66. return [
  67. [[
  68. 'name' => '', // required
  69. ]],
  70. [[
  71. 'name' => true, // string
  72. ]],
  73. [[
  74. 'name' => 0, // string
  75. ]],
  76. ];
  77. }
  78. }