AuthAccessResponseTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Illuminate\Tests\Auth;
  3. use Illuminate\Auth\Access\AuthorizationException;
  4. use Illuminate\Auth\Access\Response;
  5. use PHPUnit\Framework\TestCase;
  6. class AuthAccessResponseTest extends TestCase
  7. {
  8. public function testAllowMethod()
  9. {
  10. $response = Response::allow('some message', 'some_code');
  11. $this->assertTrue($response->allowed());
  12. $this->assertFalse($response->denied());
  13. $this->assertSame('some message', $response->message());
  14. $this->assertSame('some_code', $response->code());
  15. }
  16. public function testDenyMethod()
  17. {
  18. $response = Response::deny('some message', 'some_code');
  19. $this->assertTrue($response->denied());
  20. $this->assertFalse($response->allowed());
  21. $this->assertSame('some message', $response->message());
  22. $this->assertSame('some_code', $response->code());
  23. }
  24. public function testDenyMethodWithNoMessageReturnsNull()
  25. {
  26. $response = Response::deny();
  27. $this->assertNull($response->message());
  28. }
  29. public function testAuthorizeMethodThrowsAuthorizationExceptionWhenResponseDenied()
  30. {
  31. $response = Response::deny('Some message.', 'some_code');
  32. try {
  33. $response->authorize();
  34. } catch (AuthorizationException $e) {
  35. $this->assertSame('Some message.', $e->getMessage());
  36. $this->assertSame('some_code', $e->getCode());
  37. $this->assertEquals($response, $e->response());
  38. }
  39. }
  40. public function testAuthorizeMethodThrowsAuthorizationExceptionWithDefaultMessage()
  41. {
  42. $response = Response::deny();
  43. try {
  44. $response->authorize();
  45. } catch (AuthorizationException $e) {
  46. $this->assertSame('This action is unauthorized.', $e->getMessage());
  47. }
  48. }
  49. public function testThrowIfNeededDoesntThrowAuthorizationExceptionWhenResponseAllowed()
  50. {
  51. $response = Response::allow('Some message.', 'some_code');
  52. $this->assertEquals($response, $response->authorize());
  53. }
  54. public function testCastingToStringReturnsMessage()
  55. {
  56. $response = new Response(true, 'some data');
  57. $this->assertSame('some data', (string) $response);
  58. $response = new Response(false, null);
  59. $this->assertSame('', (string) $response);
  60. }
  61. public function testResponseToArrayMethod()
  62. {
  63. $response = new Response(false, 'Not allowed.', 'some_code');
  64. $this->assertEquals([
  65. 'allowed' => false,
  66. 'message' => 'Not allowed.',
  67. 'code' => 'some_code',
  68. ], $response->toArray());
  69. }
  70. }