Skip to content

Commit

Permalink
Merge pull request #173 from ably/php82
Browse files Browse the repository at this point in the history
Improve PHP 8.2 compatibility and testing
  • Loading branch information
AndyTWF authored Feb 22, 2023
2 parents c272809 + 46fdc0b commit 0824bb6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: [7.2, 7.3, 7.4, 8.0, 8.1]
php-version: [7.2, 7.3, 7.4, 8.0, 8.1, 8.2]
protocol: [ 'json', 'msgpack' ]
ignorePlatformReq: [ '' ]
include:
- php-version: 8.2
ignorePlatformReq: '--ignore-platform-req=php+'

steps:
- uses: actions/checkout@v2
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
"Ably\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"tests\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
Expand Down
10 changes: 6 additions & 4 deletions tests/AblyRestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

class AblyRestTest extends \PHPUnit\Framework\TestCase {

use AssertsRegularExpressions;

protected static $testApp;
protected static $defaultOptions;

Expand Down Expand Up @@ -84,7 +86,7 @@ public function testInitLibWithSpecifiedHost() {
];
$ably = new AblyRest( $opts );
$ably->time(); // make a request
$this->assertRegExp( '/^https?:\/\/some\.other\.host/', $ably->http->lastUrl, 'Unexpected host mismatch' );
$this->assertMatchesRegularExpression( '/^https?:\/\/some\.other\.host/', $ably->http->lastUrl, 'Unexpected host mismatch' );
}

/**
Expand Down Expand Up @@ -144,7 +146,7 @@ public function testTLSDefaultIsTrue() {
];
$ably = new AblyRest( $opts );
$ably->time(); // make a request
$this->assertRegExp( '/^https:\/\/rest\.ably\.io/', $ably->http->lastUrl, 'Unexpected scheme/url mismatch' );
$this->assertMatchesRegularExpression( '/^https:\/\/rest\.ably\.io/', $ably->http->lastUrl, 'Unexpected scheme/url mismatch' );
}

/**
Expand All @@ -158,7 +160,7 @@ public function testTLSCanBeFalse() {
];
$ably = new AblyRest( $opts );
$ably->time(); // make a request
$this->assertRegExp( '/^http:\/\/rest\.ably\.io/', $ably->http->lastUrl, 'Unexpected scheme/url mismatch' );
$this->assertMatchesRegularExpression( '/^http:\/\/rest\.ably\.io/', $ably->http->lastUrl, 'Unexpected scheme/url mismatch' );
}

/**
Expand All @@ -172,7 +174,7 @@ public function testTLSExplicitTrue() {
];
$ably = new AblyRest( $opts );
$ably->time(); // make a request
$this->assertRegExp( '/^https:\/\/rest\.ably\.io/', $ably->http->lastUrl, 'Unexpected scheme/url mismatch' );
$this->assertMatchesRegularExpression( '/^https:\/\/rest\.ably\.io/', $ably->http->lastUrl, 'Unexpected scheme/url mismatch' );
}


Expand Down
25 changes: 25 additions & 0 deletions tests/AssertsRegularExpressions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace tests;

/**
* PHPUnit has deprecated the old function `assertRegExp` for asserting regular
* expression matches. However, the versions of PHPUnit compatible with PHP 7.2 do
* not have the new version.
*
* This trait adds a version of the method that calls the old version for PHP 7.2
* or the new version for newer versions.
*
* Can be removed once PHP 7.2 is dropped from CI.
*/
trait AssertsRegularExpressions
{
public static function assertMatchesRegularExpression(string $pattern, string $string, string $message = ''): void
{
if (version_compare(phpversion(), '7.3.0', '<')) {
self::assertRegExp($pattern, $string, $message);
return;
}

parent::assertMatchesRegularExpression($pattern, $string, $message);
}
}
15 changes: 10 additions & 5 deletions tests/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
use Ably\Models\TokenParams;
use Ably\Models\TokenRequest;
use Ably\Utils\Miscellaneous;
use tests\AssertsRegularExpressions;

require_once __DIR__ . '/factories/TestApp.php';

class AuthTest extends \PHPUnit\Framework\TestCase {

use AssertsRegularExpressions;

protected static $testApp;
protected static $defaultOptions;

Expand Down Expand Up @@ -304,9 +307,11 @@ public function testCreateTokenRequestValidity() {
$ably->auth->authorize();

$this->assertFalse( $ably->auth->isUsingBasicAuth(), 'Expected token auth to be used' );
$this->assertGreaterThanOrEqual( $timestamp, $ably->auth->getTokenDetails()->issued,
'Expected token issued timestamp to be greater than or equal to the time of the request' );

$this->assertLessThan(
200,
abs($timestamp - $ably->auth->getTokenDetails()->issued),
'Expected token issued timestamp to be near to the time of request (allowing for clock skew)'
);
$ably->stats(); // requires valid token, throws exception if invalid
}

Expand Down Expand Up @@ -543,7 +548,7 @@ public function testHTTPHeadersKey() {

$ably->get("/dummy_test");

$this->assertRegExp('/Authorization\s*:\s*Basic\s+'.base64_encode($fakeKey).'/i', $ably->http->headers[0]);
$this->assertMatchesRegularExpression('/Authorization\s*:\s*Basic\s+'.base64_encode($fakeKey).'/i', $ably->http->headers[0]);
}

/**
Expand All @@ -558,7 +563,7 @@ public function testHTTPHeadersToken() {

$ably->get("/dummy_test");

$this->assertRegExp('/Authorization\s*:\s*Bearer\s+'.base64_encode($fakeToken).'/i', $ably->http->headers[0]);
$this->assertMatchesRegularExpression('/Authorization\s*:\s*Bearer\s+'.base64_encode($fakeToken).'/i', $ably->http->headers[0]);
}
}

Expand Down

0 comments on commit 0824bb6

Please sign in to comment.