-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathAbstractConfigurationProviderTest.php
106 lines (92 loc) · 3.32 KB
/
AbstractConfigurationProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Aws\Test;
use Aws\LruArrayCache;
use Aws\Result;
use Aws\ResultInterface;
use GuzzleHttp\Promise;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
/**
* @covers \Aws\AbstractConfigurationProvider
*/
class AbstractConfigurationProviderTest extends TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $provider;
public function __construct()
{
parent::__construct();
$this->provider = $this->getMockForAbstractClass('\Aws\AbstractConfigurationProvider');
}
public function testGetsHomeDirectoryForWindowsUsers()
{
putenv('HOME=');
putenv('HOMEDRIVE=C:');
putenv('HOMEPATH=\\My\\Home');
$ref = new \ReflectionClass('\Aws\AbstractConfigurationProvider');
$meth = $ref->getMethod('getHomeDir');
$meth->setAccessible(true);
$this->assertSame('C:\\My\\Home', $meth->invoke(null));
}
public function testMemoizes()
{
$called = 0;
$expected = ['expected', 'value'];
$f = function () use (&$called, $expected) {
$called++;
return Promise\Create::promiseFor($expected);
};
$p = call_user_func([$this->provider, 'memoize'], $f);
$this->assertSame($expected, $p()->wait());
$this->assertSame(1, $called);
$this->assertSame($expected, $p()->wait());
$this->assertSame(1, $called);
}
public function testChainsConfiguration()
{
$expected = ['expected', 'value'];
$a = function () {
return Promise\Create::rejectionFor(new \Exception('Failure'));
};
$b = function () use ($expected) {
return Promise\Create::promiseFor($expected);
};
$c = function () {
$this->fail('Should not have called');
};
$chained = call_user_func([$this->provider, 'chain'], $a, $b, $c);
$result = $chained()->wait();
$this->assertSame($expected, $result);
}
public function testChainThrowsExceptionOnEmptyArgs()
{
$this->expectException(\InvalidArgumentException::class);
call_user_func([$this->provider, 'chain']);
}
public function testsPersistsToCache()
{
$cache = new LruArrayCache();
$expected = new Result(['expected_key' => 'expected_value']);
// Set interfaceClass property that's normally set by child class
$ref = new \ReflectionClass('\Aws\AbstractConfigurationProvider');
$property = $ref->getProperty('interfaceClass');
$property->setAccessible(true);
$property->setValue(null,'\Aws\ResultInterface');
$timesCalled = 0;
$volatileProvider = function () use ($expected, &$timesCalled) {
if (0 === $timesCalled) {
++$timesCalled;
return Promise\Create::promiseFor($expected);
}
throw new \BadFunctionCallException('I was called too many times!');
};
for ($i = 0; $i < 10; $i++) {
/** @var ResultInterface $result */
$result = call_user_func(
call_user_func([$this->provider, 'cache'], $volatileProvider, $cache)
)->wait();
}
$this->assertSame(1, $timesCalled);
$this->assertCount(1, $cache);
$this->assertSame($expected->toArray(), $result->toArray());
}
}