2020-05-22 14:15:19 -07:00
< ? php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2023-05-12 05:28:46 -07:00
2020-05-22 14:15:19 -07:00
/*
Relies on PHPUnit to test the functionality in ./UpdateDistributionS3.php.
Related custom constants are defined in ./phpunit.xml.
Example PHPUnit run command from this file's parent directory:
./vendor/bin/phpunit --testsuite cloudfront-updatedistribution
*/
2023-05-12 05:28:46 -07:00
namespace Cloudfront ;
use Aws\CloudFront\CloudFrontClient ;
2020-05-22 14:15:19 -07:00
use Aws\MockHandler ;
use Aws\Result ;
2023-05-12 05:28:46 -07:00
use PHPUnit\Framework\TestCase ;
2020-05-22 14:15:19 -07:00
class UpdateDistributionTest extends TestCase
{
public static function setUpBeforeClass () : void
{
2023-05-12 05:28:46 -07:00
require ( __DIR__ . '/../UpdateDistributionS3.php' );
2020-05-22 14:15:19 -07:00
}
protected function setUp () : void
{
global $cloudFrontClient ;
global $distributionId ;
global $eTag ;
global $distributionConfig ;
$distributionId = CLOUDFRONT_DISTRIBUTION_ID ;
$eTag = CLOUDFRONT_DISTRIBUTION_ETAG ;
2023-05-12 05:28:46 -07:00
2020-05-22 14:15:19 -07:00
$distributionConfig = [
'CacheBehaviors' => [
2023-05-12 05:28:46 -07:00
'Quantity' => 0
2020-05-22 14:15:19 -07:00
],
'CallerReference' => 'my-' ,
'Comment' => '' ,
'DefaultCacheBehavior' => [
'TargetOriginId' => 'my-target-origin-id' ,
'ForwardedValues' => [
'Cookies' => [
'Forward' => 'none'
],
'Headers' => [
'Quantity' => 0
],
'QueryString' => false ,
'QueryStringCacheKeys' => [
'Quantity' => 0
]
],
'ViewerProtocolPolicy' => 'allow-all' ,
'MinTTL' => '0' ,
'AllowedMethods' => [
'CachedMethods' => [
'Items' => [ 'HEAD' , 'GET' ],
'Quantity' => 2
],
'Items' => [ 'HEAD' , 'GET' ],
'Quantity' => 2
],
'SmoothStreaming' => false ,
'DefaultTTL' => '86400' ,
'MaxTTL' => '31536000' ,
'Compress' => false ,
'LambdaFunctionAssociations' => [
'Quantity' => 0
],
'FieldLevelEncryptionId' => '' ,
'TrustedSigners' => [
'Enabled' => false ,
'Quantity' => 0
]
],
'DefaultRootObject' => '' ,
'Enabled' => false ,
'Origins' => [
'Items' => [
[
'DomainName' => 'my-bucket-name.s3.amazonaws.com' ,
'Id' => 'my-unique-origin-name' ,
'OriginPath' => '' ,
'CustomHeaders' => [
'Quantity' => 0
],
'S3OriginConfig' => [
'OriginAccessIdentity' => ''
]
]
],
'Quantity' => 1
],
2023-05-12 05:28:46 -07:00
'Aliases' => [
2020-05-22 14:15:19 -07:00
'Quantity' => 0
],
'CustomErrorResponses' => [
'Quantity' => 0
],
'HttpVersion' => 'http2' ,
'Logging' => [
'Enabled' => false ,
'IncludeCookies' => false ,
'Bucket' => '' ,
'Prefix' => ''
],
'PriceClass' => 'PriceClass_All' ,
'Restrictions' => [
'GeoRestriction' => [
'RestrictionType' => 'none' ,
'Quantity' => 0
]
],
'ViewerCertificate' => [
'CloudFrontDefaultCertificate' => true ,
'MinimumProtocolVersion' => 'TLSv1' ,
'CertificateSource' => 'cloudfront' ,
'WebACLId' => ''
]
];
$mock = new MockHandler ();
$mock -> append ( new Result ( array ( true )));
$cloudFrontClient = new CloudFrontClient ([
'profile' => AWS_PROFILE ,
'version' => CLOUDFRONT_VERSION ,
'region' => AWS_REGION ,
'handler' => $mock
]);
}
protected function tearDown () : void
{
unset ( $GLOBALS [ 'cloudFrontClient' ]);
}
public function testUpdatesTheDistribution ()
{
$result = updateDistribution (
$GLOBALS [ 'cloudFrontClient' ],
$GLOBALS [ 'distributionId' ],
$GLOBALS [ 'distributionConfig' ],
$GLOBALS [ 'eTag' ]
);
$this -> assertStringContainsString (
2023-05-12 05:28:46 -07:00
'https://cloudfront.amazonaws.com/' .
CLOUDFRONT_VERSION . '/distribution/' .
2020-05-22 14:15:19 -07:00
CLOUDFRONT_DISTRIBUTION_ID . '/config' ,
$result
);
}
public function testGetsADistributionConfig ()
{
$result = getDistributionConfig (
2023-05-12 05:28:46 -07:00
$GLOBALS [ 'cloudFrontClient' ],
2020-05-22 14:15:19 -07:00
$GLOBALS [ 'distributionId' ]
);
$this -> assertContains ( 'https://cloudfront.amazonaws.com/' .
2023-05-12 05:28:46 -07:00
CLOUDFRONT_VERSION . '/distribution/' .
CLOUDFRONT_DISTRIBUTION_ID , $result );
2020-05-22 14:15:19 -07:00
}
public function testGetsADistributionETag ()
{
$result = getDistributionETag (
2023-05-12 05:28:46 -07:00
$GLOBALS [ 'cloudFrontClient' ],
2020-05-22 14:15:19 -07:00
$GLOBALS [ 'distributionId' ]
);
$this -> assertContains ( 'https://cloudfront.amazonaws.com/' .
2023-05-12 05:28:46 -07:00
CLOUDFRONT_VERSION . '/distribution/' .
CLOUDFRONT_DISTRIBUTION_ID , $result );
2020-05-22 14:15:19 -07:00
}
2023-05-12 05:28:46 -07:00
}