BroadcastEventTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Illuminate\Tests\Broadcasting;
  3. use Illuminate\Broadcasting\BroadcastEvent;
  4. use Illuminate\Broadcasting\InteractsWithBroadcasting;
  5. use Illuminate\Contracts\Broadcasting\Broadcaster;
  6. use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. class BroadcastEventTest extends TestCase
  10. {
  11. protected function tearDown(): void
  12. {
  13. m::close();
  14. }
  15. public function testBasicEventBroadcastParameterFormatting()
  16. {
  17. $broadcaster = m::mock(Broadcaster::class);
  18. $broadcaster->shouldReceive('broadcast')->once()->with(
  19. ['test-channel'], TestBroadcastEvent::class, ['firstName' => 'Taylor', 'lastName' => 'Otwell', 'collection' => ['foo' => 'bar']]
  20. );
  21. $manager = m::mock(BroadcastingFactory::class);
  22. $manager->shouldReceive('connection')->once()->with(null)->andReturn($broadcaster);
  23. $event = new TestBroadcastEvent;
  24. (new BroadcastEvent($event))->handle($manager);
  25. }
  26. public function testManualParameterSpecification()
  27. {
  28. $broadcaster = m::mock(Broadcaster::class);
  29. $broadcaster->shouldReceive('broadcast')->once()->with(
  30. ['test-channel'], TestBroadcastEventWithManualData::class, ['name' => 'Taylor', 'socket' => null]
  31. );
  32. $manager = m::mock(BroadcastingFactory::class);
  33. $manager->shouldReceive('connection')->once()->with(null)->andReturn($broadcaster);
  34. $event = new TestBroadcastEventWithManualData;
  35. (new BroadcastEvent($event))->handle($manager);
  36. }
  37. public function testSpecificBroadcasterGiven()
  38. {
  39. $broadcaster = m::mock(Broadcaster::class);
  40. $broadcaster->shouldReceive('broadcast')->once();
  41. $manager = m::mock(BroadcastingFactory::class);
  42. $manager->shouldReceive('connection')->once()->with('log')->andReturn($broadcaster);
  43. $event = new TestBroadcastEventWithSpecificBroadcaster;
  44. (new BroadcastEvent($event))->handle($manager);
  45. }
  46. }
  47. class TestBroadcastEvent
  48. {
  49. public $firstName = 'Taylor';
  50. public $lastName = 'Otwell';
  51. public $collection;
  52. private $title = 'Developer';
  53. public function __construct()
  54. {
  55. $this->collection = collect(['foo' => 'bar']);
  56. }
  57. public function broadcastOn()
  58. {
  59. return ['test-channel'];
  60. }
  61. }
  62. class TestBroadcastEventWithManualData extends TestBroadcastEvent
  63. {
  64. public function broadcastWith()
  65. {
  66. return ['name' => 'Taylor'];
  67. }
  68. }
  69. class TestBroadcastEventWithSpecificBroadcaster extends TestBroadcastEvent
  70. {
  71. use InteractsWithBroadcasting;
  72. public function __construct()
  73. {
  74. $this->broadcastVia('log');
  75. }
  76. }