44 lines
1.3 KiB
Plaintext
44 lines
1.3 KiB
Plaintext
== 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.
|
|
|
|
|
|
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
|
|
|
|
[source,php]
|
|
----
|
|
public function testA() {
|
|
try {
|
|
assertTrue(getValue()); // Noncompliant
|
|
} catch (\PHPUnit\Framework\ExpectationFailedException $e) {
|
|
|
|
}
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[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[]
|