SimpleContainerTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. interface TestInterface {
  26. }
  27. class ClassEmptyConstructor implements IInterfaceConstructor {
  28. }
  29. class ClassSimpleConstructor implements IInterfaceConstructor {
  30. public $test;
  31. public function __construct($test) {
  32. $this->test = $test;
  33. }
  34. }
  35. class ClassComplexConstructor {
  36. public $class;
  37. public $test;
  38. public function __construct(ClassSimpleConstructor $class, $test) {
  39. $this->class = $class;
  40. $this->test = $test;
  41. }
  42. }
  43. interface IInterfaceConstructor {
  44. }
  45. class ClassInterfaceConstructor {
  46. public $class;
  47. public $test;
  48. public function __construct(IInterfaceConstructor $class, $test) {
  49. $this->class = $class;
  50. $this->test = $test;
  51. }
  52. }
  53. class SimpleContainerTest extends \Test\TestCase {
  54. private $container;
  55. protected function setUp(): void {
  56. $this->container = new SimpleContainer();
  57. }
  58. public function testRegister() {
  59. $this->container->registerParameter('test', 'abc');
  60. $this->assertEquals('abc', $this->container->query('test'));
  61. }
  62. public function testNothingRegistered() {
  63. $this->expectException(\OCP\AppFramework\QueryException::class);
  64. $this->container->query('something really hard');
  65. }
  66. public function testNotAClass() {
  67. $this->expectException(\OCP\AppFramework\QueryException::class);
  68. $this->container->query('Test\AppFramework\Utility\TestInterface');
  69. }
  70. public function testNoConstructorClass() {
  71. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  72. $this->assertTrue($object instanceof ClassEmptyConstructor);
  73. }
  74. public function testInstancesOnlyOnce() {
  75. $object = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  76. $object2 = $this->container->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  77. $this->assertSame($object, $object2);
  78. }
  79. public function testConstructorSimple() {
  80. $this->container->registerParameter('test', 'abc');
  81. $object = $this->container->query(
  82. 'Test\AppFramework\Utility\ClassSimpleConstructor'
  83. );
  84. $this->assertTrue($object instanceof ClassSimpleConstructor);
  85. $this->assertEquals('abc', $object->test);
  86. }
  87. public function testConstructorComplex() {
  88. $this->container->registerParameter('test', 'abc');
  89. $object = $this->container->query(
  90. 'Test\AppFramework\Utility\ClassComplexConstructor'
  91. );
  92. $this->assertTrue($object instanceof ClassComplexConstructor);
  93. $this->assertEquals('abc', $object->class->test);
  94. $this->assertEquals('abc', $object->test);
  95. }
  96. public function testConstructorComplexInterface() {
  97. $this->container->registerParameter('test', 'abc');
  98. $this->container->registerService(
  99. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  100. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  101. });
  102. $object = $this->container->query(
  103. 'Test\AppFramework\Utility\ClassInterfaceConstructor'
  104. );
  105. $this->assertTrue($object instanceof ClassInterfaceConstructor);
  106. $this->assertEquals('abc', $object->class->test);
  107. $this->assertEquals('abc', $object->test);
  108. }
  109. public function testOverrideService() {
  110. $this->container->registerService(
  111. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  112. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  113. });
  114. $this->container->registerService(
  115. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  116. return $c->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  117. });
  118. $object = $this->container->query(
  119. 'Test\AppFramework\Utility\IInterfaceConstructor'
  120. );
  121. $this->assertTrue($object instanceof ClassEmptyConstructor);
  122. }
  123. public function testRegisterAliasParamter() {
  124. $this->container->registerParameter('test', 'abc');
  125. $this->container->registerAlias('test1', 'test');
  126. $this->assertEquals('abc', $this->container->query('test1'));
  127. }
  128. public function testRegisterAliasService() {
  129. $this->container->registerService('test', function () {
  130. return new \StdClass;
  131. }, true);
  132. $this->container->registerAlias('test1', 'test');
  133. $this->assertSame(
  134. $this->container->query('test'), $this->container->query('test'));
  135. $this->assertSame(
  136. $this->container->query('test1'), $this->container->query('test1'));
  137. $this->assertSame(
  138. $this->container->query('test'), $this->container->query('test1'));
  139. }
  140. public function sanitizeNameProvider() {
  141. return [
  142. ['ABC\\Foo', 'ABC\\Foo'],
  143. ['\\ABC\\Foo', '\\ABC\\Foo'],
  144. ['\\ABC\\Foo', 'ABC\\Foo'],
  145. ['ABC\\Foo', '\\ABC\\Foo'],
  146. ];
  147. }
  148. /**
  149. * @dataProvider sanitizeNameProvider
  150. */
  151. public function testSanitizeName($register, $query) {
  152. $this->container->registerService($register, function () {
  153. return 'abc';
  154. });
  155. $this->assertEquals('abc', $this->container->query($query));
  156. }
  157. public function testConstructorComplexNoTestParameterFound() {
  158. $this->expectException(\OCP\AppFramework\QueryException::class);
  159. $object = $this->container->query(
  160. 'Test\AppFramework\Utility\ClassComplexConstructor'
  161. );
  162. }
  163. public function testRegisterFactory() {
  164. $this->container->registerService('test', function () {
  165. return new \StdClass();
  166. }, false);
  167. $this->assertNotSame(
  168. $this->container->query('test'), $this->container->query('test'));
  169. }
  170. public function testRegisterAliasFactory() {
  171. $this->container->registerService('test', function () {
  172. return new \StdClass();
  173. }, false);
  174. $this->container->registerAlias('test1', 'test');
  175. $this->assertNotSame(
  176. $this->container->query('test'), $this->container->query('test'));
  177. $this->assertNotSame(
  178. $this->container->query('test1'), $this->container->query('test1'));
  179. $this->assertNotSame(
  180. $this->container->query('test'), $this->container->query('test1'));
  181. }
  182. }