Skip to content

Commit a0dfe3b

Browse files
committed
Reimplement negated string matches format assertions which were removed in PHPUnit 12
1 parent 754423e commit a0dfe3b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/Codeception/Util/Shared/InheritedAsserts.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Codeception\PHPUnit\TestCase;
88
use PHPUnit\Framework\Assert;
99
use PHPUnit\Framework\Constraint\Constraint as PHPUnitConstraint;
10+
use PHPUnit\Framework\Constraint\LogicalNot;
11+
use PHPUnit\Framework\Constraint\StringMatchesFormatDescription;
1012

1113
trait InheritedAsserts
1214
{
@@ -1082,15 +1084,37 @@ protected function assertStringNotEqualsFileIgnoringCase(string $expectedFile, s
10821084
*/
10831085
protected function assertStringNotMatchesFormat(string $format, string $string, string $message = '')
10841086
{
1085-
Assert::assertStringNotMatchesFormat($format, $string, $message);
1087+
trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12', E_USER_DEPRECATED);
1088+
1089+
if (method_exists(Assert::class, 'assertStringNotMatchesFormat')) {
1090+
Assert::assertStringNotMatchesFormat($format, $string, $message);
1091+
} else {
1092+
$constraint = new LogicalNot(new StringMatchesFormatDescription($format));
1093+
1094+
Assert::assertThat($string, $constraint, $message);
1095+
}
10861096
}
10871097

10881098
/**
10891099
* Asserts that a string does not match a given format string.
10901100
*/
10911101
protected function assertStringNotMatchesFormatFile(string $formatFile, string $string, string $message = '')
10921102
{
1093-
Assert::assertStringNotMatchesFormatFile($formatFile, $string, $message);
1103+
trigger_error(__FUNCTION__ . ' was removed from PHPUnit since PHPUnit 12', E_USER_DEPRECATED);
1104+
1105+
if (method_exists(Assert::class, 'assertStringNotMatchesFormatFile')) {
1106+
Assert::assertStringNotMatchesFormatFile($formatFile, $string, $message);
1107+
} else {
1108+
Assert::assertFileExists($formatFile);
1109+
1110+
$constraint = new LogicalNot(
1111+
new StringMatchesFormatDescription(
1112+
file_get_contents($formatFile)
1113+
)
1114+
);
1115+
1116+
Assert::assertThat($string, $constraint, $message);
1117+
}
10941118
}
10951119

10961120
/**

0 commit comments

Comments
 (0)