Skip to content

Commit 3b425e3

Browse files
Change PHPSpec to PHPUnit
1 parent c652e38 commit 3b425e3

File tree

6 files changed

+401
-204
lines changed

6 files changed

+401
-204
lines changed

src/Bundle/spec/Event/ResourceControllerEventSpec.php

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,87 +11,98 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace spec\Sylius\Bundle\ResourceBundle\Event;
14+
namespace Sylius\Bundle\ResourceBundle\Tests\Bundle\Event;
1515

16-
use PhpSpec\ObjectBehavior;
16+
use PHPUnit\Framework\TestCase;
1717
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
1818
use Symfony\Component\HttpFoundation\Response;
1919

20-
final class ResourceControllerEventSpec extends ObjectBehavior
20+
final class ResourceControllerEventTest extends TestCase
2121
{
22-
function let(): void
22+
private ResourceControllerEvent $event;
23+
24+
protected function setUp(): void
2325
{
24-
$this->beConstructedWith('message');
26+
$this->event = new ResourceControllerEvent('message');
2527
}
2628

27-
function it_stops_event_propagation(): void
29+
public function testItHasEmptyMessageByDefault(): void
2830
{
29-
$this->stop('message', ResourceControllerEvent::TYPE_SUCCESS, ['parameter']);
30-
$this->getMessageType()->shouldReturn(ResourceControllerEvent::TYPE_SUCCESS);
31-
$this->getMessageParameters()->shouldReturn(['parameter']);
32-
$this->getMessage()->shouldReturn('message');
33-
$this->isPropagationStopped()->shouldReturn(true);
31+
$event = new ResourceControllerEvent();
32+
33+
$this->assertSame('', $event->getMessage());
3434
}
3535

36-
function it_check_if_an_error_has_been_detected(): void
36+
public function testItCanBeInstantiatedWithMessage(): void
3737
{
38-
$this->isStopped()->shouldReturn(false);
39-
$this->stop('message');
40-
$this->isStopped()->shouldReturn(true);
38+
$this->assertSame('message', $this->event->getMessage());
4139
}
4240

43-
function it_has_no_message_type_by_default(): void
41+
public function testItCanSetAndGetMessage(): void
4442
{
45-
$this->getMessageType()->shouldReturn('');
43+
$this->event->setMessage('custom_message');
44+
45+
$this->assertSame('custom_message', $this->event->getMessage());
4646
}
4747

48-
function its_message_type_is_mutable(): void
48+
public function testItHasEmptyMessageTypeByDefault(): void
4949
{
50-
$this->setMessageType(ResourceControllerEvent::TYPE_SUCCESS);
51-
$this->getMessageType()->shouldReturn(ResourceControllerEvent::TYPE_SUCCESS);
50+
$this->assertSame('', $this->event->getMessageType());
5251
}
5352

54-
function it_has_not_message_by_default(): void
53+
public function testItCanSetAndGetMessageType(): void
5554
{
56-
$this->getMessage()->shouldReturn('');
55+
$this->event->setMessageType(ResourceControllerEvent::TYPE_SUCCESS);
56+
57+
$this->assertSame(ResourceControllerEvent::TYPE_SUCCESS, $this->event->getMessageType());
5758
}
5859

59-
function its_message_is_mutable(): void
60+
public function testItHasEmptyMessageParametersByDefault(): void
6061
{
61-
$this->setMessage('message');
62-
$this->getMessage()->shouldReturn('message');
62+
$this->assertSame([], $this->event->getMessageParameters());
6363
}
6464

65-
function it_has_empty_message_parameters_by_default(): void
65+
public function testItCanSetAndGetMessageParameters(): void
6666
{
67-
$this->getMessageParameters()->shouldReturn([]);
67+
$this->event->setMessageParameters(['parameter_1', 'parameter_2']);
68+
69+
$this->assertSame(['parameter_1', 'parameter_2'], $this->event->getMessageParameters());
6870
}
6971

70-
function its_message_parameter_is_mutable(): void
72+
public function testItIsNotStoppedByDefault(): void
7173
{
72-
$this->setMessageParameters(['parameters']);
73-
$this->getMessageParameters()->shouldReturn(['parameters']);
74+
$this->assertFalse($this->event->isStopped());
7475
}
7576

76-
function it_has_response(): void
77+
public function testItCanBeStopped(): void
7778
{
78-
$response = new Response();
79-
80-
$this->setResponse($response);
79+
$this->event->stop('error_message');
8180

82-
$this->getResponse()->shouldReturn($response);
81+
$this->assertTrue($this->event->isStopped());
8382
}
8483

85-
function it_has_response_if_it_was_set_before(): void
84+
public function testItStopsPropagationWhenStopped(): void
8685
{
87-
$response = new Response();
88-
$this->setResponse($response);
86+
$this->event->stop('error_message', ResourceControllerEvent::TYPE_SUCCESS, ['parameter']);
87+
88+
$this->assertTrue($this->event->isPropagationStopped());
89+
$this->assertSame('error_message', $this->event->getMessage());
90+
$this->assertSame(ResourceControllerEvent::TYPE_SUCCESS, $this->event->getMessageType());
91+
$this->assertSame(['parameter'], $this->event->getMessageParameters());
92+
}
8993

90-
$this->hasResponse()->shouldReturn(true);
94+
public function testItDoesNotHaveResponseByDefault(): void
95+
{
96+
$this->assertFalse($this->event->hasResponse());
9197
}
9298

93-
function it_has_not_response_if_it_was_not_set_before(): void
99+
public function testItCanSetAndGetResponse(): void
94100
{
95-
$this->hasResponse()->shouldReturn(false);
101+
$response = new Response();
102+
103+
$this->event->setResponse($response);
104+
105+
$this->assertSame($response, $this->event->getResponse());
106+
$this->assertTrue($this->event->hasResponse());
96107
}
97108
}

