1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\MediaBundle\Tests\CDN; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Javier Spagnoletti <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class CloudFrontTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @group legacy |
21
|
|
|
*/ |
22
|
|
|
public function testLegacyCloudFront() |
23
|
|
|
{ |
24
|
|
|
$client = $this->getMock('CloudFrontClientSpy', array('createInvalidation'), array(), '', false); |
25
|
|
|
$client->expects($this->exactly(3))->method('createInvalidation')->will($this->returnValue(new CloudFrontResultSpy())); |
26
|
|
|
|
27
|
|
|
$cloudFront = $this->getMockBuilder('Sonata\MediaBundle\CDN\CloudFront') |
28
|
|
|
->setConstructorArgs(array('/foo', 'secret', 'key', 'xxxxxxxxxxxxxx')) |
29
|
|
|
->setMethods(null) |
30
|
|
|
->getMock(); |
31
|
|
|
$cloudFront->setClient($client); |
32
|
|
|
|
33
|
|
|
$this->assertSame('/foo/bar.jpg', $cloudFront->getPath('bar.jpg', true)); |
34
|
|
|
|
35
|
|
|
$path = '/mypath/file.jpg'; |
36
|
|
|
|
37
|
|
|
$cloudFront->flushByString($path); |
38
|
|
|
$cloudFront->flush($path); |
39
|
|
|
$cloudFront->flushPaths(array($path)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @group legacy |
44
|
|
|
*/ |
45
|
|
|
public function testLegacyException() |
46
|
|
|
{ |
47
|
|
|
$this->setExpectedException('\RuntimeException', 'Unable to flush : '); |
48
|
|
|
$client = $this->getMock('CloudFrontClientSpy', array('createInvalidation'), array(), '', false); |
49
|
|
|
$client->expects($this->exactly(1))->method('createInvalidation')->will($this->returnValue(new CloudFrontResultSpy(true))); |
50
|
|
|
$cloudFront = $this->getMockBuilder('Sonata\MediaBundle\CDN\CloudFront') |
51
|
|
|
->setConstructorArgs(array('/foo', 'secret', 'key', 'xxxxxxxxxxxxxx')) |
52
|
|
|
->setMethods(null) |
53
|
|
|
->getMock(); |
54
|
|
|
$cloudFront->setClient($client); |
55
|
|
|
$cloudFront->flushPaths(array('boom')); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
class CloudFrontClientSpy |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
public function createInvalidation() |
62
|
|
|
{ |
63
|
|
|
return new CloudFrontResultSpy(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
class CloudFrontResultSpy |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
protected $fail = false; |
70
|
|
|
|
71
|
|
|
public function __construct($fail = false) |
72
|
|
|
{ |
73
|
|
|
$this->fail = $fail; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function get($data) |
77
|
|
|
{ |
78
|
|
|
if ('Status' !== $data || $this->fail) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return 'InProgress'; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.