Skip to content

Commit

Permalink
Use Dependency Injection to register the editor metabox inside the Ad…
Browse files Browse the repository at this point in the history
…min class
  • Loading branch information
pattonwebz committed Mar 18, 2024
1 parent 0f6eff8 commit c95e2be
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
22 changes: 18 additions & 4 deletions admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@
class Admin {

/**
* Class constructor.
* Meta boxes instance.
*
* @since 1.10.0
*
* @var Meta_Boxes
*/
public function __construct() {
private Meta_Boxes $meta_boxes;

/**
* Class constructor for injecting dependencies.
*
* @param Meta_Boxes|null $meta_boxes Meta boxes instance.
*/
public function __construct( Meta_Boxes $meta_boxes = null ) {
$this->meta_boxes = $meta_boxes;
}

/**
* Initialize.
*
* @return void
*/
public function init() {
public function init(): void {

$update_database = new Update_Database();
$update_database->init_hooks();
Expand All @@ -44,14 +56,16 @@ public function init() {
$site_health_info->init_hooks();

$this->init_ajax();

$this->meta_boxes->init_hooks();
}

/**
* Initialize ajax.
*
* @return void
*/
private function init_ajax() {
private function init_ajax(): void {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
return;
}
Expand Down
8 changes: 5 additions & 3 deletions includes/classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace EDAC\Inc;

use EDAC\Admin\Admin;
use EDAC\Admin\Meta_Boxes;

/**
* Main plugin functionality class.
Expand All @@ -19,7 +20,8 @@ class Plugin {
*/
public function __construct() {
if ( \is_admin() ) {
$admin = new Admin();
$meta_boxes = new Meta_Boxes();
$admin = new Admin( $meta_boxes );
$admin->init();
} else {
$this->init();
Expand All @@ -36,9 +38,9 @@ public function __construct() {
* @return void
*/
private function init() {

add_action( 'wp_enqueue_scripts', array( 'EDAC\Inc\Enqueue_Frontend', 'enqueue' ) );

$accessibility_statement = new Accessibility_Statement();
$accessibility_statement->init_hooks();

Expand Down
77 changes: 77 additions & 0 deletions tests/phpunit/Admin/MetaBoxesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Test class for the editor metabox.
*
* @package Accessibility_Checker
*/

use EDAC\Admin\Admin;
use EDAC\Admin\Meta_Boxes;

/**
* Tests for the editor meta box class.
*/
class MetaBoxesTest extends WP_UnitTestCase {

/**
* Test the hooks are added only in admin.
*
* @return void
*/
public function test_meta_boxes_are_registered_in_admin(): void {

$meta_boxes = $this->getMockBuilder( Meta_Boxes::class )
->onlyMethods( array( 'register_meta_boxes' ) )
->getMock();

$meta_boxes->expects( $this->once() )
->method( 'register_meta_boxes' );

$this->invoke_admin_init( $meta_boxes );
do_action( 'add_meta_boxes' );
}

/**
* Test the init_hooks method.
*
* @return void
*/
public function test_init_hooks(): void {
$meta_boxes = new Meta_Boxes();
$meta_boxes->init_hooks();

$this->assertEquals(
10,
has_action(
'add_meta_boxes',
array(
$meta_boxes,
'register_meta_boxes',
)
)
);
}

/**
* Test the render method.
*
* @return void
*/
public function test_render(): void {
$meta_boxes = new Meta_Boxes();
$meta_boxes->render();

$this->expectOutputRegex( '/^<div id="edac-tabs">/' );
}

/**
* Invoke the admin init method.
*
* @param Meta_Boxes $meta_boxes The metabox class.
* @return void
*/
private function invoke_admin_init( $meta_boxes ): void {
$admin = new Admin( $meta_boxes );
$admin->init();
}
}

0 comments on commit c95e2be

Please sign in to comment.