src/Bundle/spec/EventListener/ODMRepositoryClassSubscriberSpec.php

Lines changed: 105 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,137 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace spec\Sylius\Bundle\ResourceBundle\EventListener;
14+
namespace Sylius\Bundle\ResourceBundle\Tests\Bundle\EventListener;
1515

1616
use Doctrine\Common\EventSubscriber;
1717
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
1818
use Doctrine\ODM\MongoDB\Events;
1919
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;
2222
use Sylius\Resource\Metadata\MetadataInterface;
2323
use Sylius\Resource\Metadata\RegistryInterface;
2424

2525
/**
26-
* @require Doctrine\ODM\MongoDB\Events
26+
* @requires extension mongodb
2727
*/
28-
final class ODMRepositoryClassSubscriberSpec extends ObjectBehavior
28+
final class ODMRepositoryClassSubscriberTest extends TestCase
2929
{
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;
3431

35-
$this->beConstructedWith($registry);
36-
}
32+
private ODMRepositoryClassSubscriber $subscriber;
3733

38-
function it_implements_event_subscriber_interface(): void
34+
protected function setUp(): void
3935
{
40-
$this->shouldImplement(EventSubscriber::class);
36+
$this->registry = $this->createMock(RegistryInterface::class);
37+
$this->subscriber = new ODMRepositoryClassSubscriber($this->registry);
4138
}
4239

43-
function it_is_subscribed_to_load_class_metadata_doctrine_orm_event(): void
40+
public function testItImplementsEventSubscriberInterface(): void
4441
{
45-
$this->getSubscribedEvents()->shouldReturn([Events::loadClassMetadata]);
42+
$this->assertInstanceOf(EventSubscriber::class, $this->subscriber);
4643
}
4744

48-
function it_sets_custom_repository_class(LoadClassMetadataEventArgs $event, RegistryInterface $registry, ClassMetadata $classMetadata, MetadataInterface $metadata): void
45+
public function testItSubscribesToLoadClassMetadataEvent(): void
4946
{
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());
5748
}
5849

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
6051
{
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);
6787
}
6888

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
7090
{
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+
}
74121

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);
76146
}
77147
}

0 commit comments

Comments
 (0)