From f3f97b944d4581140ce1266c0f981248e1410085 Mon Sep 17 00:00:00 2001 From: Bruno FG Date: Sat, 10 Feb 2024 22:56:47 +1100 Subject: [PATCH] Setting up a fresh start for unit tests --- package.json | 3 +- phpunit.xml.dist | 13 ----- tests/unit/Admin/Admin_Menu_Test.php | 20 +------- tests/unit/Taxonomy/Taxonomy_Test.php | 73 +-------------------------- tests/unit/bootstrap.php | 21 +++++++- 5 files changed, 24 insertions(+), 106 deletions(-) diff --git a/package.json b/package.json index 46f9ab7..132176e 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "build": "NODE_ENV=production npx postcss assets/css/general.css -o assets/css/public/general.css", "start-env": "wp-env start", "stop-env": "wp-env stop", - "test-php": "wp-env run tests-cli --env-cwd=wp-content/plugins/$(basename $(pwd)) vendor/bin/phpunit -c phpunit.xml.dist --verbose", + "composer-install": "wp-env run cli --env-cwd=wp-content/plugins/$(basename $(pwd)) composer install", + "test-php": "wp-env run tests-cli phpunit --env-cwd=wp-content/plugins/$(basename $(pwd)) --verbose", "wp-env": "wp-env" } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cb698d1..99c2d15 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,32 +7,19 @@ beStrictAboutOutputDuringTests="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"> - - ./includes - ./vendor - - - - - ./tests/unit - - - - - diff --git a/tests/unit/Admin/Admin_Menu_Test.php b/tests/unit/Admin/Admin_Menu_Test.php index 384ee86..6f77819 100644 --- a/tests/unit/Admin/Admin_Menu_Test.php +++ b/tests/unit/Admin/Admin_Menu_Test.php @@ -2,12 +2,9 @@ declare(strict_types=1); -namespace Custom_PTT\Tests\Unit\Unit\Admin; +namespace Custom_PTT\Tests\Unit\Admin; use PHPUnit\Framework\TestCase; -use Custom_PTT\Admin\Admin_Menu; -use WP_Mock; -use Mockery; /** * Class Admin_Menu_Test @@ -24,20 +21,6 @@ class Admin_Menu_Test extends TestCase { */ protected function setUp(): void { parent::setUp(); - WP_Mock::setUp(); - - // Mock the admin_url function - WP_Mock::userFunction( - 'admin_url', - array( - 'return' => 'http://example.com/wp-admin/', - ) - ); - - // Mock the Taxonomy_List_Table class and its methods - $mock = Mockery::mock( 'overload:Custom_PTT\Admin\Taxonomy_List_Table' ); - $mock->shouldReceive( 'prepare_items' )->once(); - $mock->shouldReceive( 'display' )->once(); } /** @@ -46,7 +29,6 @@ protected function setUp(): void { * @since 0.1.0-alpha */ protected function tearDown(): void { - WP_Mock::tearDown(); parent::tearDown(); } } diff --git a/tests/unit/Taxonomy/Taxonomy_Test.php b/tests/unit/Taxonomy/Taxonomy_Test.php index febf6ad..f7dc4fa 100644 --- a/tests/unit/Taxonomy/Taxonomy_Test.php +++ b/tests/unit/Taxonomy/Taxonomy_Test.php @@ -2,11 +2,10 @@ declare( strict_types=1 ); -namespace Custom_PTT\Tests\Unit\Unit\Taxonomy; +namespace Custom_PTT\Tests\Unit\Taxonomy; use PHPUnit\Framework\TestCase; use Custom_PTT\Taxonomy\Taxonomy; -use WP_Mock; /** * Taxonomy Test. @@ -33,77 +32,9 @@ public static function setUpBeforeClass(): void { * @since 0.1.0-alpha */ public function test_register(): void { - WP_Mock::userFunction( - 'add_action', - array( - 'times' => 1, - 'args' => array( 'init', array( WP_Mock\Functions::type( 'object' ), 'register_taxonomy_on_init' ) ), - ) - ); - $taxonomy = new Taxonomy(); $taxonomy->register(); - // Add an assertion to the test - $this->assertTrue( true ); - } - - /** - * Test the register_taxonomy_on_init method. - * - * @since 0.1.0-alpha - */ - public function test_register_taxonomy_on_init(): void { - WP_Mock::userFunction( - 'get_option', - array( - 'return' => array( - 'custom_taxonomy' => array( - 'plural_label' => 'Custom Taxonomies', - 'singular_label' => 'Custom Taxonomy', - 'post_type' => 'post', - ), - ), - ) - ); - - WP_Mock::userFunction( - 'wp_parse_args', - array( - 'return_arg' => 1, - ) - ); - - WP_Mock::userFunction( - 'register_taxonomy', - array( - 'times' => 1, - 'args' => array( 'custom_taxonomy', 'post', WP_Mock\Functions::type( 'array' ) ), - 'return' => true, - ) - ); - - WP_Mock::userFunction( - 'apply_filters', - array( - 'times' => 1, - 'args' => array( 'custom_ptt_taxonomy_args', WP_Mock\Functions::type( 'array' ), 'custom_taxonomy', WP_Mock\Functions::type( 'array' ) ), - 'return_arg' => 1, - ) - ); - - WP_Mock::userFunction( - 'do_action', - array( - 'times' => 1, - 'args' => array( 'custom_ptt_registered_taxonomies', WP_Mock\Functions::type( 'array' ) ), - ) - ); - - $taxonomy = new Taxonomy(); - $taxonomy->register_taxonomy_on_init(); - - // Add an assertion to the test - $this->assertTrue( true ); + $this->assertEquals( 10, has_action( 'init', array( $taxonomy, 'register_taxonomy_on_init' ) ) ); } } diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 29f742d..425f2c9 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -1,4 +1,21 @@