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.8k
フレームワークが生み出す負債や複雑さに対して、PHPUnitと付き合っていく
stwile
1
2.7k
継続的にLaravelのUnitTestを書く上で 気をつけていること
stwile
1
240
Other Decks in Programming
See All in Programming
Swiftのレキシカルスコープ管理
kntkymt
0
220
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
540
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
110
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
120
CSC307 Lecture 17
javiergs
PRO
0
320
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
2
1.4k
3Dシーンの圧縮
fadis
1
740
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.6k
AIで効率化できた業務・日常
ochtum
0
120
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
210
dRuby over BLE
makicamel
2
330
Vite+ Unified Toolchain for the Web
naokihaba
0
280
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
463
34k
Exploring anti-patterns in Rails
aemeredith
3
400
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
160
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
200
Designing Experiences People Love
moore
143
24k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
320
The Limits of Empathy - UXLibs8
cassininazir
1
350
The Spectacular Lies of Maps
axbom
PRO
1
800
Being A Developer After 40
akosma
91
590k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
1.7k
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 ͷϝιουͷςετॻ͖ͮΒ͍😇
·ͱΊ ᶃ Ϣχοτςετ͕ॻ͖͍͢ઃܭߜΒΕΔ ᶄ ͷॴࡏ͕ڞଘ͍ͯ͠ΔͱɺςετΫϥε͕ཎΉ ᶅ ԿͰΔΫϥεɺςετ͕ͭΒ͍🥺