Skip to content

Commit

Permalink
update the phpunit configuration file and adjust the tests environmen…
Browse files Browse the repository at this point in the history
…t to be more dynamic
  • Loading branch information
wordpressfan committed Jan 23, 2025
1 parent 2e9685d commit bb9461c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
27 changes: 25 additions & 2 deletions Tests/Unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,28 @@
define( 'IMAGIFY_PLUGIN_ROOT', dirname( dirname( __DIR__ ) ) . DIRECTORY_SEPARATOR );
define( 'IMAGIFY_PLUGIN_TESTS_FIXTURES_DIR', dirname( __DIR__ ) . '/Fixtures' );

require_once IMAGIFY_PLUGIN_ROOT . 'inc/functions/api.php';
include_once IMAGIFY_PLUGIN_TESTS_FIXTURES_DIR . '/WP/class-wp-error.php';
/**
* The original files need to loaded into memory before we mock them with Patchwork. Add files here before the unit
* tests start.
*
* @since 3.5
*/
function load_original_files_before_mocking() {
$originals = [
'inc/functions/api.php',
'inc/functions/attachments.php',
'inc/functions/common.php',
];
foreach ( $originals as $file ) {
require_once IMAGIFY_PLUGIN_ROOT . $file;
}

$fixtures = [
'/WP/class-wp-error.php',
];
foreach ( $fixtures as $file ) {
require_once IMAGIFY_PLUGIN_TESTS_FIXTURES_DIR . $file;
}
}

load_original_files_before_mocking();
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
use wpdb;

/**
* Test class covering inc/functions/attachments::imagify_has_attachments_without_required_metadata
*
* @covers imagify_has_attachments_without_required_metadata
*/
class Test_ImagifyAttachmentMetadata extends TestCase {
class Test_ImagifyHasAttachmentsWithoutRequiredMetadata extends TestCase {

private $wpdb;

Expand All @@ -25,9 +26,6 @@ public function setUp(): void {
$GLOBALS['wpdb'] = $this->wpdb = new wpdb( 'dbuser', 'dbpassword', 'dbname', 'dbhost' );

$this->wpdb->posts = 'wp_posts';

require_once IMAGIFY_PLUGIN_ROOT . 'inc/functions/attachments.php';
require_once IMAGIFY_PLUGIN_ROOT . 'inc/functions/common.php';
}


Expand All @@ -40,8 +38,7 @@ protected function tearDown(): void {
/**
* @dataProvider configTestData
*/
public function testShouldReturnExpected( $config, $expected )
{
public function testShouldReturnExpected( $config, $expected ) {
$this->wpdb->set_var( $expected );

if( $expected ) {
Expand Down
7 changes: 0 additions & 7 deletions Tests/Unit/inc/functions/imagifySanitizeContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@
use Imagify\Tests\Unit\TestCase;

class Test_ImagifySanitizeContext extends TestCase {

protected function setUp(): void {
parent::setUp();

require_once IMAGIFY_PLUGIN_ROOT . 'inc/functions/common.php';
}

/**
* Test should return sanitized key.
*/
Expand Down
38 changes: 12 additions & 26 deletions Tests/Unit/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
bootstrap="init-tests.php"
backupGlobals="false"
colors="true"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true">

<testsuites>
<testsuite name="unit">
<directory suffix=".php">inc</directory>
<directory suffix=".php">classes</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../../inc</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="init-tests.php" backupGlobals="false" colors="true" beStrictAboutCoversAnnotation="false" beStrictAboutOutputDuringTests="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutTodoAnnotatedTests="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" verbose="true">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">../../inc</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory suffix=".php">inc</directory>
<directory suffix=".php">classes</directory>
</testsuite>
</testsuites>
</phpunit>

0 comments on commit bb9461c

Please sign in to comment.