Skip to content

Commit

Permalink
Setting up a fresh start for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freibergergarcia committed Feb 10, 2024
1 parent ac4c3fb commit f3f97b9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 106 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
13 changes: 0 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,19 @@
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>
20 changes: 1 addition & 19 deletions tests/unit/Admin/Admin_Menu_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}

/**
Expand All @@ -46,7 +29,6 @@ protected function setUp(): void {
* @since 0.1.0-alpha
*/
protected function tearDown(): void {
WP_Mock::tearDown();
parent::tearDown();
}
}
73 changes: 2 additions & 71 deletions tests/unit/Taxonomy/Taxonomy_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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' ) ) );
}
}
21 changes: 19 additions & 2 deletions tests/unit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php

// First we need to load the composer autoloader, so we can use WP Mock
require_once dirname( __DIR__ ) . '../../vendor/autoload.php';
if ( false === getenv( 'WP_TESTS_DIR' ) ) {
exit( 'WP_TESTS_DIR environment variable is not set. Please set it to the root of your WordPress installation.' );
}

define( 'TESTS_PLUGIN_DIR', dirname( __DIR__, 2 ) );

if ( ! file_exists( TESTS_PLUGIN_DIR . '/vendor/autoload.php' ) ) {
exit( 'The vendor directory is missing. Please run composer install.' );
}

require TESTS_PLUGIN_DIR . '/vendor/autoload.php';

$_test_root = getenv( 'WP_TESTS_DIR' );

if ( ! file_exists( $_test_root . '/includes/bootstrap.php' ) ) {
exit( 'Could not load /includes/bootstrap.php' );
}

require $_test_root . '/includes/bootstrap.php';

0 comments on commit f3f97b9

Please sign in to comment.