Skip to content

Commit

Permalink
test: add test for deprecation warning
Browse files Browse the repository at this point in the history
Modify PHPUnit config in some cases to prevent warnings from interrupting tests
  • Loading branch information
Sherwin H committed Apr 25, 2019
1 parent 4439e8d commit 7446ba0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/Imgix/Tests/UrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -29,6 +41,8 @@ public function testUrlBuilderCycleShard() {
$used[] = $curDomain;
}
}
// Re-enable original warning behavior
PHPUnit\Framework\Error\Warning::$enabled = TRUE;
}

public function testExamplePlain() {
Expand Down Expand Up @@ -156,12 +170,24 @@ 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. ' .
'"example.imgix.net".');

$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);
}
}
?>

0 comments on commit 7446ba0

Please sign in to comment.