DIIntergrationTests.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program 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 License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\DependencyInjection;
  24. use OC\AppFramework\DependencyInjection\DIContainer;
  25. use OC\AppFramework\Utility\SimpleContainer;
  26. use OC\ServerContainer;
  27. use Test\TestCase;
  28. interface Interface1 {}
  29. class ClassA1 implements Interface1 {}
  30. class ClassA2 implements Interface1 {}
  31. class ClassB {
  32. /** @var Interface1 */
  33. public $interface1;
  34. /**
  35. * ClassB constructor.
  36. *
  37. * @param Interface1 $interface1
  38. */
  39. public function __construct(Interface1 $interface1) {
  40. $this->interface1 = $interface1;
  41. }
  42. }
  43. class DIIntergrationTests extends TestCase {
  44. /** @var DIContainer */
  45. private $container;
  46. /** @var ServerContainer */
  47. private $server;
  48. public function setUp() {
  49. parent::setUp();
  50. $this->server = new ServerContainer();
  51. $this->container = new DIContainer('App1', [], $this->server);
  52. }
  53. public function testInjectFromServer() {
  54. $this->server->registerService(Interface1::class, function () {
  55. return new ClassA1();
  56. });
  57. $this->server->registerService(ClassB::class, function (SimpleContainer $c) {
  58. return new ClassB(
  59. $c->query(Interface1::class)
  60. );
  61. });
  62. /** @var ClassB $res */
  63. $res = $this->container->query(ClassB::class);
  64. $this->assertSame(ClassA1::class, get_class($res->interface1));
  65. }
  66. public function testInjectDepFromServer() {
  67. $this->server->registerService(Interface1::class, function () {
  68. return new ClassA1();
  69. });
  70. $this->container->registerService(ClassB::class, function (SimpleContainer $c) {
  71. return new ClassB(
  72. $c->query(Interface1::class)
  73. );
  74. });
  75. /** @var ClassB $res */
  76. $res = $this->container->query(ClassB::class);
  77. $this->assertSame(ClassA1::class, get_class($res->interface1));
  78. }
  79. public function testOverwriteDepFromServer() {
  80. $this->server->registerService(Interface1::class, function () {
  81. return new ClassA1();
  82. });
  83. $this->container->registerService(Interface1::class, function () {
  84. return new ClassA2();
  85. });
  86. $this->container->registerService(ClassB::class, function (SimpleContainer $c) {
  87. return new ClassB(
  88. $c->query(Interface1::class)
  89. );
  90. });
  91. /** @var ClassB $res */
  92. $res = $this->container->query(ClassB::class);
  93. $this->assertSame(ClassA2::class, get_class($res->interface1));
  94. }
  95. public function testIgnoreOverwriteInServerClass() {
  96. $this->server->registerService(Interface1::class, function () {
  97. return new ClassA1();
  98. });
  99. $this->container->registerService(Interface1::class, function () {
  100. return new ClassA2();
  101. });
  102. $this->server->registerService(ClassB::class, function (SimpleContainer $c) {
  103. return new ClassB(
  104. $c->query(Interface1::class)
  105. );
  106. });
  107. /** @var ClassB $res */
  108. $res = $this->container->query(ClassB::class);
  109. $this->assertSame(ClassA1::class, get_class($res->interface1));
  110. }
  111. }