From 691d978dc2575f90effe6b1e9abe4d40643ecaf0 Mon Sep 17 00:00:00 2001 From: Beda Schmid Date: Wed, 29 May 2024 18:10:12 +0700 Subject: [PATCH] Refactor Code and Improve Test Cases ### Changed - Refactored test cases. --- CHANGELOG.md | 4 ++- tests/bootstrap.php | 4 +-- tests/class-filesystem-utilitytest.php | 49 ++++++++++++++++++-------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 827fdaf..2e8d3ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,13 +18,15 @@ This is NOT the changelog of the WordPress Plugin you ultimately develop! - Updated minimum WordPress version requirement to 4.0. - Refactored how metaboxes are registered and saved, improving code clarity . - Renamed keys for shortcodes, taxonomies, and custom post types using a configurable slug. -- Replaced specific metabox class with a more generic example . +- Replaced specific metabox class with a more generic example. +- Refactored Test Cases. ### Removed - Unused function imports across multiple files. ### Fixed - Incorrect author information in several files. +- Require `wp-admin/includes/file.php` if WP_Filesystem is unavailable. ## [v5-alpha] - 2024-05-26 diff --git a/tests/bootstrap.php b/tests/bootstrap.php index f6df293..df10bf7 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -54,8 +54,8 @@ } tests_add_filter( 'muplugins_loaded', - function() use ( $_plugin_slug ) { - require_once dirname( dirname( __FILE__ ) ) . '/src/' . $_plugin_slug . '.php'; + function () use ( $_plugin_slug ) { + require_once dirname( __DIR__ ) . '/src/' . $_plugin_slug . '.php'; } ); diff --git a/tests/class-filesystem-utilitytest.php b/tests/class-filesystem-utilitytest.php index fca0d51..bc2b684 100644 --- a/tests/class-filesystem-utilitytest.php +++ b/tests/class-filesystem-utilitytest.php @@ -1,4 +1,25 @@ + */ + +/** + * Declare strict typing + * + * @see https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.strict + */ +declare( strict_types = 1 ); namespace Company\Plugins\PluginName\Tests; @@ -7,24 +28,22 @@ class WP_Filesystem_UtilityTest extends TestCase { - public function testGetBase64EncodedContents() { - $filesystemUtility = new WP_Filesystem_Utility(); + public function testGetBase64EncodedContents() { + $filesystemUtility = new WP_Filesystem_Utility(); - // Replace with the actual path to the SVG file you want to test - $svgFilePath = __DIR__ . '/../src/public/icons/cpt-icon.svg'; + // Replace with the actual path to the SVG file you want to test + $svgFilePath = __DIR__ . '/../src/public/icons/cpt-icon.svg'; - // Ensure that the file exists before testing - $this->assertFileExists($svgFilePath); + // Ensure that the file exists before testing + $this->assertFileExists( $svgFilePath ); - // Get the expected base64 encoded content - $expectedBase64Content = base64_encode(file_get_contents($svgFilePath)); + // Get the expected base64 encoded content + $expectedBase64Content = base64_encode( file_get_contents( $svgFilePath ) ); - // Call the method to get base64 encoded content - $actualBase64Content = $filesystemUtility->get_base64_encoded_contents($svgFilePath); + // Call the method to get base64 encoded content + $actualBase64Content = $filesystemUtility->get_base64_encoded_contents( $svgFilePath ); - // Check if the actual content matches the expected content - $this->assertEquals($expectedBase64Content, $actualBase64Content); - } - - // Add more test cases as needed for other methods/functions in WP_Filesystem_Utility + // Check if the actual content matches the expected content + $this->assertEquals( $expectedBase64Content, $actualBase64Content ); + } }