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
継続的にLaravelのUnitTestを書く上で 気をつけていること
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
stwile
September 28, 2021
Technology
240
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
継続的にLaravelのUnitTestを書く上で 気をつけていること
stwile
September 28, 2021
More Decks by stwile
See All by stwile
Fat Controller は悪か? ~光のFat Controller・闇のガリController~
stwile
2
1.8k
フレームワークが生み出す負債や複雑さに対して、PHPUnitと付き合っていく
stwile
1
2.7k
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
0
150
Other Decks in Technology
See All in Technology
あなたの AI ワークスペースに、 専門コーダーを連れてくる - Amazon Quick Desktop 最新情報
kawaji_scratch
1
130
AI-DLCを活用した高品質・安全なAI駆動開発実践 / AI Driven Development with AI-DLC
yoshidashingo
0
170
2026TECHFRESH畢業分享會 - Lightning Talk - E起 See See : 電商推薦讀心術? 數據說了算
line_developers_tw
PRO
0
810
チームで進めるAI駆動アジャイル×ウォーターフォール
kumaiu
0
150
フロンティアAIのゲート化と地政学リスク
nagatsu
0
130
手塩にかけりゃいいってもんじゃない
ming_ayami
0
420
AIのReact習熟度を測る
uhyo
1
170
RSA暗号を手計算したくなること、ありますよね?? (20260615_orestudy6_rsa)
thousanda
0
250
LLMにもCAP定理があるという話
harukasakihara
0
300
MCP Appsを作ってみよう
iwamot
PRO
4
540
AGENTS.mdとSkillsで始めるAIエージェント活用
sonoda_mj
3
200
Oracle AI Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
6
1.5k
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Marketing to machines
jonoalderson
1
5.4k
Rails Girls Zürich Keynote
gr2m
96
14k
Mobile First: as difficult as doing things right
swwweet
225
10k
Navigating Team Friction
lara
192
16k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
830
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
190
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Evolving SEO for Evolving Search Engines
ryanjones
0
210
Joys of Absence: A Defence of Solitary Play
codingconduct
1
390
Transcript
継続的にLaravelのUnitTestを書く上で 気をつけていること 2021 年 9 月 28 日 ( 火
)
自己紹介 hyroky.(@handm871) 専門領域: 趣味: バックエンド🍓 ユニットテスト・設計 筋トレ🦾
テストとPHP周りについて • テスト駆動開発歴:5年 • PHP歴:5年 • Laravel歴:3年
テストコードについて思うこと • サンプルコードが圧倒的に少ない • プロダクトコードの設計は たくさん ある ◦ テストコードの設計は あんまり
ない
伝えたい人 テスト駆動開発に • 興味がある人 • 初心者
今日話すこと UnitTest について 依存注入できる形で実装する
サンプルコード class FetchTodoService { public function exec(string $user_id, int $todo_id):
array { $todo_repos = new TodoRepository(); $result = $todo_repos->fetchTodo($todo_id, $user_id); if ($result === []) { throw new NotFoundException('Todoのデータが存在しません'); } return $result; } }
何がまずいのか? • TodoRepositoryに依存している ◦ メソッド内で `new` している ◦ 代替できないことが問題 ◦
DBにテストデータを作る必要がある
Feature Test • [GET]: /api/todo • 実データを用いて機能の振る舞いを検証
Unit Test • TodoListController ◦ TodoFetchService ▪ TodoRepository
Unit Test TodoListController Jsonを返す FetchTodoService DBから受け取った値を検証する TodoRepository DBへ接続・SQLを実行
Unit Test TodoListController Jsonを返す FetchTodoService DBから受け取った値を検証する TodoRepository DBへ接続・SQLを実行 FetchTodoService はDB接続の責務は不要
変更前 class FetchTodoService { public function exec(string $user_id, int $todo_id):
array { $todo_repos = new TodoRepository(); $result = $todo_repos->fetchTodo($todo_id, $user_id); if ($result === []) { throw new NotFoundException('Todoのデータが存在しません'); } return $result; } }
変更後 class FetchTodoService { public function exec( string $user_id, int
$todo_id, TodoRepository $todo_repos, ): array { $result = $todo_repos->fetchTodo($todo_id, $user_id); if ($result === []) { throw new LogicException('Todoのデータが存在しません'); } return $result; } } TodoRepository を外部から注入
テストのサンプル class FetchTodoServiceTest extends TestCase { /** @test */ public
function データが存在しない場合、空の配列を返すべき (): void { $user_id = 'user_id'; $todo_id = 999999; $todo_repos_mock = Mockery::mock(TodoRepository ::class); $todo_repos_mock ->shouldReceive ('fetchTodo') ->once() ->with($user_id, $todo_id) ->andReturn([]); $service = new TodoService(); parent::expectedException (LogicException ::class); parent::expectExceptionMessage ('Todoのデータが存在しません '); $service->exec($user_id, $todo_id, $todo_repos_mock ); } } 引数を設定 返り値を設定
何がまずいのか? • TodoRepositoryに依存している ◦ メソッド内で `new` している ◦ 代替できないことが問題 ◦
DBにテストデータを作る必要がある
テスト駆動開発から見えるもの • テストを書くことが大変...とおもったら ◦ 設計を見直すいい機会
おわり