RedisBroadcasterTest.php 5.4 KB

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