From f04ba956a97b7fb984b109ce3281bc26c1f55920 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 21 May 2017 23:26:36 -0400 Subject: [PATCH] Tweaks to Feed tests - Ensure Web server is up before running tests (skip otherwise) - Place expected timestamps closer to assertions, to lessen chances of off-by-one failures --- tests/Feed/TestFeed.php | 29 +++++++++++++++----- tests/docroot/Feed/NextFetch/NotModified.php | 2 +- tests/docroot/IsUp.php | 4 +++ 3 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 tests/docroot/IsUp.php diff --git a/tests/Feed/TestFeed.php b/tests/Feed/TestFeed.php index 0ae58d52..5e4d1f6c 100644 --- a/tests/Feed/TestFeed.php +++ b/tests/Feed/TestFeed.php @@ -7,13 +7,23 @@ Use Phake; class TestFeed extends \PHPUnit\Framework\TestCase { use Test\Tools; - protected $base = "http://localhost:8000/Feed/"; - + protected static $host = "http://localhost:8000/"; + protected static $serverUp = true; + protected $base = ""; + function time(string $t): string { return gmdate("D, d M Y H:i:s \G\M\T", strtotime($t)); } + static function setUpBeforeClass() { + if(!@file_get_contents(self::$host."IsUp")) self::$serverUp = false; + } + function setUp() { + if(!self::$serverUp) { + $this->markTestSkipped("Test Web server is not accepting requests"); + } + $this->base = self::$host."Feed/"; $this->clearData(); Data::$conf = new Conf(); } @@ -32,44 +42,49 @@ class TestFeed extends \PHPUnit\Framework\TestCase { function testComputeNextFetchFrom304() { // if less than half an hour, check in 15 minutes - $exp = strtotime("now + 15 minutes"); $t = strtotime("now"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 15 minutes"); $this->assertTime($exp, $f->nextFetch); $t = strtotime("now - 29 minutes"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 15 minutes"); $this->assertTime($exp, $f->nextFetch); // if less than an hour, check in 30 minutes - $exp = strtotime("now + 30 minutes"); $t = strtotime("now - 30 minutes"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 30 minutes"); $this->assertTime($exp, $f->nextFetch); $t = strtotime("now - 59 minutes"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 30 minutes"); $this->assertTime($exp, $f->nextFetch); // if less than three hours, check in an hour - $exp = strtotime("now + 1 hour"); $t = strtotime("now - 1 hour"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 1 hour"); $this->assertTime($exp, $f->nextFetch); $t = strtotime("now - 2 hours 59 minutes"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 1 hour"); $this->assertTime($exp, $f->nextFetch); // if more than 36 hours, check in 24 hours - $exp = strtotime("now + 1 day"); $t = strtotime("now - 36 hours"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 1 day"); $this->assertTime($exp, $f->nextFetch); $t = strtotime("now - 2 years"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 1 day"); $this->assertTime($exp, $f->nextFetch); // otherwise check in three hours - $exp = strtotime("now + 3 hours"); $t = strtotime("now - 3 hours"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 3 hours"); $this->assertTime($exp, $f->nextFetch); $t = strtotime("now - 35 hours"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); + $exp = strtotime("now + 3 hours"); $this->assertTime($exp, $f->nextFetch); } } \ No newline at end of file diff --git a/tests/docroot/Feed/NextFetch/NotModified.php b/tests/docroot/Feed/NextFetch/NotModified.php index 6a33b462..71dcb849 100644 --- a/tests/docroot/Feed/NextFetch/NotModified.php +++ b/tests/docroot/Feed/NextFetch/NotModified.php @@ -7,6 +7,6 @@ if(array_key_exists("t", $_GET)) { } else { return [ 'code' => 304, - 'cache' => fasel, + 'cache' => false, ]; } \ No newline at end of file diff --git a/tests/docroot/IsUp.php b/tests/docroot/IsUp.php new file mode 100644 index 00000000..83fbe21c --- /dev/null +++ b/tests/docroot/IsUp.php @@ -0,0 +1,4 @@ + 200, + 'content' => "I'm up!", +]; \ No newline at end of file