Skip to content

Commit 58a6770

Browse files
ar2rArtur Khasanovtaylorotwell
authored
[9.x] Testing methods. Making error messages with json_encode more readable (#44397)
* Added JSON_UNESCAPED_UNICODE in json_encode to make error messages mo readable. * Removed redundant spaces * formatting Co-authored-by: Artur Khasanov <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent d8eb542 commit 58a6770

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

src/Illuminate/Testing/AssertableJsonString.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ public function assertExact(array $data)
113113
*/
114114
public function assertSimilar(array $data)
115115
{
116-
$actual = json_encode(Arr::sortRecursive(
117-
(array) $this->decoded
118-
));
116+
$actual = json_encode(
117+
Arr::sortRecursive((array) $this->decoded),
118+
JSON_UNESCAPED_UNICODE
119+
);
119120

120-
PHPUnit::assertEquals(json_encode(Arr::sortRecursive($data)), $actual);
121+
PHPUnit::assertEquals(json_encode(Arr::sortRecursive($data), JSON_UNESCAPED_UNICODE), $actual);
121122

122123
return $this;
123124
}
@@ -130,17 +131,18 @@ public function assertSimilar(array $data)
130131
*/
131132
public function assertFragment(array $data)
132133
{
133-
$actual = json_encode(Arr::sortRecursive(
134-
(array) $this->decoded
135-
));
134+
$actual = json_encode(
135+
Arr::sortRecursive((array) $this->decoded),
136+
JSON_UNESCAPED_UNICODE
137+
);
136138

137139
foreach (Arr::sortRecursive($data) as $key => $value) {
138140
$expected = $this->jsonSearchStrings($key, $value);
139141

140142
PHPUnit::assertTrue(
141143
Str::contains($actual, $expected),
142144
'Unable to find JSON fragment: '.PHP_EOL.PHP_EOL.
143-
'['.json_encode([$key => $value]).']'.PHP_EOL.PHP_EOL.
145+
'['.json_encode([$key => $value], JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL.
144146
'within'.PHP_EOL.PHP_EOL.
145147
"[{$actual}]."
146148
);
@@ -162,17 +164,18 @@ public function assertMissing(array $data, $exact = false)
162164
return $this->assertMissingExact($data);
163165
}
164166

165-
$actual = json_encode(Arr::sortRecursive(
166-
(array) $this->decoded
167-
));
167+
$actual = json_encode(
168+
Arr::sortRecursive((array) $this->decoded),
169+
JSON_UNESCAPED_UNICODE
170+
);
168171

169172
foreach (Arr::sortRecursive($data) as $key => $value) {
170173
$unexpected = $this->jsonSearchStrings($key, $value);
171174

172175
PHPUnit::assertFalse(
173176
Str::contains($actual, $unexpected),
174177
'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL.
175-
'['.json_encode([$key => $value]).']'.PHP_EOL.PHP_EOL.
178+
'['.json_encode([$key => $value], JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL.
176179
'within'.PHP_EOL.PHP_EOL.
177180
"[{$actual}]."
178181
);
@@ -189,9 +192,10 @@ public function assertMissing(array $data, $exact = false)
189192
*/
190193
public function assertMissingExact(array $data)
191194
{
192-
$actual = json_encode(Arr::sortRecursive(
193-
(array) $this->decoded
194-
));
195+
$actual = json_encode(
196+
Arr::sortRecursive((array) $this->decoded),
197+
JSON_UNESCAPED_UNICODE
198+
);
195199

196200
foreach (Arr::sortRecursive($data) as $key => $value) {
197201
$unexpected = $this->jsonSearchStrings($key, $value);
@@ -203,7 +207,7 @@ public function assertMissingExact(array $data)
203207

204208
PHPUnit::fail(
205209
'Found unexpected JSON fragment: '.PHP_EOL.PHP_EOL.
206-
'['.json_encode($data).']'.PHP_EOL.PHP_EOL.
210+
'['.json_encode($data, JSON_UNESCAPED_UNICODE).']'.PHP_EOL.PHP_EOL.
207211
'within'.PHP_EOL.PHP_EOL.
208212
"[{$actual}]."
209213
);
@@ -322,9 +326,9 @@ protected function reorderAssocKeys(array $data)
322326
*/
323327
protected function assertJsonMessage(array $data)
324328
{
325-
$expected = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
329+
$expected = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
326330

327-
$actual = json_encode($this->decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
331+
$actual = json_encode($this->decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
328332

329333
return 'Unable to find JSON: '.PHP_EOL.PHP_EOL.
330334
"[{$expected}]".PHP_EOL.PHP_EOL.
@@ -341,7 +345,7 @@ protected function assertJsonMessage(array $data)
341345
*/
342346
protected function jsonSearchStrings($key, $value)
343347
{
344-
$needle = substr(json_encode([$key => $value]), 1, -1);
348+
$needle = Str::substr(json_encode([$key => $value], JSON_UNESCAPED_UNICODE), 1, -1);
345349

346350
return [
347351
$needle.']',

tests/Testing/TestResponseTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,16 @@ public function testAssertJsonFragmentCanFail()
865865
$response->assertJsonFragment(['id' => 1]);
866866
}
867867

868+
public function testAssertJsonFragmentUnicodeCanFail()
869+
{
870+
$this->expectException(AssertionFailedError::class);
871+
$this->expectExceptionMessageMatches('/Привет|Мир/');
872+
873+
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithUnicodeStub));
874+
875+
$response->assertJsonFragment(['id' => 1]);
876+
}
877+
868878
public function testAssertJsonStructure()
869879
{
870880
$response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
@@ -1951,6 +1961,18 @@ public function jsonSerialize(): array
19511961
}
19521962
}
19531963

1964+
class JsonSerializableSingleResourceWithUnicodeStub implements JsonSerializable
1965+
{
1966+
public function jsonSerialize(): array
1967+
{
1968+
return [
1969+
['id' => 10, 'foo' => 'bar'],
1970+
['id' => 20, 'foo' => 'Привет'],
1971+
['id' => 30, 'foo' => 'Мир'],
1972+
];
1973+
}
1974+
}
1975+
19541976
class TestModel extends Model
19551977
{
19561978
protected $guarded = [];

0 commit comments

Comments
 (0)