test pages with args properly

This commit is contained in:
Shish 2020-10-27 21:19:19 +00:00
parent 8bc44f6cb5
commit e8561f6a04
4 changed files with 40 additions and 37 deletions

View file

@ -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);

View file

@ -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");
}
}

View file

@ -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");

View file

@ -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