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
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
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
150
Generative UI & AI-Assistants for Your Angular Solutions
manfredsteyer
PRO
0
120
数百円から始めるRuby電子工作
tarosay
0
110
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
180
今さら聞けない .NET CLI
htkym
0
140
型も通る、synthも通る、それでも危ない 〜AIのCDKの権限とコストを機械で検証する〜 / It Passes Type Checks, It Passes Synth Checks, but It’s Still Risky — Automatically Verifying Permissions and Costs in AI’s CDK —
seike460
PRO
1
440
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
240
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
8
5.5k
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1.2k
AWS CDK を「作」ってみた 〜フルスクラッチで見えた CDK の裏側〜 / aws-cdk-from-scratch
gotok365
3
2.6k
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
150
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
120
Featured
See All Featured
Test your architecture with Archunit
thirion
1
2.3k
How to train your dragon (web standard)
notwaldorf
97
6.7k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Rails Girls Zürich Keynote
gr2m
96
14k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
410
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
430
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
210
Claude Code のすすめ
schroneko
67
230k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
Statistics for Hackers
jakevdp
799
230k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Done Done
chrislema
186
16k
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 ͷϝιουͷςετॻ͖ͮΒ͍😇
·ͱΊ ᶃ Ϣχοτςετ͕ॻ͖͍͢ઃܭߜΒΕΔ ᶄ ͷॴࡏ͕ڞଘ͍ͯ͠ΔͱɺςετΫϥε͕ཎΉ ᶅ ԿͰΔΫϥεɺςετ͕ͭΒ͍🥺