From 20774a2c25d608a55a262540cf7a299ab18877fc Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 25 Jun 2025 14:21:27 +0300 Subject: [PATCH] Solve the risky test warnings in PHPUnit --- tests/Hamcrest/AbstractMatcherTest.php | 22 +++++++++++++++++++ tests/Hamcrest/Core/CombinableMatcherTest.php | 2 ++ tests/Hamcrest/Core/IsInstanceOfTest.php | 2 ++ tests/Hamcrest/Core/SetTest.php | 2 ++ tests/Hamcrest/FeatureMatcherTest.php | 2 ++ .../Text/IsEqualIgnoringWhiteSpaceTest.php | 2 ++ .../Text/StringContainsIgnoringCaseTest.php | 2 ++ .../Text/StringContainsInOrderTest.php | 2 ++ tests/Hamcrest/Text/StringContainsTest.php | 2 ++ tests/Hamcrest/Text/StringEndsWithTest.php | 2 ++ tests/Hamcrest/Text/StringStartsWithTest.php | 2 ++ tests/Hamcrest/UtilTest.php | 3 +++ 12 files changed, 45 insertions(+) diff --git a/tests/Hamcrest/AbstractMatcherTest.php b/tests/Hamcrest/AbstractMatcherTest.php index 8a1fb2a95..952647824 100644 --- a/tests/Hamcrest/AbstractMatcherTest.php +++ b/tests/Hamcrest/AbstractMatcherTest.php @@ -12,6 +12,14 @@ abstract class AbstractMatcherTest extends TestCase const ARGUMENT_IGNORED = "ignored"; const ANY_NON_NULL_ARGUMENT = "notnull"; + /** + * @before + */ + protected function setUpTest() + { + MatcherAssert::resetCount(); + } + abstract protected function createMatcher(); public function assertMatches(\Hamcrest\Matcher $matcher, $arg, $message) @@ -46,6 +54,9 @@ public function assertMismatchDescription($expected, \Hamcrest\Matcher $matcher, ); } + /** + * @doesNotPerformAssertions + */ public function testIsNullSafe() { //Should not generate any notices @@ -56,6 +67,9 @@ public function testIsNullSafe() ); } + /** + * @doesNotPerformAssertions + */ public function testCopesWithUnknownTypes() { //Should not generate any notices @@ -65,4 +79,12 @@ public function testCopesWithUnknownTypes() new NullDescription() ); } + + /** + * @after + */ + protected function tearDownTest() + { + $this->addToAssertionCount(MatcherAssert::getCount()); + } } diff --git a/tests/Hamcrest/Core/CombinableMatcherTest.php b/tests/Hamcrest/Core/CombinableMatcherTest.php index ef9418674..4f4fe7d0a 100644 --- a/tests/Hamcrest/Core/CombinableMatcherTest.php +++ b/tests/Hamcrest/Core/CombinableMatcherTest.php @@ -12,6 +12,8 @@ class CombinableMatcherTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_either_3_or_4 = \Hamcrest\Core\CombinableMatcher::either(equalTo(3))->orElse(equalTo(4)); $this->_not_3_and_not_4 = \Hamcrest\Core\CombinableMatcher::both(not(equalTo(3)))->andAlso(not(equalTo(4))); } diff --git a/tests/Hamcrest/Core/IsInstanceOfTest.php b/tests/Hamcrest/Core/IsInstanceOfTest.php index b4fdd04fd..795b43012 100644 --- a/tests/Hamcrest/Core/IsInstanceOfTest.php +++ b/tests/Hamcrest/Core/IsInstanceOfTest.php @@ -12,6 +12,8 @@ class IsInstanceOfTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_baseClassInstance = new \Hamcrest\Core\SampleBaseClass('good'); $this->_subClassInstance = new \Hamcrest\Core\SampleSubClass('good'); } diff --git a/tests/Hamcrest/Core/SetTest.php b/tests/Hamcrest/Core/SetTest.php index f00efa4f4..fe0a23708 100644 --- a/tests/Hamcrest/Core/SetTest.php +++ b/tests/Hamcrest/Core/SetTest.php @@ -12,6 +12,8 @@ class SetTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + self::$_classProperty = null; unset($this->_instanceProperty); } diff --git a/tests/Hamcrest/FeatureMatcherTest.php b/tests/Hamcrest/FeatureMatcherTest.php index dfae872bf..4e6e40c37 100644 --- a/tests/Hamcrest/FeatureMatcherTest.php +++ b/tests/Hamcrest/FeatureMatcherTest.php @@ -39,6 +39,8 @@ class FeatureMatcherTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_resultMatcher = $this->_resultMatcher(); } diff --git a/tests/Hamcrest/Text/IsEqualIgnoringWhiteSpaceTest.php b/tests/Hamcrest/Text/IsEqualIgnoringWhiteSpaceTest.php index 42444489f..b21e9336b 100644 --- a/tests/Hamcrest/Text/IsEqualIgnoringWhiteSpaceTest.php +++ b/tests/Hamcrest/Text/IsEqualIgnoringWhiteSpaceTest.php @@ -11,6 +11,8 @@ class IsEqualIgnoringWhiteSpaceTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_matcher = \Hamcrest\Text\IsEqualIgnoringWhiteSpace::equalToIgnoringWhiteSpace( "Hello World how\n are we? " ); diff --git a/tests/Hamcrest/Text/StringContainsIgnoringCaseTest.php b/tests/Hamcrest/Text/StringContainsIgnoringCaseTest.php index 8b4463a9e..6950697dc 100644 --- a/tests/Hamcrest/Text/StringContainsIgnoringCaseTest.php +++ b/tests/Hamcrest/Text/StringContainsIgnoringCaseTest.php @@ -13,6 +13,8 @@ class StringContainsIgnoringCaseTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_stringContains = \Hamcrest\Text\StringContainsIgnoringCase::containsStringIgnoringCase( strtolower(self::EXCERPT) ); diff --git a/tests/Hamcrest/Text/StringContainsInOrderTest.php b/tests/Hamcrest/Text/StringContainsInOrderTest.php index adb8658f4..9aee16d14 100644 --- a/tests/Hamcrest/Text/StringContainsInOrderTest.php +++ b/tests/Hamcrest/Text/StringContainsInOrderTest.php @@ -11,6 +11,8 @@ class StringContainsInOrderTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_m = \Hamcrest\Text\StringContainsInOrder::stringContainsInOrder(array('a', 'b', 'c')); } diff --git a/tests/Hamcrest/Text/StringContainsTest.php b/tests/Hamcrest/Text/StringContainsTest.php index 814c1ca1b..70e1f0ce0 100644 --- a/tests/Hamcrest/Text/StringContainsTest.php +++ b/tests/Hamcrest/Text/StringContainsTest.php @@ -13,6 +13,8 @@ class StringContainsTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_stringContains = \Hamcrest\Text\StringContains::containsString(self::EXCERPT); } diff --git a/tests/Hamcrest/Text/StringEndsWithTest.php b/tests/Hamcrest/Text/StringEndsWithTest.php index 46177f464..f1b447fca 100644 --- a/tests/Hamcrest/Text/StringEndsWithTest.php +++ b/tests/Hamcrest/Text/StringEndsWithTest.php @@ -13,6 +13,8 @@ class StringEndsWithTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_stringEndsWith = \Hamcrest\Text\StringEndsWith::endsWith(self::EXCERPT); } diff --git a/tests/Hamcrest/Text/StringStartsWithTest.php b/tests/Hamcrest/Text/StringStartsWithTest.php index f0eb14b7e..cf98be1fd 100644 --- a/tests/Hamcrest/Text/StringStartsWithTest.php +++ b/tests/Hamcrest/Text/StringStartsWithTest.php @@ -13,6 +13,8 @@ class StringStartsWithTest extends \Hamcrest\AbstractMatcherTest */ protected function setUpTest() { + parent::setUpTest(); + $this->_stringStartsWith = \Hamcrest\Text\StringStartsWith::startsWith(self::EXCERPT); } diff --git a/tests/Hamcrest/UtilTest.php b/tests/Hamcrest/UtilTest.php index 1fec63eed..123599c48 100644 --- a/tests/Hamcrest/UtilTest.php +++ b/tests/Hamcrest/UtilTest.php @@ -20,6 +20,9 @@ public function testWrapValueWithIsEqualWrapsPrimitive() $this->assertTrue($matcher->matches('foo')); } + /** + * @doesNotPerformAssertions + */ public function testCheckAllAreMatchersAcceptsMatchers() { \Hamcrest\Util::checkAllAreMatchers(array(