ContainerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace think\tests;
  3. use PHPUnit\Framework\TestCase;
  4. use ReflectionMethod;
  5. use stdClass;
  6. use think\Container;
  7. use think\exception\ClassNotFoundException;
  8. use think\exception\FuncNotFoundException;
  9. class Taylor
  10. {
  11. public $name;
  12. public function __construct($name)
  13. {
  14. $this->name = $name;
  15. }
  16. public function some(Container $container)
  17. {
  18. }
  19. protected function protectionFun()
  20. {
  21. return true;
  22. }
  23. public static function test(Container $container)
  24. {
  25. return $container;
  26. }
  27. public static function __make()
  28. {
  29. return new self('Taylor');
  30. }
  31. }
  32. class SomeClass
  33. {
  34. public $container;
  35. public $count = 0;
  36. public function __construct(Container $container)
  37. {
  38. $this->container = $container;
  39. }
  40. }
  41. class ContainerTest extends TestCase
  42. {
  43. protected function tearDown(): void
  44. {
  45. Container::setInstance(null);
  46. }
  47. public function testClosureResolution()
  48. {
  49. $container = new Container;
  50. Container::setInstance($container);
  51. $container->bind('name', function () {
  52. return 'Taylor';
  53. });
  54. $this->assertEquals('Taylor', $container->make('name'));
  55. $this->assertEquals('Taylor', Container::pull('name'));
  56. }
  57. public function testGet()
  58. {
  59. $container = new Container;
  60. $this->expectException(ClassNotFoundException::class);
  61. $this->expectExceptionMessage('class not exists: name');
  62. $container->get('name');
  63. $container->bind('name', function () {
  64. return 'Taylor';
  65. });
  66. $this->assertSame('Taylor', $container->get('name'));
  67. }
  68. public function testExist()
  69. {
  70. $container = new Container;
  71. $container->bind('name', function () {
  72. return 'Taylor';
  73. });
  74. $this->assertFalse($container->exists("name"));
  75. $container->make('name');
  76. $this->assertTrue($container->exists('name'));
  77. }
  78. public function testInstance()
  79. {
  80. $container = new Container;
  81. $container->bind('name', function () {
  82. return 'Taylor';
  83. });
  84. $this->assertEquals('Taylor', $container->get('name'));
  85. $container->bind('name2', Taylor::class);
  86. $object = new stdClass();
  87. $this->assertFalse($container->exists('name2'));
  88. $container->instance('name2', $object);
  89. $this->assertTrue($container->exists('name2'));
  90. $this->assertTrue($container->exists(Taylor::class));
  91. $this->assertEquals($object, $container->make(Taylor::class));
  92. unset($container->name1);
  93. $this->assertFalse($container->exists('name1'));
  94. $container->delete('name2');
  95. $this->assertFalse($container->exists('name2'));
  96. foreach ($container as $class => $instance) {
  97. }
  98. }
  99. public function testBind()
  100. {
  101. $container = new Container;
  102. $object = new stdClass();
  103. $container->bind(['name' => Taylor::class]);
  104. $container->bind('name2', $object);
  105. $container->bind('name3', Taylor::class);
  106. $container->bind('name3', Taylor::class);
  107. $container->name4 = $object;
  108. $container['name5'] = $object;
  109. $this->assertTrue(isset($container->name4));
  110. $this->assertTrue(isset($container['name5']));
  111. $this->assertInstanceOf(Taylor::class, $container->get('name'));
  112. $this->assertSame($object, $container->get('name2'));
  113. $this->assertSame($object, $container->name4);
  114. $this->assertSame($object, $container['name5']);
  115. $this->assertInstanceOf(Taylor::class, $container->get('name3'));
  116. unset($container['name']);
  117. $this->assertFalse(isset($container['name']));
  118. unset($container->name3);
  119. $this->assertFalse(isset($container->name3));
  120. }
  121. public function testAutoConcreteResolution()
  122. {
  123. $container = new Container;
  124. $taylor = $container->make(Taylor::class);
  125. $this->assertInstanceOf(Taylor::class, $taylor);
  126. $this->assertAttributeSame('Taylor', 'name', $taylor);
  127. }
  128. public function testGetAndSetInstance()
  129. {
  130. $this->assertInstanceOf(Container::class, Container::getInstance());
  131. $object = new stdClass();
  132. Container::setInstance($object);
  133. $this->assertSame($object, Container::getInstance());
  134. Container::setInstance(function () {
  135. return $this;
  136. });
  137. $this->assertSame($this, Container::getInstance());
  138. }
  139. public function testResolving()
  140. {
  141. $container = new Container();
  142. $container->bind(Container::class, $container);
  143. $container->resolving(function (SomeClass $taylor, Container $container) {
  144. $taylor->count++;
  145. });
  146. $container->resolving(SomeClass::class, function (SomeClass $taylor, Container $container) {
  147. $taylor->count++;
  148. });
  149. /** @var SomeClass $someClass */
  150. $someClass = $container->invokeClass(SomeClass::class);
  151. $this->assertEquals(2, $someClass->count);
  152. }
  153. public function testInvokeFunctionWithoutMethodThrowsException()
  154. {
  155. $this->expectException(FuncNotFoundException::class);
  156. $this->expectExceptionMessage('function not exists: ContainerTestCallStub()');
  157. $container = new Container();
  158. $container->invokeFunction('ContainerTestCallStub', []);
  159. }
  160. public function testInvokeProtectionMethod()
  161. {
  162. $container = new Container();
  163. $this->assertTrue($container->invokeMethod([Taylor::class, 'protectionFun'], [], true));
  164. }
  165. public function testInvoke()
  166. {
  167. $container = new Container();
  168. Container::setInstance($container);
  169. $container->bind(Container::class, $container);
  170. $stub = $this->createMock(Taylor::class);
  171. $stub->expects($this->once())->method('some')->with($container)->will($this->returnSelf());
  172. $container->invokeMethod([$stub, 'some']);
  173. $this->assertEquals('48', $container->invoke('ord', ['0']));
  174. $this->assertSame($container, $container->invoke(Taylor::class . '::test', []));
  175. $this->assertSame($container, $container->invokeMethod(Taylor::class . '::test'));
  176. $reflect = new ReflectionMethod($container, 'exists');
  177. $this->assertTrue($container->invokeReflectMethod($container, $reflect, [Container::class]));
  178. $this->assertSame($container, $container->invoke(function (Container $container) {
  179. return $container;
  180. }));
  181. $this->assertSame($container, $container->invoke(Taylor::class . '::test'));
  182. $object = $container->invokeClass(SomeClass::class);
  183. $this->assertInstanceOf(SomeClass::class, $object);
  184. $this->assertSame($container, $object->container);
  185. $stdClass = new stdClass();
  186. $container->invoke(function (Container $container, stdClass $stdObject, $key1, $lowKey, $key2 = 'default') use ($stdClass) {
  187. $this->assertEquals('value1', $key1);
  188. $this->assertEquals('default', $key2);
  189. $this->assertEquals('value2', $lowKey);
  190. $this->assertSame($stdClass, $stdObject);
  191. return $container;
  192. }, ['some' => $stdClass, 'key1' => 'value1', 'low_key' => 'value2']);
  193. }
  194. public function testInvokeMethodNotExists()
  195. {
  196. $container = $this->resolveContainer();
  197. $this->expectException(FuncNotFoundException::class);
  198. $container->invokeMethod([SomeClass::class, 'any']);
  199. }
  200. public function testInvokeClassNotExists()
  201. {
  202. $container = new Container();
  203. Container::setInstance($container);
  204. $container->bind(Container::class, $container);
  205. $this->expectExceptionObject(new ClassNotFoundException('class not exists: SomeClass'));
  206. $container->invokeClass('SomeClass');
  207. }
  208. protected function resolveContainer()
  209. {
  210. $container = new Container();
  211. Container::setInstance($container);
  212. return $container;
  213. }
  214. }