PersonApiTest::test_it_can_create_a_new_person()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Simplex\Quickstart\Module\Demo\Test\Functional;
4
5
use Lukasoppermann\Httpstatus\Httpstatuscodes;
6
use Simplex\Quickstart\Module\Demo\DataFixture\PersonLoader;
7
use Simplex\Quickstart\Shared\Testing\FunctionalTest;
8
9
final class PersonApiTest extends FunctionalTest
10
{
11
    public function test_it_can_be_queried_for_a_list_of_people()
12
    {
13
        $response = $this->sendGet('/');
14
15
        $body = $this->getBody($response);
16
17
        self::assertCount(1, $body);
18
        self::assertJsonDocumentMatchesSchema(
19
            json_encode($body[0]),
20
            [
21
                '$.name' => 'Joe Smith',
22
                '$.email' => '[email protected]',
23
            ]
24
        );
25
    }
26
27
    public function test_it_can_be_queried_for_a_specific_person()
28
    {
29
        $response = $this->sendGet('/person/' . PersonLoader::PERSON_1_ID);
30
31
        self::assertMessageBodyMatchesJson(
32
            $response,
33
            [
34
                '$.name' => 'Joe Smith',
35
                '$.email' => '[email protected]'
36
            ]
37
        );
38
39
        self::assertJsonDocumentMatchesSchema(
40
            $this->getRawBody($response),
41
            [
42
                'type'       => 'object',
43
                'required'   => ['name', 'email', '_links'],
44
                'properties' => [
45
                    'name' => ['type' => 'string'],
46
                    'email' => ['type' => 'string'],
47
                    'links' => ['type' => 'array'],
48
                ]
49
            ]
50
        );
51
    }
52
53
    public function test_it_can_create_a_new_person()
54
    {
55
        $response = $this->sendPost(
56
            '/person',
57
            [
58
                'name' => 'Will',
59
                'email' => '[email protected]',
60
                'password' => 'foobar'
61
            ]
62
        );
63
64
        self::assertResponseHasStatus($response, Httpstatuscodes::HTTP_CREATED);
65
        self::assertMessageHasHeader($response, 'Location');
66
    }
67
68
    public function test_bad_request_response_returned_if_missing_required_param_when_creating_new_person()
69
    {
70
        $this->reloadDb = false;
71
72
        $response = $this->sendPost(
73
            '/person',
74
            [
75
                'name' => 'Will',
76
                'email' => '[email protected]',
77
            ]
78
        );
79
80
        self::assertResponseHasStatus($response, Httpstatuscodes::HTTP_BAD_REQUEST);
81
        self::assertMessageBodyMatchesJson(
82
            $response,
83
            [
84
                '$.[0].property' => 'password',
85
                '$.[0].message' => 'This value should not be blank.'
86
            ]
87
        );
88
    }
89
90
    public function test_bad_request_response_returned_if_required_param_name_spelt_incorrectly()
91
    {
92
        $this->reloadDb = false;
93
94
        $response = $this->sendPost(
95
            '/person',
96
            [
97
                'name' => 'Will',
98
                'email' => '[email protected]',
99
                'pass' => 'foobar'
100
            ]
101
        );
102
103
        self::assertResponseHasStatus($response, Httpstatuscodes::HTTP_BAD_REQUEST);
104
        self::assertMessageBodyMatchesJson(
105
            $response,
106
            [
107
                '$.[0].property' => 'password',
108
                '$.[0].message' => 'This value should not be blank.'
109
            ]
110
        );
111
    }
112
113
    public function test_bad_request_response_returned_if_required_param_blank()
114
    {
115
        $this->reloadDb = false;
116
117
        $response = $this->sendPost(
118
            '/person',
119
            [
120
                'name' => '',
121
                'email' => '[email protected]',
122
                'password' => 'foobar'
123
            ]
124
        );
125
126
        self::assertResponseHasStatus($response, Httpstatuscodes::HTTP_BAD_REQUEST);
127
        self::assertMessageBodyMatchesJson(
128
            $response,
129
            [
130
                '$.[0].property' => 'name',
131
                '$.[0].message' => 'This value should not be blank.'
132
            ]
133
        );
134
    }
135
}
136