ConfigTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace AlibabaCloud\Client\Tests\LowerthanVersion7_2\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\LowerthanVersion7_2\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. * @throws ReflectionException
  25. */
  26. public function tearDown()
  27. {
  28. self::setStaticProperty(null);
  29. }
  30. /**
  31. * @param $value
  32. *
  33. * @throws ReflectionException
  34. */
  35. private static function setStaticProperty($value)
  36. {
  37. $ref = new \ReflectionClass(Config::class);
  38. $property = $ref->getProperty('configManager');
  39. $property->setAccessible(true);
  40. $property->setValue($value);
  41. }
  42. /**
  43. * @throws ReflectionException
  44. */
  45. public function testGetConfigManager()
  46. {
  47. $ref = new \ReflectionClass(Config::class);
  48. $method = $ref->getMethod('getConfigManager');
  49. $method->setAccessible(true);
  50. self::assertInstanceOf(ConfigManager::class, $method->invoke(null));
  51. }
  52. /**
  53. * @depends testGetConfigManager
  54. * @throws ReflectionException
  55. * @throws Exception
  56. */
  57. public function testSetAndGet()
  58. {
  59. self::setStaticProperty(new ConfigManager(self::file()->url()));
  60. Config::set('vfs', __METHOD__);
  61. self::assertEquals(Config::get('vfs'), __METHOD__);
  62. }
  63. /**
  64. * @return vfsStreamFile|string
  65. */
  66. private static function file()
  67. {
  68. if (self::$vfs !== null) {
  69. return self::$vfs;
  70. }
  71. $content = <<<EOT
  72. <?php
  73. return [];
  74. EOT;
  75. $root = vfsStream::setup('AlibabaCloud');
  76. self::$vfs = vfsStream::newFile('config')
  77. ->withContent($content)
  78. ->at($root);
  79. return self::$vfs;
  80. }
  81. }