From 7446ba09c8eedc125cc02b99f36d7c716d073165 Mon Sep 17 00:00:00 2001 From: Sherwin H Date: Thu, 25 Apr 2019 15:29:48 -0700 Subject: [PATCH] test: add test for deprecation warning Modify PHPUnit config in some cases to prevent warnings from interrupting tests --- tests/Imgix/Tests/UrlBuilderTest.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/Imgix/Tests/UrlBuilderTest.php b/tests/Imgix/Tests/UrlBuilderTest.php index e1ea7c6..874c212 100644 --- a/tests/Imgix/Tests/UrlBuilderTest.php +++ b/tests/Imgix/Tests/UrlBuilderTest.php @@ -2,10 +2,17 @@ use Imgix\UrlBuilder; use Imgix\ShardStrategy; +use PHPUnit\Framework\Error\Warning; class UrlBuilderTest extends \PHPUnit\Framework\TestCase { public function testURLBuilderRaisesExceptionOnNoDomains() { + // Because PHP treats warnings the same as exceptions, + // they will be temporarily disabled in certain cases + // to prevent interruption of testing + $warningEnabled = PHPUnit\Framework\Error\Warning::$enabled; + PHPUnit\Framework\Error\Warning::$enabled = FALSE; + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage("UrlBuilder requires at least one domain"); $domains = array(); @@ -15,6 +22,11 @@ public function testURLBuilderRaisesExceptionOnNoDomains() { public function testUrlBuilderCycleShard() { // generate a url for the number of domains in use ensure they're cycled through... + // Because PHP treats warnings the same as exceptions, + // they will be temporarily disabled in certain cases + // to prevent interruption of testing + $warningEnabled = PHPUnit\Framework\Error\Warning::$enabled; + PHPUnit\Framework\Error\Warning::$enabled = FALSE; $domains = array("jackangers.imgix.net", "jackangers2.imgix.net", "jackangers3.imgix.net"); $ub = new URLBuilder($domains, false, "", ShardStrategy::CRC, false); @@ -29,6 +41,8 @@ public function testUrlBuilderCycleShard() { $used[] = $curDomain; } } + // Re-enable original warning behavior + PHPUnit\Framework\Error\Warning::$enabled = TRUE; } public function testExamplePlain() { @@ -156,6 +170,12 @@ public function test_invalid_domain_append_dash() { $builder = new UrlBuilder("demos.imgix.net-", true, "", ShardStrategy::CRC, false); } public function test_invalid_domain_array() { + // Because PHP treats warnings the same as exceptions, + // they will be temporarily disabled in certain cases + // to prevent interruption of testing + $warningEnabled = PHPUnit\Framework\Error\Warning::$enabled; + PHPUnit\Framework\Error\Warning::$enabled = FALSE; + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Domains must be passed in as fully-qualified ' . 'domain names and should not include a protocol or any path element, i.e. ' . @@ -163,5 +183,11 @@ public function test_invalid_domain_array() { $builder = new UrlBuilder(array("demos.imgix.net","demos.imgix.net-"), true, "", ShardStrategy::CYCLE, false); } + public function test_deprecation_warning(){ + PHPUnit\Framework\Error\Warning::$enabled = TRUE; + $this->expectException(Warning::class); + $this->expectExceptionMessage('Domain sharding has been deprecated and will be removed in the next major version.'); + $builder = new UrlBuilder(array("demos.imgix.net","demos.imgix.net"), true, "", ShardStrategy::CYCLE, false); + } } ?>