Skip to content

Commit 3858d04

Browse files
committed
[minor] small adjustments
1 parent 90393a9 commit 3858d04

File tree

2 files changed

+19
-43
lines changed

2 files changed

+19
-43
lines changed

src/Browser/Json.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* @author Kevin Bond <kevinbond@gmail.com>
11+
*
1112
* @mixin Expectation
1213
*/
1314
final class Json
@@ -54,7 +55,11 @@ public function assertMatches(string $expression, $expected): self
5455
*/
5556
public function assertHas(string $selector): self
5657
{
57-
Assert::try(fn() => $this->search("length({$selector})"));
58+
Assert::try(
59+
fn() => $this->search("length({$selector})"),
60+
'Element with selector "{selector}" not found.',
61+
['selector' => $selector]
62+
);
5863

5964
return $this;
6065
}
@@ -72,7 +77,7 @@ public function assertMissing(string $selector): self
7277
return $this;
7378
}
7479

75-
Assert::fail("Selector \"{$selector}\" does exists.");
80+
Assert::fail('Element with selector "{selector}" exists but it should not.', ['selector' => $selector]);
7681
}
7782

7883
/**
@@ -93,7 +98,7 @@ public function assertThatEach(string $selector, callable $assert): void
9398
$value = $this->search($selector);
9499

95100
if (!\is_array($value)) {
96-
Assert::fail("Value for selector \"{$selector}\" is not an array.");
101+
Assert::fail('Value for selector "{selector}" is not an array.', ['selector' => $selector]);
97102
}
98103

99104
Assert::that($value)->isNotEmpty();

tests/JsonTest.php

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PHPUnit\Framework\AssertionFailedError;
88
use PHPUnit\Framework\TestCase;
9-
use Zenstruck\Assert;
109
use Zenstruck\Browser\Json;
1110

1211
class JsonTest extends TestCase
@@ -17,11 +16,7 @@ class JsonTest extends TestCase
1716
*/
1817
public function assert_has_passes_if_selector_exists(string $json, string $selector): void
1918
{
20-
try {
21-
(new Json($json))->assertHas($selector);
22-
} catch (AssertionFailedError $exception) {
23-
Assert::fail("assertHas() did not assert that selector \"{$selector}\" exists.");
24-
}
19+
(new Json($json))->assertHas($selector);
2520
}
2621

2722
/**
@@ -30,15 +25,10 @@ public function assert_has_passes_if_selector_exists(string $json, string $selec
3025
*/
3126
public function assert_has_fails_if_selector_does_not_exist(string $json, string $selector): void
3227
{
33-
try {
34-
(new Json($json))->assertHas($selector);
35-
} catch (AssertionFailedError $exception) {
36-
Assert::pass();
28+
$this->expectException(AssertionFailedError::class);
29+
$this->expectExceptionMessage("Element with selector \"{$selector}\" not found.");
3730

38-
return;
39-
}
40-
41-
Assert::fail("assertHas() asserted that selector \"{$selector}\" exists although it does not.");
31+
(new Json($json))->assertHas($selector);
4232
}
4333

4434
/**
@@ -47,11 +37,7 @@ public function assert_has_fails_if_selector_does_not_exist(string $json, string
4737
*/
4838
public function assert_missing_passes_if_selector_does_not_exist(string $json, string $selector): void
4939
{
50-
try {
51-
(new Json($json))->assertMissing($selector);
52-
} catch (AssertionFailedError $exception) {
53-
Assert::fail("assertMissing() asserted that selector \"{$selector}\" is missing although it does not.");
54-
}
40+
(new Json($json))->assertMissing($selector);
5541
}
5642

5743
/**
@@ -60,15 +46,10 @@ public function assert_missing_passes_if_selector_does_not_exist(string $json, s
6046
*/
6147
public function assert_missing_fails_if_selector_exists(string $json, string $selector): void
6248
{
63-
try {
64-
(new Json($json))->assertMissing($selector);
65-
} catch (AssertionFailedError $exception) {
66-
Assert::pass();
67-
68-
return;
69-
}
49+
$this->expectException(AssertionFailedError::class);
50+
$this->expectExceptionMessage("Element with selector \"{$selector}\" exists but it should not.");
7051

71-
Assert::fail("assertMissing() did not assert that \"{$selector}\" is missing.");
52+
(new Json($json))->assertMissing($selector);
7253
}
7354

7455
public function selectorExistsProvider(): iterable
@@ -92,11 +73,7 @@ public function selectorDoesNotExistProvider(): iterable
9273
*/
9374
public function can_assert_a_selector_has_count(string $json, int $expectedCount): void
9475
{
95-
try {
96-
(new Json($json))->hasCount($expectedCount);
97-
} catch (AssertionFailedError $exception) {
98-
Assert::fail("assertCount() did not assert that json matches expected count \"{$expectedCount}\"");
99-
}
76+
(new Json($json))->hasCount($expectedCount);
10077
}
10178

10279
public function selectHasCountProvider(): iterable
@@ -149,15 +126,9 @@ public function arrayChildAssertionProvider(): iterable
149126
*/
150127
public function assert_that_each_throws_if_invalid_array_given(string $json, string $selector, callable $asserter): void
151128
{
152-
try {
153-
(new Json($json))->assertThatEach($selector, $asserter);
154-
} catch (AssertionFailedError $e) {
155-
Assert::pass();
129+
$this->expectException(AssertionFailedError::class);
156130

157-
return;
158-
}
159-
160-
Assert::fail('Json::assertThatEach() should raise exception with invalid arrays.');
131+
(new Json($json))->assertThatEach($selector, $asserter);
161132
}
162133

163134
public function invalidArrayChildAssertionProvider(): iterable

0 commit comments

Comments
 (0)