From e8561f6a04ef4e8074d231e6440667376e3974d2 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 27 Oct 2020 21:19:19 +0000 Subject: [PATCH] test pages with args properly --- ext/danbooru_api/test.php | 8 +++--- ext/tag_list/test.php | 8 +++--- ext/view/test.php | 7 +++-- tests/bootstrap.php | 54 +++++++++++++++++++-------------------- 4 files changed, 40 insertions(+), 37 deletions(-) diff --git a/ext/danbooru_api/test.php b/ext/danbooru_api/test.php index f2a82e0a..40a4f1f4 100644 --- a/ext/danbooru_api/test.php +++ b/ext/danbooru_api/test.php @@ -8,12 +8,12 @@ class DanbooruApiTest extends ShimmiePHPUnitTestCase $image_id = $this->post_image("tests/bedroom_workshop.jpg", "data"); $this->get_page("api/danbooru/find_posts"); - $this->get_page("api/danbooru/find_posts?id=$image_id"); - $this->get_page("api/danbooru/find_posts?md5=17fc89f372ed3636e28bd25cc7f3bac1"); + $this->get_page("api/danbooru/find_posts", ["id"=>$image_id]); + $this->get_page("api/danbooru/find_posts", ["md5"=>"17fc89f372ed3636e28bd25cc7f3bac1"]); $this->get_page("api/danbooru/find_tags"); - $this->get_page("api/danbooru/find_tags?id=1"); - $this->get_page("api/danbooru/find_tags?name=data"); + $this->get_page("api/danbooru/find_tags", ["id"=>1]); + $this->get_page("api/danbooru/find_tags", ["name"=>"data"]); $page = $this->get_page("api/danbooru/post/show/$image_id"); $this->assertEquals(302, $page->code); diff --git a/ext/tag_list/test.php b/ext/tag_list/test.php index 3278c491..ad13e7ab 100644 --- a/ext/tag_list/test.php +++ b/ext/tag_list/test.php @@ -23,16 +23,16 @@ class TagListTest extends ShimmiePHPUnitTestCase public function testMinCount() { foreach ($this->pages as $page) { - $this->get_page("tags/$page?mincount=999999"); + $this->get_page("tags/$page", ["mincount"=>999999]); $this->assert_title("Tag List"); - $this->get_page("tags/$page?mincount=1"); + $this->get_page("tags/$page", ["mincount"=>1]); $this->assert_title("Tag List"); - $this->get_page("tags/$page?mincount=0"); + $this->get_page("tags/$page", ["mincount"=>0]); $this->assert_title("Tag List"); - $this->get_page("tags/$page?mincount=-1"); + $this->get_page("tags/$page", ["mincount"=>-1]); $this->assert_title("Tag List"); } } diff --git a/ext/view/test.php b/ext/view/test.php index a0f18c75..797429b4 100644 --- a/ext/view/test.php +++ b/ext/view/test.php @@ -42,8 +42,11 @@ class ViewImageTest extends ShimmiePHPUnitTestCase $this->assertEquals("/test/post/view/$image_id_2", $page->redirect); // When searching, we skip the middle - $page = $this->get_page("post/prev/$image_id_1?search=test"); - $this->assertEquals("/test/post/view/$image_id_2", $page->redirect); + $page = $this->get_page("post/prev/$image_id_1", ["search"=>"test"]); + $this->assertEquals("/test/post/view/$image_id_3", $page->redirect); + + $page = $this->get_page("post/next/$image_id_3", ["search"=>"test"]); + $this->assertEquals("/test/post/view/$image_id_1", $page->redirect); // Middle image: has next and prev $page = $this->get_page("post/next/$image_id_2"); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 51d3ae6c..d0d868a0 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -101,16 +101,31 @@ abstract class ShimmiePHPUnitTestCase extends TestCase } } - protected static function get_page($page_name, $args=null): Page + private static function check_args(?array $args): array { + if(!$args) return []; + foreach ($args as $k=>$v) { + if (is_array($v)) { + $args[$k] = $v; + } else { + $args[$k] = (string)$v; + } + } + return $args; + } + + protected static function request($page_name, $get_args=null, $post_args=null): Page { // use a fresh page global $page; - if (!$args) { - $args = []; + $get_args = self::check_args($get_args); + $post_args = self::check_args($post_args); + + if (str_contains($page_name, "?")) { + throw new RuntimeException("Query string included in page name"); } - $_SERVER['REQUEST_URI'] = make_link($page_name, http_build_query($args)); - $_GET = $args; - $_POST = []; + $_SERVER['REQUEST_URI'] = make_link($page_name, http_build_query($get_args)); + $_GET = $get_args; + $_POST = $post_args; $page = new Page(); send_event(new PageRequestEvent($page_name)); if ($page->mode == PageMode::REDIRECT) { @@ -119,29 +134,14 @@ abstract class ShimmiePHPUnitTestCase extends TestCase return $page; } + protected static function get_page($page_name, $args=null): Page + { + return self::request($page_name, $args, null); + } + protected static function post_page($page_name, $args=null): Page { - // use a fresh page - global $page; - if (!$args) { - $args = []; - } - $_SERVER['REQUEST_URI'] = make_link($page_name); - foreach ($args as $k=>$v) { - if (is_array($v)) { - $args[$k] = $v; - } else { - $args[$k] = (string)$v; - } - } - $_GET = []; - $_POST = $args; - $page = new Page(); - send_event(new PageRequestEvent($page_name)); - if ($page->mode == PageMode::REDIRECT) { - $page->code = 302; - } - return $page; + return self::request($page_name, null, $args); } // page things