1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace spec\Sylius\Component\Registry; |
15
|
|
|
|
16
|
|
|
require_once __DIR__.'/Fixture/SampleServiceInterface.php'; |
17
|
|
|
|
18
|
|
|
use PhpSpec\ObjectBehavior; |
19
|
|
|
use Prophecy\Argument; |
20
|
|
|
use spec\Sylius\Component\Registry\Fixture\SampleServiceInterface; |
21
|
|
|
use Sylius\Component\Registry\NonExistingServiceException; |
22
|
|
|
use Sylius\Component\Registry\PrioritizedServiceRegistry; |
23
|
|
|
use Sylius\Component\Registry\PrioritizedServiceRegistryInterface; |
24
|
|
|
use Zend\Stdlib\PriorityQueue; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Mark McKelvie <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class PrioritizedServiceRegistrySpec extends ObjectBehavior |
30
|
|
|
{ |
31
|
|
|
function let(): void |
32
|
|
|
{ |
33
|
|
|
$this->beConstructedWith(SampleServiceInterface::class); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function it_implements_prioritized_service_registry_interface(): void |
37
|
|
|
{ |
38
|
|
|
$this->shouldImplement(PrioritizedServiceRegistryInterface::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
function it_initializes_services_priority_queue_by_default(): void |
42
|
|
|
{ |
43
|
|
|
$this->all()->shouldReturnAnInstanceOf(PriorityQueue::class); |
44
|
|
|
|
45
|
|
|
$this->all()->shouldBeEmpty(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
function it_registers_services_in_the_correct_prioritized_order( |
49
|
|
|
SampleServiceInterface $serviceOne, |
50
|
|
|
SampleServiceInterface $serviceTwo, |
51
|
|
|
SampleServiceInterface $serviceThree |
52
|
|
|
): void { |
53
|
|
|
$this->has($serviceOne)->shouldReturn(false); |
54
|
|
|
$this->has($serviceTwo)->shouldReturn(false); |
55
|
|
|
$this->has($serviceThree)->shouldReturn(false); |
56
|
|
|
|
57
|
|
|
$this->register($serviceOne); |
58
|
|
|
$this->register($serviceTwo, -1); |
59
|
|
|
$this->register($serviceThree, 1); |
60
|
|
|
|
61
|
|
|
$this->has($serviceOne)->shouldReturn(true); |
62
|
|
|
$this->has($serviceTwo)->shouldReturn(true); |
63
|
|
|
$this->has($serviceThree)->shouldReturn(true); |
64
|
|
|
|
65
|
|
|
$this->all()->shouldHaveCount(3); |
66
|
|
|
$this->all()->shouldHavePriority(1); |
67
|
|
|
$this->all()->shouldHavePriority(0); |
68
|
|
|
$this->all()->shouldHavePriority(-1); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
function it_throws_exception_when_trying_to_register_service_without_required_interface(\stdClass $service): void |
72
|
|
|
{ |
73
|
|
|
$this |
74
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
75
|
|
|
->duringRegister($service) |
76
|
|
|
; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function it_throws_exception_when_trying_to_check_for_a_registered_service_without_required_interface( |
80
|
|
|
\stdClass $service |
81
|
|
|
): void { |
82
|
|
|
$this |
83
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
84
|
|
|
->duringHas($service) |
85
|
|
|
; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function it_unregisters_service(SampleServiceInterface $service): void |
89
|
|
|
{ |
90
|
|
|
$this->register($service); |
91
|
|
|
$this->has($service)->shouldReturn(true); |
92
|
|
|
|
93
|
|
|
$this->unregister($service); |
94
|
|
|
$this->has($service)->shouldReturn(false); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
function it_throws_exception_if_trying_to_unregister_service_of_non_existing_type(SampleServiceInterface $service): void |
98
|
|
|
{ |
99
|
|
|
$this |
100
|
|
|
->shouldThrow(NonExistingServiceException::class) |
101
|
|
|
->duringUnregister($service) |
102
|
|
|
; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.