1
0

DIIntergrationTests.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. }
  30. class ClassA1 implements Interface1 {
  31. }
  32. class ClassA2 implements Interface1 {
  33. }
  34. class ClassB {
  35. /** @var Interface1 */
  36. public $interface1;
  37. /**
  38. * ClassB constructor.
  39. *
  40. * @param Interface1 $interface1
  41. */
  42. public function __construct(Interface1 $interface1) {
  43. $this->interface1 = $interface1;
  44. }
  45. }
  46. class DIIntergrationTests extends TestCase {
  47. /** @var DIContainer */
  48. private $container;
  49. /** @var ServerContainer */
  50. private $server;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->server = new ServerContainer();
  54. $this->container = new DIContainer('App1', [], $this->server);
  55. }
  56. public function testInjectFromServer() {
  57. $this->server->registerService(Interface1::class, function () {
  58. return new ClassA1();
  59. });
  60. $this->server->registerService(ClassB::class, function (SimpleContainer $c) {
  61. return new ClassB(
  62. $c->query(Interface1::class)
  63. );
  64. });
  65. /** @var ClassB $res */
  66. $res = $this->container->query(ClassB::class);
  67. $this->assertSame(ClassA1::class, get_class($res->interface1));
  68. }
  69. public function testInjectDepFromServer() {
  70. $this->server->registerService(Interface1::class, function () {
  71. return new ClassA1();
  72. });
  73. $this->container->registerService(ClassB::class, function (SimpleContainer $c) {
  74. return new ClassB(
  75. $c->query(Interface1::class)
  76. );
  77. });
  78. /** @var ClassB $res */
  79. $res = $this->container->query(ClassB::class);
  80. $this->assertSame(ClassA1::class, get_class($res->interface1));
  81. }
  82. public function testOverwriteDepFromServer() {
  83. $this->server->registerService(Interface1::class, function () {
  84. return new ClassA1();
  85. });
  86. $this->container->registerService(Interface1::class, function () {
  87. return new ClassA2();
  88. });
  89. $this->container->registerService(ClassB::class, function (SimpleContainer $c) {
  90. return new ClassB(
  91. $c->query(Interface1::class)
  92. );
  93. });
  94. /** @var ClassB $res */
  95. $res = $this->container->query(ClassB::class);
  96. $this->assertSame(ClassA2::class, get_class($res->interface1));
  97. }
  98. public function testIgnoreOverwriteInServerClass() {
  99. $this->server->registerService(Interface1::class, function () {
  100. return new ClassA1();
  101. });
  102. $this->container->registerService(Interface1::class, function () {
  103. return new ClassA2();
  104. });
  105. $this->server->registerService(ClassB::class, function (SimpleContainer $c) {
  106. return new ClassB(
  107. $c->query(Interface1::class)
  108. );
  109. });
  110. /** @var ClassB $res */
  111. $res = $this->container->query(ClassB::class);
  112. $this->assertSame(ClassA1::class, get_class($res->interface1));
  113. }
  114. }