|
12 | 12 | namespace FOS\ElasticaBundle\Persister; |
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase; |
15 | | -use Symfony\Component\DependencyInjection\Container; |
16 | | -use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
| 15 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
17 | 16 |
|
18 | 17 | class PagerPersisterRegistryTest extends TestCase |
19 | 18 | { |
20 | | - public function testShouldImplementContainerAwareInterface() |
21 | | - { |
22 | | - $rc = new \ReflectionClass(PagerPersisterRegistry::class); |
23 | | - |
24 | | - $this->assertTrue($rc->implementsInterface(ContainerAwareInterface::class)); |
25 | | - } |
26 | | - |
27 | 19 | public function testShouldBeFinal() |
28 | 20 | { |
29 | 21 | $rc = new \ReflectionClass(PagerPersisterRegistry::class); |
30 | 22 |
|
31 | 23 | $this->assertTrue($rc->isFinal()); |
32 | 24 | } |
33 | 25 |
|
34 | | - public function testCouldBeConstructedWithNameToServiceIdMap() |
35 | | - { |
36 | | - new PagerPersisterRegistry([]); |
37 | | - } |
38 | | - |
39 | 26 | public function testThrowsIfThereIsNoSuchEntryInNameToServiceIdMap() |
40 | 27 | { |
41 | | - $container = new Container(); |
42 | | - |
43 | | - $registry = new PagerPersisterRegistry([ |
44 | | - 'the_name' => 'the_service_id', |
45 | | - ]); |
46 | | - $registry->setContainer($container); |
| 28 | + $serviceLocator = $this->createMock(ServiceLocator::class); |
| 29 | + $serviceLocator->expects($this->once())->method('has')->with('the_name')->willReturn(false); |
47 | 30 |
|
48 | 31 | $this->expectException(\InvalidArgumentException::class); |
49 | | - $this->expectExceptionMessage('No pager persister was registered for the give name "the_other_name".'); |
50 | | - $registry->getPagerPersister('the_other_name'); |
51 | | - } |
| 32 | + $this->expectExceptionMessage('No pager persister was registered for the give name "the_name".'); |
52 | 33 |
|
53 | | - public function testThrowsIfRelatedServiceDoesNotImplementPagerPersisterInterface() |
54 | | - { |
55 | | - $container = new Container(); |
56 | | - $container->set('the_service_id', new \stdClass()); |
57 | | - |
58 | | - $registry = new PagerPersisterRegistry([ |
59 | | - 'the_name' => 'the_service_id', |
60 | | - ]); |
61 | | - $registry->setContainer($container); |
62 | | - |
63 | | - $this->expectException(\LogicException::class); |
64 | | - $this->expectExceptionMessage('The pager provider service "the_service_id" must implement "FOS\ElasticaBundle\Persister\PagerPersisterInterface" interface but it is an instance of "stdClass" class.'); |
65 | | - $registry->getPagerPersister('the_name'); |
| 34 | + (new PagerPersisterRegistry($serviceLocator))->getPagerPersister('the_name'); |
66 | 35 | } |
67 | 36 |
|
68 | | - public function testThrowsIfThereIsServiceWithSuchId() |
| 37 | + public function testThrowsIfRelatedServiceDoesNotImplementPagerPersisterInterface() |
69 | 38 | { |
70 | | - $container = new Container(); |
| 39 | + $serviceLocator = $this->createMock(ServiceLocator::class); |
| 40 | + $serviceLocator->expects($this->once())->method('has')->with('the_name')->willReturn(true); |
| 41 | + $serviceLocator->expects($this->once())->method('get')->with('the_name')->willReturn(new \stdClass()); |
71 | 42 |
|
72 | | - $registry = new PagerPersisterRegistry([ |
73 | | - 'the_name' => 'the_service_id', |
74 | | - ]); |
75 | | - $registry->setContainer($container); |
| 43 | + $this->expectException(\TypeError::class); |
| 44 | + $this->expectExceptionMessage('Return value of FOS\ElasticaBundle\Persister\PagerPersisterRegistry::getPagerPersister() must implement interface FOS\ElasticaBundle\Persister\PagerPersisterInterface, instance of stdClass returned'); |
76 | 45 |
|
77 | | - $this->expectException(\LogicException::class); |
78 | | - $this->expectExceptionMessage('You have requested a non-existent service "the_service_id".'); |
79 | | - $registry->getPagerPersister('the_name'); |
| 46 | + (new PagerPersisterRegistry($serviceLocator))->getPagerPersister('the_name'); |
80 | 47 | } |
81 | 48 |
|
82 | 49 | public function testShouldReturnPagerPersisterByGivenName() |
83 | 50 | { |
84 | 51 | $pagerPersisterMock = $this->createPagerPersisterMock(); |
85 | 52 |
|
86 | | - $container = new Container(); |
87 | | - $container->set('the_service_id', $pagerPersisterMock); |
| 53 | + $serviceLocator = $this->createMock(ServiceLocator::class); |
| 54 | + $serviceLocator->expects($this->once())->method('has')->with('the_name')->willReturn(true); |
| 55 | + $serviceLocator->expects($this->once())->method('get')->with('the_name')->willReturn($pagerPersisterMock); |
88 | 56 |
|
89 | | - $registry = new PagerPersisterRegistry([ |
90 | | - 'the_name' => 'the_service_id', |
91 | | - ]); |
92 | | - $registry->setContainer($container); |
| 57 | + $registry = new PagerPersisterRegistry($serviceLocator); |
93 | 58 |
|
94 | 59 | $actualPagerPersister = $registry->getPagerPersister('the_name'); |
95 | 60 |
|
|
0 commit comments