1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Liip\ImagineBundle\Tests\Imagine\Cache\Resolver; |
4
|
|
|
|
5
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Resolver\ProxyResolver; |
6
|
|
|
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface; |
7
|
|
|
use Liip\ImagineBundle\Model\Binary; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Liip\ImagineBundle\Imagine\Cache\Resolver\ProxyResolver |
11
|
|
|
*/ |
12
|
|
|
class ProxyResolverTest extends \Phpunit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ResolverInterface |
16
|
|
|
*/ |
17
|
|
|
private $primaryResolver; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var ProxyResolver |
21
|
|
|
*/ |
22
|
|
|
private $resolver; |
23
|
|
|
|
24
|
|
|
public function setUp() |
25
|
|
|
{ |
26
|
|
|
$this->primaryResolver = $this->getMock('Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface'); |
27
|
|
|
|
28
|
|
|
$this->resolver = new ProxyResolver($this->primaryResolver, array('http://images.example.com')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function testProxyCallAndRewriteReturnedUrlOnResolve() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$expectedPath = '/foo/bar/bazz.png'; |
34
|
|
|
$expectedFilter = 'test'; |
35
|
|
|
|
36
|
|
|
$this->primaryResolver |
|
|
|
|
37
|
|
|
->expects($this->once()) |
38
|
|
|
->method('resolve') |
39
|
|
|
->with($expectedPath, $expectedFilter) |
40
|
|
|
->will($this->returnValue('http://foo.com/thumbs/foo/bar/bazz.png')) |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
$result = $this->resolver->resolve($expectedPath, $expectedFilter); |
44
|
|
|
|
45
|
|
|
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testProxyCallAndRewriteReturnedUrlEvenSchemesDiffersOnResolve() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$expectedPath = '/foo/bar/bazz.png'; |
51
|
|
|
$expectedFilter = 'test'; |
52
|
|
|
|
53
|
|
|
$this->primaryResolver |
|
|
|
|
54
|
|
|
->expects($this->once()) |
55
|
|
|
->method('resolve') |
56
|
|
|
->with($expectedPath, $expectedFilter) |
57
|
|
|
->will($this->returnValue('http://foo.com/thumbs/foo/bar/bazz.png')) |
58
|
|
|
; |
59
|
|
|
|
60
|
|
|
$result = $this->resolver->resolve($expectedPath, $expectedFilter); |
61
|
|
|
|
62
|
|
|
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testProxyCallAndRewriteReturnedUrlWithMatchReplaceOnResolve() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$expectedPath = '/foo/bar/bazz.png'; |
68
|
|
|
$expectedFilter = 'test'; |
69
|
|
|
|
70
|
|
|
$this->primaryResolver |
|
|
|
|
71
|
|
|
->expects($this->once()) |
72
|
|
|
->method('resolve') |
73
|
|
|
->with($expectedPath, $expectedFilter) |
74
|
|
|
->will($this->returnValue('https://s3-eu-west-1.amazonaws.com/s3-cache.example.com/thumbs/foo/bar/bazz.png')) |
75
|
|
|
; |
76
|
|
|
|
77
|
|
|
$this->resolver = new ProxyResolver($this->primaryResolver, array( |
78
|
|
|
'https://s3-eu-west-1.amazonaws.com/s3-cache.example.com' => 'http://images.example.com', |
79
|
|
|
)); |
80
|
|
|
|
81
|
|
|
$result = $this->resolver->resolve($expectedPath, $expectedFilter); |
82
|
|
|
|
83
|
|
|
$this->assertEquals('http://images.example.com/thumbs/foo/bar/bazz.png', $result); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
View Code Duplication |
public function testProxyCallAndRewriteReturnedUrlWithRegExpOnResolve() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$expectedPath = '/foo/bar/bazz.png'; |
89
|
|
|
$expectedFilter = 'test'; |
90
|
|
|
|
91
|
|
|
$this->primaryResolver |
|
|
|
|
92
|
|
|
->expects($this->once()) |
93
|
|
|
->method('resolve') |
94
|
|
|
->with($expectedPath, $expectedFilter) |
95
|
|
|
->will($this->returnValue('http://foo.com/thumbs/foo/bar/bazz.png')) |
96
|
|
|
; |
97
|
|
|
|
98
|
|
|
$this->resolver = new ProxyResolver($this->primaryResolver, array( |
99
|
|
|
'regexp/http:\/\/.*?\//' => 'http://bar.com/', |
100
|
|
|
)); |
101
|
|
|
|
102
|
|
|
$result = $this->resolver->resolve($expectedPath, $expectedFilter); |
103
|
|
|
|
104
|
|
|
$this->assertEquals('http://bar.com/thumbs/foo/bar/bazz.png', $result); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testProxyCallAndReturnedValueOnIsStored() |
108
|
|
|
{ |
109
|
|
|
$expectedPath = 'thePath'; |
110
|
|
|
$expectedFilter = 'theFilter'; |
111
|
|
|
|
112
|
|
|
$this->primaryResolver |
|
|
|
|
113
|
|
|
->expects($this->once()) |
114
|
|
|
->method('isStored') |
115
|
|
|
->with($expectedPath, $expectedFilter) |
116
|
|
|
->will($this->returnValue(true)) |
117
|
|
|
; |
118
|
|
|
|
119
|
|
|
$this->assertTrue($this->resolver->isStored($expectedPath, $expectedFilter)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testProxyCallOnStore() |
123
|
|
|
{ |
124
|
|
|
$expectedPath = 'thePath'; |
125
|
|
|
$expectedFilter = 'theFilter'; |
126
|
|
|
$expectedBinary = new Binary('aContent', 'image/png', 'png'); |
127
|
|
|
|
128
|
|
|
$this->primaryResolver |
|
|
|
|
129
|
|
|
->expects($this->once()) |
130
|
|
|
->method('store') |
131
|
|
|
->with($expectedBinary, $expectedPath, $expectedFilter) |
132
|
|
|
; |
133
|
|
|
|
134
|
|
|
$this->resolver->store($expectedBinary, $expectedPath, $expectedFilter); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function testProxyCallOnRemove() |
138
|
|
|
{ |
139
|
|
|
$expectedPaths = array('thePath'); |
140
|
|
|
$expectedFilters = array('theFilter'); |
141
|
|
|
|
142
|
|
|
$this->primaryResolver |
|
|
|
|
143
|
|
|
->expects($this->once()) |
144
|
|
|
->method('remove') |
145
|
|
|
->with($expectedPaths, $expectedFilters) |
146
|
|
|
; |
147
|
|
|
|
148
|
|
|
$this->resolver->remove($expectedPaths, $expectedFilters); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.