-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestUtilities.php
59 lines (45 loc) · 1.68 KB
/
TestUtilities.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
namespace Tests;
use Illuminate\Database\Eloquent\Model;
trait TestUtilities
{
protected $endpoint = null;
protected function fieldIsPresent(string $field, string $endpoint = null, string $method = 'POST')
{
$url = $endpoint ?? $this->endpoint();
$this
->json($method, $url, [])
->assertStatus(422)
->assertJsonValidationErrors([$field => 'required']);
$this
->json($method, $url, [$field => null])
->assertStatus(422)
->assertJsonValidationErrors([$field => 'required']);
$this
->json($method, $url, [$field => ''])
->assertStatus(422)
->assertJsonValidationErrors([$field => 'required']);
}
protected function fieldIsUnique(string $field, Model $existing, string $databaseField = null, string $endpoint = null, string $method = 'POST')
{
$url = $endpoint ?? $this->endpoint();
if (!$databaseField) $databaseField = $field;
$this
->json($method, $url, [$field => $existing[$databaseField]])
->assertStatus(422)
->assertJsonValidationErrors([$field => 'taken']);
}
protected function fieldValueIsInvalid(string $field, $invalidValue, string $endpoint = null, string $method = 'POST')
{
$url = $endpoint ?? $this->endpoint();
$this
->json($method, $url, [$field => $invalidValue])
->assertStatus(422)
->assertJsonValidationErrors([$field => 'invalid']);
}
private function endpoint()
{
if (!$this->endpoint) throw new Exception('Endpoint not provided.');
return $this->endpoint;
}
}