From cdbcdeef4530c490f7e9d95e31af63bbb9b8c148 Mon Sep 17 00:00:00 2001 From: Yukihiko Shinoda Date: Sun, 28 Jun 2020 22:17:40 +0900 Subject: [PATCH] Remove anonymous function Since PHP cannot serialize anonymous functions. @see https://core.trac.wordpress.org/ticket/50482 @see https://github.com/sebastianbergmann/phpunit/issues/2739 --- .../test-class-static-press-database.php | 90 +++++-------------- .../class-ajax-fetch-invoker.php | 24 +++++ .../class-ajax-finalyze-invoker.php | 24 +++++ .../ajax_invokers/class-ajax-init-invoker.php | 24 +++++ .../ajax_invokers/class-ajax-invoker.php | 49 ++++++++++ tests/testlibraries/class-error-handler.php | 34 +++++++ .../creators/class-model-url-creator.php | 13 +-- 7 files changed, 179 insertions(+), 79 deletions(-) create mode 100644 tests/testlibraries/ajax_invokers/class-ajax-fetch-invoker.php create mode 100644 tests/testlibraries/ajax_invokers/class-ajax-finalyze-invoker.php create mode 100644 tests/testlibraries/ajax_invokers/class-ajax-init-invoker.php create mode 100644 tests/testlibraries/ajax_invokers/class-ajax-invoker.php create mode 100644 tests/testlibraries/class-error-handler.php diff --git a/tests/includes/test-class-static-press-database.php b/tests/includes/test-class-static-press-database.php index d430935..7956604 100644 --- a/tests/includes/test-class-static-press-database.php +++ b/tests/includes/test-class-static-press-database.php @@ -7,17 +7,21 @@ namespace static_press\tests\includes; +require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/ajax_invokers/class-ajax-init-invoker.php'; +require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/ajax_invokers/class-ajax-fetch-invoker.php'; +require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/ajax_invokers/class-ajax-finalyze-invoker.php'; require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/repositories/class-repository-for-test.php'; require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/creators/class-mock-creator.php'; -require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/exceptions/class-die-exception.php'; require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/infrastructure/class-environment.php'; require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/infrastructure/class-file-system-operator.php'; require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/class-model-url.php'; use static_press\includes\Static_Press; use static_press\includes\models\Static_Press_Model_Url; +use static_press\tests\testlibraries\ajax_invokers\Ajax_Init_Invoker; +use static_press\tests\testlibraries\ajax_invokers\Ajax_Fetch_Invoker; +use static_press\tests\testlibraries\ajax_invokers\Ajax_Finalyze_Invoker; use static_press\tests\testlibraries\repositories\Repository_For_Test; use static_press\tests\testlibraries\creators\Mock_Creator; -use static_press\tests\testlibraries\exceptions\Die_Exception; use static_press\tests\testlibraries\infrastructure\Environment; use static_press\tests\testlibraries\infrastructure\File_System_Operator; use static_press\tests\testlibraries\Model_Url; @@ -114,7 +118,8 @@ public function test_ajax_fetch_without_record() { 'final' => true, ); $static_press = new Static_Press(); - $array_json = $this->request_fetch( $static_press ); + $ajax_invoker = new Ajax_Fetch_Invoker( $this, $static_press ); + $array_json = $ajax_invoker->request(); $this->assertEquals( $expect, $array_json ); } @@ -123,8 +128,8 @@ public function test_ajax_fetch_without_record() { * * @dataProvider provider_ajax_fetch_with_record * - * @param string $array_record Array record. - * @param string $expect Expect return value. + * @param Model_Url[] $array_record Array record. + * @param string $expect Expect return value. * @runInSeparateProcess */ public function test_ajax_fetch_with_record( $array_record, $expect ) { @@ -135,7 +140,8 @@ public function test_ajax_fetch_with_record( $array_record, $expect ) { } $static_press = new Static_Press( '/', '', array(), null, Mock_Creator::create_remote_getter_mock() ); - $this->assertEquals( $expect, $this->request_fetch( $static_press ) ); + $ajax_invoker = new Ajax_Fetch_Invoker( $this, $static_press ); + $this->assertEquals( $expect, $ajax_invoker->request() ); } /** @@ -289,7 +295,8 @@ public function test_ajax_finalyze() { set_transient( "static static - {$user_id}", array( 'fetch_last_id' => 2 ), 3600 ); $expect = array( 'result' => true ); $static_press = new Static_Press(); - $this->assertEquals( $expect, $this->request_finalyze( $static_press ) ); + $ajax_invoker = new Ajax_Finalyze_Invoker( $this, $static_press ); + $this->assertEquals( $expect, $ajax_invoker->request() ); } /** @@ -309,7 +316,8 @@ public function test_all() { Mock_Creator::set_up_seo_url( 'http://example.org/' ), Mock_Creator::create_docuemnt_root_getter_mock() ); - $array_json = $this->request_init( $static_press ); + $ajax_invoker = new Ajax_Init_Invoker( $this, $static_press ); + $array_json = $ajax_invoker->request(); $this->assertTrue( $array_json['result'] ); $array_urls_count = $array_json['urls_count']; $url_count_content = $array_urls_count[0]; @@ -331,7 +339,8 @@ public function test_all() { Mock_Creator::create_docuemnt_root_getter_mock() ); while ( true ) { - $response = $this->request_fetch( $static_press ); + $ajax_invoker = new Ajax_Fetch_Invoker( $this, $static_press ); + $response = $ajax_invoker->request(); if ( ! $response['result'] || $response['final'] ) { break; } @@ -339,7 +348,8 @@ public function test_all() { $expect = array( 'result' => true ); $static_press = new Static_Press( '/', File_System_Operator::OUTPUT_DIRECTORY ); - $this->assertEquals( $expect, $this->request_finalyze( $static_press ) ); + $ajax_invoker = new Ajax_Finalyze_Invoker( $this, $static_press ); + $this->assertEquals( $expect, $ajax_invoker->request() ); $path_to_expect_file = File_System_Operator::OUTPUT_DIRECTORY . Environment::DIRECTORY_NAME_WORD_PRESS . '/wp-content/uploads/2020/03/white.png'; $files = File_System_Operator::get_array_file_in_output_directory(); $message = 'File ' . $path_to_expect_file . "doesn't exist.\nExisting file list:\n" . implode( "\n", $files ); @@ -363,64 +373,4 @@ private function sign_on_to_word_press() { wp_set_current_user( $result->ID ); return $result->ID; } - - /** - * Requests init. - * - * @param Static_Press $static_press StaticPress. - * @return array JSON responce. - */ - private function request_init( $static_press ) { - return $this->request( - function() use ( $static_press ) { - $static_press->ajax_init( Mock_Creator::create_terminator_mock() ); - } - ); - } - - /** - * Requests fetch. - * - * @param Static_Press $static_press StaticPress. - * @return array JSON responce. - */ - private function request_fetch( $static_press ) { - return $this->request( - function() use ( $static_press ) { - $static_press->ajax_fetch( Mock_Creator::create_terminator_mock() ); - } - ); - } - - /** - * Requests finalyze. - * - * @param Static_Press $static_press StaticPress. - * @return array JSON responce. - */ - private function request_finalyze( $static_press ) { - return $this->request( - function() use ( $static_press ) { - $static_press->ajax_finalyze( Mock_Creator::create_terminator_mock() ); - } - ); - } - - /** - * Requests. - * - * @param callable $function StaticPress. - * @return array JSON responce. - */ - private function request( $function ) { - ob_start(); - try { - $function(); - } catch ( Die_Exception $exception ) { - $output = ob_get_clean(); - $this->assertEquals( 'Dead!', $exception->getMessage() ); - return json_decode( $output, true ); - } - $this->fail(); - } } diff --git a/tests/testlibraries/ajax_invokers/class-ajax-fetch-invoker.php b/tests/testlibraries/ajax_invokers/class-ajax-fetch-invoker.php new file mode 100644 index 0000000..022557d --- /dev/null +++ b/tests/testlibraries/ajax_invokers/class-ajax-fetch-invoker.php @@ -0,0 +1,24 @@ +static_press->ajax_fetch( Mock_Creator::create_terminator_mock() ); + } +} diff --git a/tests/testlibraries/ajax_invokers/class-ajax-finalyze-invoker.php b/tests/testlibraries/ajax_invokers/class-ajax-finalyze-invoker.php new file mode 100644 index 0000000..34556ae --- /dev/null +++ b/tests/testlibraries/ajax_invokers/class-ajax-finalyze-invoker.php @@ -0,0 +1,24 @@ +static_press->ajax_finalyze( Mock_Creator::create_terminator_mock() ); + } +} diff --git a/tests/testlibraries/ajax_invokers/class-ajax-init-invoker.php b/tests/testlibraries/ajax_invokers/class-ajax-init-invoker.php new file mode 100644 index 0000000..5ba515c --- /dev/null +++ b/tests/testlibraries/ajax_invokers/class-ajax-init-invoker.php @@ -0,0 +1,24 @@ +static_press->ajax_init( Mock_Creator::create_terminator_mock() ); + } +} diff --git a/tests/testlibraries/ajax_invokers/class-ajax-invoker.php b/tests/testlibraries/ajax_invokers/class-ajax-invoker.php new file mode 100644 index 0000000..6b9c276 --- /dev/null +++ b/tests/testlibraries/ajax_invokers/class-ajax-invoker.php @@ -0,0 +1,49 @@ +test_case = $test_case; + $this->static_press = $static_press; + } + + /** + * Requests. + * + * @return array JSON responce. + */ + public function request() { + ob_start(); + try { + $this->invoke_ajax(); + } catch ( Die_Exception $exception ) { + $output = ob_get_clean(); + $this->test_case->assertEquals( 'Dead!', $exception->getMessage() ); + return json_decode( $output, true ); + } + $this->test_case->fail(); + } + + /** + * Invokes ajax function. + */ + abstract protected function invoke_ajax(); +} diff --git a/tests/testlibraries/class-error-handler.php b/tests/testlibraries/class-error-handler.php new file mode 100644 index 0000000..cb5fb5a --- /dev/null +++ b/tests/testlibraries/class-error-handler.php @@ -0,0 +1,34 @@ +