-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathRegistrationTest.php
executable file
·91 lines (78 loc) · 2.65 KB
/
RegistrationTest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
class RegistrationTest extends TestCase
{
/** @test */
public function it_returns_user_with_token_on_valid_registration()
{
$user = factory('App\Models\User')->make();
$data = [
'user' => [
'username' => $user->username,
'email' => $user->email,
'password' => $user->password,
]
];
$response = $this->json('POST', '/api/users', $data);
$response->assertResponseStatus(201);
$response->seeJsonStructure([
'user' => [
'email',
'username',
]
]);
$this->assertArrayHasKey('token', $this->getResponseData()['user'], 'Token not found');
}
/** @test */
public function it_returns_field_required_validation_errors_on_invalid_registration()
{
$data = [];
$response = $this->json('POST', '/api/users', $data);
$response->assertResponseStatus(422);
$response->seeJsonEquals([
'errors' => [
'username' => ['field is required.'],
'email' => ['field is required.'],
'password' => ['field is required.'],
]
]);
}
/** @test */
public function it_returns_appropriate_field_validation_errors_on_invalid_registration()
{
$data = [
'user' => [
'username' => 'invalid username',
'email' => 'invalid email',
'password' => '1',
]
];
$response = $this->json('POST', '/api/users', $data);
$response->assertResponseStatus(422);
$response->seeJsonEquals([
'errors' => [
'username' => ['may only contain letters and numbers.'],
'email' => ['must be a valid email address.'],
'password' => ['must be at least 8 characters.'],
]
]);
}
/** @test */
public function it_returns_username_and_email_taken_validation_errors_when_using_duplicate_values_on_registration()
{
$data = [
'user' => [
'username' => $this->user->username,
'email' => $this->user->email,
'password' => $this->user->password,
]
];
$response = $this->json('POST', '/api/users', $data);
$response->assertResponseStatus(422);
$response->seeJsonEquals([
'errors' => [
'username' => ['has already been taken.'],
'email' => ['has already been taken.'],
]
]);
}
}