From 818c276c803f848610a2f7712e274980f3b54085 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Tue, 4 Jun 2024 09:59:47 +0000 Subject: [PATCH] Better messages for "JSON node should (not) contain" --- src/Context/JsonContext.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Context/JsonContext.php b/src/Context/JsonContext.php index 91ba055e..704ef97a 100644 --- a/src/Context/JsonContext.php +++ b/src/Context/JsonContext.php @@ -226,7 +226,13 @@ public function theJsonNodeShouldContain($node, $text): void $actual = $this->inspector->evaluate($json, $node); - $this->assertContains($text, (string) $actual); + $printedActual = \strlen($actual) > 50 ? substr($actual, 0, 50).'[…]' : $actual; + + $this->assertContains( + $text, + (string) $actual, + 'The node "'.$node.'" does not contain the expected text "'.$text.'" (found "'.$printedActual.'")' + ); } /** @@ -252,7 +258,13 @@ public function theJsonNodeShouldNotContain($node, $text): void $actual = $this->inspector->evaluate($json, $node); - $this->assertNotContains($text, (string) $actual); + $printedActual = \strlen($actual) > 50 ? substr($actual, 0, 50).'[…]' : $actual; + + $this->assertNotContains( + $text, + (string) $actual, + 'The node "'.$node.'" does contain "'.$text.'" while it should not (found "'.$printedActual.'")' + ); } /**