Skip to content

Commit

Permalink
fix(WPTestCase) add missing assertions, handle uploads removal
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Jan 30, 2024
1 parent a54a40f commit f7c94cf
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
},
"files": [
"src/version-4-aliases.php",
"src/Deprecated/deprecated-functions.php",
"src/deprecated-functions.php",
"src/functions.php"
]
},
Expand Down
33 changes: 33 additions & 0 deletions src/TestCase/WPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace lucatume\WPBrowser\TestCase;

use AllowDynamicProperties;
use Codeception\Actor;
use Codeception\Test\Unit;
use lucatume\WPBrowser\Module\WPQueries;
Expand All @@ -10,10 +11,16 @@
use ReflectionProperty;
use WP_UnitTestCase;

#[AllowDynamicProperties]
class WPTestCase extends Unit
{
use WPTestCasePHPUnitMethodsTrait;

/**
* @var string[]|null
*/
private array|null $coreTestCaseProperties = null;

protected Actor $tester;

// Backup, and reset, globals between tests.
Expand Down Expand Up @@ -232,11 +239,27 @@ protected function queries(): WPQueries
return $wpQueries;
}

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()
);
}

return in_array($name, $this->coreTestCaseProperties, true);
}

/**
* @throws ReflectionException
*/
public function __get(string $name): mixed
{
if (!$this->isCoreTestCaseProperty($name)) {
return $this->{$name} ?? null;
}

$coreTestCase = self::getCoreTestCase();
$reflectionProperty = new ReflectionProperty($coreTestCase, $name);
$reflectionProperty->setAccessible(true);
Expand All @@ -248,6 +271,12 @@ public function __get(string $name): mixed
*/
public function __set(string $name, mixed $value): void
{
if (!$this->isCoreTestCaseProperty($name)) {
// Just set a dynamic property on the test case.
$this->{$name} = $value;
return;
}

$coreTestCase = self::getCoreTestCase();
$reflectionProperty = new ReflectionProperty($coreTestCase, $name);
$reflectionProperty->setAccessible(true);
Expand All @@ -259,6 +288,10 @@ public function __set(string $name, mixed $value): void
*/
public function __isset(string $name): bool
{
if (!$this->isCoreTestCaseProperty($name)) {
return isset($this->{$name});
}

$coreTestCase = self::getCoreTestCase();
$reflectionProperty = new ReflectionProperty($coreTestCase, $name);
$reflectionProperty->setAccessible(true);
Expand Down
18 changes: 18 additions & 0 deletions src/TestCase/WPTestCasePHPUnitMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public static function setUpBeforeClass()
protected function setUp()
{
parent::setUp();

// Restores the uploads directory if removed during tests.
$uploads = wp_upload_dir();
if (!is_dir($uploads['basedir'])
&& !mkdir($uploads['basedir'], 0755, true)
&& !is_dir($uploads['basedir'])) {
throw new \RuntimeException('Failed to create uploads base directory.');
}

$this->set_up(); //@phpstan-ignore-line magic __call
$this->backupAdditionalGlobals();
}
Expand Down Expand Up @@ -46,6 +55,15 @@ public static function setUpBeforeClass(): void
protected function setUp(): void
{
parent::setUp();

// Restores the uploads directory if removed during tests.
$uploads = wp_upload_dir();
if (!is_dir($uploads['basedir'])
&& !mkdir($uploads['basedir'], 0755, true)
&& !is_dir($uploads['basedir'])) {
throw new \RuntimeException('Failed to create uploads base directory.');
}

$this->set_up(); //@phpstan-ignore-line magic __call
$this->backupAdditionalGlobals();
}
Expand Down
15 changes: 13 additions & 2 deletions src/TestCase/WPUnitTestCasePolyfillsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@

trait WPUnitTestCasePolyfillsTrait
{
protected function assertIsArray($actual, string $message = ''): void
/**
* @param array<mixed> $arguments
*/
public function __call(string $name, array $arguments)
{
TestCase::assertIsArray($actual, $message);
return TestCase::$name(...$arguments);
}

/**
* @param array<mixed> $arguments
*/
public static function __callStatic(string $name, array $arguments)
{
return TestCase::$name(...$arguments);
}
}
File renamed without changes.
39 changes: 39 additions & 0 deletions tests/wploadersuite/DynamicPropertyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use lucatume\WPBrowser\TestCase\WPTestCase;

/**
* Class DynamicPropertyTest.
*
* @since TBD
*
* @package wploadersuite;
*/
class DynamicPropertyTest extends WPTestCase
{
/**
* It should allow setting and getting a dynamic property on the test case
*
* @test
*/
public function should_allow_setting_and_getting_a_dynamic_property_on_the_test_case(): void
{
$this->assertFalse(isset($this->testDynamicProperty));
$this->assertNull($this->testDynamicProperty);

$this->testDynamicProperty = 23;

$this->assertTrue(isset($this->testDynamicProperty));
$this->assertEquals(23,$this->testDynamicProperty);

$this->testDynamicProperty = 89;

$this->assertTrue(isset($this->testDynamicProperty));
$this->assertEquals(89,$this->testDynamicProperty);

unset($this->testDynamicProperty);

$this->assertFalse(isset($this->testDynamicProperty));
$this->assertNull($this->testDynamicProperty);
}
}

0 comments on commit f7c94cf

Please sign in to comment.