Skip to content

Commit

Permalink
refactor(WPTestCase) update to support PHPUnit 10
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Jul 29, 2024
1 parent ed1ae51 commit cd94796
Show file tree
Hide file tree
Showing 10 changed files with 511 additions and 142 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"ext-curl": "*",
"ext-zip": "*",
"composer-runtime-api": "^2.2",
"phpunit/phpunit": "<=12.0.0",
"codeception/codeception": "^5.0",
"codeception/module-asserts": "^2.0 || ^3.0",
"codeception/module-phpbrowser": "^2.0 || ^3.0",
Expand Down
90 changes: 63 additions & 27 deletions src/TestCase/WPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use Codeception\Module;
use Codeception\Test\Unit;
use lucatume\WPBrowser\Module\WPQueries;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionProperty;
use WP_UnitTestCase;
use PHPUnit\Runner\Version as PHPUnitVersion;

/**
* @method static commit_transaction()
Expand Down Expand Up @@ -140,14 +142,15 @@ class WPTestCase extends Unit
];

/**
* Backup, and reset, static class attributes between tests.
* Backup, and reset, static class attributes between tests for PHPUnit < 10.0.0.
*
* @var bool
*/
protected $backupStaticAttributes = false;

/**
* A list of static attributes that should not be backed up as they are wired to explode when doing so.
* PHPUnit < 10.0.0.
*
* @var array<string,array<int,string>>
*/
Expand All @@ -170,17 +173,11 @@ class WPTestCase extends Unit
private ?float $requestTimeFloat = null;
private ?int $requestTime = null;

/**
* @param array<mixed> $data
* @param string $dataName
* @throws ReflectionException
*/
public function __construct(?string $name = null, array $data = [], $dataName = '')
private function initBackupGlobalsProperties():void
{
global $_wpTestsBackupGlobals,
$_wpTestsBackupGlobalsExcludeList,
$_wpTestsBackupStaticAttributes,
$_wpTestsBackupStaticAttributesExcludeList;
$_wpTestsBackupGlobalsExcludeList;
$phpunitVersion = (int)PHPUnitVersion::series();

$backupGlobalsReflectionProperty = new ReflectionProperty($this, 'backupGlobals');
$backupGlobalsReflectionProperty->setAccessible(true);
Expand All @@ -189,12 +186,13 @@ public function __construct(?string $name = null, array $data = [], $dataName =
$this->backupGlobals = $_wpTestsBackupGlobals;
}

if (property_exists($this, 'backupGlobalsExcludeList')) {
$backupGlobalsExcludeListReflectionProperty = new ReflectionProperty($this, 'backupGlobalsExcludeList');
} else {
if ($phpunitVersion < 9) {
// Older versions of PHPUnit.
$backupGlobalsExcludeListReflectionProperty = new ReflectionProperty($this, 'backupGlobalsBlacklist');
} else {
$backupGlobalsExcludeListReflectionProperty = new ReflectionProperty($this, 'backupGlobalsExcludeList');
}

$backupGlobalsExcludeListReflectionProperty->setAccessible(true);
$isDefinedInThis = $backupGlobalsExcludeListReflectionProperty->getDeclaringClass()
->getName() !== WPTestCase::class;
Expand All @@ -207,27 +205,27 @@ public function __construct(?string $name = null, array $data = [], $dataName =
$_wpTestsBackupGlobalsExcludeList
);
}
}

