1
0

DIIntergrationTests.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\DependencyInjection;
  7. use OC\AppFramework\DependencyInjection\DIContainer;
  8. use OC\AppFramework\Utility\SimpleContainer;
  9. use OC\ServerContainer;
  10. use Test\TestCase;
  11. interface Interface1 {
  12. }
  13. class ClassA1 implements Interface1 {
  14. }
  15. class ClassA2 implements Interface1 {
  16. }
  17. class ClassB {
  18. /** @var Interface1 */
  19. public $interface1;
  20. /**
  21. * ClassB constructor.
  22. *
  23. * @param Interface1 $interface1
  24. */
  25. public function __construct(Interface1 $interface1) {
  26. $this->interface1 = $interface1;
  27. }
  28. }
  29. class DIIntergrationTests extends TestCase {
  30. /** @var DIContainer */
  31. private $container;
  32. /** @var ServerContainer */
  33. private $server;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->server = new ServerContainer();
  37. $this->container = new DIContainer('App1', [], $this->server);
  38. }
  39. public function testInjectFromServer(): void {
  40. $this->server->registerService(Interface1::class, function () {
  41. return new ClassA1();
  42. });
  43. $this->server->registerService(ClassB::class, function (SimpleContainer $c) {
  44. return new ClassB(
  45. $c->query(Interface1::class)
  46. );
  47. });
  48. /** @var ClassB $res */
  49. $res = $this->container->query(ClassB::class);
  50. $this->assertSame(ClassA1::class, get_class($res->interface1));
  51. }
  52. public function testInjectDepFromServer(): void {
  53. $this->server->registerService(Interface1::class, function () {
  54. return new ClassA1();
  55. });
  56. $this->container->registerService(ClassB::class, function (SimpleContainer $c) {
  57. return new ClassB(
  58. $c->query(Interface1::class)
  59. );
  60. });
  61. /** @var ClassB $res */
  62. $res = $this->container->query(ClassB::class);
  63. $this->assertSame(ClassA1::class, get_class($res->interface1));
  64. }
  65. public function testOverwriteDepFromServer(): void {
  66. $this->server->registerService(Interface1::class, function () {
  67. return new ClassA1();
  68. });
  69. $this->container->registerService(Interface1::class, function () {
  70. return new ClassA2();
  71. });
  72. $this->container->registerService(ClassB::class, function (SimpleContainer $c) {
  73. return new ClassB(
  74. $c->query(Interface1::class)
  75. );
  76. });
  77. /** @var ClassB $res */
  78. $res = $this->container->query(ClassB::class);
  79. $this->assertSame(ClassA2::class, get_class($res->interface1));
  80. }
  81. public function testIgnoreOverwriteInServerClass(): void {
  82. $this->server->registerService(Interface1::class, function () {
  83. return new ClassA1();
  84. });
  85. $this->container->registerService(Interface1::class, function () {
  86. return new ClassA2();
  87. });
  88. $this->server->registerService(ClassB::class, function (SimpleContainer $c) {
  89. return new ClassB(
  90. $c->query(Interface1::class)
  91. );
  92. });
  93. /** @var ClassB $res */
  94. $res = $this->container->query(ClassB::class);
  95. $this->assertSame(ClassA1::class, get_class($res->interface1));
  96. }
  97. }