ContainerTest.php 7.2 KB

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