• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10597328253

28 Aug 2024 01:07PM UTC coverage: 7.69%. Remained the same
10597328253

push

github

web-flow
fix(laravel): entrypoint with doc formats (#6552)

0 of 6 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

12488 of 162387 relevant lines covered (7.69%)

22.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Laravel/Tests/JsonApiTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Laravel\Tests;
15

16
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17
use Illuminate\Contracts\Config\Repository;
18
use Illuminate\Foundation\Application;
19
use Illuminate\Foundation\Testing\RefreshDatabase;
20
use Orchestra\Testbench\Concerns\WithWorkbench;
21
use Orchestra\Testbench\TestCase;
22
use Workbench\App\Models\Author;
23
use Workbench\App\Models\Book;
24

25
class JsonApiTest extends TestCase
26
{
27
    use ApiTestAssertionsTrait;
28
    use RefreshDatabase;
29
    use WithWorkbench;
30

31
    /**
32
     * @param Application $app
33
     */
34
    protected function defineEnvironment($app): void
35
    {
36
        tap($app['config'], function (Repository $config): void {
×
37
            $config->set('api-platform.formats', ['jsonapi' => ['application/vnd.api+json']]);
×
NEW
38
            $config->set('api-platform.docs_formats', ['jsonapi' => ['application/vnd.api+json']]);
×
UNCOV
39
        });
×
40
    }
41

42
    public function testGetEntrypoint(): void
43
    {
44
        $response = $this->get('/api/', ['accept' => ['application/vnd.api+json']]);
×
45
        $response->assertStatus(200);
×
46
        $response->assertHeader('content-type', 'application/vnd.api+json; charset=utf-8');
×
47
        $this->assertJsonContains([
×
48
            'links' => [
×
49
                'self' => 'http://localhost/api',
×
50
                'book' => 'http://localhost/api/books',
×
51
            ],
×
52
        ],
×
53
            $response->json());
×
54
    }
55

56
    public function testGetCollection(): void
57
    {
58
        $response = $this->get('/api/books', ['accept' => ['application/vnd.api+json']]);
×
59
        $response->assertStatus(200);
×
60
        $response->assertHeader('content-type', 'application/vnd.api+json; charset=utf-8');
×
61
        $response->assertJsonFragment([
×
62
            'links' => [
×
63
                'self' => '/api/books?page=1',
×
64
                'first' => '/api/books?page=1',
×
65
                'last' => '/api/books?page=2',
×
66
                'next' => '/api/books?page=2',
×
67
            ],
×
68
            'meta' => ['totalItems' => 10, 'itemsPerPage' => 5, 'currentPage' => 1],
×
69
        ]);
×
70
        $response->assertJsonCount(5, 'data');
×
71
    }
72

73
    public function testGetBook(): void
74
    {
75
        $book = Book::first();
×
76
        $iri = $this->getIriFromResource($book);
×
77
        $response = $this->get($iri, ['accept' => ['application/vnd.api+json']]);
×
78
        $response->assertStatus(200);
×
79
        $response->assertHeader('content-type', 'application/vnd.api+json; charset=utf-8');
×
80

81
        $this->assertJsonContains([
×
82
            'data' => [
×
83
                'id' => $iri,
×
84
                'type' => 'Book',
×
85
                'attributes' => [
×
86
                    'name' => $book->name, // @phpstan-ignore-line
×
87
                ],
×
88
            ],
×
89
        ], $response->json());
×
90
    }
91

92
    public function testCreateBook(): void
93
    {
94
        $author = Author::find(1);
×
95
        $response = $this->postJson(
×
96
            '/api/books',
×
97
            [
×
98
                'data' => [
×
99
                    'attributes' => [
×
100
                        'name' => 'Don Quichotte',
×
101
                        'isbn' => fake()->isbn13(),
×
102
                        'publicationDate' => fake()->optional()->date(),
×
103
                    ],
×
104
                    'relationships' => [
×
105
                        'author' => [
×
106
                            'data' => [
×
107
                                'id' => $this->getIriFromResource($author),
×
108
                                'type' => 'Author',
×
109
                            ],
×
110
                        ],
×
111
                    ],
×
112
                ],
×
113
            ],
×
114
            [
×
115
                'accept' => 'application/vnd.api+json',
×
116
                'content_type' => 'application/vnd.api+json',
×
117
            ]
×
118
        );
×
119

120
        $response->assertStatus(201);
×
121
        $response->assertHeader('content-type', 'application/vnd.api+json; charset=utf-8');
×
122
        $this->assertJsonContains([
×
123
            'data' => [
×
124
                'type' => 'Book',
×
125
                'attributes' => [
×
126
                    'name' => 'Don Quichotte',
×
127
                ],
×
128
            ],
×
129
        ], $response->json());
×
130
        $this->assertMatchesRegularExpression('~^/api/books/~', $response->json('data.id'));
×
131
    }
132

133
    public function testUpdateBook(): void
134
    {
135
        $book = Book::first();
×
136
        $iri = $this->getIriFromResource($book);
×
137
        $response = $this->putJson(
×
138
            $iri,
×
139
            [
×
140
                'data' => ['attributes' => ['name' => 'updated title']],
×
141
            ],
×
142
            [
×
143
                'accept' => 'application/vnd.api+json',
×
144
                'content_type' => 'application/vnd.api+json',
×
145
            ]
×
146
        );
×
147
        $response->assertStatus(200);
×
148
        $this->assertJsonContains([
×
149
            'data' => [
×
150
                'id' => $iri,
×
151
                'attributes' => [
×
152
                    'name' => 'updated title',
×
153
                ],
×
154
            ],
×
155
        ], $response->json());
×
156
    }
157

158
    public function testPatchBook(): void
159
    {
160
        $book = Book::first();
×
161
        $iri = $this->getIriFromResource($book);
×
162
        $response = $this->patchJson(
×
163
            $iri,
×
164
            [
×
165
                'name' => 'updated title',
×
166
            ],
×
167
            [
×
168
                'accept' => 'application/vnd.api+json',
×
169
                'content_type' => 'application/merge-patch+json',
×
170
            ]
×
171
        );
×
172
        $response->assertStatus(200);
×
173
        $this->assertJsonContains([
×
174
            'data' => [
×
175
                'id' => $iri,
×
176
                'attributes' => [
×
177
                    'name' => 'updated title',
×
178
                ],
×
179
            ],
×
180
        ], $response->json());
×
181
    }
182

183
    public function testDeleteBook(): void
184
    {
185
        $book = Book::first();
×
186
        $iri = $this->getIriFromResource($book);
×
187
        $response = $this->delete($iri, ['accept' => 'application/vnd.api+json']);
×
188
        $response->assertStatus(204);
×
189
        $this->assertNull(Book::find($book->id));
×
190
    }
191
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc