Skip to content

Commit

Permalink
chore: changes to meet php>=7.2.5 requirements (#66)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yenfryherrerafeliz committed Dec 18, 2023
1 parent ea20e69 commit de457ea
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 68 deletions.
10 changes: 6 additions & 4 deletions composer.json
Expand Up @@ -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/" }
Expand Down
4 changes: 3 additions & 1 deletion tests/FunctionalValidationsTest.php
Expand Up @@ -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-----
Expand Down
33 changes: 10 additions & 23 deletions tests/MessageTest.php
Expand Up @@ -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',
Expand All @@ -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]));
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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']);
Expand All @@ -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',
'/',
Expand Down
69 changes: 29 additions & 40 deletions tests/MessageValidatorTest.php
@@ -1,17 +1,20 @@
<?php
namespace Aws\Sns;

use Aws\Sns\Exception\InvalidSnsMessageException;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
* @covers Aws\Sns\MessageValidator
*/
class MessageValidatorTest extends \PHPUnit_Framework_TestCase
class MessageValidatorTest extends TestCase
{
const VALID_CERT_URL = 'https://sns.foo.amazonaws.com/bar.pem';

private static $pKey;
private static $certificate;

public static function setUpBeforeClass()
public static function set_up_before_class()
{
self::$pKey = openssl_pkey_new();
$csr = openssl_csr_new([], self::$pKey);
Expand All @@ -20,7 +23,7 @@ public static function setUpBeforeClass()
openssl_x509_free($x509);
}

public static function tearDownAfterClass()
public static function tear_down_after_class()
{
openssl_pkey_free(self::$pKey);
}
Expand All @@ -34,38 +37,32 @@ public function testIsValidReturnsFalseOnFailedValidation()
$this->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',
]);
$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',
]);
$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',
Expand All @@ -88,55 +85,47 @@ 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'),
]);
$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()
{
Expand Down

0 comments on commit de457ea

Please sign in to comment.