Skip to content

Commit 30685f4

Browse files
committed
add tests for assertHasJsonValidationError assertion
1 parent 58ec90a commit 30685f4

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tests/Integration/Http/ValidationResponseTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,52 @@ public function test_assert_json_by_keys_assertion(): void
6767
->assertJsonByKeys(['id' => 1, 'title' => 'The Odyssee'], ['id', 'title'])
6868
->assertJsonByKeys(['author' => ['name' => 'Homer']], ['author.name']);
6969
}
70+
71+
public function test_update_book(): void
72+
{
73+
$this->migrate(
74+
CreateMigrationsTable::class,
75+
CreateBookTable::class,
76+
CreateAuthorTable::class,
77+
);
78+
79+
$book = Book::create(
80+
title: 'The Odyssee',
81+
author: Author::create(name: 'Homer'),
82+
);
83+
84+
$this->http->post(
85+
uri([ValidationController::class, 'updateBook'], book: 1),
86+
body: ['title' => 'Beyond the Odyssee'],
87+
)
88+
->assertOk();
89+
90+
$book->refresh();
91+
92+
$this->assertSame($book->title, 'Beyond the Odyssee');
93+
}
94+
95+
public function test_failing_post_request(): void
96+
{
97+
$this->migrate(
98+
CreateMigrationsTable::class,
99+
CreateBookTable::class,
100+
CreateAuthorTable::class,
101+
);
102+
103+
Book::create(
104+
title: 'The Odyssee',
105+
author: Author::create(name: 'Homer'),
106+
);
107+
108+
$this->http->post(
109+
uri([ValidationController::class, 'updateBook'], book: 1),
110+
body: ['book' => ['title' => 1]],
111+
)
112+
->assertHasJsonValidationError('title', function (array $errors): void {
113+
$this->assertContains('Value should be between 1 and 120', $errors);
114+
});
115+
116+
$this->assertSame('The Odyssee', Book::find(id: 1)->first()->title);
117+
}
70118
}

0 commit comments

Comments
 (0)