|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\Tests\CDN; |
|
15
|
|
|
|
|
16
|
|
|
use Aws\CloudFront\CloudFrontClient; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use Sonata\MediaBundle\CDN\CloudFront; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author Javier Spagnoletti <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class CloudFrontTest extends TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @group legacy |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testLegacyCloudFront(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$client = $this->createMock(CloudFrontClientSpy::class); |
|
31
|
|
|
|
|
32
|
|
|
$client->expects($this->exactly(3))->method('createInvalidation')->willReturn(new CloudFrontResultSpy()); |
|
33
|
|
|
|
|
34
|
|
|
$cloudFront = $this->getMockBuilder(CloudFront::class) |
|
35
|
|
|
->setConstructorArgs(['/foo', 'secret', 'key', 'xxxxxxxxxxxxxx']) |
|
36
|
|
|
->onlyMethods([]) |
|
37
|
|
|
->getMock(); |
|
38
|
|
|
$cloudFront->setClient($client); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertSame('/foo/bar.jpg', $cloudFront->getPath('bar.jpg', true)); |
|
41
|
|
|
|
|
42
|
|
|
$path = '/mypath/file.jpg'; |
|
43
|
|
|
|
|
44
|
|
|
$cloudFront->flushByString($path); |
|
45
|
|
|
$cloudFront->flush($path); |
|
46
|
|
|
$cloudFront->flushPaths([$path]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @group legacy |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testLegacyException(): void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->expectException(\RuntimeException::class); |
|
55
|
|
|
$this->expectExceptionMessage('Unable to flush : '); |
|
56
|
|
|
|
|
57
|
|
|
$client = $this->createMock(CloudFrontClientSpy::class); |
|
58
|
|
|
$client->expects($this->once())->method('createInvalidation')->willReturn(new CloudFrontResultSpy(true)); |
|
59
|
|
|
$cloudFront = $this->getMockBuilder(CloudFront::class) |
|
60
|
|
|
->setConstructorArgs(['/foo', 'secret', 'key', 'xxxxxxxxxxxxxx']) |
|
61
|
|
|
->onlyMethods([]) |
|
62
|
|
|
->getMock(); |
|
63
|
|
|
$cloudFront->setClient($client); |
|
64
|
|
|
$cloudFront->flushPaths(['boom']); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
class CloudFrontClientSpy extends CloudFrontClient |
|
69
|
|
|
{ |
|
70
|
|
|
public function createInvalidation(): CloudFrontResultSpy |
|
71
|
|
|
{ |
|
72
|
|
|
return new CloudFrontResultSpy(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
final class CloudFrontResultSpy |
|
77
|
|
|
{ |
|
78
|
|
|
private $fail; |
|
79
|
|
|
|
|
80
|
|
|
public function __construct(bool $fail = false) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->fail = $fail; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function get($data): string |
|
86
|
|
|
{ |
|
87
|
|
|
if ('Status' !== $data || $this->fail) { |
|
88
|
|
|
return ''; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return 'InProgress'; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|