From de457eaf8fbfa05f2f611d546d7357c6fdec86f8 Mon Sep 17 00:00:00 2001 From: Yenfry Herrera Feliz Date: Mon, 18 Dec 2023 12:03:51 -0500 Subject: [PATCH] chore: changes to meet php>=7.2.5 requirements (#66) Since we are deprecating php<7.2.5 some code changes are done here, due to that a bump for some dependencies is also done in this commit. One example is phpunit which requires that test units extends from PHPUnit\Framework\TestCas and no from \PHPUnit_Framework_TestCase. Another example is that the annotations @expectedException, @expectedExceptionMessage, @expectedExceptionMessageRegExp, among others, are deprecated in phpunit 9, and instead the code had to be refactored to use the recommended function for each annotation, such as instead of @expectedException we need to use expectException(exceptionClassName), etc. --- composer.json | 10 +++-- tests/FunctionalValidationsTest.php | 4 +- tests/MessageTest.php | 33 +++++--------- tests/MessageValidatorTest.php | 69 ++++++++++++----------------- 4 files changed, 48 insertions(+), 68 deletions(-) diff --git a/composer.json b/composer.json index ad5e86e..352360e 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,16 @@ "issues": "https://github.com/aws/aws-sns-message-validator/issues" }, "require": { - "php": ">=5.4", + "php": ">=7.2.5", "ext-openssl": "*", - "psr/http-message": "^1.0" + "psr/http-message": "^1.0 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^4.0", + "phpunit/phpunit": "^5.6.3 || ^8.5 || ^9.5", "squizlabs/php_codesniffer": "^2.3", - "guzzlehttp/psr7": "^1.4" + "guzzlehttp/psr7": "^1.9.1 || ^2.4.5", + "yoast/phpunit-polyfills": "^1.0" + }, "autoload": { "psr-4": { "Aws\\Sns\\": "src/" } diff --git a/tests/FunctionalValidationsTest.php b/tests/FunctionalValidationsTest.php index ce43f4b..96e680a 100644 --- a/tests/FunctionalValidationsTest.php +++ b/tests/FunctionalValidationsTest.php @@ -2,11 +2,13 @@ namespace Aws\Sns; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; + /** * @covers Aws\Sns\MessageValidator * @covers Aws\Sns\Message */ -class FunctionalValidationsTest extends \PHPUnit_Framework_TestCase +class FunctionalValidationsTest extends TestCase { private static $certificate = '-----BEGIN CERTIFICATE----- diff --git a/tests/MessageTest.php b/tests/MessageTest.php index c0d1486..91ed755 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -2,11 +2,12 @@ namespace Aws\Sns; use GuzzleHttp\Psr7\Request; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; /** * @covers \Aws\Sns\Message */ -class MessageTest extends \PHPUnit_Framework_TestCase +class MessageTest extends TestCase { public $messageData = array( 'Message' => 'a', @@ -25,7 +26,7 @@ class MessageTest extends \PHPUnit_Framework_TestCase public function testGetters() { $message = new Message($this->messageData); - $this->assertInternalType('array', $message->toArray()); + $this->assertIsArray($message->toArray()); foreach ($this->messageData as $key => $expectedValue) { $this->assertTrue(isset($message[$key])); @@ -65,29 +66,23 @@ public function messageTypeProvider() ]; } - /** - * @expectedException \InvalidArgumentException - */ public function testConstructorFailsWithNoType() { + $this->expectException(\InvalidArgumentException::class); $data = $this->messageData; unset($data['Type']); new Message($data); } - /** - * @expectedException \InvalidArgumentException - */ public function testConstructorFailsWithMissingData() { + $this->expectException(\InvalidArgumentException::class); new Message(['Type' => 'Notification']); } - /** - * @expectedException \InvalidArgumentException - */ public function testRequiresTokenAndSubscribeUrlForSubscribeMessage() { + $this->expectException(\InvalidArgumentException::class); new Message( ['Type' => 'SubscriptionConfirmation'] + array_diff_key( $this->messageData, @@ -96,11 +91,9 @@ public function testRequiresTokenAndSubscribeUrlForSubscribeMessage() ); } - /** - * @expectedException \InvalidArgumentException - */ public function testRequiresTokenAndSubscribeUrlForUnsubscribeMessage() { + $this->expectException(\InvalidArgumentException::class); new Message( ['Type' => 'UnsubscribeConfirmation'] + array_diff_key( $this->messageData, @@ -125,19 +118,15 @@ public function testCanCreateFromRawPost() unset($_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE']); } - /** - * @expectedException \RuntimeException - */ public function testCreateFromRawPostFailsWithMissingHeader() { + $this->expectException(\RuntimeException::class); Message::fromRawPostData(); } - /** - * @expectedException \RuntimeException - */ public function testCreateFromRawPostFailsWithMissingData() { + $this->expectException(\RuntimeException::class); $_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE'] = 'Notification'; Message::fromRawPostData(); unset($_SERVER['HTTP_X_AMZ_SNS_MESSAGE_TYPE']); @@ -155,11 +144,9 @@ public function testCanCreateFromPsr7Request() $this->assertInstanceOf('Aws\Sns\Message', $message); } - /** - * @expectedException \RuntimeException - */ public function testCreateFromPsr7RequestFailsWithMissingData() { + $this->expectException(\RuntimeException::class); $request = new Request( 'POST', '/', diff --git a/tests/MessageValidatorTest.php b/tests/MessageValidatorTest.php index 06aaa40..75501b1 100644 --- a/tests/MessageValidatorTest.php +++ b/tests/MessageValidatorTest.php @@ -1,17 +1,20 @@ assertFalse($validator->isValid($message)); } - /** - * @expectedException \Aws\Sns\Exception\InvalidSnsMessageException - * @expectedExceptionMessage The SignatureVersion "3" is not supported. - */ public function testValidateFailsWhenSignatureVersionIsInvalid() { + $this->expectException(InvalidSnsMessageException::class); + $this->expectExceptionMessage('The SignatureVersion "3" is not supported.'); $validator = new MessageValidator($this->getMockCertServerClient()); $message = $this->getTestMessage([ 'SignatureVersion' => '3', @@ -47,12 +48,10 @@ public function testValidateFailsWhenSignatureVersionIsInvalid() $validator->validate($message); } - /** - * @expectedException \Aws\Sns\Exception\InvalidSnsMessageException - * @expectedExceptionMessage The certificate is located on an invalid domain. - */ public function testValidateFailsWhenCertUrlInvalid() { + $this->expectException(InvalidSnsMessageException::class); + $this->expectExceptionMessage('The certificate is located on an invalid domain.'); $validator = new MessageValidator(); $message = $this->getTestMessage([ 'SigningCertURL' => 'https://foo.amazonaws.com/bar.pem', @@ -60,12 +59,10 @@ public function testValidateFailsWhenCertUrlInvalid() $validator->validate($message); } - /** - * @expectedException \Aws\Sns\Exception\InvalidSnsMessageException - * @expectedExceptionMessage The certificate is located on an invalid domain. - */ public function testValidateFailsWhenCertUrlNotAPemFile() { + $this->expectException(InvalidSnsMessageException::class); + $this->expectExceptionMessage('The certificate is located on an invalid domain.'); $validator = new MessageValidator(); $message = $this->getTestMessage([ 'SigningCertURL' => 'https://foo.amazonaws.com/bar', @@ -88,34 +85,28 @@ function () { $this->assertTrue($validator->isValid($message)); } - /** - * @expectedException \Aws\Sns\Exception\InvalidSnsMessageException - * @expectedExceptionMessageRegExp /Cannot get the certificate from ".+"./ - */ public function testValidateFailsWhenCannotGetCertificate() { + $this->expectException(InvalidSnsMessageException::class); + $this->expectDeprecationMessageMatches('/Cannot get the certificate from ".+"./'); $validator = new MessageValidator($this->getMockHttpClient(false)); $message = $this->getTestMessage(); $validator->validate($message); } - /** - * @expectedException \Aws\Sns\Exception\InvalidSnsMessageException - * @expectedExceptionMessage Cannot get the public key from the certificate. - */ public function testValidateFailsWhenCannotDeterminePublicKey() { + $this->expectException(InvalidSnsMessageException::class); + $this->expectExceptionMessage('Cannot get the public key from the certificate.'); $validator = new MessageValidator($this->getMockHttpClient()); $message = $this->getTestMessage(); $validator->validate($message); } - /** - * @expectedException \Aws\Sns\Exception\InvalidSnsMessageException - * @expectedExceptionMessage The message signature is invalid. - */ public function testValidateFailsWhenMessageIsInvalid() { + $this->expectException(InvalidSnsMessageException::class); + $this->expectExceptionMessage('The message signature is invalid.'); $validator = new MessageValidator($this->getMockCertServerClient()); $message = $this->getTestMessage([ 'Signature' => $this->getSignature('foo'), @@ -123,20 +114,18 @@ public function testValidateFailsWhenMessageIsInvalid() $validator->validate($message); } - /** - * @expectedException \Aws\Sns\Exception\InvalidSnsMessageException - * @expectedExceptionMessage The message signature is invalid. - */ - public function testValidateFailsWhenSha256MessageIsInvalid() - { - $validator = new MessageValidator($this->getMockCertServerClient()); - $message = $this->getTestMessage([ - 'Signature' => $this->getSignature('foo'), - 'SignatureVersion' => '2' + public function testValidateFailsWhenSha256MessageIsInvalid() + { + $this->expectException(InvalidSnsMessageException::class); + $this->expectExceptionMessage('The message signature is invalid.'); + $validator = new MessageValidator($this->getMockCertServerClient()); + $message = $this->getTestMessage([ + 'Signature' => $this->getSignature('foo'), + 'SignatureVersion' => '2' - ]); - $validator->validate($message); - } + ]); + $validator->validate($message); + } public function testValidateSucceedsWhenMessageIsValid() {