Skip to content

Commit

Permalink
Remove anonymous function
Browse files Browse the repository at this point in the history
  • Loading branch information
yukihiko-shinoda committed Jun 28, 2020
1 parent d8edd0c commit cdbcdee
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 79 deletions.
90 changes: 20 additions & 70 deletions tests/includes/test-class-static-press-database.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}

Expand All @@ -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 ) {
Expand All @@ -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() );
}

/**
Expand Down Expand Up @@ -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() );
}

/**
Expand All @@ -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];
Expand All @@ -331,15 +339,17 @@ 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;
}
}

$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 );
Expand All @@ -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();
}
}
24 changes: 24 additions & 0 deletions tests/testlibraries/ajax_invokers/class-ajax-fetch-invoker.php
@@ -0,0 +1,24 @@
<?php
/**
* Ajax_Fetch_Invoker
*
* @package static_press\tests\testlibraries\ajax_invokers
*/

namespace static_press\tests\testlibraries\ajax_invokers;

require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/ajax_invokers/class-ajax-invoker.php';
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/creators/class-mock-creator.php';
use static_press\tests\testlibraries\ajax_invokers\Ajax_Invoker;
use static_press\tests\testlibraries\creators\Mock_Creator;
/**
* Class Ajax_Fetch_Invoker
*/
class Ajax_Fetch_Invoker extends Ajax_Invoker {
/**
* Invokes ajax function.
*/
protected function invoke_ajax() {
$this->static_press->ajax_fetch( Mock_Creator::create_terminator_mock() );
}
}
24 changes: 24 additions & 0 deletions tests/testlibraries/ajax_invokers/class-ajax-finalyze-invoker.php
@@ -0,0 +1,24 @@
<?php
/**
* Ajax_Finalyze_Invoker
*
* @package static_press\tests\testlibraries\ajax_invokers
*/

namespace static_press\tests\testlibraries\ajax_invokers;

require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/ajax_invokers/class-ajax-invoker.php';
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/creators/class-mock-creator.php';
use static_press\tests\testlibraries\ajax_invokers\Ajax_Invoker;
use static_press\tests\testlibraries\creators\Mock_Creator;
/**
* Class Ajax_Finalyze_Invoker
*/
class Ajax_Finalyze_Invoker extends Ajax_Invoker {
/**
* Invokes ajax function.
*/
protected function invoke_ajax() {
$this->static_press->ajax_finalyze( Mock_Creator::create_terminator_mock() );
}
}
24 changes: 24 additions & 0 deletions tests/testlibraries/ajax_invokers/class-ajax-init-invoker.php
@@ -0,0 +1,24 @@
<?php
/**
* Ajax_Init_Invoker
*
* @package static_press\tests\testlibraries\ajax_invokers
*/

namespace static_press\tests\testlibraries\ajax_invokers;

require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/ajax_invokers/class-ajax-invoker.php';
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/creators/class-mock-creator.php';
use static_press\tests\testlibraries\ajax_invokers\Ajax_Invoker;
use static_press\tests\testlibraries\creators\Mock_Creator;
/**
* Class Ajax_Init_Invoker
*/
class Ajax_Init_Invoker extends Ajax_Invoker {
/**
* Invokes ajax function.
*/
protected function invoke_ajax() {
$this->static_press->ajax_init( Mock_Creator::create_terminator_mock() );
}
}
49 changes: 49 additions & 0 deletions tests/testlibraries/ajax_invokers/class-ajax-invoker.php
@@ -0,0 +1,49 @@
<?php
/**
* Ajax_Invoker
*
* @package static_press\tests\testlibraries\ajax_invokers
*/

namespace static_press\tests\testlibraries\ajax_invokers;

require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/exceptions/class-die-exception.php';
use static_press\tests\testlibraries\exceptions\Die_Exception;

/**
* Class Ajax_Invoker
*/
abstract class Ajax_Invoker {
/**
* ExpectUrl constructor.
*
* @param PHPUnit_Framework_TestCase $test_case Expect type of URL object.
* @param Static_Press $static_press StaticPress.
*/
public function __construct( $test_case, $static_press ) {
$this->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();
}
34 changes: 34 additions & 0 deletions tests/testlibraries/class-error-handler.php
@@ -0,0 +1,34 @@
<?php
/**
* Class Error_Handler
*
* @package static_press\tests\testlibraries
*/

namespace static_press\tests\testlibraries;

/**
* Error handler.
*/
class Error_Handler {
/**
* Handles error.
*
* @param int $errno The first parameter, errno, contains the level of the error raised, as an integer.
* @param string $errstr The second parameter, errstr, contains the error message, as a string.
* @param string $errfile The third parameter is optional, errfile, which contains the filename that the error was raised in, as a string.
* @param int $errline The fourth parameter is optional, errline, which contains the line number the error was raised at, as an integer.
* @param array $errcontext The fifth parameter is optional, errcontext, which is an array that points to the active symbol table at the point the error occurred.
* In other words, errcontext will contain an array of every variable that existed in the scope the error was triggered in.
* User error handler must not modify error context.
* @return false
* @throws \LogicException When error.
*/
public function handle( $errno, $errstr, $errfile, $errline, $errcontext ) {
// error was suppressed with the @-operator.
if ( 0 === error_reporting() ) {
return false;
}
throw new \LogicException( $errstr, $errno );
}
}
13 changes: 4 additions & 9 deletions tests/testlibraries/creators/class-model-url-creator.php
Expand Up @@ -10,6 +10,7 @@
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/creators/class-mock-creator.php';
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/class-expect-urls-static-files.php';
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/repositories/class-repository-for-test.php';
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/class-error-handler.php';
require_once STATIC_PRESS_PLUGIN_DIR . 'tests/testlibraries/class-model-url.php';
use static_press\includes\models\Static_Press_Model_Url;
use static_press\includes\models\Static_Press_Model_Url_Author;
Expand All @@ -20,6 +21,7 @@
use static_press\includes\models\Static_Press_Model_Url_Static_File;
use static_press\includes\models\Static_Press_Model_Url_Term;
use static_press\tests\testlibraries\creators\Mock_Creator;
use static_press\tests\testlibraries\Error_Handler;
use static_press\tests\testlibraries\Expect_Urls_Static_Files;
use static_press\tests\testlibraries\Model_Url;
use static_press\tests\testlibraries\repositories\Repository_For_Test;
Expand Down Expand Up @@ -127,15 +129,8 @@ public static function get_expect_urls_static_files() {
*
* @see https://stackoverflow.com/questions/1241728/can-i-try-catch-a-warning/1241751#1241751
*/
set_error_handler(
function( $errno, $errstr, $errfile, $errline, $errcontext ) {
// error was suppressed with the @-operator.
if ( 0 === error_reporting() ) {
return false;
}
throw new \LogicException( $errstr, $errno );
}
);
$error_handler = new Error_Handler();
set_error_handler( array( $error_handler, 'handle' ) );
$expect = array();
$array_logic_exception = array();
foreach ( Expect_Urls_Static_Files::EXPECT_URLS as $expect_url ) {
Expand Down

0 comments on commit cdbcdee

Please sign in to comment.