1
0

SimpleContainerTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. namespace Test\AppFramework\Utility;
  9. use OC\AppFramework\Utility\SimpleContainer;
  10. use Psr\Container\NotFoundExceptionInterface;
  11. interface TestInterface {
  12. }
  13. class ClassEmptyConstructor implements IInterfaceConstructor {
  14. }
  15. class ClassSimpleConstructor implements IInterfaceConstructor {
  16. public $test;
  17. public function __construct($test) {
  18. $this->test = $test;
  19. }
  20. }
  21. class ClassComplexConstructor {
  22. public $class;
  23. public $test;
  24. public function __construct(ClassSimpleConstructor $class, $test) {
  25. $this->class = $class;
  26. $this->test = $test;
  27. }
  28. }
  29. class ClassNullableUntypedConstructorArg {
  30. public function __construct($class) {
  31. }
  32. }
  33. class ClassNullableTypedConstructorArg {
  34. public $class;
  35. public function __construct(?\Some\Class $class) {
  36. $this->class = $class;
  37. }
  38. }
  39. interface IInterfaceConstructor {
  40. }
  41. class ClassInterfaceConstructor {
  42. public $class;
  43. public $test;
  44. public function __construct(IInterfaceConstructor $class, $test) {
  45. $this->class = $class;
  46. $this->test = $test;
  47. }
  48. }
  49. class SimpleContainerTest extends \Test\TestCase {
  50. private $container;
  51. protected function setUp(): void {
  52. $this->container = new SimpleContainer();
  53. }
  54. public function testRegister() {
  55. $this->container->registerParameter('test', 'abc');
  56. $this->assertEquals('abc', $this->container->query('test'));
  57. }
  58. /**
  59. * Test querying a class that is not registered without autoload enabled
  60. */
  61. public function testNothingRegistered() {
  62. try {
  63. $this->container->query('something really hard', false);
  64. $this->fail('Expected `QueryException` exception was not thrown');
  65. } catch (\Throwable $exception) {
  66. $this->assertInstanceOf(\OCP\AppFramework\QueryException::class, $exception);
  67. $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
  68. }
  69. }
  70. /**
  71. * Test querying a class that is not registered with autoload enabled
  72. */
  73. public function testNothingRegistered_autoload() {
  74. try {
  75. $this->container->query('something really hard');
  76. $this->fail('Expected `QueryException` exception was not thrown');
  77. } catch (\Throwable $exception) {
  78. $this->assertInstanceOf(\OCP\AppFramework\QueryException::class, $exception);
  79. $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
  80. }
  81. }
  82. public function testNotAClass() {
  83. $this->expectException(\OCP\AppFramework\QueryException::class);
  84. $this->container->query('Test\AppFramework\Utility\TestInterface');
  85. }
  86. public function testNoConstructorClass() {
  87. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  88. $this->assertTrue($object instanceof ClassEmptyConstructor);
  89. }
  90. public function testInstancesOnlyOnce() {
  91. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  92. $object2 = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  93. $this->assertSame($object, $object2);
  94. }
  95. public function testConstructorSimple() {
  96. $this->container->registerParameter('test', 'abc');
  97. $object = $this->container->query(
  98. 'Test\AppFramework\Utility\ClassSimpleConstructor'
  99. );
  100. $this->assertTrue($object instanceof ClassSimpleConstructor);
  101. $this->assertEquals('abc', $object->test);
  102. }
  103. public function testConstructorComplex() {
  104. $this->container->registerParameter('test', 'abc');
  105. $object = $this->container->query(
  106. 'Test\AppFramework\Utility\ClassComplexConstructor'
  107. );
  108. $this->assertTrue($object instanceof ClassComplexConstructor);
  109. $this->assertEquals('abc', $object->class->test);
  110. $this->assertEquals('abc', $object->test);
  111. }
  112. public function testConstructorComplexInterface() {
  113. $this->container->registerParameter('test', 'abc');
  114. $this->container->registerService(
  115. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  116. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  117. });
  118. $object = $this->container->query(
  119. 'Test\AppFramework\Utility\ClassInterfaceConstructor'
  120. );
  121. $this->assertTrue($object instanceof ClassInterfaceConstructor);
  122. $this->assertEquals('abc', $object->class->test);
  123. $this->assertEquals('abc', $object->test);
  124. }
  125. public function testOverrideService() {
  126. $this->container->registerService(
  127. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  128. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  129. });
  130. $this->container->registerService(
  131. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  132. return $c->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  133. });
  134. $object = $this->container->query(
  135. 'Test\AppFramework\Utility\IInterfaceConstructor'
  136. );
  137. $this->assertTrue($object instanceof ClassEmptyConstructor);
  138. }
  139. public function testRegisterAliasParamter() {
  140. $this->container->registerParameter('test', 'abc');
  141. $this->container->registerAlias('test1', 'test');
  142. $this->assertEquals('abc', $this->container->query('test1'));
  143. }
  144. public function testRegisterAliasService() {
  145. $this->container->registerService('test', function () {
  146. return new \StdClass;
  147. }, true);
  148. $this->container->registerAlias('test1', 'test');
  149. $this->assertSame(
  150. $this->container->query('test'), $this->container->query('test'));
  151. $this->assertSame(
  152. $this->container->query('test1'), $this->container->query('test1'));
  153. $this->assertSame(
  154. $this->container->query('test'), $this->container->query('test1'));
  155. }
  156. public function sanitizeNameProvider() {
  157. return [
  158. ['ABC\\Foo', 'ABC\\Foo'],
  159. ['\\ABC\\Foo', '\\ABC\\Foo'],
  160. ['\\ABC\\Foo', 'ABC\\Foo'],
  161. ['ABC\\Foo', '\\ABC\\Foo'],
  162. ];
  163. }
  164. /**
  165. * @dataProvider sanitizeNameProvider
  166. */
  167. public function testSanitizeName($register, $query) {
  168. $this->container->registerService($register, function () {
  169. return 'abc';
  170. });
  171. $this->assertEquals('abc', $this->container->query($query));
  172. }
  173. public function testConstructorComplexNoTestParameterFound() {
  174. $this->expectException(\OCP\AppFramework\QueryException::class);
  175. $object = $this->container->query(
  176. 'Test\AppFramework\Utility\ClassComplexConstructor'
  177. );
  178. }
  179. public function testRegisterFactory() {
  180. $this->container->registerService('test', function () {
  181. return new \StdClass();
  182. }, false);
  183. $this->assertNotSame(
  184. $this->container->query('test'), $this->container->query('test'));
  185. }
  186. public function testRegisterAliasFactory() {
  187. $this->container->registerService('test', function () {
  188. return new \StdClass();
  189. }, false);
  190. $this->container->registerAlias('test1', 'test');
  191. $this->assertNotSame(
  192. $this->container->query('test'), $this->container->query('test'));
  193. $this->assertNotSame(
  194. $this->container->query('test1'), $this->container->query('test1'));
  195. $this->assertNotSame(
  196. $this->container->query('test'), $this->container->query('test1'));
  197. }
  198. public function testQueryUntypedNullable(): void {
  199. $this->expectException(\OCP\AppFramework\QueryException::class);
  200. $this->container->query(ClassNullableUntypedConstructorArg::class);
  201. }
  202. public function testQueryTypedNullable(): void {
  203. /** @var ClassNullableTypedConstructorArg $service */
  204. $service = $this->container->query(ClassNullableTypedConstructorArg::class);
  205. self::assertNull($service->class);
  206. }
  207. }