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 spec\Sylius\Component\Registry\Fixture\SampleServiceInterface; |
20
|
|
|
use Sylius\Component\Registry\ExistingServiceException; |
21
|
|
|
use Sylius\Component\Registry\NonExistingServiceException; |
22
|
|
|
use Sylius\Component\Registry\ServiceRegistry; |
23
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class ServiceRegistrySpec extends ObjectBehavior |
29
|
|
|
{ |
30
|
|
|
function let(): void |
31
|
|
|
{ |
32
|
|
|
$this->beConstructedWith(SampleServiceInterface::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function it_implements_service_registry_interface(): void |
36
|
|
|
{ |
37
|
|
|
$this->shouldImplement(ServiceRegistryInterface::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function it_initializes_services_array_by_default(): void |
41
|
|
|
{ |
42
|
|
|
$this->all()->shouldReturn([]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function it_registers_service_with_given_type(SampleServiceInterface $service): void |
46
|
|
|
{ |
47
|
|
|
$this->has('test')->shouldReturn(false); |
48
|
|
|
$this->register('test', $service); |
49
|
|
|
|
50
|
|
|
$this->has('test')->shouldReturn(true); |
51
|
|
|
$this->get('test')->shouldReturn($service); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
function it_throws_exception_when_trying_to_register_service_with_taken_type(SampleServiceInterface $service): void |
55
|
|
|
{ |
56
|
|
|
$this->register('test', $service); |
57
|
|
|
|
58
|
|
|
$this |
59
|
|
|
->shouldThrow(ExistingServiceException::class) |
60
|
|
|
->duringRegister('test', $service) |
61
|
|
|
; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
function it_throws_exception_when_trying_to_register_service_without_required_interface( |
65
|
|
|
\stdClass $service |
66
|
|
|
): void { |
67
|
|
|
$this |
68
|
|
|
->shouldThrow(\InvalidArgumentException::class) |
69
|
|
|
->duringRegister('test', $service) |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
function it_unregisters_service_with_given_type(SampleServiceInterface $service): void |
74
|
|
|
{ |
75
|
|
|
$this->register('foo', $service); |
76
|
|
|
$this->has('foo')->shouldReturn(true); |
77
|
|
|
|
78
|
|
|
$this->unregister('foo'); |
79
|
|
|
$this->has('foo')->shouldReturn(false); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function it_retrieves_registered_service_by_type(SampleServiceInterface $service): void |
83
|
|
|
{ |
84
|
|
|
$this->register('test', $service); |
85
|
|
|
$this->get('test')->shouldReturn($service); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function it_throws_exception_if_trying_to_get_service_of_non_existing_type(): void |
89
|
|
|
{ |
90
|
|
|
$this |
91
|
|
|
->shouldThrow(new NonExistingServiceException('service', 'foo', [])) |
92
|
|
|
->duringGet('foo') |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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.