From 97a8f473a79bc7d9573952e001ff5d0692651f32 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sun, 21 May 2017 19:51:03 -0400 Subject: [PATCH] More Feed tests; Linux fixes - Adjusted some namespace case - Included Linux test runner; should also work with macOS - Made the PHP server run in the background on Windows --- lib/Feed.php | 2 +- tests/Feed/TestFeed.php | 14 +++++++++++++- tests/REST/NextCloudNews/TestNCNV1_2.php | 4 ++-- .../NextCloudNews/TestNCNVersionDiscovery.php | 10 +++++----- tests/docroot/Feed/NextFetch/NotModified.php | 16 ++++++++++++---- tests/test | 7 +++++++ tests/test.bat | 4 ++-- 7 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 tests/test diff --git a/lib/Feed.php b/lib/Feed.php index ce2c803e..9119912f 100644 --- a/lib/Feed.php +++ b/lib/Feed.php @@ -298,7 +298,7 @@ class Feed { $offset = "30 minutes"; } else if($diff < (3 * 60 * 60)) { // less than three hours $offset = "1 hour"; - } else if($diff > (36 * 60 * 60)) { // more than 36 hours + } else if($diff >= (36 * 60 * 60)) { // more than 36 hours $offset = "1 day"; } else { $offset = "3 hours"; diff --git a/tests/Feed/TestFeed.php b/tests/Feed/TestFeed.php index caeac1af..0ae58d52 100644 --- a/tests/Feed/TestFeed.php +++ b/tests/Feed/TestFeed.php @@ -18,6 +18,18 @@ class TestFeed extends \PHPUnit\Framework\TestCase { Data::$conf = new Conf(); } + function testComputeNextFetchOnError() { + for($a = 0; $a < 100; $a++) { + if($a < 3) { + $this->assertTime("now + 5 minutes", Feed::nextFetchOnError($a)); + } else if($a < 15) { + $this->assertTime("now + 3 hours", Feed::nextFetchOnError($a)); + } else { + $this->assertTime("now + 1 day", Feed::nextFetchOnError($a)); + } + } + } + function testComputeNextFetchFrom304() { // if less than half an hour, check in 15 minutes $exp = strtotime("now + 15 minutes"); @@ -53,7 +65,7 @@ class TestFeed extends \PHPUnit\Framework\TestCase { $this->assertTime($exp, $f->nextFetch); // otherwise check in three hours $exp = strtotime("now + 3 hours"); - $t = strtotime("now - 6 hours"); + $t = strtotime("now - 3 hours"); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", $this->dateTransform($t, "http")); $this->assertTime($exp, $f->nextFetch); $t = strtotime("now - 35 hours"); diff --git a/tests/REST/NextCloudNews/TestNCNV1_2.php b/tests/REST/NextCloudNews/TestNCNV1_2.php index d5f6782f..bb9a4178 100644 --- a/tests/REST/NextCloudNews/TestNCNV1_2.php +++ b/tests/REST/NextCloudNews/TestNCNV1_2.php @@ -1,8 +1,8 @@ ['v1-2']]); - $h = new Rest\NextCloudNews\Versions(); + $h = new REST\NextCloudNews\Versions(); $req = new Request("GET", "/"); $res = $h->dispatch($req); $this->assertEquals($exp, $res); @@ -28,7 +28,7 @@ class TestNCNVersionDiscovery extends \PHPUnit\Framework\TestCase { function testUseIncorrectMethod() { $exp = new Response(405); - $h = new Rest\NextCloudNews\Versions(); + $h = new REST\NextCloudNews\Versions(); $req = new Request("POST", "/"); $res = $h->dispatch($req); $this->assertEquals($exp, $res); @@ -36,7 +36,7 @@ class TestNCNVersionDiscovery extends \PHPUnit\Framework\TestCase { function testUseIncorrectPath() { $exp = new Response(501); - $h = new Rest\NextCloudNews\Versions(); + $h = new REST\NextCloudNews\Versions(); $req = new Request("GET", "/ook"); $res = $h->dispatch($req); $this->assertEquals($exp, $res); diff --git a/tests/docroot/Feed/NextFetch/NotModified.php b/tests/docroot/Feed/NextFetch/NotModified.php index 865659b6..6a33b462 100644 --- a/tests/docroot/Feed/NextFetch/NotModified.php +++ b/tests/docroot/Feed/NextFetch/NotModified.php @@ -1,4 +1,12 @@ - 304, - 'lastMod' => (int) $_GET['t'], -]; \ No newline at end of file + 304, + 'lastMod' => (int) $_GET['t'], + ]; +} else { + return [ + 'code' => 304, + 'cache' => fasel, + ]; +} \ No newline at end of file diff --git a/tests/test b/tests/test new file mode 100644 index 00000000..809959cc --- /dev/null +++ b/tests/test @@ -0,0 +1,7 @@ +#! /bin/sh +base=`dirname "$0"` +php -S localhost:8000 "$base/server.php" >/dev/null & +php "$base/../vendor/phpunit/phpunit/phpunit" -c "$base/phpunit.xml" +sleep 1s +pid=`netstat -tlpn 2>/dev/null | grep ":8000 " | grep -Eo "[0-9]+/php" | grep -Eo "[0-9]+"` +kill $pid \ No newline at end of file diff --git a/tests/test.bat b/tests/test.bat index 819b0aa6..0392fdb5 100644 --- a/tests/test.bat +++ b/tests/test.bat @@ -1,10 +1,10 @@ @echo off setlocal set base=%~dp0 -start php -S localhost:8000 "%base%\server.php" +start /b php -S localhost:8000 "%base%\server.php" >nul php "%base%\..\vendor\phpunit\phpunit\phpunit" -c "%base%\phpunit.xml" timeout /nobreak /t 1 >nul for /f "usebackq tokens=5" %%a in (`netstat -aon ^| find "LISTENING" ^| find ":8000"`) do ( - taskkill /pid %%a >nul + taskkill /f /pid %%a >nul goto :eof ) \ No newline at end of file