| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- namespace AlibabaCloud\Client\Tests\Unit\Credentials\Ini;
- use ReflectionClass;
- use ReflectionMethod;
- use ReflectionException;
- use PHPUnit\Framework\TestCase;
- use AlibabaCloud\Client\Exception\ClientException;
- use AlibabaCloud\Client\Credentials\Ini\IniCredential;
- /**
- * Class IniCredentialTest
- *
- * @package AlibabaCloud\Client\Tests\Unit\Credentials\Ini
- *
- * @coversDefaultClass \AlibabaCloud\Client\Credentials\Ini\IniCredential
- */
- class IniCredentialTest extends TestCase
- {
- /**
- * @return array
- */
- public static function isNotEmpty()
- {
- return [
- [
- [],
- 'key',
- false,
- ],
- [
- [
- 'key' => '',
- ],
- 'key',
- false,
- ],
- [
- [
- 'key' => 'false',
- ],
- 'key',
- true,
- ],
- [
- [
- 'key' => true,
- ],
- 'key',
- true,
- ],
- [
- [
- 'key' => 'value',
- ],
- 'key',
- true,
- ],
- ];
- }
- /**
- * @throws ClientException
- */
- public static function testInOpenBaseDir()
- {
- if (!\AlibabaCloud\Client\isWindows()) {
- $dirs = 'vfs://AlibabaCloud:/home:/Users:/private:/a/b:/d';
- ini_set('open_basedir', $dirs);
- self::assertEquals($dirs, ini_get('open_basedir'));
- self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/Users/alibabacloud'));
- self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/private/alibabacloud'));
- self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/no/permission'));
- self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/a'));
- self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/a/b/'));
- self::assertTrue(\AlibabaCloud\Client\inOpenBasedir('/a/b/c'));
- self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/b'));
- self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/b/'));
- self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/x/d/c.txt'));
- self::assertFalse(\AlibabaCloud\Client\inOpenBasedir('/a/b.php'));
- }
- if (\AlibabaCloud\Client\isWindows()) {
- $dirs = 'C:\\projects;C:\\Users';
- ini_set('open_basedir', $dirs);
- self::assertEquals($dirs, ini_get('open_basedir'));
- }
- }
- /**
- * Independent method, the file must exist.
- *
- * @return array
- */
- public static function parseFile()
- {
- return [
- [
- VirtualAccessKeyCredential::ok(),
- 'Format error: vfs://AlibabaCloud/credentials-1',
- ],
- ];
- }
- /**
- * @covers ::__construct
- * @covers ::getDefaultFile
- * @covers ::getFilename
- * @dataProvider getFilename
- *
- * @param string $fileName
- * @param string $getFileName
- */
- public function testGetFilename($fileName, $getFileName)
- {
- $object = new IniCredential($fileName);
- self::assertEquals($getFileName, $object->getFilename());
- }
- /**
- * @return array
- */
- public function getFilename()
- {
- return [
- [
- '',
- (new IniCredential())->getDefaultFile(),
- ],
- [
- '/no/no/no',
- '/no/no/no',
- ],
- ];
- }
- /**
- * @dataProvider isNotEmpty
- *
- * @param array $array
- * @param string $key
- * @param bool $bool
- *
- * @throws ReflectionException
- */
- public function testIsNotEmpty($array, $key, $bool)
- {
- $object = new IniCredential();
- $ref = new ReflectionClass(
- IniCredential::class
- );
- $method = $ref->getMethod('isNotEmpty');
- $method->setAccessible(true);
- $result = $method->invokeArgs($object, [$array, $key]);
- self::assertEquals($result, $bool);
- }
- public function testMissingRequired()
- {
- $this->expectException(ClientException::class);
- $reg = "/Missing required 'key' option for 'name'/";
- if (method_exists($this, 'expectExceptionMessageMatches')) {
- $this->expectExceptionMessageMatches($reg);
- } elseif (method_exists($this, 'expectExceptionMessageRegExp')) {
- $this->expectExceptionMessageRegExp($reg);
- }
- $object = new IniCredential();
- $object->missingRequired('key', 'name');
- }
- /**
- * @throws ReflectionException
- */
- public function testForgetLoadedCredentialsFile()
- {
- $reflectedClass = new ReflectionClass(IniCredential::class);
- $reflectedProperty = $reflectedClass->getProperty('hasLoaded');
- $reflectedProperty->setAccessible(true);
- $reflectedProperty->setValue([
- 'FILE' => 'FILE',
- ]);
- self::assertArrayHasKey('FILE', $reflectedClass->getStaticProperties()['hasLoaded']);
- IniCredential::forgetLoadedCredentialsFile();
- self::assertEquals([], $reflectedClass->getStaticProperties()['hasLoaded']);
- }
- /**
- * @throws ClientException
- */
- public function testLoad()
- {
- $object = new IniCredential(VirtualAccessKeyCredential::ok());
- self::assertEquals($object->load(), $object->load());
- }
- /**
- * @dataProvider loadFile
- *
- * @param string $fileName
- *
- * @throws ReflectionException
- */
- public function testLoadFile($fileName)
- {
- $object = new IniCredential($fileName);
- $method = new ReflectionMethod(
- IniCredential::class,
- 'loadFile'
- );
- $method->setAccessible(true);
- try {
- $result = $method->invoke($object);
- if (method_exists($this, 'assertIsArray')) {
- self::assertIsArray($result);
- } elseif (method_exists($this, 'assertInternalType')) {
- self::assertInternalType('array', $result);
- }
- } catch (ClientException $exception) {
- self::assertEquals(
- $exception->getErrorMessage(),
- 'Credential file is not readable: /no/no.no'
- );
- }
- }
- /**
- * @return array
- */
- public function loadFile()
- {
- return [
- [''],
- ['/no/no.no'],
- ];
- }
- /**
- * @param string $fileName
- * @param string $exceptionMessage
- *
- * @throws ReflectionException
- * @dataProvider parseFile
- */
- public function testParseFile($fileName, $exceptionMessage)
- {
- $object = new IniCredential($fileName);
- $method = new ReflectionMethod(
- IniCredential::class,
- 'parseFile'
- );
- $method->setAccessible(true);
- try {
- self::assertArrayHasKey('ok', $method->invoke($object));
- } catch (ClientException $exception) {
- self::assertEquals(
- $exception->getErrorMessage(),
- $exceptionMessage
- );
- }
- }
- /**
- * @throws ReflectionException
- * @dataProvider parseFile
- */
- public function testParseFileBadFormat()
- {
- $object = new IniCredential(VirtualAccessKeyCredential::badFormat());
- $method = new ReflectionMethod(
- IniCredential::class,
- 'parseFile'
- );
- $method->setAccessible(true);
- try {
- self::assertArrayHasKey('ok', $method->invoke($object));
- } catch (ClientException $exception) {
- self::assertEquals(
- $exception->getErrorMessage(),
- 'Format error: vfs://AlibabaCloud/credentials-1-badFormat'
- );
- }
- }
- /**
- * @throws ReflectionException
- * @covers ::getHomeDirectory
- * @depends testParseFile
- */
- public function testGetsHomeDirectoryForWindowsUser()
- {
- putenv('HOME=');
- putenv('HOMEDRIVE=C:');
- putenv('HOMEPATH=\\Users\\Alibaba');
- $ref = new ReflectionClass(IniCredential::class);
- $method = $ref->getMethod('getHomeDirectory');
- $method->setAccessible(true);
- $this->assertEquals('C:\\Users\\Alibaba', $method->invoke(null));
- }
- /**
- * @throws ReflectionException
- * @covers ::getHomeDirectory
- * @depends testGetsHomeDirectoryForWindowsUser
- */
- public function testGetsHomeDirectoryForLinuxUser()
- {
- putenv('HOME=/root');
- putenv('HOMEDRIVE=');
- putenv('HOMEPATH=');
- $ref = new ReflectionClass(IniCredential::class);
- $method = $ref->getMethod('getHomeDirectory');
- $method->setAccessible(true);
- $this->assertEquals('/root', $method->invoke(null));
- }
- }
|