Completed
Push — scalar-types/registry ( 38a5a0 )
by Kamil
75:54 queued 51:59
created

ServiceRegistrySpec::it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 16.

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.

Loading history...
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