SimpleContainerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 tesOverrideService() {
  110. $this->container->registerParameter('test', 'abc');
  111. $this->container->registerService(
  112. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  113. return $c->query('Test\AppFramework\Utility\ClassSimpleConstructor');
  114. });
  115. $this->container->registerService(
  116. 'Test\AppFramework\Utility\IInterfaceConstructor', function ($c) {
  117. return $c->query('Test\AppFramework\Utility\ClassEmptyConstructor');
  118. });
  119. $object = $this->container->query(
  120. 'Test\AppFramework\Utility\ClassInterfaceConstructor'
  121. );
  122. $this->assertTrue($object instanceof ClassEmptyConstructor);
  123. $this->assertEquals('abc', $object->test);
  124. }
  125. /**
  126. * @expectedException \OCP\AppFramework\QueryException
  127. */
  128. public function testConstructorComplexNoTestParameterFound() {
  129. $object = $this->container->query(
  130. 'Test\AppFramework\Utility\ClassComplexConstructor'
  131. );
  132. }
  133. }