SimpleContainerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. interface IInterfaceConstructor {
  45. }
  46. class ClassInterfaceConstructor {
  47. public $class;
  48. public $test;
  49. public function __construct(IInterfaceConstructor $class, $test) {
  50. $this->class = $class;
  51. $this->test = $test;
  52. }
  53. }
  54. class SimpleContainerTest extends \Test\TestCase {
  55. private $container;
  56. protected function setUp(): void {
  57. $this->container = new SimpleContainer();
  58. }
  59. public function testRegister() {
  60. $this->container->registerParameter('test', 'abc');
  61. $this->assertEquals('abc', $this->container->query('test'));
  62. }
  63. /**
  64. * Test querying a class that is not registered without autoload enabled
  65. */
  66. public function testNothingRegistered() {
  67. try {
  68. $this->container->query('something really hard', false);
  69. $this->fail('Expected `QueryException` exception was not thrown');
  70. } catch (\Throwable $exception) {
  71. $this->assertInstanceOf(\OCP\AppFramework\QueryException::class, $exception);
  72. $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
  73. }
  74. }
  75. /**
  76. * Test querying a class that is not registered with autoload enabled
  77. */
  78. public function testNothingRegistered_autoload() {
  79. try {
  80. $this->container->query('something really hard');
  81. $this->fail('Expected `QueryException` exception was not thrown');
  82. } catch (\Throwable $exception) {
  83. $this->assertInstanceOf(\OCP\AppFramework\QueryException::class, $exception);
  84. $this->assertInstanceOf(NotFoundExceptionInterface::class, $exception);
  85. }
  86. }
  87. public function testNotAClass() {
  88. $this->expectException(\OCP\AppFramework\QueryException::class);
  89. $this->container->query('Test\AppFramework\Utility\TestInterface');
  90. }
  91. public function testNoConstructorClass() {
  92. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  93. $this->assertTrue($object instanceof ClassEmptyConstructor);
  94. }
  95. public function testInstancesOnlyOnce() {
  96. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  97. $object2 = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  98. $this->assertSame($object, $object2);
  99. }
  100. public function testConstructorSimple() {
  101. $this->container->registerParameter('test', 'abc');
  102. $object = $this->container->query(
  103. 'Test\AppFramework\Utility\ClassSimpleConstructor'
  104. );
  105. $this->assertTrue($object instanceof ClassSimpleConstructor);
  106. $this->assertEquals('abc', $object->test);
  107. }
  108. public function testConstructorComplex() {
  109. $this->container->registerParameter('test', 'abc');
  110. $object = $this->container->query(
  111. 'Test\AppFramework\Utility\ClassComplexConstructor'
  112. );
  113. $this->assertTrue($object instanceof ClassComplexConstructor);
  114. $this->assertEquals('abc', $object->class->test);
  115. $this->assertEquals('abc', $object->test);
  116. }
  117. public function testConstructorComplexInterface() {
  118. $this->container->registerParameter('test', 'abc');
  119. $this->container->registerService(
  120. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  121. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  122. });
  123. $object = $this->container->query(
  124. 'Test\AppFramework\Utility\ClassInterfaceConstructor'
  125. );
  126. $this->assertTrue($object instanceof ClassInterfaceConstructor);
  127. $this->assertEquals('abc', $object->class->test);
  128. $this->assertEquals('abc', $object->test);
  129. }
  130. public function testOverrideService() {
  131. $this->container->registerService(
  132. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  133. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  134. });
  135. $this->container->registerService(
  136. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  137. return $c->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  138. });
  139. $object = $this->container->query(
  140. 'Test\AppFramework\Utility\IInterfaceConstructor'
  141. );
  142. $this->assertTrue($object instanceof ClassEmptyConstructor);
  143. }
  144. public function testRegisterAliasParamter() {
  145. $this->container->registerParameter('test', 'abc');
  146. $this->container->registerAlias('test1', 'test');
  147. $this->assertEquals('abc', $this->container->query('test1'));
  148. }
  149. public function testRegisterAliasService() {
  150. $this->container->registerService('test', function () {
  151. return new \StdClass;
  152. }, true);
  153. $this->container->registerAlias('test1', 'test');
  154. $this->assertSame(
  155. $this->container->query('test'), $this->container->query('test'));
  156. $this->assertSame(
  157. $this->container->query('test1'), $this->container->query('test1'));
  158. $this->assertSame(
  159. $this->container->query('test'), $this->container->query('test1'));
  160. }
  161. public function sanitizeNameProvider() {
  162. return [
  163. ['ABC\\Foo', 'ABC\\Foo'],
  164. ['\\ABC\\Foo', '\\ABC\\Foo'],
  165. ['\\ABC\\Foo', 'ABC\\Foo'],
  166. ['ABC\\Foo', '\\ABC\\Foo'],
  167. ];
  168. }
  169. /**
  170. * @dataProvider sanitizeNameProvider
  171. */
  172. public function testSanitizeName($register, $query) {
  173. $this->container->registerService($register, function () {
  174. return 'abc';
  175. });
  176. $this->assertEquals('abc', $this->container->query($query));
  177. }
  178. public function testConstructorComplexNoTestParameterFound() {
  179. $this->expectException(\OCP\AppFramework\QueryException::class);
  180. $object = $this->container->query(
  181. 'Test\AppFramework\Utility\ClassComplexConstructor'
  182. );
  183. }
  184. public function testRegisterFactory() {
  185. $this->container->registerService('test', function () {
  186. return new \StdClass();
  187. }, false);
  188. $this->assertNotSame(
  189. $this->container->query('test'), $this->container->query('test'));
  190. }
  191. public function testRegisterAliasFactory() {
  192. $this->container->registerService('test', function () {
  193. return new \StdClass();
  194. }, false);
  195. $this->container->registerAlias('test1', 'test');
  196. $this->assertNotSame(
  197. $this->container->query('test'), $this->container->query('test'));
  198. $this->assertNotSame(
  199. $this->container->query('test1'), $this->container->query('test1'));
  200. $this->assertNotSame(
  201. $this->container->query('test'), $this->container->query('test1'));
  202. }
  203. }