Skip to content

Commit

Permalink
fix(UopzFunctions) correctly reset method return values
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Mar 13, 2024
1 parent 6be1431 commit 28b83aa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
9 changes: 9 additions & 0 deletions docs/traits/UopzFunctions.md
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,15 @@ class MyTest extends WPTestCase
}
```

#### resetObjectProperty

`resetObjectProperty(string|object $classOrObject, string $property): void`

Reset the property `$property` of the class `$class` or object `$object` to its original value.

You do not need to reset the property of an object that was set with `setObjectProperty` explicitly: the trait will take
care of cleaning up all the modifications made to the functions, methods and class attributes after each test.

#### getMethodStaticVariables

`getMethodStaticVariables(string $class, string $method): array`
Expand Down
6 changes: 5 additions & 1 deletion src/Traits/UopzFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,11 @@ protected function allowExit(): void
public function resetUopzAlterations(): void
{
foreach (self::$uopzSetFunctionReturns as $function => $k) {
$this->unsetFunctionReturn($function);
if (str_contains($function, '::')) {
$this->unsetMethodReturn(...explode('::', $function));
} else {
$this->unsetFunctionReturn($function);
}
}

foreach (self::$uopzSetFunctionHooks as $function => $k) {
Expand Down
38 changes: 35 additions & 3 deletions tests/unit/lucatume/WPBrowser/Traits/UopzFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ public function should_allow_setting_an_instance_method_return_value(): void
$this->assertEquals(89, $namespacedClass->getValueThree());
}

/**
* It should reset instance method return between tests
*
* @test
*/
public function should_reset_instance_method_return_between_tests(): void
{
$globalClass = new SomeGlobalClassOne();
$namespacedClass = new SomeNamespacedClassOne();
$this->assertEquals('original-value-one', $globalClass->getValueOne());
$this->assertEquals('original-value-two', $globalClass->getValueTwo());
$this->assertEquals('original-value-three', $globalClass->getValueThree());
$this->assertEquals('original-value-one', $namespacedClass->getValueOne());
$this->assertEquals('original-value-two', $namespacedClass->getValueTwo());
$this->assertEquals('original-value-three', $namespacedClass->getValueThree());
}

/**
* It should allow setting a static method return value
*
Expand All @@ -220,6 +237,21 @@ public function should_allow_setting_a_static_method_return_value(): void
$this->assertEquals(89, SomeNamespacedClassOne::getStaticValueThree());
}

/**
* It should reset static method return value between tests
*
* @test
*/
public function should_reset_static_method_return_value_between_tests(): void
{
$this->assertEquals('original-static-value-one', SomeGlobalClassOne::getStaticValueOne());
$this->assertEquals('original-static-value-two', SomeGlobalClassOne::getStaticValueTwo());
$this->assertEquals('original-static-value-three', SomeGlobalClassOne::getStaticValueThree());
$this->assertEquals('original-static-value-one', SomeNamespacedClassOne::getStaticValueOne());
$this->assertEquals('original-static-value-two', SomeNamespacedClassOne::getStaticValueTwo());
$this->assertEquals('original-static-value-three', SomeNamespacedClassOne::getStaticValueThree());
}

/**
* It should allow unsetting a set method return value
*
Expand Down Expand Up @@ -1223,12 +1255,12 @@ public function should_allow_preventing_exit(): void
{
$this->preventExit();

$this->assertEquals(1,ini_get('uopz.exit'));
$this->assertEquals(1, ini_get('uopz.exit'));
ob_start();
echo "Print this and die\n";
die();

$this->assertEquals("Print this and die\n",ob_get_clean());
$this->assertEquals("Print this and die\n", ob_get_clean());
}

/**
Expand All @@ -1248,6 +1280,6 @@ public function should_not_throw_if_trying_to_allow_exit_when_exit_not_prevented
*/
public function should_restore_exit_between_tests(): void
{
$this->assertEquals(1,ini_get('uopz.exit'));
$this->assertEquals(1, ini_get('uopz.exit'));
}
}

0 comments on commit 28b83aa

Please sign in to comment.