Skip to content

Commit

Permalink
Downgrade to phpunit 9* to be able to use WP_Mock 1.*
Browse files Browse the repository at this point in the history
refactor some unit tests
  • Loading branch information
freibergergarcia committed Oct 1, 2023
1 parent 286639e commit d346cbc
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 54 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
}
},
"require-dev": {
"phpunit/phpunit": "^10.3",
"10up/wp_mock": "^0.4.2",
"phpunit/phpunit": "^9.6.13",
"10up/wp_mock": "^1.0.0",
"phpcompatibility/phpcompatibility-wp": "*"
},
"autoload": {
Expand Down
5 changes: 3 additions & 2 deletions di-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function DI\create;

return [
'taxonomy' => create( 'WordPress_Related\Taxonomy\Taxonomy' ),
'post_type' => create( 'WordPress_Related\Post_Type\Post_Type' ),
'Admin_Menu' => create( 'WordPress_Related\Admin\Admin_Menu' ),
'Taxonomy' => create( 'WordPress_Related\Taxonomy\Taxonomy' ),
'Post_Type' => create( 'WordPress_Related\Post_Type\Post_Type' ),
];
103 changes: 103 additions & 0 deletions includes/Admin/Admin_Menu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare( strict_types=1 );

namespace WordPress_Related\Admin;

use Exception;
use WordPress_Related\Infrastructure\Registerable;

/**
* Admin_Menu Class
*
* This class is responsible for registering and rendering the admin menu and submenus
* for the WordPress Related plugin within the WordPress admin interface.
* It implements the Registerable interface to ensure its registration method is called
* during the plugin's boot process.
*
* @package WordPress_Related\Admin
* @since 1.0.0
* @version 1.0.0
*/
class Admin_Menu implements Registerable {

/**
* Register the service with WordPress.
*
* Hooks the register_menu method to the admin_menu action, ensuring that
* the menu and its submenus are registered with WordPress at the appropriate time.
*
* @return void
*/
public function register(): void {
add_action( 'admin_menu', array( $this, 'register_menu' ) );
}

/**
* Register the plugin menu.
*
* @return void
* @throws Exception
*/
public function register_menu(): void {
// Adding Top-level menu
add_menu_page(
__( 'WordPress Related', 'wordpress-related' ),
__( 'WordPress Related', 'wordpress-related' ),
'manage_options',
'wordpress-related',
array( $this, 'render_plugin_page' ),
'dashicons-coffee'
);

// Adding sub-menu for Custom Taxonomies
add_submenu_page(
'wordpress-related',
__( 'Custom Taxonomies', 'wordpress-related' ),
__( 'Custom Taxonomies', 'wordpress-related' ),
'manage_options',
'wordpress-related-taxonomies',
array( $this, 'render_taxonomies_page' )
);

// Adding sub-menu for Custom Post Types
add_submenu_page(
'wordpress-related',
__( 'Custom Post Types', 'wordpress-related' ),
__( 'Custom Post Types', 'wordpress-related' ),
'manage_options',
'wordpress-related-post-types',
array( $this, 'render_post_types_page' )
);
}

/**
* Render the main plugin page.
*
* @return void
* @throws Exception
*/
public function render_plugin_page(): void {
echo '<h1>' . esc_html__( 'WordPress Related', 'wordpress-related' ) . '</h1>';
}

/**
* Render the Custom Taxonomies page.
*
* @return void
* @throws Exception
*/
public function render_taxonomies_page(): void {
echo '<h1>' . esc_html__( 'Custom Taxonomies', 'wordpress-related' ) . '</h1>';
}

/**
* Render the Custom Post Types page.
*
* @return void
* @throws Exception
*/
public function render_post_types_page(): void {
echo '<h1>' . esc_html__( 'Custom Post Types', 'wordpress-related' ) . '</h1>';
}
}
1 change: 0 additions & 1 deletion includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function init(): void {
*/
public function register_services(): void {
$services = $this->container->getKnownEntryNames();

foreach ( $services as $service ) {
try {
$service_instance = $this->container->get( $service );
Expand Down
66 changes: 36 additions & 30 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/unit/bootstrap.php"
backupGlobals="false"
colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<coverage>
<report>
<clover outputFile="coverage.xml" />
<html outputDirectory="coverage" />
</report>
</coverage>
<testsuites>
<testsuite name="Unit tests">
<directory suffix="test.php">./tests/unit</directory>
</testsuite>
</testsuites>
<logging />
<source>
<include>
<directory suffix=".php">includes</directory>
</include>
<exclude>
<directory>vendor</directory>
</exclude>
</source>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="tests/unit/bootstrap.php"
backupGlobals="false"
colors="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd">

<!-- Coverage configuration -->
<coverage processUncoveredFiles="true">
<include>
<!-- Include the directories to be covered -->
<directory suffix=".php">./includes</directory>
</include>
<exclude>
<!-- Exclude the directories not to be covered -->
<directory suffix=".php">./vendor</directory>
</exclude>
<report>
<clover outputFile="coverage.xml"/>
<html outputDirectory="coverage"/>
</report>
</coverage>

<!-- Test suites configuration -->
<testsuites>
<testsuite name="Unit tests">
<directory suffix="Test.php">./tests/unit</directory>
</testsuite>
</testsuites>

<!-- Logging configuration -->
<logging>
<!-- You can specify logging preferences here -->
</logging>

</phpunit>
139 changes: 139 additions & 0 deletions tests/unit/Admin/Admin_Menu_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

declare( strict_types=1 );

namespace WordPress_Related\Tests\Unit\Admin;

use Exception;
use WordPress_Related\Admin\Admin_Menu;
use WP_Mock;
use WP_Mock\Tools\TestCase;

/**
* Admin_Menu Test.
*
* @package WordPress_Related\Tests\Unit\Admin
*/
class Admin_Menu_Test extends TestCase {

/**
* Set up.
*
* @return void
* @throws Exception
*/
public function setUp(): void {
parent::setUp();
if ( ! defined( 'WORDPRESS_RELATED_FILE' ) ) {
define( 'WORDPRESS_RELATED_FILE', __DIR__ . '/../../wordpress-related.php' );
}
WP_Mock::setUp();
}

/**
* Test register method.
*
* @return void
* @throws Exception
*/
public function test_register(): void {
$admin_menu = new Admin_Menu();

WP_Mock::expectActionAdded( 'admin_menu', array( $admin_menu, 'register_menu' ) );

$admin_menu->register();

$this->assertHooksAdded(); // This asserts that the expected hooks were added
}

/**
* Test register_menu method.
*
* @return void
* @throws Exception
*/
public function test_register_menu(): void {
$admin_menu = new Admin_Menu(); // Instantiating the object before using it

WP_Mock::userFunction(
'add_menu_page',
array(
'times' => 1,
'args' => array(
__( 'WordPress Related', 'wordpress-related' ),
__( 'WordPress Related', 'wordpress-related' ),
'manage_options',
'wordpress-related',
array( $admin_menu, 'render_plugin_page' ),
'dashicons-coffee',
),
)
);

WP_Mock::userFunction(
'add_submenu_page',
array(
'times' => 2, // Since add_submenu_page is called twice in register_menu
)
);

$admin_menu->register_menu();

$this->expectNotToPerformAssertions();
}

/**
* Test render_plugin_page method.
*
* @return void
* @throws Exception
*/
public function test_render_plugin_page(): void {
$admin_menu = new Admin_Menu();
ob_start();
$admin_menu->render_plugin_page();
$output = ob_get_clean();

$this->assertSame( '<h1>WordPress Related</h1>', $output );
}

/**
* Test render_taxonomies_page method.
*
* @return void
* @throws Exception
*/
public function test_render_taxonomies_page(): void {
$admin_menu = new Admin_Menu();
ob_start();
$admin_menu->render_taxonomies_page();
$output = ob_get_clean();

$this->assertSame( '<h1>Custom Taxonomies</h1>', $output );
}

/**
* Test render_post_types_page method.
*
* @return void
* @throws Exception
*/
public function test_render_post_types_page(): void {
$admin_menu = new Admin_Menu();
ob_start();
$admin_menu->render_post_types_page();
$output = ob_get_clean();

$this->assertSame( '<h1>Custom Post Types</h1>', $output );
}

/**
* Tear down.
*
* @return void
*/
public function tearDown(): void {
parent::tearDown();
WP_Mock::tearDown();
}
}
3 changes: 2 additions & 1 deletion tests/unit/Plugin_Factory_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace WordPress_Related\Tests\Unit;

use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use WordPress_Related\Plugin;
use WordPress_Related\Plugin_Factory;
use DI\Container;

Expand All @@ -28,6 +28,7 @@ class Plugin_Factory_Test extends TestCase {
* Set up.
*
* @return void
* @throws Exception
* @since 1.0.0
*/
public function setUp(): void {
Expand Down
Loading

0 comments on commit d346cbc

Please sign in to comment.