-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Downgrade to phpunit 9* to be able to use WP_Mock 1.*
refactor some unit tests
- Loading branch information
1 parent
286639e
commit d346cbc
Showing
8 changed files
with
419 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.