SimpleContainerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Utility;
  23. use OC\AppFramework\Utility\SimpleContainer;
  24. interface TestInterface {}
  25. class ClassEmptyConstructor implements IInterfaceConstructor {}
  26. class ClassSimpleConstructor implements IInterfaceConstructor {
  27. public $test;
  28. public function __construct($test) {
  29. $this->test = $test;
  30. }
  31. }
  32. class ClassComplexConstructor {
  33. public $class;
  34. public $test;
  35. public function __construct(ClassSimpleConstructor $class, $test) {
  36. $this->class = $class;
  37. $this->test = $test;
  38. }
  39. }
  40. interface IInterfaceConstructor {}
  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. public function setUp() {
  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. * @expectedException \OCP\AppFramework\QueryException
  60. */
  61. public function testNothingRegistered() {
  62. $this->container->query('something really hard');
  63. }
  64. /**
  65. * @expectedException \OCP\AppFramework\QueryException
  66. */
  67. public function testNotAClass() {
  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. /**
  158. * @expectedException \OCP\AppFramework\QueryException
  159. */
  160. public function testConstructorComplexNoTestParameterFound() {
  161. $object = $this->container->query(
  162. 'Test\AppFramework\Utility\ClassComplexConstructor'
  163. );
  164. }
  165. public function testRegisterFactory() {
  166. $this->container->registerService('test', function() {
  167. return new \StdClass();
  168. }, false);
  169. $this->assertNotSame(
  170. $this->container->query('test'), $this->container->query('test'));
  171. }
  172. public function testRegisterAliasFactory() {
  173. $this->container->registerService('test', function() {
  174. return new \StdClass();
  175. }, false);
  176. $this->container->registerAlias('test1', 'test');
  177. $this->assertNotSame(
  178. $this->container->query('test'), $this->container->query('test'));
  179. $this->assertNotSame(
  180. $this->container->query('test1'), $this->container->query('test1'));
  181. $this->assertNotSame(
  182. $this->container->query('test'), $this->container->query('test1'));
  183. }
  184. }