|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Bundle\Tests\Unit\Events; |
| 4 | + |
| 5 | +use Enqueue\Bundle\Events\EventTransformer; |
| 6 | +use Enqueue\Bundle\Events\PhpSerializerEventTransformer; |
| 7 | +use Enqueue\Client\Message; |
| 8 | +use Enqueue\Null\NullMessage; |
| 9 | +use Enqueue\Test\ClassExtensionTrait; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\EventDispatcher\GenericEvent; |
| 12 | +use Symfony\Component\HttpKernel\Kernel; |
| 13 | + |
| 14 | +class PhpSerializerEventTransformerTest extends TestCase |
| 15 | +{ |
| 16 | + use ClassExtensionTrait; |
| 17 | + |
| 18 | + public function testShouldImplementEventTransformerInterface() |
| 19 | + { |
| 20 | + $this->assertClassImplements(EventTransformer::class, PhpSerializerEventTransformer::class); |
| 21 | + } |
| 22 | + |
| 23 | + public function testCouldBeConstructedWithoutAnyArguments() |
| 24 | + { |
| 25 | + new PhpSerializerEventTransformer(); |
| 26 | + } |
| 27 | + |
| 28 | + public function testShouldReturnMessageWithPhpSerializedEventAsBodyOnToMessage() |
| 29 | + { |
| 30 | + if (version_compare(Kernel::VERSION, '3.0', '<')) { |
| 31 | + $this->markTestSkipped('This functionality only works on Symfony 3.0 or higher'); |
| 32 | + } |
| 33 | + |
| 34 | + $transformer = new PhpSerializerEventTransformer(); |
| 35 | + |
| 36 | + $message = $transformer->toMessage(new GenericEvent('theSubject')); |
| 37 | + |
| 38 | + $this->assertInstanceOf(Message::class, $message); |
| 39 | + $this->assertEquals(serialize(new GenericEvent('theSubject')), $message->getBody()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testShouldReturnEventUnserializedFromMessageBodyOnToEvent() |
| 43 | + { |
| 44 | + if (version_compare(Kernel::VERSION, '3.0', '<')) { |
| 45 | + $this->markTestSkipped('This functionality only works on Symfony 3.0 or higher'); |
| 46 | + } |
| 47 | + |
| 48 | + $message = new NullMessage(); |
| 49 | + $message->setBody(serialize(new GenericEvent('theSubject'))); |
| 50 | + |
| 51 | + $transformer = new PhpSerializerEventTransformer(); |
| 52 | + |
| 53 | + $event = $transformer->toEvent('anEventName', $message); |
| 54 | + |
| 55 | + $this->assertInstanceOf(GenericEvent::class, $event); |
| 56 | + $this->assertEquals('tehSubject', $event->getSubject()); |
| 57 | + } |
| 58 | + |
| 59 | + public function testThrowNotSupportedExceptionOnSymfonyPrior30OnToMessage() |
| 60 | + { |
| 61 | + if (version_compare(Kernel::VERSION, '3.0', '>=')) { |
| 62 | + $this->markTestSkipped('This functionality only works on Symfony 3.0 or higher'); |
| 63 | + } |
| 64 | + |
| 65 | + $this->expectException(\LogicException::class); |
| 66 | + $this->expectExceptionMessage('This transformer does not work on Symfony prior 3.0.'); |
| 67 | + |
| 68 | + $transformer = new PhpSerializerEventTransformer(); |
| 69 | + |
| 70 | + $transformer->toMessage(new GenericEvent()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testThrowNotSupportedExceptionOnSymfonyPrior30OnToEvent() |
| 74 | + { |
| 75 | + if (version_compare(Kernel::VERSION, '3.0', '>=')) { |
| 76 | + $this->markTestSkipped('This functionality only works on Symfony 3.0 or higher'); |
| 77 | + } |
| 78 | + |
| 79 | + $this->expectException(\LogicException::class); |
| 80 | + $this->expectExceptionMessage('This transformer does not work on Symfony prior 3.0.'); |
| 81 | + |
| 82 | + $transformer = new PhpSerializerEventTransformer(); |
| 83 | + |
| 84 | + $transformer->toEvent('anEvent', new NullMessage()); |
| 85 | + } |
| 86 | +} |
0 commit comments