/
ContainerTest.php
124 lines (103 loc) · 3.11 KB
/
ContainerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace DI\Test\UnitTest;
use DI\Container;
use DI\ContainerBuilder;
use DI\FactoryInterface;
use DI\InvokerInterface;
use stdClass;
/**
* Test class for Container.
*
* @covers \DI\Container
*/
class ContainerTest extends \PHPUnit_Framework_TestCase
{
public function testHas()
{
$container = ContainerBuilder::buildDevContainer();
$container->set('foo', 'bar');
$this->assertTrue($container->has('foo'));
$this->assertFalse($container->has('wow'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The name parameter must be of type string
*/
public function testHasNonStringParameter()
{
$container = ContainerBuilder::buildDevContainer();
$container->has(new stdClass());
}
/**
* Test that injecting an existing object returns the same reference to that object.
*/
public function testInjectOnMaintainsReferentialEquality()
{
$container = ContainerBuilder::buildDevContainer();
$instance = new stdClass();
$result = $container->injectOn($instance);
$this->assertSame($instance, $result);
}
/**
* Test that injection on null yields null.
*/
public function testInjectNull()
{
$container = ContainerBuilder::buildDevContainer();
$result = $container->injectOn(null);
$this->assertEquals($result, null);
}
/**
* We should be able to set a null value.
* @see https://github.com/mnapoli/PHP-DI/issues/79
*/
public function testSetNullValue()
{
$container = ContainerBuilder::buildDevContainer();
$container->set('foo', null);
$this->assertNull($container->get('foo'));
}
/**
* The container auto-registers itself.
*/
public function testContainerIsRegistered()
{
$container = ContainerBuilder::buildDevContainer();
$this->assertSame($container, $container->get(Container::class));
}
/**
* The container auto-registers itself (with the factory interface).
*/
public function testFactoryInterfaceIsRegistered()
{
$container = ContainerBuilder::buildDevContainer();
$this->assertSame($container, $container->get(FactoryInterface::class));
}
/**
* The container auto-registers itself (with the invoker interface).
*/
public function testInvokerInterfaceIsRegistered()
{
$container = ContainerBuilder::buildDevContainer();
$this->assertSame($container, $container->get(InvokerInterface::class));
}
/**
* @see https://github.com/mnapoli/PHP-DI/issues/126
* @test
*/
public function testSetGetSetGet()
{
$container = ContainerBuilder::buildDevContainer();
$container->set('foo', 'bar');
$container->get('foo');
$container->set('foo', 'hello');
$this->assertSame('hello', $container->get('foo'));
}
/**
* @test
*/
public function canBeBuiltWithoutParameters()
{
self::assertInstanceOf(Container::class, new Container); // Should not be an error
}
}