|
| 1 | +<?php |
| 2 | + |
| 3 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 4 | +use Doctrine\ORM\EntityManagerInterface; |
| 5 | +use LaravelDoctrine\ORM\Exceptions\NoEntityManagerFound; |
| 6 | +use LaravelDoctrine\ORM\Notifications\DoctrineChannel; |
| 7 | +use LaravelDoctrine\ORM\Notifications\Notifiable; |
| 8 | +use Mockery\Mock; |
| 9 | + |
| 10 | +class DoctrineChannelTest extends PHPUnit_Framework_TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var DoctrineChannel |
| 14 | + */ |
| 15 | + private $channel; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var Mock |
| 19 | + */ |
| 20 | + private $registry; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var Mock |
| 24 | + */ |
| 25 | + private $em; |
| 26 | + |
| 27 | + public function setUp() |
| 28 | + { |
| 29 | + $this->em = Mockery::spy(EntityManagerInterface::class); |
| 30 | + |
| 31 | + $this->channel = new DoctrineChannel( |
| 32 | + $this->registry = Mockery::mock(ManagerRegistry::class) |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function test_can_send_notification_on_default_em() |
| 37 | + { |
| 38 | + $this->registry->shouldReceive('getManagerForClass') |
| 39 | + ->with('LaravelDoctrine\ORM\Notifications\Notification') |
| 40 | + ->andReturn($this->em); |
| 41 | + |
| 42 | + $this->channel->send(new NotifiableStub, new NotificationStub); |
| 43 | + |
| 44 | + $this->em->shouldHaveReceived('persist')->once(); |
| 45 | + $this->em->shouldHaveReceived('flush')->once(); |
| 46 | + } |
| 47 | + |
| 48 | + public function test_can_send_notification_on_custom_em() |
| 49 | + { |
| 50 | + $this->registry->shouldReceive('getManager') |
| 51 | + ->with('custom') |
| 52 | + ->andReturn($this->em); |
| 53 | + |
| 54 | + $this->channel->send(new CustomNotifiableStub, new NotificationStub); |
| 55 | + |
| 56 | + $this->em->shouldHaveReceived('persist')->once(); |
| 57 | + $this->em->shouldHaveReceived('flush')->once(); |
| 58 | + } |
| 59 | + |
| 60 | + public function test_it_should_throw_exception_when_it_does_not_find_an_em() |
| 61 | + { |
| 62 | + $this->setExpectedException(NoEntityManagerFound::class); |
| 63 | + |
| 64 | + $this->registry->shouldReceive('getManager') |
| 65 | + ->with('custom') |
| 66 | + ->andReturnNull(); |
| 67 | + |
| 68 | + $this->channel->send(new CustomNotifiableStub, new NotificationStub); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class NotificationStub extends \Illuminate\Notifications\Notification |
| 73 | +{ |
| 74 | + public function toEntity() |
| 75 | + { |
| 76 | + return (new \LaravelDoctrine\ORM\Notifications\Notification); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class NotifiableStub |
| 81 | +{ |
| 82 | + use Notifiable; |
| 83 | +} |
| 84 | + |
| 85 | +class CustomNotifiableStub |
| 86 | +{ |
| 87 | + use Notifiable; |
| 88 | + |
| 89 | + public function routeNotificationForDoctrine() |
| 90 | + { |
| 91 | + return 'custom'; |
| 92 | + } |
| 93 | +} |
0 commit comments