diff --git a/docs/traits/UopzFunctions.md b/docs/traits/UopzFunctions.md index 14e85ab40..93eec7aed 100644 --- a/docs/traits/UopzFunctions.md +++ b/docs/traits/UopzFunctions.md @@ -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` diff --git a/src/Traits/UopzFunctions.php b/src/Traits/UopzFunctions.php index b14f667a0..faa996f36 100644 --- a/src/Traits/UopzFunctions.php +++ b/src/Traits/UopzFunctions.php @@ -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) { diff --git a/tests/unit/lucatume/WPBrowser/Traits/UopzFunctionsTest.php b/tests/unit/lucatume/WPBrowser/Traits/UopzFunctionsTest.php index 6c58766c0..7b1c9e35a 100644 --- a/tests/unit/lucatume/WPBrowser/Traits/UopzFunctionsTest.php +++ b/tests/unit/lucatume/WPBrowser/Traits/UopzFunctionsTest.php @@ -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 * @@ -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 * @@ -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()); } /** @@ -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')); } }