-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathApiControllerTest.php
More file actions
186 lines (156 loc) · 6.14 KB
/
ApiControllerTest.php
File metadata and controls
186 lines (156 loc) · 6.14 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace StatamicRadPack\Runway\Tests\Http\Controllers;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Config;
use StatamicRadPack\Runway\Tests\Fixtures\Models\Post;
use StatamicRadPack\Runway\Tests\TestCase;
class ApiControllerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Config::set('statamic.api.resources.runway', [
'posts' => ['allowed_filters' => ['title']],
]);
}
#[Test]
public function gets_a_resource_that_exists()
{
$posts = Post::factory()->count(2)->create();
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts']))
->assertOk()
->assertJsonStructure(['data', 'meta'])
->assertJsonPath('data.0.id', $posts[0]->id)
->assertJsonPath('data.1.id', $posts[1]->id);
}
#[Test]
public function returns_not_found_on_a_resource_that_doesnt_exist()
{
Post::factory()->count(2)->create();
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts2']))
->assertNotFound();
}
#[Test]
public function it_filters_out_unpublished_models()
{
$posts = Post::factory()->count(2)->create();
Post::factory()->count(2)->unpublished()->create();
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts']))
->assertOk()
->assertJsonStructure(['data', 'meta'])
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.id', $posts[0]->id)
->assertJsonPath('data.1.id', $posts[1]->id);
}
#[Test]
public function paginates_a_resource_list()
{
Post::factory()->count(10)->create();
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts', 'limit' => 5]))
->assertOk()
->assertJsonStructure(['data', 'meta'])
->assertJsonPath('meta.current_page', 1)
->assertJsonPath('meta.last_page', 2)
->assertJsonPath('meta.total', 10);
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts', 'limit' => 5, 'page' => 2]))
->assertOk()
->assertJsonStructure(['data', 'meta'])
->assertJsonPath('meta.current_page', 2)
->assertJsonPath('meta.last_page', 2)
->assertJsonPath('meta.total', 10);
}
#[Test]
public function filters_a_resource_list()
{
[$postA, $postB, $postC] = Post::factory()->count(3)->create();
$postA->update(['title' => 'Test One']);
$postB->update(['title' => 'Test Two']);
$postC->update(['title' => 'Test Three']);
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts']))
->assertOk()
->assertJsonStructure(['data', 'meta'])
->assertJsonPath('meta.total', 3);
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts', 'filter[title:contains]' => 'one']))
->assertOk()
->assertJsonStructure(['data', 'meta'])
->assertJsonPath('meta.total', 1);
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts', 'filter[title:contains]' => 'test']))
->assertOk()
->assertJsonStructure(['data', 'meta'])
->assertJsonPath('meta.total', 3);
}
#[Test]
public function wont_filter_a_resource_list_on_a_forbidden_filter()
{
[$postA, $postB, $postC] = Post::factory()->count(3)->create();
$postA->update(['title' => 'Test One']);
$postB->update(['title' => 'Test Two']);
$postC->update(['title' => 'Test Three']);
$this
->get(route('statamic.api.runway.index', ['resourceHandle' => 'posts', 'filter[slug:contains]' => 'one']))
->assertStatus(422);
}
#[Test]
public function gets_a_resource_model_that_exists()
{
$post = Post::factory()->create();
$this
->get(route('statamic.api.runway.show', ['resourceHandle' => 'posts', 'model' => $post->id]))
->assertOk()
->assertSee(['data'])
->assertJsonPath('data.id', $post->id)
->assertJsonPath('data.title', $post->title);
}
#[Test]
public function gets_a_resource_model_with_nested_fields()
{
$post = Post::factory()->create([
'values' => [
'alt_title' => 'Alternative Title...',
'alt_body' => 'This is a **great** post! You should *read* it.',
],
]);
$this
->get(route('statamic.api.runway.show', ['resourceHandle' => 'posts', 'model' => $post->id]))
->assertOk()
->assertSee(['data'])
->assertJsonPath('data.id', $post->id)
->assertJsonPath('data.values.alt_title', 'Alternative Title...')
->assertJsonPath('data.values.alt_body', fn ($value) => trim($value) === '<p>This is a <strong>great</strong> post! You should <em>read</em> it.</p>');
}
#[Test]
public function gets_a_resource_model_with_belongs_to_relationship()
{
$post = Post::factory()->create();
$this
->get(route('statamic.api.runway.show', ['resourceHandle' => 'posts', 'model' => $post->id]))
->assertOk()
->assertSee(['data'])
->assertJsonPath('data.id', $post->id)
->assertJsonPath('data.author_id.id', $post->author->id)
->assertJsonPath('data.author_id.name', $post->author->name);
}
#[Test]
public function returns_not_found_on_a_model_that_does_not_exist()
{
$this
->get(route('statamic.api.runway.show', ['resourceHandle' => 'posts', 'model' => 44]))
->assertNotFound();
}
#[Test]
public function it_doesnt_return_unpublished_model()
{
$post = Post::factory()->unpublished()->create();
$this
->get(route('statamic.api.runway.show', ['resourceHandle' => 'posts', 'model' => $post->id]))
->assertNotFound();
}
}