Skip to content

Commit 5f79a88

Browse files
committed
更新範例
1 parent a557507 commit 5f79a88

File tree

6 files changed

+162
-29
lines changed

6 files changed

+162
-29
lines changed

publishes/resources/ExamplePack/Resource.php

Lines changed: 0 additions & 22 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.

publishes/resources/ExamplePack/Model.php renamed to publishes/resources/example_model_pack/pack_model.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
use Database\Factories\DummyFactory;
66
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
89

10+
/**
11+
* @method static \Illuminate\Database\Eloquent\Builder orderByPriority(bool $reversed = false)
12+
* @method static \Illuminate\Database\Eloquent\Builder published()
13+
*/
914
class Dummy extends Model
1015
{
1116
use HasFactory;
@@ -48,16 +53,27 @@ protected static function newFactory()
4853
return DummyFactory::new();
4954
}
5055

56+
/* =========================================================================
57+
* = Relations
58+
* =========================================================================
59+
*/
60+
61+
// /**
62+
// * @return \Illuminate\Database\Eloquent\Relations\Relation
63+
// */
64+
// public function relation(): Relations\Relation
65+
// {
66+
// return $this->belongsTo(Relation::class);
67+
// }
68+
5169
/* =========================================================================
5270
* = Scopes
5371
* =========================================================================
5472
*/
5573

5674
/**
57-
* 按照優先度排序
58-
*
59-
* ```
60-
* $query->orderByPriority();
75+
* ```php
76+
* Dummy::orderByPriority();
6177
* ```
6278
*
6379
* @param \Illuminate\Database\Eloquent\Builder $query
@@ -70,10 +86,8 @@ public function scopeOrderByPriority($query, $reversed = false)
7086
}
7187

7288
/**
73-
* 查詢僅上架
74-
*
7589
* ```php
76-
* $query->published();
90+
* Dummy::published();
7791
* ```
7892
*
7993
* @param \Illuminate\Database\Eloquent\Builder $query
File renamed without changes.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Tests\Feature;
4+
5+
use App\Models\Dummy;
6+
use Database\Factories\DummyFactory;
7+
use Illuminate\Testing\Fluent\AssertableJson;
8+
use Tests\TestCase;
9+
10+
class DummyApiTest extends TestCase
11+
{
12+
/**
13+
* Base path of API.
14+
*
15+
* @var string
16+
*/
17+
protected string $base = '/api/dummies';
18+
19+
/**
20+
* @param int|null $count
21+
* @return \Database\Factories\DummyFactory
22+
*/
23+
protected function factory($count = null): DummyFactory
24+
{
25+
return Dummy::factory($count);
26+
}
27+
28+
// =========================================================================
29+
// = BREAD Specs
30+
// =========================================================================
31+
32+
/**
33+
* @return void
34+
*/
35+
public function test_browse_records()
36+
{
37+
$this->factory(3)->create();
38+
39+
$response = $this->getJson($this->base);
40+
41+
$response->assertOk();
42+
43+
$response->assertJsonStructure([
44+
'meta',
45+
'links',
46+
'data' => [
47+
'*' => [
48+
'id',
49+
50+
// ... Here is where you should put the expected schema of response records.
51+
]
52+
]
53+
]);
54+
}
55+
56+
/**
57+
* @return void
58+
*/
59+
public function test_read_specified_record()
60+
{
61+
$record = $this->factory()->create();
62+
63+
$response = $this->getJson("{$this->base}/{$record->getKey()}");
64+
65+
$response->assertOk();
66+
67+
$response->assertJsonStructure([
68+
'data' => [
69+
'id',
70+
71+
// ... Here is where you should put the expected schema of response record.
72+
]
73+
]);
74+
}
75+
76+
/**
77+
* @return void
78+
*/
79+
public function test_edit_specified_record()
80+
{
81+
$record = $this->factory()->create();
82+
83+
$response = $this->putJson(
84+
"{$this->base}/{$record->getKey()}",
85+
$data = [
86+
87+
// ... Here is where you should put the form data for updating.
88+
89+
]
90+
);
91+
92+
$response->assertNoContent();
93+
94+
$this->assertDatabaseHas('dummies', $data);
95+
}
96+
97+
/**
98+
* @return void
99+
*/
100+
public function test_add_new_record()
101+
{
102+
$response = $this->postJson(
103+
"{$this->base}",
104+
$data = [
105+
106+
// ... Here is where you should put the form data for creating.
107+
108+
]
109+
);
110+
111+
$response->assertCreated();
112+
113+
$this->assertDatabaseHas('dummies', [
114+
...$data,
115+
116+
// ... extra checks
117+
]);
118+
}
119+
120+
/**
121+
* @return void
122+
*/
123+
public function test_delete_specified_record()
124+
{
125+
$record = $this->factory()->create();
126+
127+
$response = $this->deleteJson(
128+
"{$this->base}/{$record->getKey()}"
129+
);
130+
131+
$response->assertNoContent();
132+
133+
$this->assertDatabaseCount('dummies', 0);
134+
}
135+
136+
// =========================================================================
137+
// = Extra Specs
138+
// =========================================================================
139+
140+
// ...
141+
}

0 commit comments

Comments
 (0)