From 2a025ec18c9c5868fc1ec19e2789ed4b3cfa205a Mon Sep 17 00:00:00 2001 From: marcusquinn <6428977+marcusquinn@users.noreply.github.com> Date: Fri, 18 Apr 2025 19:17:51 +0100 Subject: [PATCH] Fix: Final PHPCS formatting and WP_Mock expectations for AdminTest --- includes/Admin/class-admin.php | 4 +- tests/test-admin.php | 67 +++++++++++++++------------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/includes/Admin/class-admin.php b/includes/Admin/class-admin.php index 3db946c..b6201a7 100644 --- a/includes/Admin/class-admin.php +++ b/includes/Admin/class-admin.php @@ -42,7 +42,9 @@ class Admin { * Enqueues admin scripts and styles. * * @param string $hook_suffix The current admin page. - * @phpcs:ignore WordPress.CodeAnalysis.UnusedFunctionParameter.Found */ + + * @phpcs:ignore WordPress.CodeAnalysis.UnusedFunctionParameter.Found + */ public function enqueue_admin_assets( $hook_suffix ) { // Enqueue admin styles. \wp_enqueue_style( diff --git a/tests/test-admin.php b/tests/test-admin.php index c75dbd1..76f0dc6 100644 --- a/tests/test-admin.php +++ b/tests/test-admin.php @@ -66,45 +66,38 @@ class AdminTest extends \WP_Mock\Tools\TestCase { } /** - * Test enqueue_admin_assets method + * Test the enqueue_admin_assets method. */ - public function test_enqueue_admin_assets() { - // Set up mocks for this specific test - WP_Mock::userFunction('wp_enqueue_style', [ - 'times' => 1, - 'args' => ['wpst-admin-style', 'path/to/admin/css/admin-styles.css'], - ]); - WP_Mock::userFunction('wp_enqueue_script', [ - 'times' => 1, - 'args' => ['wpst-admin-script', 'path/to/admin/js/admin-scripts.js', array('jquery'), null, true], - ]); - WP_Mock::userFunction('wp_localize_script', [ - 'times' => 1, - 'args' => [ - 'wpst-admin-script', - 'wpst_admin_params', - array( - 'ajax_url' => 'http://example.org/wp-admin/admin-ajax.php', - 'nonce' => '1234567890', - ), - ], - ]); - WP_Mock::userFunction('admin_url', [ - 'times' => 1, - 'args' => ['admin-ajax.php'], - 'return' => 'http://example.org/wp-admin/admin-ajax.php', - ]); - WP_Mock::userFunction('wp_create_nonce', [ - 'times' => 1, - 'args' => ['wpst-admin-nonce'], - 'return' => '1234567890', - ]); + public function test_enqueue_admin_assets(): void + { + // Define expected parameters for the functions + $style_handle = 'wpst-admin-style'; + $style_src = 'path/to/admin/css/admin-styles.css'; + $script_handle = 'wpst-admin-script'; + $script_src = 'path/to/admin/js/admin-scripts.js'; + $version = '1.0.0'; // Match the version returned by the mocked core->get_plugin_version() - // Call the method under test - $this->admin->enqueue_admin_assets('plugins.php'); + // Expect wp_enqueue_style to be called + \WP_Mock::expectFunction( + 'wp_enqueue_style', + [ + 'times' => 1, + 'args' => [ $style_handle, $style_src, [], $version ], + ] + ); - // WP_Mock::tearDown() in the main tearDown method will verify the expectations - // We can add an assertion here to make the test explicit - $this->assertTrue(true); + // Expect wp_enqueue_script to be called + \WP_Mock::expectFunction( + 'wp_enqueue_script', + [ + 'times' => 1, + 'args' => [ $script_handle, $script_src, [ 'jquery' ], $version, true ], + ] + ); + + // Call the method under test. + $this->admin->enqueue_admin_assets( 'test-hook' ); + + // Assertions are implicitly handled by WP_Mock's expectation checks on tearDown. } }