|
11 | 11 |
|
12 | 12 | declare(strict_types=1); |
13 | 13 |
|
14 | | -namespace spec\Sylius\Bundle\ResourceBundle\EventListener; |
| 14 | +namespace Sylius\Bundle\ResourceBundle\Tests\Bundle\EventListener; |
15 | 15 |
|
16 | 16 | use Doctrine\Common\EventSubscriber; |
17 | 17 | use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs; |
18 | 18 | use Doctrine\ODM\MongoDB\Events; |
19 | 19 | use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
20 | | -use PhpSpec\ObjectBehavior; |
21 | | -use Prophecy\Argument; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | +use Sylius\Bundle\ResourceBundle\EventListener\ODMRepositoryClassSubscriber; |
22 | 22 | use Sylius\Resource\Metadata\MetadataInterface; |
23 | 23 | use Sylius\Resource\Metadata\RegistryInterface; |
24 | 24 |
|
25 | 25 | /** |
26 | | - * @require Doctrine\ODM\MongoDB\Events |
| 26 | + * @requires extension mongodb |
27 | 27 | */ |
28 | | -final class ODMRepositoryClassSubscriberSpec extends ObjectBehavior |
| 28 | +final class ODMRepositoryClassSubscriberTest extends TestCase |
29 | 29 | { |
30 | | - function let(RegistryInterface $registry, LoadClassMetadataEventArgs $event, ClassMetadata $classMetadata): void |
31 | | - { |
32 | | - $classMetadata->getName()->willReturn('Foo'); |
33 | | - $event->getClassMetadata()->willReturn($classMetadata); |
| 30 | + private RegistryInterface $registry; |
34 | 31 |
|
35 | | - $this->beConstructedWith($registry); |
36 | | - } |
| 32 | + private ODMRepositoryClassSubscriber $subscriber; |
37 | 33 |
|
38 | | - function it_implements_event_subscriber_interface(): void |
| 34 | + protected function setUp(): void |
39 | 35 | { |
40 | | - $this->shouldImplement(EventSubscriber::class); |
| 36 | + $this->registry = $this->createMock(RegistryInterface::class); |
| 37 | + $this->subscriber = new ODMRepositoryClassSubscriber($this->registry); |
41 | 38 | } |
42 | 39 |
|
43 | | - function it_is_subscribed_to_load_class_metadata_doctrine_orm_event(): void |
| 40 | + public function testItImplementsEventSubscriberInterface(): void |
44 | 41 | { |
45 | | - $this->getSubscribedEvents()->shouldReturn([Events::loadClassMetadata]); |
| 42 | + $this->assertInstanceOf(EventSubscriber::class, $this->subscriber); |
46 | 43 | } |
47 | 44 |
|
48 | | - function it_sets_custom_repository_class(LoadClassMetadataEventArgs $event, RegistryInterface $registry, ClassMetadata $classMetadata, MetadataInterface $metadata): void |
| 45 | + public function testItSubscribesToLoadClassMetadataEvent(): void |
49 | 46 | { |
50 | | - $registry->getByClass('Foo')->willReturn($metadata); |
51 | | - $metadata->hasClass('repository')->willReturn(true); |
52 | | - $metadata->getClass('repository')->willReturn('FooRepository'); |
53 | | - |
54 | | - $classMetadata->setCustomRepositoryClass('FooRepository')->shouldBeCalled(); |
55 | | - |
56 | | - $this->loadClassMetadata($event); |
| 47 | + $this->assertSame([Events::loadClassMetadata], $this->subscriber->getSubscribedEvents()); |
57 | 48 | } |
58 | 49 |
|
59 | | - function it_does_not_set_custom_repository_class_if_not_configured(LoadClassMetadataEventArgs $event, RegistryInterface $registry, ClassMetadata $classMetadata, MetadataInterface $metadata): void |
| 50 | + public function testItSetsCustomRepositoryClassWhenConfigured(): void |
60 | 51 | { |
61 | | - $registry->getByClass('Foo')->willReturn($metadata); |
62 | | - $metadata->hasClass('repository')->willReturn(false); |
63 | | - |
64 | | - $classMetadata->setCustomRepositoryClass(Argument::any())->shouldNotBeCalled(); |
65 | | - |
66 | | - $this->loadClassMetadata($event); |
| 52 | + $metadata = $this->createMock(MetadataInterface::class); |
| 53 | + $metadata |
| 54 | + ->expects($this->once()) |
| 55 | + ->method('hasClass') |
| 56 | + ->with('repository') |
| 57 | + ->willReturn(true); |
| 58 | + $metadata |
| 59 | + ->expects($this->once()) |
| 60 | + ->method('getClass') |
| 61 | + ->with('repository') |
| 62 | + ->willReturn('FooRepository'); |
| 63 | + |
| 64 | + $this->registry |
| 65 | + ->expects($this->once()) |
| 66 | + ->method('getByClass') |
| 67 | + ->with('Foo') |
| 68 | + ->willReturn($metadata); |
| 69 | + |
| 70 | + $classMetadata = $this->createMock(ClassMetadata::class); |
| 71 | + $classMetadata |
| 72 | + ->expects($this->once()) |
| 73 | + ->method('getName') |
| 74 | + ->willReturn('Foo'); |
| 75 | + $classMetadata |
| 76 | + ->expects($this->once()) |
| 77 | + ->method('setCustomRepositoryClass') |
| 78 | + ->with('FooRepository'); |
| 79 | + |
| 80 | + $event = $this->createMock(LoadClassMetadataEventArgs::class); |
| 81 | + $event |
| 82 | + ->expects($this->once()) |
| 83 | + ->method('getClassMetadata') |
| 84 | + ->willReturn($classMetadata); |
| 85 | + |
| 86 | + $this->subscriber->loadClassMetadata($event); |
67 | 87 | } |
68 | 88 |
|
69 | | - function it_does_not_set_custom_repository_class_if_registry_does_not_have_class(LoadClassMetadataEventArgs $event, RegistryInterface $registry, ClassMetadata $classMetadata): void |
| 89 | + public function testItDoesNotSetCustomRepositoryClassWhenNotConfigured(): void |
70 | 90 | { |
71 | | - $registry->getByClass('Foo')->willThrow(\InvalidArgumentException::class); |
72 | | - |
73 | | - $classMetadata->setCustomRepositoryClass(Argument::any())->shouldNotBeCalled(); |
| 91 | + $metadata = $this->createMock(MetadataInterface::class); |
| 92 | + $metadata |
| 93 | + ->expects($this->once()) |
| 94 | + ->method('hasClass') |
| 95 | + ->with('repository') |
| 96 | + ->willReturn(false); |
| 97 | + |
| 98 | + $this->registry |
| 99 | + ->expects($this->once()) |
| 100 | + ->method('getByClass') |
| 101 | + ->with('Foo') |
| 102 | + ->willReturn($metadata); |
| 103 | + |
| 104 | + $classMetadata = $this->createMock(ClassMetadata::class); |
| 105 | + $classMetadata |
| 106 | + ->expects($this->once()) |
| 107 | + ->method('getName') |
| 108 | + ->willReturn('Foo'); |
| 109 | + $classMetadata |
| 110 | + ->expects($this->never()) |
| 111 | + ->method('setCustomRepositoryClass'); |
| 112 | + |
| 113 | + $event = $this->createMock(LoadClassMetadataEventArgs::class); |
| 114 | + $event |
| 115 | + ->expects($this->once()) |
| 116 | + ->method('getClassMetadata') |
| 117 | + ->willReturn($classMetadata); |
| 118 | + |
| 119 | + $this->subscriber->loadClassMetadata($event); |
| 120 | + } |
74 | 121 |
|
75 | | - $this->loadClassMetadata($event); |
| 122 | + public function testItDoesNotSetCustomRepositoryClassWhenClassNotInRegistry(): void |
| 123 | + { |
| 124 | + $this->registry |
| 125 | + ->expects($this->once()) |
| 126 | + ->method('getByClass') |
| 127 | + ->with('Foo') |
| 128 | + ->willThrowException(new \InvalidArgumentException()); |
| 129 | + |
| 130 | + $classMetadata = $this->createMock(ClassMetadata::class); |
| 131 | + $classMetadata |
| 132 | + ->expects($this->once()) |
| 133 | + ->method('getName') |
| 134 | + ->willReturn('Foo'); |
| 135 | + $classMetadata |
| 136 | + ->expects($this->never()) |
| 137 | + ->method('setCustomRepositoryClass'); |
| 138 | + |
| 139 | + $event = $this->createMock(LoadClassMetadataEventArgs::class); |
| 140 | + $event |
| 141 | + ->expects($this->once()) |
| 142 | + ->method('getClassMetadata') |
| 143 | + ->willReturn($classMetadata); |
| 144 | + |
| 145 | + $this->subscriber->loadClassMetadata($event); |
76 | 146 | } |
77 | 147 | } |
0 commit comments