1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @author Yuriy Davletshin <[email protected]> |
6
|
|
|
* @copyright 2015 Yuriy Davletshin |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
namespace PhpLab\Di; |
10
|
|
|
|
11
|
|
|
use PhpLab\Di\Fake\{Component, Service, Object}; |
12
|
|
|
|
13
|
|
|
class ContainerUsesMagicMethodsTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
protected $app; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->app = new Container(); |
20
|
|
|
$this->app->component = function () { |
21
|
|
|
return new Component(function ($value) { |
22
|
|
|
return '#' . $value; |
23
|
|
|
}); |
24
|
|
|
}; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testShouldGetPresettedComponentUsingMagic() |
28
|
|
|
{ |
29
|
|
|
$service = $this->app->component; |
30
|
|
|
$this->assertInstanceOf('\PhpLab\Di\Fake\Component', $service); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testShouldThrowExceptionIfServiceDefinitionNotFoundUsingMagic() |
34
|
|
|
{ |
35
|
|
|
$this->setExpectedException('\PhpLab\Di\NotFoundException'); |
36
|
|
|
$this->app->commonService; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testShouldGetServiceUsingMagic() |
40
|
|
|
{ |
41
|
|
|
$this->app->commonService = function (Container $di) { |
42
|
|
|
return new Service($di->component); |
43
|
|
|
}; |
44
|
|
|
$service = $this->app->commonService; |
45
|
|
|
$this->assertInstanceOf('\PhpLab\Di\Fake\Service', $service); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testShouldGetServiceAfterChangeDefinitionUsingMagic() |
49
|
|
|
{ |
50
|
|
|
$this->app->commonService = function (Container $di) { |
51
|
|
|
return new Service($di->component); |
52
|
|
|
}; |
53
|
|
|
$this->app->commonService = function (Container $di) { |
54
|
|
|
return new Service($di->component, 'xml'); |
55
|
|
|
}; |
56
|
|
|
$service = $this->app->commonService; |
57
|
|
|
$this->assertEquals('xml', $service->getFormat()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testShouldThrowExceptionIfChangeDefinitionAfterGettingServiceUsingMagic() |
61
|
|
|
{ |
62
|
|
|
$this->setExpectedException('\PhpLab\Di\FrozenException'); |
63
|
|
|
$this->app->commonService = function (Container $di) { |
64
|
|
|
return new Service($di->component); |
65
|
|
|
}; |
66
|
|
|
$service = $this->app->commonService; |
|
|
|
|
67
|
|
|
$this->app->commonService = function (Container $di) { |
68
|
|
|
return new Service($di->component, 'xml'); |
69
|
|
|
}; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testShouldGetObjectUsingMagic() |
73
|
|
|
{ |
74
|
|
|
$this->app->_newObjectEveryTime = function () { |
75
|
|
|
return new Object(); |
76
|
|
|
}; |
77
|
|
|
$instance = $this->app->_newObjectEveryTime; |
78
|
|
|
$this->assertInstanceOf('\PhpLab\Di\Fake\Object', $instance); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testShouldGetNewInstanceOfObjectUsingMagic() |
82
|
|
|
{ |
83
|
|
|
$this->app->_newObjectEveryTime = function () { |
84
|
|
|
return new Object(); |
85
|
|
|
}; |
86
|
|
|
$instance1 = $this->app->_newObjectEveryTime; |
87
|
|
|
$instance2 = $this->app->_newObjectEveryTime; |
88
|
|
|
$this->assertNotSame($instance1, $instance2); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testShouldGetObjectAfterChangeDefinitionUsingMagic() |
92
|
|
|
{ |
93
|
|
|
$this->app->_newObjectEveryTime = function () { |
94
|
|
|
return new Object(); |
95
|
|
|
}; |
96
|
|
|
$this->app->_newObjectEveryTime = function () { |
97
|
|
|
return new Object('value'); |
98
|
|
|
}; |
99
|
|
|
$instance = $this->app->_newObjectEveryTime; |
100
|
|
|
$this->assertEquals('value', $instance->getValue()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testShouldThrowExceptionIfChangeDefinitionAfterGettingObjectUsingMagic() |
104
|
|
|
{ |
105
|
|
|
$this->setExpectedException('\PhpLab\Di\FrozenException'); |
106
|
|
|
$this->app->_newObjectEveryTime = function () { |
107
|
|
|
return new Object(); |
108
|
|
|
}; |
109
|
|
|
$instance = $this->app->_newObjectEveryTime; |
|
|
|
|
110
|
|
|
$this->app->_newObjectEveryTime = function () { |
111
|
|
|
return new Object('value'); |
112
|
|
|
}; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.