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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.