Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
TDD視点から見る、Laravel・Requestクラスの依存性
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
stwile
February 22, 2021
Programming
150
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
February 22, 2021
More Decks by stwile
See All by stwile
Fat Controller は悪か? ~光のFat Controller・闇のガリController~
stwile
2
1.9k
フレームワークが生み出す負債や複雑さに対して、PHPUnitと付き合っていく
stwile
1
2.8k
継続的にLaravelのUnitTestを書く上で 気をつけていること
stwile
1
250
Other Decks in Programming
See All in Programming
数百円から始めるRuby電子工作
tarosay
0
140
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
3.6k
夏だ!祭りだ!祭りとはドメインモデリングでは?
ryugen04
0
190
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
160
今さら聞けない .NET CLI
htkym
0
180
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
540
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
180
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
210
5分で問診!Composer セキュリティ健康診断
codmoninc
0
910
わからない話を追いかけたら、プログラミング言語を作る側にいた
ydah
3
480
Go 1.27 における memory allocation の高速化
andpad
0
110
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
240
Featured
See All Featured
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
890
Balancing Empowerment & Direction
lara
6
1.2k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
990
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
360
How GitHub (no longer) Works
holman
316
150k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
360
Paper Plane (Part 1)
katiecoart
PRO
1
10k
Designing for humans not robots
tammielis
254
26k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
380
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
Navigating Team Friction
lara
192
16k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.3k
Transcript
!IBOEN 5%%ࢹ͔ΒݟΔɺ 3FRVFTUΫϥεͷґଘੑ
!IBOEN ࣗݾհ hyroky . ઐྖҬɿ ɾόοΫΤϯυ ɾϢχοτςετ झຯɿ ɾಡॻ📓ʢॐɾපݪەɾమʣ ɾےτϨ🦾
LaravelͷϦΫΤετΫϥε͕Ͱ͖Δ͜ͱ • ݖݶνΣοΫ • όϦσʔγϣϯ • HTTPϦΫΤετใͷอ࣋ • HTTPϨεϙϯεॲཧͷ࣮
3FTQPOTF ใͷฦ٫ 3FRVFTU ใͷอ࣋ 7BMJEBUJPO ͷݕূ "VUIFOUJDBUJPO Ϣʔβೝূ LaravelͷRequestΫϥε͕࣋ͯΔ 3FRVFTU
$MBTT
όϦσʔγϣϯͷྫɿ class HogeRequest extends FormRequest { public function authorize(): bool
{ return true; } public function rules(): array { return [ 'name' => 'required|string|max:128', 'content' => 'required|string|max:500', ]; } public function attributes(): array { return ['name' => '໊લ','content' => '༰']; } protected function failedValidation(Validator $validator) { $errors = []; $validateErrors = $validator->errors()->toArray(); foreach ($validateErrors as $e) { $errors[] = $e[0]; } throw new HttpResponseException( response()->json($errors, 422) ); } } FormRequestΛܧঝ Α͋͘ΔFormRequestͷ֦ு • ೝূ • όϦσʔγϣϯϧʔϧͷఆٛ • όϦσʔγϣϯͰམͪͨͱ͖ͷ ڞ௨ॲཧ
͍ͭݟΔFormRequestͷܠ৭
ศརͳ໘… Ϣχοτςετ͕ ॻ͖ͮΒ͍😖
class HogeRequest extends FormRequest { public function authorize(): bool {
return true; } public function rules(): array { return [ 'name' => 'required|string|max:128', 'content' => 'required|string|max:500', ]; } public function attributes(): array { return ['name' => '໊લ','content' => '༰']; } protected function failedValidation(Validator $validator) { $errors = []; $validateErrors = $validator->errors()->toArray(); foreach ($validateErrors as $e) { $errors[] = $e[0]; } throw new HttpResponseException( response()->json($errors, 422) ); } } ιʔείʔυͱςετίʔυͷྫ class HogeRequestTest extends TestCase { /** * @dataProvider dataProvider */ public function test(array $param, array, $expected): void { $request = new Request(); $actual = Validator::make( $params, $request->rules(), $request->messages() ) ->errors() ->all(); parent::assertSame($expected, $actual); } public function dataProvider(): array { return [ 'ඞਢܥ NG' => [ [], ['໊લ͕ඞਢͰ͢ɻ','༰͕ඞਢͰ͢ɻ'], ], ] } } ᶃ ᶄ ᶅ
public function authorize(): bool { return true; } ιʔείʔυͱςετίʔυͷྫ ᶃ
ͷॏෳ ೝূͷͱόϦσʔγϣϯͷ͕ ڞଘ͍ͯ͠Δ ςετΫϥεͷจ຺͕ϒϨΔ → υΩϡϝϯτ📄ͱͯ͠ಡΈͮΒ͍😂 → ॻ͍͍ͯͯҧײ͕ग़Δ
ιʔείʔυͱςετίʔυͷྫ class HogeRequestTest extends TestCase { /** * @dataProvider dataProvider
*/ public function test(array $param, array, $expected): void { $request = new HogeRequest(); $actual = Validator::make( $params, $request->rules(), $request->messages() ) ->errors() ->all(); parent::assertSame($expected, $actual); } public function dataProvider(): array { return [ 'ඞਢܥ NG' => [ [], ['໊લ͕ඞਢͰ͢ɻ','༰͕ඞਢͰ͢ɻ'], ], ] } } ᶄ ෆཁͳґଘ ϧʔϧɾϝοηʔδͱ ࣮ߦͷॴࡏ͕ผΕ͍ͯΔ όϦσʔγϣϯ͚͍ͩͨ͠ͷʹɺ RequestΫϥεΛ ΠϯελϯεԽ͠ͳ͚ΕͳΒͳ͍ → ΩϞνϫϧΠ🤮
class HogeRequest extends FormRequest { protected function failedValidation( Validator $validator
) { $errors = []; $validateErrors = $validator->errors() ->toArray(); foreach ($validateErrors as $e) { $errors[] = $e[0]; } throw new HttpResponseException( response()->json($errors, 422) ); } } ιʔείʔυͱςετίʔυͷྫ ᶅ ϑϨʔϜϫʔΫґଘͷڧ͞ ϝοηʔδΛՃ͢ΔͨΊʹɺ ܧঝݩͷ࣮Λؾʹ͢Δ ϑϨʔϜϫʔΫͷଥڠ → ࣮ͷࣗ༝͕Լ͕Δ protected ͷϝιουͷςετॻ͖ͮΒ͍😇
·ͱΊ ᶃ Ϣχοτςετ͕ॻ͖͍͢ઃܭߜΒΕΔ ᶄ ͷॴࡏ͕ڞଘ͍ͯ͠ΔͱɺςετΫϥε͕ཎΉ ᶅ ԿͰΔΫϥεɺςετ͕ͭΒ͍🥺