Skip to content
Open
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
include:
- php: '8.0'
- php: '8.1'
- php: '8.2'
- php: '8.3'
- php: '8.4'

runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"phpunit": "phpunit -c phpunit.xml.dist"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "9.6.34"
},
"replace": {
"onoi/http-request": "*"
Expand Down
12 changes: 6 additions & 6 deletions src/CachedCurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CachedCurlRequest extends CurlRequest {
private $cache;

/**
* @var boolean
* @var bool
*/
private $isFromCache = false;

Expand All @@ -46,7 +46,7 @@ public function __construct( CurlHandle|false $handle, Cache $cache ) {
* @deprecated since 1.3, use option ONOI_HTTP_REQUEST_RESPONSECACHE_TTL instead
* @since 1.0
*
* @param integer $expiry
* @param int $expiry
*/
public function setExpiryInSeconds( $expiry ) {
$this->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, (int)$expiry );
Expand All @@ -66,7 +66,7 @@ public function setCachePrefix( $cachePrefix ) {
* @deprecated since 1.3, use CachedCurlRequest::isFromCache instead
* @since 1.0
*
* @return boolean
* @return bool
*/
public function isCached() {
return $this->isFromCache();
Expand All @@ -75,7 +75,7 @@ public function isCached() {
/**
* @since 1.3
*
* @return boolean
* @return bool
*/
public function isFromCache() {
return $this->isFromCache;
Expand Down Expand Up @@ -133,9 +133,9 @@ private function getKeysFromOptions() {
);

// Reuse the handle but clear the options
$this->options = array();
$this->options = [];

return array( $key, $expiry );
return [ $key, $expiry ];
}

}
12 changes: 6 additions & 6 deletions src/CurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CurlRequest implements HttpRequest {
/**
* @var array
*/
protected $options = array();
protected $options = [];

/**
* @since 1.0
Expand All @@ -35,7 +35,7 @@ public function __construct( CurlHandle|false $handle ) {
/**
* @since 1.0
*
* @return boolean
* @return bool
*/
public function ping() {

Expand All @@ -46,13 +46,13 @@ public function ping() {
// Copy the handle to avoid diluting the resource
$handle = curl_copy_handle( $this->handle );

curl_setopt_array( $handle, array(
curl_setopt_array( $handle, [
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER, true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_FRESH_CONNECT => false,
CURLOPT_FAILONERROR => true
) );
] );

curl_exec( $handle );

Expand Down Expand Up @@ -115,7 +115,7 @@ public function getLastError() {
/**
* @since 1.0
*
* @return integer
* @return int
*/
public function getLastErrorCode() {
return curl_errno( $this->handle );
Expand All @@ -127,7 +127,7 @@ public function getLastErrorCode() {
* @return mixed
*/
public function execute() {
$this->options = array();
$this->options = [];
return curl_exec( $this->handle );
}

Expand Down
90 changes: 62 additions & 28 deletions tests/phpunit/Unit/CachedCurlRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ public function testExecuteForRepeatedRequest() {

$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
->disableOriginalConstructor()
->setMethods( array( 'contains', 'fetch' ) )
->setMethods( [ 'contains', 'fetch' ] )
->getMockForAbstractClass();

$cache->expects( $this->once() )
->method( 'contains' )
->with( $this->equalTo( 'foo:onoi:http:39aa03567d7d983dab1f1bdc5dc75e84' ) )
->will( $this->returnValue( true ) );
if ( version_compare( PHP_VERSION, '8.2.0', '>=' ) ) {
$cache->expects( $this->once() )
->method( 'contains' )
->with( $this->equalTo( 'foo:onoi:http:40f45decb6e5aa031fb9add61045bbb2' ) )
->will( $this->returnValue( true ) );
} else {
$cache->expects( $this->once() )
->method( 'contains' )
->with( $this->equalTo( 'foo:onoi:http:39aa03567d7d983dab1f1bdc5dc75e84' ) )
->will( $this->returnValue( true ) );
}

$cache->expects( $this->once() )
->method( 'fetch' )
Expand All @@ -69,15 +76,24 @@ public function testDifferentOptionsToGenerateDifferentKeys() {

$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
->disableOriginalConstructor()
->setMethods( array( 'contains' ) )
->setMethods( [ 'contains' ] )
->getMockForAbstractClass();

$cache->expects( $this->exactly( 2 ) )
->method( 'contains' )
->withConsecutive(
[ $this->equalTo( 'onoi:http:7ccbcfd552a597d67077d6cc037580ac' ) ],
[ $this->equalTo( 'onoi:http:e2015ad4244c4663f10f305e299d5c4f' ) ]
);
if ( version_compare( PHP_VERSION, '8.2.0', '>=' ) ) {
$cache->expects( $this->exactly( 2 ) )
->method( 'contains' )
->withConsecutive(
[ $this->equalTo( 'onoi:http:fb5f4cec184805a163be587063e6a72a' ) ],
[ $this->equalTo( 'onoi:http:e2015ad4244c4663f10f305e299d5c4f' ) ]
);
} else {
$cache->expects( $this->exactly( 2 ) )
->method( 'contains' )
->withConsecutive(
[ $this->equalTo( 'onoi:http:7ccbcfd552a597d67077d6cc037580ac' ) ],
[ $this->equalTo( 'onoi:http:e2015ad4244c4663f10f305e299d5c4f' ) ]
);
}

$instance = new CachedCurlRequest( curl_init(), $cache );

Expand All @@ -92,15 +108,24 @@ public function testToHaveTargetUrlAsPartOfTheCacheKey() {

$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
->disableOriginalConstructor()
->setMethods( array( 'contains', 'save' ) )
->setMethods( [ 'contains', 'save' ] )
->getMockForAbstractClass();

$cache->expects( $this->exactly( 2 ) )
->method( 'contains' )
->withConsecutive(
[ $this->equalTo( 'onoi:http:236b194825be3d614ce5fc1b7763a278' ) ],
[ $this->equalTo( 'onoi:http:823a603f972819c10d13f32b14460573' ) ]
);
if ( version_compare( PHP_VERSION, '8.2.0', '>=' ) ) {
$cache->expects( $this->exactly( 2 ) )
->method( 'contains' )
->withConsecutive(
[ $this->equalTo( 'onoi:http:2b9630899f52846eb4a5ecb95cec297f' ) ],
[ $this->equalTo( 'onoi:http:823a603f972819c10d13f32b14460573' ) ]
);
} else {
$cache->expects( $this->exactly( 2 ) )
->method( 'contains' )
->withConsecutive(
[ $this->equalTo( 'onoi:http:236b194825be3d614ce5fc1b7763a278' ) ],
[ $this->equalTo( 'onoi:http:823a603f972819c10d13f32b14460573' ) ]
);
}

$instance = new CachedCurlRequest( curl_init( 'http://example.org' ), $cache );

Expand All @@ -116,15 +141,24 @@ public function testSaveResponse() {

$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
->disableOriginalConstructor()
->setMethods( array( 'save', 'contains' ) )
->setMethods( [ 'save', 'contains' ] )
->getMockForAbstractClass();

$cache->expects( $this->once() )
->method( 'save' )
->with(
$this->equalTo( 'foo:onoi:http:b87e897ea862a410aff82e3122e2d955' ),
$this->anything(),
$this->equalTo( 42 ) );
if ( version_compare( PHP_VERSION, '8.2.0', '>=' ) ) {
$cache->expects( $this->once() )
->method( 'save' )
->with(
$this->equalTo( 'foo:onoi:http:77da068c8dd8aa7667ca414a86a9bddf' ),
$this->anything(),
$this->equalTo( 42 ) );
} else {
$cache->expects( $this->once() )
->method( 'save' )
->with(
$this->equalTo( 'foo:onoi:http:b87e897ea862a410aff82e3122e2d955' ),
$this->anything(),
$this->equalTo( 42 ) );
}

$instance = new CachedCurlRequest(
curl_init(),
Expand All @@ -146,10 +180,10 @@ public function testDefinedConstants() {
->disableOriginalConstructor()
->getMockForAbstractClass();

$constants = array(
$constants = [
'ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX',
'ONOI_HTTP_REQUEST_RESPONSECACHE_TTL'
);
];

$instance = new CachedCurlRequest( curl_init(), $cache );

Expand Down
Loading