DiTest.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace EasySwoole\Component\Tests;
  3. use EasySwoole\Component\Di;
  4. use EasySwoole\Component\Tests\Lib\Get;
  5. use PHPUnit\Framework\TestCase;
  6. use EasySwoole\Component\Tests\Lib\Bar;
  7. use EasySwoole\Component\Tests\Lib\Foo;
  8. class DiTest extends TestCase
  9. {
  10. protected function setUp(): void
  11. {
  12. Di::getInstance()->clear();
  13. }
  14. public function testSetAndGet()
  15. {
  16. // string
  17. Di::getInstance()->set('string', 'string');
  18. $this->assertEquals(Di::getInstance()->get('string'), 'string');
  19. // callback
  20. Di::getInstance()->set('callback', function () {
  21. return 'callback';
  22. });
  23. $this->assertIsCallable(Di::getInstance()->get('callback'));
  24. $this->assertEquals('callback', call_user_func(Di::getInstance()->get('callback')));
  25. // object
  26. Di::getInstance()->set('object', new class {
  27. public $foo = 1;
  28. public function bar()
  29. {
  30. return 'bar';
  31. }
  32. });
  33. $this->assertIsObject(Di::getInstance()->get('object'));
  34. $this->assertEquals('bar', call_user_func([Di::getInstance()->get('object'), 'bar']));
  35. Di::getInstance()->get('object')->foo = 2;
  36. $this->assertEquals(2, Di::getInstance()->get('object')->foo);
  37. // ref di
  38. Di::getInstance()->set(Bar::class, Bar::class);
  39. Di::getInstance()->set(Foo::class, Foo::class);
  40. $this->assertEquals(Di::getInstance()->get(Foo::class)->bar->bar, 'bar');
  41. Di::getInstance()->set(Get::class, Get::class);
  42. Di::getInstance()->set('foo', Foo::class);
  43. $this->assertInstanceOf(Foo::class, Di::getInstance()->get(Get::class)->foo);
  44. Di::getInstance()->delete('foo');
  45. Di::getInstance()->set(Get::class, Get::class);
  46. $this->assertEquals(1, Di::getInstance()->get(Get::class)->foo);
  47. }
  48. public function testAlias()
  49. {
  50. Di::getInstance()->set('string', 'value');
  51. Di::getInstance()->alias('string-alias', 'string');
  52. $this->assertEquals('value', Di::getInstance()->get('string-alias'));
  53. Di::getInstance()->deleteAlias('string-alias');
  54. $this->assertEquals(null, Di::getInstance()->get('string-alias'));
  55. }
  56. public function testDelete()
  57. {
  58. Di::getInstance()->set('string', 'value');
  59. Di::getInstance()->set('string1', 'value');
  60. $this->assertEquals('value', Di::getInstance()->get('string'));
  61. $this->assertEquals('value', Di::getInstance()->get('string1'));
  62. Di::getInstance()->delete('string');
  63. $this->assertEquals(null, Di::getInstance()->get('string'));
  64. $this->assertEquals('value', Di::getInstance()->get('string1'));
  65. }
  66. public function testClear()
  67. {
  68. Di::getInstance()->set('string', 'value');
  69. Di::getInstance()->set('string1', 'value');
  70. $this->assertEquals('value', Di::getInstance()->get('string'));
  71. $this->assertEquals('value', Di::getInstance()->get('string1'));
  72. Di::getInstance()->clear();
  73. $this->assertEquals(null, Di::getInstance()->get('string'));
  74. $this->assertEquals(null, Di::getInstance()->get('string1'));
  75. }
  76. public function testOnMissKey()
  77. {
  78. Di::getInstance()->setOnKeyMiss(function ($key) {
  79. return "{$key} miss";
  80. });
  81. $this->assertEquals('string miss', Di::getInstance()->get('string'));
  82. }
  83. }