Completed
Branch develop (9f43d2)
by Filipe
05:14
created

CustomMethodObject   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 1
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of slick/di package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace spec\Slick\Di;
11
12
use Interop\Container\ContainerInterface;
13
use Interop\Container\Exception\NotFoundException;
14
use Slick\Di\Container;
15
use PhpSpec\ObjectBehavior;
16
use Prophecy\Argument;
17
use Slick\Di\ContainerInjectionInterface;
18
use Slick\Di\Definition\Scope;
19
use Slick\Di\DefinitionInterface;
20
use Slick\Di\ObjectHydratorAwareInterface;
21
use Slick\Di\ObjectHydratorInterface;
22
23
/**
24
 * ContainerSpec
25
 *
26
 * @package spec\Slick\Di
27
 * @author  Filipe Silva <[email protected]>
28
 */
29
class ContainerSpec extends ObjectBehavior
30
{
31
32
    function let()
33
    {
34
        $this->register('foo', 'bar');
35
    }
36
37
    function it_is_initializable()
38
    {
39
        $this->shouldHaveType(ContainerInterface::class);
40
    }
41
42
    function its_hydrator_aware()
43
    {
44
        $this->shouldBeAnInstanceOf(ObjectHydratorAwareInterface::class);
45
    }
46
47
    function it_registers_values_under_provided_keys()
48
    {
49
        $this->register('baz', 'test')->shouldBeAnInstanceOf(Container::class);
50
    }
51
52
    function it_returns_values_stored_under_provided_keys()
53
    {
54
        $this->get('foo')->shouldBe('bar');
55
    }
56
57
    function it_checks_if_a_definition_exists()
58
    {
59
        $this->has('foo')->shouldBe(true);
60
    }
61
62
    function it_throws_a_not_found_exception_for_unregistered_definitions()
63
    {
64
        $this->shouldThrow(NotFoundException::class)
65
            ->during('get', ['unknown']);
66
    }
67
68
    function it_can_resolve_definitions(DefinitionInterface $definition)
69
    {
70
        $definition->resolve()->willReturn('value');
71
        $definition->setContainer($this)->willReturn($this);
72
        $definition->getScope()->willReturn(Scope::SINGLETON);
73
        $this->register('test-value', $definition);
74
        $this->get('test-value')->shouldBe('value');
75
    }
76
77
    function it_registers_a_callable_executing_it_when_resolving()
78
    {
79
        $callable = function ($test) {
80
            return $test;
81
        };
82
        $this->register(
0 ignored issues
show
Bug introduced by
The method register() does not exist on spec\Slick\Di\ContainerSpec. Did you maybe mean it_registers_values_under_provided_keys()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
83
            'callable-test',
84
            $callable,
85
            Scope::Singleton(),
86
            ['Hello test!']
87
        );
88
        $this->get('callable-test')->shouldReturn('Hello test!');
89
    }
90
91
    function it_registers_an_alias_that_points_to_other_definition()
92
    {
93
        $object = (object)[];
94
        $this->register('test-object', $object);
95
        $this->register('alias', '@test-object');
96
        $this->get('alias')->shouldBe($object);
97
    }
98
99
    function it_is_aware_of_definition_resolution_scope()
100
    {
101
        $callable = function () {
102
            static $calls;
103
104
            if (!$calls) { $calls = 0;}
105
106
            return $calls++;
107
        };
108
109
        $this->register('test', $callable, Scope::Prototype());
0 ignored issues
show
Bug introduced by
The method register() does not exist on spec\Slick\Di\ContainerSpec. Did you maybe mean it_registers_values_under_provided_keys()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
110
111
        $first = $this->get('test');
112
        $this->get('test')->shouldNotBe($first);
113
    }
114
115
    function it_creates_objects_injecting_its_dependencies(ObjectHydratorInterface $hydrator)
116
    {
117
        $this->register('the-value', 33);
118
        $this->setHydrator($hydrator);
119
        $this->make(CreatableObject::class, '@the-value')
120
            ->shouldBeAnInstanceOf(CreatableObject::class);
121
        $hydrator->hydrate(Argument::any())->shouldHaveBeenCalled();
122
    }
123
124
    function it_creates_container_injection_implementations()
125
    {
126
        $this->register('some-value', new \stdClass());
127
        $this->make(CustomMethodObject::class)
128
            ->shouldBeAnInstanceOf(CustomMethodObject::class);
129
130
    }
131
132
    function it_references_last_created_container_as_its_parent()
133
    {
134
        $this->parent()->shouldBeAnInstanceOf(\Slick\Di\ContainerInterface::class);
135
    }
136
}
137
138
139
class CreatableObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
140
{
141
    private $value;
142
143
    public function __construct($value)
144
    {
145
        $this->value = $value;
146
    }
147
}
148
149
class CustomMethodObject implements ContainerInjectionInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
150
{
151
    /**
152
     * @var
153
     */
154
    private $value;
155
156
    public function __construct(\stdClass $value)
157
    {
158
        $this->value = $value;
159
    }
160
161
    /**
162
     * Instantiates a new instance of this class.
163
     *
164
     * This is a factory method that returns a new instance of this class. The
165
     * factory should pass any needed dependencies into the constructor of this
166
     * class, but not the container itself. Every call to this method must return
167
     * a new instance of this class; that is, it may not implement a singleton.
168
     *
169
     * @param ContainerInterface $container
170
     *   The service container this instance should use.
171
     *
172
     * @return CustomMethodObject
173
     */
174
    public static function create(ContainerInterface $container)
175
    {
176
        return new static($container->get('some-value'));
177
    }
178
}