66
77use PHPUnit \Framework \AssertionFailedError ;
88use PHPUnit \Framework \TestCase ;
9- use Zenstruck \Assert ;
109use Zenstruck \Browser \Json ;
1110
1211class 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