ConfigTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\Unit\Credentials;
  3. use Exception;
  4. use ReflectionException;
  5. use org\bovigo\vfs\vfsStream;
  6. use PHPUnit\Framework\TestCase;
  7. use org\bovigo\vfs\vfsStreamFile;
  8. use AlibabaCloud\Client\Config\Config;
  9. use clagiordano\weblibs\configmanager\ConfigManager;
  10. /**
  11. * Class AccessKeyCredentialTest
  12. *
  13. * @package AlibabaCloud\Client\Tests\Unit\Credentials
  14. */
  15. class ConfigTest extends TestCase
  16. {
  17. /**
  18. * @var string
  19. */
  20. private static $vfs;
  21. /**
  22. * Restore static attribute.
  23. *
  24. * @after
  25. * @throws ReflectionException
  26. */
  27. protected function finalize()
  28. {
  29. parent::tearDown();
  30. self::setStaticProperty(null);
  31. }
  32. /**
  33. * @param $value
  34. *
  35. * @throws ReflectionException
  36. */
  37. private static function setStaticProperty($value)
  38. {
  39. $ref = new \ReflectionClass(Config::class);
  40. $property = $ref->getProperty('configManager');
  41. $property->setAccessible(true);
  42. $property->setValue($value);
  43. }
  44. /**
  45. * @throws ReflectionException
  46. */
  47. public function testGetConfigManager()
  48. {
  49. $ref = new \ReflectionClass(Config::class);
  50. $method = $ref->getMethod('getConfigManager');
  51. $method->setAccessible(true);
  52. self::assertInstanceOf(ConfigManager::class, $method->invoke(null));
  53. }
  54. /**
  55. * @depends testGetConfigManager
  56. * @throws ReflectionException
  57. * @throws Exception
  58. */
  59. public function testSetAndGet()
  60. {
  61. self::setStaticProperty(new ConfigManager(self::file()->url()));
  62. Config::set('vfs', __METHOD__);
  63. self::assertEquals(Config::get('vfs'), __METHOD__);
  64. }
  65. /**
  66. * @return vfsStreamFile|string
  67. */
  68. private static function file()
  69. {
  70. if (self::$vfs !== null) {
  71. return self::$vfs;
  72. }
  73. $content = <<<EOT
  74. <?php
  75. return [];
  76. EOT;
  77. $root = vfsStream::setup('AlibabaCloud');
  78. self::$vfs = vfsStream::newFile('config')
  79. ->withContent($content)
  80. ->at($root);
  81. return self::$vfs;
  82. }
  83. }