|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tjovaisas\Bundle\DelayedEventBundle\Tests\Functional; |
| 6 | + |
| 7 | +use stdClass; |
| 8 | +use Doctrine\ORM\EntityManagerInterface; |
| 9 | +use Symfony\Component\HttpKernel\Kernel; |
| 10 | +use PHPUnit\Framework\MockObject\MockObject; |
| 11 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 12 | +use Tjovaisas\Bundle\DelayedEventBundle\Tests\Functional\Fixtures\Events; |
| 13 | +use Tjovaisas\Bundle\DelayedEventBundle\Tests\Functional\Fixtures\Entity\Entity; |
| 14 | +use Tjovaisas\Bundle\DelayedEventBundle\Tests\Functional\Fixtures\Event\SomeEvent; |
| 15 | +use Tjovaisas\Bundle\DelayedEventBundle\Tests\Functional\Fixtures\Listener\AttributeEventListener; |
| 16 | + |
| 17 | +class AttributeEventsTest extends FunctionalTestCase |
| 18 | +{ |
| 19 | + private MockObject|AttributeEventListener $attributeEventListener; |
| 20 | + private EventDispatcherInterface $eventDispatcher; |
| 21 | + private EntityManagerInterface $entityManager; |
| 22 | + |
| 23 | + protected function setUp(): void |
| 24 | + { |
| 25 | + $container = $this->setUpContainer(); |
| 26 | + |
| 27 | + $this->setUpDatabase(); |
| 28 | + |
| 29 | + $this->eventDispatcher = $container->get('event_dispatcher'); |
| 30 | + $this->entityManager = $container->get('doctrine.orm.entity_manager'); |
| 31 | + $this->attributeEventListener = $this->createMock(AttributeEventListener::class); |
| 32 | + |
| 33 | + $container->set('attribute_event_listener', $this->attributeEventListener); |
| 34 | + } |
| 35 | + |
| 36 | + public function testAttributesMethodIsNotDefined(): void |
| 37 | + { |
| 38 | + $this->attributeEventListener |
| 39 | + ->expects(static::once()) |
| 40 | + ->method('onFoo') |
| 41 | + ; |
| 42 | + |
| 43 | + $this->eventDispatcher->dispatch(new stdClass(), 'foo'); |
| 44 | + |
| 45 | + $this->entityManager->flush(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider dataProviderDefinedEvents |
| 50 | + */ |
| 51 | + public function testListenerIsTriggeredOnMultipleOccasions(?string $event, string $method): void |
| 52 | + { |
| 53 | + $entity = new Entity(); |
| 54 | + |
| 55 | + $this->entityManager->persist($entity); |
| 56 | + |
| 57 | + $this->eventDispatcher->dispatch(new SomeEvent($entity), $event); |
| 58 | + |
| 59 | + $this->attributeEventListener |
| 60 | + ->expects(static::once()) |
| 61 | + ->method($method) |
| 62 | + ->willReturnCallback(function (SomeEvent $someEvent): void { |
| 63 | + $this->assertSame(1, $someEvent->getEntity()->getId()); |
| 64 | + }) |
| 65 | + ; |
| 66 | + |
| 67 | + $this->entityManager->flush(); |
| 68 | + } |
| 69 | + |
| 70 | + public function dataProviderDefinedEvents(): array |
| 71 | + { |
| 72 | + $providers = [ |
| 73 | + 'listener is triggered on class defined event' => [ |
| 74 | + null, |
| 75 | + 'onClassDefinedEvent', |
| 76 | + ], |
| 77 | + ]; |
| 78 | + |
| 79 | + if (Kernel::VERSION_ID >= 60000) { |
| 80 | + $providers = array_merge( |
| 81 | + $providers, |
| 82 | + [ |
| 83 | + 'listener is triggered on method defined event' => [ |
| 84 | + Events::ON_EVENT, |
| 85 | + 'onMethodDefinedEvent', |
| 86 | + ], |
| 87 | + ], |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return $providers; |
| 92 | + } |
| 93 | +} |
0 commit comments