Completed
Push — develop ( 9f43d2...2581dc )
by Filipe
01:49
created

ResolverSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 25.81 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 16
loc 62
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace spec\Slick\Di\Definition\Object;
4
5
use Slick\Di\ContainerInterface;
6
use Slick\Di\Definition\Object\DefinitionData;
7
use Slick\Di\Definition\Object\Resolver;
8
use Slick\Di\Definition\Object\ResolverInterface;
9
use PhpSpec\ObjectBehavior;
10
use Prophecy\Argument;
11
12
class ResolverSpec extends ObjectBehavior
13
{
14
    function it_is_initializable()
15
    {
16
        $this->shouldHaveType(Resolver::class);
17
    }
18
19
    function its_a_resolver()
20
    {
21
        $this->shouldBeAnInstanceOf(ResolverInterface::class);
22
    }
23
24
    function it_resolves_simple_objects()
25
    {
26
        $data = new DefinitionData(SimpleObject::class);
27
        $this->resolve($data)->shouldBeAnInstanceOf(SimpleObject::class);
28
    }
29
30
    function it_resolvers_objects_with_constructor_dependencies(ContainerInterface $container)
31
    {
32
        $object = new SimpleObject();
33
        $container->get('first-object')->willReturn($object);
34
        $this->setContainer($container);
35
        $data = new DefinitionData(ConstructorObject::class, ['@first-object', 'test']);
36
        $this->resolve($data)->shouldBeAnInstanceOf(ConstructorObject::class);
37
    }
38
39
    function it_can_call_methods_with_arguments_on_resolved_objects(ContainerInterface $container, SimpleObject $object)
40
    {
41
        $container->get('first-object')->willReturn($object);
42
        $this->setContainer($container);
43
        $data = new DefinitionData(ConstructorObject::class, ['@first-object']);
44
        $data->addCall(DefinitionData::METHOD, 'setOther', ['@first-object']);
45
        $this->resolve($data)->shouldHavePropertyEquals('other', $object);
46
    }
47
48
    function it_can_assign_property_values_on_resolved_objects(ContainerInterface $container, SimpleObject $object)
49
    {
50
        $container->get('first-object')->willReturn($object);
51
        $this->setContainer($container);
52
        $data = new DefinitionData(ConstructorObject::class, ['@first-object']);
53
        $data->addCall(DefinitionData::PROPERTY, 'other', '@first-object');
54
        $this->resolve($data)->shouldHavePropertyEquals('other', $object);
55
    }
56
57
    function it_creates_object_from_classes_without_constructor()
58
    {
59
        $data = new DefinitionData(WithoutConstructor::class);
60
        $this->resolve($data)->shouldBeAnInstanceOf(WithoutConstructor::class);
61
62
    }
63
64
    public function getMatchers()
65
    {
66
        return [
67
            'havePropertyEquals' => function(ConstructorObject $subject, $property, $expected) {
68
                \PHPUnit_Framework_Assert::assertEquals($expected, $subject->{$property});
69
                return true;
70
            }
71
        ];
72
    }
73
}
74
75
76
class SimpleObject
77
{
78
}
79
80
class ConstructorObject
81
{
82
    /**
83
     * @var SimpleObject
84
     */
85
    private $object;
86
87
    /**
88
     * @var null
89
     */
90
    public $other;
91
92
    public function __construct(SimpleObject $object, $other = null)
93
    {
94
        $this->object = $object;
95
        $this->other = $other;
96
    }
97
98
    public function setOther($value)
99
    {
100
        $this->other = $value;
101
    }
102
}
103
104
class WithoutConstructor
105
{
106
107
}