ContainerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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->name4 = $object;
  107. $container['name5'] = $object;
  108. $this->assertTrue(isset($container->name4));
  109. $this->assertTrue(isset($container['name5']));
  110. $this->assertInstanceOf(Taylor::class, $container->get('name'));
  111. $this->assertSame($object, $container->get('name2'));
  112. $this->assertSame($object, $container->name4);
  113. $this->assertSame($object, $container['name5']);
  114. $this->assertInstanceOf(Taylor::class, $container->get('name3'));
  115. unset($container['name']);
  116. $this->assertFalse(isset($container['name']));
  117. unset($container->name3);
  118. $this->assertFalse(isset($container->name3));
  119. }
  120. public function testAutoConcreteResolution()
  121. {
  122. $container = new Container;
  123. $taylor = $container->make(Taylor::class);
  124. $this->assertInstanceOf(Taylor::class, $taylor);
  125. $this->assertAttributeSame('Taylor', 'name', $taylor);
  126. }
  127. public function testGetAndSetInstance()
  128. {
  129. $this->assertInstanceOf(Container::class, Container::getInstance());
  130. $object = new stdClass();
  131. Container::setInstance($object);
  132. $this->assertSame($object, Container::getInstance());
  133. Container::setInstance(function () {
  134. return $this;
  135. });
  136. $this->assertSame($this, Container::getInstance());
  137. }
  138. public function testResolving()
  139. {
  140. $container = new Container();
  141. $container->bind(Container::class, $container);
  142. $container->resolving(function (SomeClass $taylor, Container $container) {
  143. $taylor->count++;
  144. });
  145. $container->resolving(SomeClass::class, function (SomeClass $taylor, Container $container) {
  146. $taylor->count++;
  147. });
  148. /** @var SomeClass $someClass */
  149. $someClass = $container->invokeClass(SomeClass::class);
  150. $this->assertEquals(2, $someClass->count);
  151. }
  152. public function testInvokeFunctionWithoutMethodThrowsException()
  153. {
  154. $this->expectException(FuncNotFoundException::class);
  155. $this->expectExceptionMessage('function not exists: ContainerTestCallStub()');
  156. $container = new Container();
  157. $container->invokeFunction('ContainerTestCallStub', []);
  158. }
  159. public function testInvokeProtectionMethod()
  160. {
  161. $container = new Container();
  162. $this->assertTrue($container->invokeMethod([Taylor::class, 'protectionFun'], [], true));
  163. }
  164. public function testInvoke()
  165. {
  166. $container = new Container();
  167. Container::setInstance($container);
  168. $container->bind(Container::class, $container);
  169. $stub = $this->createMock(Taylor::class);
  170. $stub->expects($this->once())->method('some')->with($container)->will($this->returnSelf());
  171. $container->invokeMethod([$stub, 'some']);
  172. $this->assertEquals('48', $container->invoke('ord', ['0']));
  173. $this->assertSame($container, $container->invoke(Taylor::class . '::test', []));
  174. $this->assertSame($container, $container->invokeMethod(Taylor::class . '::test'));
  175. $reflect = new ReflectionMethod($container, 'exists');
  176. $this->assertTrue($container->invokeReflectMethod($container, $reflect, [Container::class]));
  177. $this->assertSame($container, $container->invoke(function (Container $container) {
  178. return $container;
  179. }));
  180. $this->assertSame($container, $container->invoke(Taylor::class . '::test'));
  181. $object = $container->invokeClass(SomeClass::class);
  182. $this->assertInstanceOf(SomeClass::class, $object);
  183. $this->assertSame($container, $object->container);
  184. $stdClass = new stdClass();
  185. $container->invoke(function (Container $container, stdClass $stdObject, $key1, $lowKey, $key2 = 'default') use ($stdClass) {
  186. $this->assertEquals('value1', $key1);
  187. $this->assertEquals('default', $key2);
  188. $this->assertEquals('value2', $lowKey);
  189. $this->assertSame($stdClass, $stdObject);
  190. return $container;
  191. }, ['some' => $stdClass, 'key1' => 'value1', 'low_key' => 'value2']);
  192. }
  193. public function testInvokeMethodNotExists()
  194. {
  195. $container = $this->resolveContainer();
  196. $this->expectException(FuncNotFoundException::class);
  197. $container->invokeMethod([SomeClass::class, 'any']);
  198. }
  199. public function testInvokeClassNotExists()
  200. {
  201. $container = new Container();
  202. Container::setInstance($container);
  203. $container->bind(Container::class, $container);
  204. $this->expectExceptionObject(new ClassNotFoundException('class not exists: SomeClass'));
  205. $container->invokeClass('SomeClass');
  206. }
  207. protected function resolveContainer()
  208. {
  209. $container = new Container();
  210. Container::setInstance($container);
  211. return $container;
  212. }
  213. }