SimpleContainerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * ownCloud - App Framework
  5. *
  6. * @author Bernhard Posselt
  7. * @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\Utility;
  24. use OC\AppFramework\Utility\SimpleContainer;
  25. use Psr\Container\NotFoundExceptionInterface;
  26. interface TestInterface {
  27. }
  28. class ClassEmptyConstructor implements IInterfaceConstructor {
  29. }
  30. class ClassSimpleConstructor implements IInterfaceConstructor {
  31. public $test;
  32. public function __construct($test) {
  33. $this->test = $test;
  34. }
  35. }
  36. class ClassComplexConstructor {
  37. public $class;
  38. public $test;
  39. public function __construct(ClassSimpleConstructor $class, $test) {
  40. $this->class = $class;
  41. $this->test = $test;
  42. }
  43. }
  44. class ClassNullableUntypedConstructorArg {
  45. public function __construct($class) {
  46. }
  47. }
  48. class ClassNullableTypedConstructorArg {
  49. public $class;
  50. public function __construct(?\Some\Class $class) {
  51. $this->class = $class;
  52. }
  53. }
  54. interface IInterfaceConstructor {
  55. }
  56. class ClassInterfaceConstructor {
  57. public $class;
  58. public $test;
  59. public function __construct(IInterfaceConstructor $class, $test) {
  60. $this->class = $class;
  61. $this->test = $test;
  62. }
  63. }
  64. class SimpleContainerTest extends \Test\TestCase {
  65. private $container;
  66. protected function setUp(): void {
  67. $this->container = new SimpleContainer();
  68. }
  69. public function testRegister() {
  70. $this->container->registerParameter('test', 'abc');
  71. $this->assertEquals('abc', $this->container->query('test'));
  72. }
  73. /**
  74. * Test querying a class that is not registered without autoload enabled
  75. */
  76. public function testNothingRegistered() {
  77. try {
  78. $this->container->query('something really hard', false);
  79. $this->fail('Expected `QueryException` exception was not thrown');
  80. } catch (\Throwable $exception) {
  81. $this->assertInstanceOf(\OCP\AppFramework\QueryException::class, $exception);
  82. $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
  83. }
  84. }
  85. /**
  86. * Test querying a class that is not registered with autoload enabled
  87. */
  88. public function testNothingRegistered_autoload() {
  89. try {
  90. $this->container->query('something really hard');
  91. $this->fail('Expected `QueryException` exception was not thrown');
  92. } catch (\Throwable $exception) {
  93. $this->assertInstanceOf(\OCP\AppFramework\QueryException::class, $exception);
  94. $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
  95. }
  96. }
  97. public function testNotAClass() {
  98. $this->expectException(\OCP\AppFramework\QueryException::class);
  99. $this->container->query('Test\AppFramework\Utility\TestInterface');
  100. }
  101. public function testNoConstructorClass() {
  102. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  103. $this->assertTrue($object instanceof ClassEmptyConstructor);
  104. }
  105. public function testInstancesOnlyOnce() {
  106. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  107. $object2 = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  108. $this->assertSame($object, $object2);
  109. }
  110. public function testConstructorSimple() {
  111. $this->container->registerParameter('test', 'abc');
  112. $object = $this->container->query(
  113. 'Test\AppFramework\Utility\ClassSimpleConstructor'
  114. );
  115. $this->assertTrue($object instanceof ClassSimpleConstructor);
  116. $this->assertEquals('abc', $object->test);
  117. }
  118. public function testConstructorComplex() {
  119. $this->container->registerParameter('test', 'abc');
  120. $object = $this->container->query(
  121. 'Test\AppFramework\Utility\ClassComplexConstructor'
  122. );
  123. $this->assertTrue($object instanceof ClassComplexConstructor);
  124. $this->assertEquals('abc', $object->class->test);
  125. $this->assertEquals('abc', $object->test);
  126. }
  127. public function testConstructorComplexInterface() {
  128. $this->container->registerParameter('test', 'abc');
  129. $this->container->registerService(
  130. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  131. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  132. });
  133. $object = $this->container->query(
  134. 'Test\AppFramework\Utility\ClassInterfaceConstructor'
  135. );
  136. $this->assertTrue($object instanceof ClassInterfaceConstructor);
  137. $this->assertEquals('abc', $object->class->test);
  138. $this->assertEquals('abc', $object->test);
  139. }
  140. public function testOverrideService() {
  141. $this->container->registerService(
  142. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  143. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  144. });
  145. $this->container->registerService(
  146. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  147. return $c->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  148. });
  149. $object = $this->container->query(
  150. 'Test\AppFramework\Utility\IInterfaceConstructor'
  151. );
  152. $this->assertTrue($object instanceof ClassEmptyConstructor);
  153. }
  154. public function testRegisterAliasParamter() {
  155. $this->container->registerParameter('test', 'abc');
  156. $this->container->registerAlias('test1', 'test');
  157. $this->assertEquals('abc', $this->container->query('test1'));
  158. }
  159. public function testRegisterAliasService() {
  160. $this->container->registerService('test', function () {
  161. return new \StdClass;
  162. }, true);
  163. $this->container->registerAlias('test1', 'test');
  164. $this->assertSame(
  165. $this->container->query('test'), $this->container->query('test'));
  166. $this->assertSame(
  167. $this->container->query('test1'), $this->container->query('test1'));
  168. $this->assertSame(
  169. $this->container->query('test'), $this->container->query('test1'));
  170. }
  171. public function sanitizeNameProvider() {
  172. return [
  173. ['ABC\\Foo', 'ABC\\Foo'],
  174. ['\\ABC\\Foo', '\\ABC\\Foo'],
  175. ['\\ABC\\Foo', 'ABC\\Foo'],
  176. ['ABC\\Foo', '\\ABC\\Foo'],
  177. ];
  178. }
  179. /**
  180. * @dataProvider sanitizeNameProvider
  181. */
  182. public function testSanitizeName($register, $query) {
  183. $this->container->registerService($register, function () {
  184. return 'abc';
  185. });
  186. $this->assertEquals('abc', $this->container->query($query));
  187. }
  188. public function testConstructorComplexNoTestParameterFound() {
  189. $this->expectException(\OCP\AppFramework\QueryException::class);
  190. $object = $this->container->query(
  191. 'Test\AppFramework\Utility\ClassComplexConstructor'
  192. );
  193. }
  194. public function testRegisterFactory() {
  195. $this->container->registerService('test', function () {
  196. return new \StdClass();
  197. }, false);
  198. $this->assertNotSame(
  199. $this->container->query('test'), $this->container->query('test'));
  200. }
  201. public function testRegisterAliasFactory() {
  202. $this->container->registerService('test', function () {
  203. return new \StdClass();
  204. }, false);
  205. $this->container->registerAlias('test1', 'test');
  206. $this->assertNotSame(
  207. $this->container->query('test'), $this->container->query('test'));
  208. $this->assertNotSame(
  209. $this->container->query('test1'), $this->container->query('test1'));
  210. $this->assertNotSame(
  211. $this->container->query('test'), $this->container->query('test1'));
  212. }
  213. public function testQueryUntypedNullable(): void {
  214. $this->expectException(\OCP\AppFramework\QueryException::class);
  215. $this->container->query(ClassNullableUntypedConstructorArg::class);
  216. }
  217. public function testQueryTypedNullable(): void {
  218. /** @var ClassNullableTypedConstructorArg $service */
  219. $service = $this->container->query(ClassNullableTypedConstructorArg::class);
  220. self::assertNull($service->class);
  221. }
  222. }