Skip to content

Commit

Permalink
Introduce cyr_to_lat() function.
Browse files Browse the repository at this point in the history
Add instance:: to the Main class.
  • Loading branch information
kagg-design committed Jul 23, 2023
1 parent b3931d3 commit 2216dda
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 82 deletions.
28 changes: 20 additions & 8 deletions cyr-to-lat.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
*/

// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/** @noinspection PhpParamsInspection */
/** @noinspection PhpDefineCanBeReplacedWithConstInspection */

namespace CyrToLat;
use CyrToLat\Main;

if ( ! defined( 'ABSPATH' ) ) {
// @codeCoverageIgnoreStart
Expand Down Expand Up @@ -86,12 +86,24 @@
define( 'CYR_TO_LAT_REQUIRED_MAX_INPUT_VARS', 1000 );

/**
* Init plugin on plugin load.
* Get the instance of the main plugin class.
*
* @return Main
*/
require_once constant( 'CYR_TO_LAT_PATH' ) . '/vendor/autoload.php';
require_once constant( 'CYR_TO_LAT_PATH' ) . '/libs/polyfill-mbstring/bootstrap.php';
function cyr_to_lat(): Main {

// Global for backwards compatibility.
global $cyr_to_lat_plugin;

global $cyr_to_lat_plugin;
require_once constant( 'CYR_TO_LAT_PATH' ) . '/vendor/autoload.php';
require_once constant( 'CYR_TO_LAT_PATH' ) . '/libs/polyfill-mbstring/bootstrap.php';

$cyr_to_lat_plugin = new Main();
$cyr_to_lat_plugin->init();
$cyr_to_lat_plugin = Main::instance();

return $cyr_to_lat_plugin;
}

/**
* Init plugin on plugin load.
*/
cyr_to_lat()->init();
69 changes: 44 additions & 25 deletions src/php/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,50 @@ class Main {
protected $is_frontend;

/**
* Main constructor.
* Get a single instance of the plugin.
*
* @return Main
*/
public static function instance(): Main {
static $instance;

if ( ! $instance ) {
$instance = new self();
}

return $instance;
}

/**
* Init plugin.
*
* @noinspection PhpUndefinedClassInspection
*/
public function __construct() {
public function init() {
$this->init_classes();

if ( $this->request->is_cli() ) {
try {
/**
* Method WP_CLI::add_command() accepts class as callable.
*
* @noinspection PhpParamsInspection
*/
WP_CLI::add_command( 'cyr2lat', $this->cli );
} catch ( Exception $ex ) {
return;
}
}

$this->init_hooks();
}

/**
* Init other classes.
*
* @return void
*/
public function init_classes() {
$this->request = new Request();
$this->settings = new Settings(
[
Expand Down Expand Up @@ -171,29 +212,7 @@ public function __construct() {
}

/**
* Init class.
*
* @noinspection PhpUndefinedClassInspection
*/
public function init() {
if ( $this->request->is_cli() ) {
try {
/**
* Method WP_CLI::add_command() accepts class as callable.
*
* @noinspection PhpParamsInspection
*/
WP_CLI::add_command( 'cyr2lat', $this->cli );
} catch ( Exception $ex ) {
return;
}
}

$this->init_hooks();
}

/**
* Init class hooks.
* Init hooks.
*/
public function init_hooks() {
if ( $this->is_frontend ) {
Expand Down
79 changes: 36 additions & 43 deletions tests/unit/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use Exception;
use Mockery;
use PHPUnit\Runner\Version;
use ReflectionClass;
use ReflectionException;
use WP_Mock;
use WP_REST_Server;
Expand Down Expand Up @@ -59,16 +58,15 @@ public function tearDown(): void {
}

/**
* Test constructor
* Test init_classes().
*
* @throws ReflectionException Reflection Exception.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_constructor() {
$classname = Main::class;
$frontend = false;
public function test_init_classes() {
$frontend = false;

// Test when requirements are met.
$requirements_met = true;
Expand Down Expand Up @@ -97,44 +95,37 @@ function () use ( &$requirements_met ) {
Mockery::mock( 'overload:' . WPCli::class );
Mockery::mock( 'overload:' . ACF::class );

// Get mock, without the constructor being called.
$mock = $this->getMockBuilder( $classname )->disableOriginalConstructor()->getMock();
$subject = new Main();
$method = 'init_classes';

// Now call the constructor.
$reflected_class = new ReflectionClass( $classname );
$constructor = $reflected_class->getConstructor();
$constructor->invoke( $mock );
$subject->$method();

self::assertInstanceOf( Request::class, $this->get_protected_property( $mock, 'request' ) );
self::assertInstanceOf( Settings::class, $this->get_protected_property( $mock, 'settings' ) );
self::assertInstanceOf( AdminNotices::class, $this->get_protected_property( $mock, 'admin_notices' ) );
self::assertInstanceOf( PostConversionProcess::class, $this->get_protected_property( $mock, 'process_all_posts' ) );
self::assertInstanceOf( TermConversionProcess::class, $this->get_protected_property( $mock, 'process_all_terms' ) );
self::assertInstanceOf( Converter::class, $this->get_protected_property( $mock, 'converter' ) );
self::assertInstanceOf( WPCli::class, $this->get_protected_property( $mock, 'cli' ) );
self::assertInstanceOf( ACF::class, $this->get_protected_property( $mock, 'acf' ) );
self::assertSame( $frontend, $this->get_protected_property( $mock, 'is_frontend' ) );
self::assertInstanceOf( Request::class, $this->get_protected_property( $subject, 'request' ) );
self::assertInstanceOf( Settings::class, $this->get_protected_property( $subject, 'settings' ) );
self::assertInstanceOf( AdminNotices::class, $this->get_protected_property( $subject, 'admin_notices' ) );
self::assertInstanceOf( PostConversionProcess::class, $this->get_protected_property( $subject, 'process_all_posts' ) );
self::assertInstanceOf( TermConversionProcess::class, $this->get_protected_property( $subject, 'process_all_terms' ) );
self::assertInstanceOf( Converter::class, $this->get_protected_property( $subject, 'converter' ) );
self::assertInstanceOf( WPCli::class, $this->get_protected_property( $subject, 'cli' ) );
self::assertInstanceOf( ACF::class, $this->get_protected_property( $subject, 'acf' ) );
self::assertSame( $frontend, $this->get_protected_property( $subject, 'is_frontend' ) );

// Test when requirements are not met.
$requirements_met = false;

// Get mock, without the constructor being called.
$mock = $this->getMockBuilder( $classname )->disableOriginalConstructor()->getMock();

// Now call the constructor.
$reflected_class = new ReflectionClass( $classname );
$constructor = $reflected_class->getConstructor();
$constructor->invoke( $mock );

self::assertInstanceOf( Request::class, $this->get_protected_property( $mock, 'request' ) );
self::assertInstanceOf( Settings::class, $this->get_protected_property( $mock, 'settings' ) );
self::assertInstanceOf( AdminNotices::class, $this->get_protected_property( $mock, 'admin_notices' ) );
self::assertNull( $this->get_protected_property( $mock, 'process_all_posts' ) );
self::assertNull( $this->get_protected_property( $mock, 'process_all_terms' ) );
self::assertNull( $this->get_protected_property( $mock, 'converter' ) );
self::assertNull( $this->get_protected_property( $mock, 'cli' ) );
self::assertNull( $this->get_protected_property( $mock, 'acf' ) );
self::assertNull( $this->get_protected_property( $mock, 'is_frontend' ) );
$subject = new Main();

$subject->$method();

self::assertInstanceOf( Request::class, $this->get_protected_property( $subject, 'request' ) );
self::assertInstanceOf( Settings::class, $this->get_protected_property( $subject, 'settings' ) );
self::assertInstanceOf( AdminNotices::class, $this->get_protected_property( $subject, 'admin_notices' ) );
self::assertNull( $this->get_protected_property( $subject, 'process_all_posts' ) );
self::assertNull( $this->get_protected_property( $subject, 'process_all_terms' ) );
self::assertNull( $this->get_protected_property( $subject, 'converter' ) );
self::assertNull( $this->get_protected_property( $subject, 'cli' ) );
self::assertNull( $this->get_protected_property( $subject, 'acf' ) );
self::assertNull( $this->get_protected_property( $subject, 'is_frontend' ) );
}

/**
Expand All @@ -147,9 +138,9 @@ public function test_init() {
$request->shouldReceive( 'is_cli' )->andReturn( false );

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );

$subject->shouldReceive( 'init_classes' )->once();
$subject->shouldReceive( 'init_hooks' )->once();
$this->set_protected_property( $subject, 'request', $request );

$subject->init();
}
Expand All @@ -165,8 +156,9 @@ public function test_init_with_cli_error() {
$request->shouldReceive( 'is_cli' )->andReturn( true );

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
$subject->shouldReceive( 'init_classes' )->once();
$subject->shouldReceive( 'init_hooks' )->never();
$this->set_protected_property( $subject, 'request', $request );

$add_command = FunctionMocker::replace(
'\WP_CLI::add_command',
Expand All @@ -191,8 +183,9 @@ public function test_init_with_cli() {
$request->shouldReceive( 'is_cli' )->andReturn( true );

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
$subject->shouldReceive( 'init_classes' )->once();
$subject->shouldReceive( 'init_hooks' )->once();
$this->set_protected_property( $subject, 'request', $request );

$add_command = FunctionMocker::replace(
'\WP_CLI::add_command',
Expand Down Expand Up @@ -220,9 +213,9 @@ public function test_init_hooks( $polylang, $sitepress, $frontend ) {
$request = Mockery::mock( Request::class );
$request->shouldReceive( 'is_allowed' )->andReturn( true );

$subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods();
$subject = Mockery::mock( Main::class )->makePartial();
$subject->shouldAllowMockingProtectedMethods();
$this->set_protected_property( $subject, 'request', $request );

$this->set_protected_property( $subject, 'is_frontend', $frontend );

WP_Mock::expectFilterAdded( 'sanitize_title', [ $subject, 'sanitize_title' ], 9, 3 );
Expand Down
7 changes: 1 addition & 6 deletions tests/unit/PluginFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,11 @@ static function ( $name ) {
);

$main = Mockery::mock( 'overload:' . Main::class );
$main->shouldReceive( 'instance' )->once()->andReturn( $main );
$main->shouldReceive( 'init' )->once();

require PLUGIN_MAIN_FILE;

// Include main file the second time to make sure that plugin is not activated again.
include PLUGIN_MAIN_FILE;

$defined->wasCalledWithTimes( [ 'ABSPATH' ], 2 );
$defined->wasCalledWithTimes( [ 'CYR_TO_LAT_VERSION' ], 2 );

$expected = [
'version' => CYR_TO_LAT_TEST_VERSION,
];
Expand Down

0 comments on commit 2216dda

Please sign in to comment.