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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

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

0.0
/src/Laravel/Tests/JsonProblemTest.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 Illuminate\Foundation\Application;
17
use Illuminate\Foundation\Testing\RefreshDatabase;
18
use Orchestra\Testbench\Attributes\DefineEnvironment;
19
use Orchestra\Testbench\Concerns\WithWorkbench;
20
use Orchestra\Testbench\TestCase;
21
use PHPUnit\Framework\Attributes\DataProvider;
22

23
class JsonProblemTest extends TestCase
24
{
25
    use RefreshDatabase;
26
    use WithWorkbench;
27

28
    public function testNotFound(): void
29
    {
30
        $response = $this->get('/api/books/notfound', headers: ['accept' => 'application/ld+json']);
×
31
        $response->assertStatus(404);
×
32
        $response->assertHeader('content-type', 'application/problem+json; charset=utf-8');
×
33
        $response->assertJsonFragment([
×
34
            '@context' => '/api/contexts/Error',
×
NEW
35
            '@id' => '/api/errors/404',
×
NEW
36
            '@type' => 'Error',
×
37
            'type' => '/errors/404',
×
38
            'title' => 'An error occurred',
×
39
            'status' => 404,
×
40
            'detail' => 'Not Found',
×
41
        ]);
×
42
    }
43

44
    /**
45
     * @param Application $app
46
     */
47
    protected function useProductionMode($app): void
48
    {
49
        $app['config']->set('app.debug', false);
×
50
    }
51

52
    #[DefineEnvironment('useProductionMode')]
53
    public function testProductionError(): void
54
    {
55
        $response = $this->post('/api/books', ['content-type' => 'application/vnd.api+json']);
×
56
        $response->assertStatus(415);
×
57
        $content = $response->json();
×
58
        $this->assertArrayNotHasKey('trace', $content);
×
59
        $this->assertArrayNotHasKey('line', $content);
×
60
        $this->assertArrayNotHasKey('file', $content);
×
61
    }
62

63
    /**
64
     * @param list<array{0: string, 1: string, 2: array<string, mixed>}> $expected
65
     */
66
    #[DefineEnvironment('useProductionMode')]
67
    #[DataProvider('formatsProvider')]
68
    public function testRetrieveError(string $format, string $status, array $expected): void
69
    {
NEW
70
        $response = $this->get('/api/errors/'.$status, ['accept' => $format]);
×
NEW
71
        $this->assertEquals($expected, $response->json());
×
72
    }
73

74
    #[DefineEnvironment('useProductionMode')]
75
    public function testRetrieveErrorHtml(): void
76
    {
NEW
77
        $response = $this->get('/api/errors/403', ['accept' => 'text/html']);
×
NEW
78
        $this->assertEquals('<!DOCTYPE html>
×
79
<html>
80
<head>
81
  <meta charset="UTF-8" />
82
  <title>Error 403</title>
83
    </head>
84
    <body><h1>Error 403</h1>Forbidden</body>
NEW
85
</html>', $response->getContent());
×
86
    }
87

88
    /**
89
     * @return list<array{0: string, 1: string, 2: array<string, mixed>}>
90
     */
91
    public static function formatsProvider(): array
92
    {
NEW
93
        return [
×
NEW
94
            [
×
NEW
95
                'application/vnd.api+json',
×
NEW
96
                '401',
×
NEW
97
                [
×
NEW
98
                    'errors' => [
×
NEW
99
                        [
×
NEW
100
                            'id' => '/api/errors/401',
×
NEW
101
                            'detail' => 'Unauthorized',
×
NEW
102
                            'type' => 'about:blank',
×
NEW
103
                            'title' => 'Error 401',
×
NEW
104
                            'status' => 401,
×
NEW
105
                            'code' => '401',
×
NEW
106
                            'links' => [
×
NEW
107
                                'type' => 'about:blank',
×
NEW
108
                            ],
×
NEW
109
                        ],
×
NEW
110
                    ],
×
NEW
111
                ],
×
NEW
112
            ],
×
NEW
113
            [
×
NEW
114
                'application/ld+json',
×
NEW
115
                '401',
×
NEW
116
                [
×
NEW
117
                    '@context' => '/api/contexts/Error',
×
NEW
118
                    '@type' => 'Error',
×
NEW
119
                    '@id' => '/api/errors/401',
×
NEW
120
                    'detail' => 'Unauthorized',
×
NEW
121
                    'title' => 'Error 401',
×
NEW
122
                    'status' => 401,
×
NEW
123
                    'type' => 'about:blank',
×
NEW
124
                ],
×
NEW
125
            ],
×
NEW
126
            [
×
NEW
127
                'application/json',
×
NEW
128
                '401',
×
NEW
129
                [
×
NEW
130
                    'type' => 'about:blank',
×
NEW
131
                    'detail' => 'Unauthorized',
×
NEW
132
                    'title' => 'Error 401',
×
NEW
133
                    'status' => 401,
×
NEW
134
                ],
×
NEW
135
            ],
×
NEW
136
        ];
×
137
    }
138
}
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

© 2026 Coveralls, Inc