PusherBroadcasterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace Illuminate\Tests\Broadcasting;
  3. use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster;
  4. use Illuminate\Http\Request;
  5. use Mockery as m;
  6. use PHPUnit\Framework\TestCase;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. class PusherBroadcasterTest extends TestCase
  9. {
  10. /**
  11. * @var \Illuminate\Broadcasting\Broadcasters\PusherBroadcaster
  12. */
  13. public $broadcaster;
  14. public $pusher;
  15. protected function setUp(): void
  16. {
  17. parent::setUp();
  18. $this->pusher = m::mock('Pusher\Pusher');
  19. $this->broadcaster = m::mock(PusherBroadcaster::class, [$this->pusher])->makePartial();
  20. }
  21. public function testAuthCallValidAuthenticationResponseWithPrivateChannelWhenCallbackReturnTrue()
  22. {
  23. $this->broadcaster->channel('test', function () {
  24. return true;
  25. });
  26. $this->broadcaster->shouldReceive('validAuthenticationResponse')
  27. ->once();
  28. $this->broadcaster->auth(
  29. $this->getMockRequestWithUserForChannel('private-test')
  30. );
  31. }
  32. public function testAuthThrowAccessDeniedHttpExceptionWithPrivateChannelWhenCallbackReturnFalse()
  33. {
  34. $this->expectException(AccessDeniedHttpException::class);
  35. $this->broadcaster->channel('test', function () {
  36. return false;
  37. });
  38. $this->broadcaster->auth(
  39. $this->getMockRequestWithUserForChannel('private-test')
  40. );
  41. }
  42. public function testAuthThrowAccessDeniedHttpExceptionWithPrivateChannelWhenRequestUserNotFound()
  43. {
  44. $this->expectException(AccessDeniedHttpException::class);
  45. $this->broadcaster->channel('test', function () {
  46. return true;
  47. });
  48. $this->broadcaster->auth(
  49. $this->getMockRequestWithoutUserForChannel('private-test')
  50. );
  51. }
  52. public function testAuthCallValidAuthenticationResponseWithPresenceChannelWhenCallbackReturnAnArray()
  53. {
  54. $returnData = [1, 2, 3, 4];
  55. $this->broadcaster->channel('test', function () use ($returnData) {
  56. return $returnData;
  57. });
  58. $this->broadcaster->shouldReceive('validAuthenticationResponse')
  59. ->once();
  60. $this->broadcaster->auth(
  61. $this->getMockRequestWithUserForChannel('presence-test')
  62. );
  63. }
  64. public function testAuthThrowAccessDeniedHttpExceptionWithPresenceChannelWhenCallbackReturnNull()
  65. {
  66. $this->expectException(AccessDeniedHttpException::class);
  67. $this->broadcaster->channel('test', function () {
  68. //
  69. });
  70. $this->broadcaster->auth(
  71. $this->getMockRequestWithUserForChannel('presence-test')
  72. );
  73. }
  74. public function testAuthThrowAccessDeniedHttpExceptionWithPresenceChannelWhenRequestUserNotFound()
  75. {
  76. $this->expectException(AccessDeniedHttpException::class);
  77. $this->broadcaster->channel('test', function () {
  78. return [1, 2, 3, 4];
  79. });
  80. $this->broadcaster->auth(
  81. $this->getMockRequestWithoutUserForChannel('presence-test')
  82. );
  83. }
  84. public function testValidAuthenticationResponseCallPusherSocketAuthMethodWithPrivateChannel()
  85. {
  86. $request = $this->getMockRequestWithUserForChannel('private-test');
  87. $data = [
  88. 'auth' => 'abcd:efgh',
  89. ];
  90. $this->pusher->shouldReceive('socket_auth')
  91. ->once()
  92. ->andReturn(json_encode($data));
  93. $this->assertEquals(
  94. $data,
  95. $this->broadcaster->validAuthenticationResponse($request, true)
  96. );
  97. }
  98. public function testValidAuthenticationResponseCallPusherPresenceAuthMethodWithPresenceChannel()
  99. {
  100. $request = $this->getMockRequestWithUserForChannel('presence-test');
  101. $data = [
  102. 'auth' => 'abcd:efgh',
  103. 'channel_data' => [
  104. 'user_id' => 42,
  105. 'user_info' => [1, 2, 3, 4],
  106. ],
  107. ];
  108. $this->pusher->shouldReceive('presence_auth')
  109. ->once()
  110. ->andReturn(json_encode($data));
  111. $this->assertEquals(
  112. $data,
  113. $this->broadcaster->validAuthenticationResponse($request, true)
  114. );
  115. }
  116. /**
  117. * @param string $channel
  118. * @return \Illuminate\Http\Request
  119. */
  120. protected function getMockRequestWithUserForChannel($channel)
  121. {
  122. $request = m::mock(Request::class);
  123. $request->channel_name = $channel;
  124. $request->socket_id = 'abcd.1234';
  125. $request->shouldReceive('input')
  126. ->with('callback', false)
  127. ->andReturn(false);
  128. $user = m::mock('User');
  129. $user->shouldReceive('getAuthIdentifierForBroadcasting')
  130. ->andReturn(42);
  131. $user->shouldReceive('getAuthIdentifier')
  132. ->andReturn(42);
  133. $request->shouldReceive('user')
  134. ->andReturn($user);
  135. return $request;
  136. }
  137. /**
  138. * @param string $channel
  139. * @return \Illuminate\Http\Request
  140. */
  141. protected function getMockRequestWithoutUserForChannel($channel)
  142. {
  143. $request = m::mock(Request::class);
  144. $request->channel_name = $channel;
  145. $request->shouldReceive('user')
  146. ->andReturn(null);
  147. return $request;
  148. }
  149. }