rspec/rules/S5779/php/rule.adoc

44 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
PHPUnit assertions do throw a _PHPUnit\Framework\ExpectationFailedException_ exception when they fail. This is how PHPUnit internally notices when assertions within testcases fail. However, if such an exception type or one of its parent types is captured within a try-catch block and not rethrown, PHPUnit does not notice the assertion failure.
2021-02-02 15:02:10 +01:00
This check raises an issue on assertions within the _try_ body of a _try-catch_ block that do catch exceptions of the type _PHPUnit\Framework\ExpectationFailedException_, _PHPUnit\Framework\AssertionFailedError_, or _Exception_, and do not handle the variable holding the exception.
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,php]
----
public function testA() {
try {
assertTrue(getValue()); // Noncompliant
} catch (\PHPUnit\Framework\ExpectationFailedException $e) {
}
}
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[source,php]
----
public function testB() {
try {
assertTrue(getValue()); // Compliant
} catch (\PHPUnit\Framework\ExpectationFailedException $e) {
assertEquals("Some message", $e->getMessage());
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../highlighting.adoc[]
endif::env-github,rspecator-view[]