File: RouteApiResourceTest.php

package info (click to toggle)
php-laravel-framework 6.20.14%2Bdfsg-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,932 kB
  • sloc: php: 122,752; sh: 136; javascript: 45; makefile: 44
file content (115 lines) | stat: -rw-r--r-- 4,667 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

namespace Illuminate\Tests\Integration\Routing;

use Illuminate\Support\Facades\Route;
use Illuminate\Tests\Integration\Routing\Fixtures\ApiResourceTaskController;
use Illuminate\Tests\Integration\Routing\Fixtures\ApiResourceTestController;
use Orchestra\Testbench\TestCase;

/**
 * @group integration
 */
class RouteApiResourceTest extends TestCase
{
    public function testApiResource()
    {
        Route::apiResource('tests', ApiResourceTestController::class);

        $response = $this->get('/tests');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m index', $response->getContent());

        $response = $this->post('/tests');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m store', $response->getContent());

        $response = $this->get('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m show', $response->getContent());

        $response = $this->put('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m update', $response->getContent());
        $response = $this->patch('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m update', $response->getContent());

        $response = $this->delete('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m destroy', $response->getContent());
    }

    public function testApiResourceWithOnly()
    {
        Route::apiResource('tests', ApiResourceTestController::class)->only(['index', 'store']);

        $response = $this->get('/tests');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m index', $response->getContent());

        $response = $this->post('/tests');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m store', $response->getContent());

        $this->assertEquals(404, $this->get('/tests/1')->getStatusCode());
        $this->assertEquals(404, $this->put('/tests/1')->getStatusCode());
        $this->assertEquals(404, $this->patch('/tests/1')->getStatusCode());
        $this->assertEquals(404, $this->delete('/tests/1')->getStatusCode());
    }

    public function testApiResources()
    {
        Route::apiResources([
            'tests' => ApiResourceTestController::class,
            'tasks' => ApiResourceTaskController::class,
        ]);

        $response = $this->get('/tests');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m index', $response->getContent());

        $response = $this->post('/tests');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m store', $response->getContent());

        $response = $this->get('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m show', $response->getContent());

        $response = $this->put('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m update', $response->getContent());
        $response = $this->patch('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m update', $response->getContent());

        $response = $this->delete('/tests/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m destroy', $response->getContent());

        /////////////////////
        $response = $this->get('/tasks');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m index tasks', $response->getContent());

        $response = $this->post('/tasks');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m store tasks', $response->getContent());

        $response = $this->get('/tasks/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m show tasks', $response->getContent());

        $response = $this->put('/tasks/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m update tasks', $response->getContent());
        $response = $this->patch('/tasks/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m update tasks', $response->getContent());

        $response = $this->delete('/tasks/1');
        $this->assertEquals(200, $response->getStatusCode());
        $this->assertSame('I`m destroy tasks', $response->getContent());
    }
}