1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\HttpRequest\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\HttpRequest\CachedCurlRequest; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\HttpRequest\CachedCurlRequest |
9
|
|
|
* @group onoi-http-request |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.0 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class CachedCurlRequestTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
19
|
|
|
|
20
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
21
|
|
|
->disableOriginalConstructor() |
22
|
|
|
->getMockForAbstractClass(); |
23
|
|
|
|
24
|
|
|
$instance = new CachedCurlRequest( curl_init(), $cache ); |
25
|
|
|
|
26
|
|
|
$this->assertInstanceOf( |
27
|
|
|
'\Onoi\HttpRequest\CurlRequest', |
28
|
|
|
$instance |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf( |
32
|
|
|
'\Onoi\HttpRequest\CachedCurlRequest', |
33
|
|
|
$instance |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testExecuteForRepeatedRequest() { |
38
|
|
|
|
39
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
40
|
|
|
->disableOriginalConstructor() |
41
|
|
|
->setMethods( array( 'contains', 'fetch' ) ) |
42
|
|
|
->getMockForAbstractClass(); |
43
|
|
|
|
44
|
|
|
$cache->expects( $this->once() ) |
45
|
|
|
->method( 'contains' ) |
46
|
|
|
->with( $this->equalTo( 'foo:onoi:http:39aa03567d7d983dab1f1bdc5dc75e84' ) ) |
47
|
|
|
->will( $this->returnValue( true ) ); |
48
|
|
|
|
49
|
|
|
$cache->expects( $this->once() ) |
50
|
|
|
->method( 'fetch' ) |
51
|
|
|
->will( $this->returnValue( 22 ) ); |
52
|
|
|
|
53
|
|
|
$instance = new CachedCurlRequest( curl_init(), $cache ); |
54
|
|
|
|
55
|
|
|
$instance->setCachePrefix( 'foo:' ); |
|
|
|
|
56
|
|
|
$instance->setOption( CURLOPT_RETURNTRANSFER, true ); |
57
|
|
|
|
58
|
|
|
$this->assertEquals( |
59
|
|
|
22, |
60
|
|
|
$instance->execute() |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$this->assertTrue( |
64
|
|
|
$instance->isCached() |
|
|
|
|
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testDifferentOptionsToGenerateDifferentKeys() { |
69
|
|
|
|
70
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
71
|
|
|
->disableOriginalConstructor() |
72
|
|
|
->setMethods( array( 'contains' ) ) |
73
|
|
|
->getMockForAbstractClass(); |
74
|
|
|
|
75
|
|
|
$cache->expects( $this->at( 0 ) ) |
76
|
|
|
->method( 'contains' ) |
77
|
|
|
->with( $this->equalTo( 'onoi:http:7ccbcfd552a597d67077d6cc037580ac' ) ); |
78
|
|
|
|
79
|
|
|
$cache->expects( $this->at( 1 ) ) |
80
|
|
|
->method( 'contains' ) |
81
|
|
|
->with( $this->equalTo( 'onoi:http:e2015ad4244c4663f10f305e299d5c4f' ) ); |
82
|
|
|
|
83
|
|
|
$instance = new CachedCurlRequest( curl_init(), $cache ); |
84
|
|
|
|
85
|
|
|
$instance->setOption( CURLOPT_RETURNTRANSFER, true ); |
86
|
|
|
$instance->execute(); |
87
|
|
|
|
88
|
|
|
$instance->setOption( CURLOPT_RETURNTRANSFER, false ); |
89
|
|
|
$instance->execute(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testToHaveTargetUrlAsPartOfTheCacheKey() { |
93
|
|
|
|
94
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
95
|
|
|
->disableOriginalConstructor() |
96
|
|
|
->setMethods( array( 'contains', 'save' ) ) |
97
|
|
|
->getMockForAbstractClass(); |
98
|
|
|
|
99
|
|
|
$cache->expects( $this->at( 0 ) ) |
100
|
|
|
->method( 'contains' ) |
101
|
|
|
->with( $this->equalTo( 'onoi:http:236b194825be3d614ce5fc1b7763a278' ) ); |
102
|
|
|
|
103
|
|
|
$cache->expects( $this->at( 2 ) ) |
104
|
|
|
->method( 'contains' ) |
105
|
|
|
->with( $this->equalTo( 'onoi:http:823a603f972819c10d13f32b14460573' ) ); |
106
|
|
|
|
107
|
|
|
$instance = new CachedCurlRequest( curl_init( 'http://example.org' ), $cache ); |
108
|
|
|
|
109
|
|
|
$instance->setOption( CURLOPT_RETURNTRANSFER, true ); |
110
|
|
|
$instance->execute(); |
111
|
|
|
|
112
|
|
|
$instance->setOption( CURLOPT_RETURNTRANSFER, true ); |
113
|
|
|
$instance->setOption( CURLOPT_URL, 'http://example.org' ); |
114
|
|
|
$instance->execute(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testSaveResponse() { |
118
|
|
|
|
119
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
120
|
|
|
->disableOriginalConstructor() |
121
|
|
|
->setMethods( array( 'save', 'contains' ) ) |
122
|
|
|
->getMockForAbstractClass(); |
123
|
|
|
|
124
|
|
|
$cache->expects( $this->once() ) |
125
|
|
|
->method( 'save' ) |
126
|
|
|
->with( |
127
|
|
|
$this->equalTo( 'foo:onoi:http:b87e897ea862a410aff82e3122e2d955' ), |
128
|
|
|
$this->anything(), |
129
|
|
|
$this->equalTo( 42 ) ); |
130
|
|
|
|
131
|
|
|
$instance = new CachedCurlRequest( |
132
|
|
|
curl_init(), |
133
|
|
|
$cache |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, 42 ); |
137
|
|
|
$instance->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, 'foo:' ); |
138
|
|
|
|
139
|
|
|
$instance->setOption( CURLOPT_URL, 'http://example.org' ); |
140
|
|
|
$instance->setOption( CURLOPT_RETURNTRANSFER, true ); |
141
|
|
|
|
142
|
|
|
$instance->execute(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function testDefinedConstants() { |
146
|
|
|
|
147
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
148
|
|
|
->disableOriginalConstructor() |
149
|
|
|
->getMockForAbstractClass(); |
150
|
|
|
|
151
|
|
|
$constants = array( |
152
|
|
|
'ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX', |
153
|
|
|
'ONOI_HTTP_REQUEST_RESPONSECACHE_TTL' |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$instance = new CachedCurlRequest( curl_init(), $cache ); |
|
|
|
|
157
|
|
|
|
158
|
|
|
foreach ( $constants as $constant ) { |
159
|
|
|
$this->assertTrue( defined( $constant ) ); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testDeprecatedFunctions() { |
164
|
|
|
|
165
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
166
|
|
|
->disableOriginalConstructor() |
167
|
|
|
->getMockForAbstractClass(); |
168
|
|
|
|
169
|
|
|
$instance = new CachedCurlRequest( curl_init(), $cache ); |
170
|
|
|
|
171
|
|
|
$instance->setExpiryInSeconds( 42 ); |
|
|
|
|
172
|
|
|
$instance->setCachePrefix( 'Foo' ); |
|
|
|
|
173
|
|
|
|
174
|
|
|
$this->assertEquals( |
175
|
|
|
42, |
176
|
|
|
$instance->getOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL ) |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$this->assertEquals( |
180
|
|
|
'Foo', |
181
|
|
|
$instance->getOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX ) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.