private function initBackupStaticPropertiesForPHPUnit(
string $backupStaticAttributesPropertyName,
string $backupStaticAttributesExcludeListPropertyName
): void {
global $_wpTestsBackupStaticAttributes,
$_wpTestsBackupStaticAttributesExcludeList;

$backupStaticAttributesReflectionProperty = new ReflectionProperty($this, 'backupStaticAttributes');
$backupStaticAttributesReflectionProperty = new ReflectionProperty($this, $backupStaticAttributesPropertyName);
$backupStaticAttributesReflectionProperty->setAccessible(true);
$isDefinedInThis = $backupStaticAttributesReflectionProperty->getDeclaringClass()
->getName() !== WPTestCase::class;
if (!$isDefinedInThis && isset($_wpTestsBackupStaticAttributes) && is_bool($_wpTestsBackupStaticAttributes)) {
$this->backupStaticAttributes = $_wpTestsBackupStaticAttributes;
}

if (property_exists($this, 'backupStaticAttributesExcludeList')) {
$backupStaticAttributesExcludeListReflectionProperty = new ReflectionProperty(
$this,
'backupStaticAttributesExcludeList'
);
} else {
// Older versions of PHPUnit.
$backupStaticAttributesExcludeListReflectionProperty = new ReflectionProperty(
$this,
'backupStaticAttributesBlacklist'
);
}
$backupStaticAttributesExcludeListReflectionProperty = new ReflectionProperty(
$this,
$backupStaticAttributesExcludeListPropertyName
);
$backupStaticAttributesExcludeListReflectionProperty->setAccessible(true);
$isDefinedInThis = $backupStaticAttributesExcludeListReflectionProperty->getDeclaringClass()
->getName() !== WPTestCase::class;
Expand All @@ -240,6 +238,44 @@ public function __construct(?string $name = null, array $data = [], $dataName =
$_wpTestsBackupStaticAttributesExcludeList
);
}
}

private function initBackupStaticPropertiesForPHPUnitGte10(): void
{
global $_wpTestsBackupStaticAttributes,
$_wpTestsBackupStaticAttributesExcludeList;

$backupStaticProperties = property_exists($this, 'backupStaticProperties') ?
$this->backupStaticProperties :
$_wpTestsBackupStaticAttributes;
// @phpstan-ignore-next-line exists in PHPUnit >= 10.0.0
$this->setBackupStaticProperties($backupStaticProperties);

$backupStaticPropertiesExcludeList = property_exists($this, 'backupStaticPropertiesExcludeList') ?
$this->backupStaticPropertiesExcludeList :
array_merge($this->backupStaticAttributesExcludeList, $_wpTestsBackupStaticAttributesExcludeList);
// @phpstan-ignore-next-line exists in PHPUnit >= 10.0.0
$this->setBackupStaticPropertiesExcludeList($backupStaticPropertiesExcludeList);
}

/**
* @param array<mixed> $data
* @param string $dataName
* @throws ReflectionException
*/
public function __construct(?string $name = null, array $data = [], $dataName = '')
{
$this->initBackupGlobalsProperties();

$phpunitVersion = (int)PHPUnitVersion::series();

if ($phpunitVersion < 9) {
$this->initBackupStaticPropertiesForPHPUnit('backupStaticAttributes', 'backupStaticAttributesBlacklist');
} elseif ($phpunitVersion === 9) {
$this->initBackupStaticPropertiesForPHPUnit('backupStaticAttributes', 'backupStaticAttributesExcludeList');
} else {
$this->initBackupStaticPropertiesForPHPUnitGte10();
}

parent::__construct($name ?: 'testMethod', $data, $dataName);
}
Expand Down Expand Up @@ -376,7 +412,7 @@ private function isCoreTestCaseProperty(string $name): bool
if ($this->coreTestCaseProperties === null) {
$this->coreTestCaseProperties = array_map(
static fn(ReflectionProperty $p) => $p->getName(),
(new \ReflectionClass(self::getCoreTestCase()))->getProperties()
(new ReflectionClass(self::getCoreTestCase()))->getProperties()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use lucatume\WPBrowser\TestCase\WPTestCase;

class BackupControlTestCaseOverridingStore
{
public static $staticAttribute = 'initial_value';
}

class BackupControlTestCaseOverridingTestCase extends WPTestCase
{
protected $backupGlobals = false;
protected $backupStaticProperties = false;

/**
* Override the method to avoid issues with Codeception specific meta data.
*/
protected function _setUp()
{
$this->_before();
}

public function testBackupGlobalsIsFalse(): void
{
$this->assertFalse($this->backupGlobals);
}

public function testWillAlterStoreStaticAttribute(): void
{
BackupControlTestCaseOverridingStore::$staticAttribute = 'updated_value';
$this->assertTrue(true); // Useless assertion to avoid the test to be marked as risky. }
}
}
Loading

0 comments on commit cd94796

Please sign in to comment.