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
stwile
September 28, 2021
Technology
250
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.8k
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
0
150
Other Decks in Technology
See All in Technology
Multi-Agent並列開発を 安全に回すための技術 / Technology for Safely Multi-Agent Parallel Development
tooppoo
0
250
Agentic AI 時代のテスト手法: Kiro とはじめるプロパティベーステスト (AWS Summit Japan 2026 | DEV212)
ymhiroki
0
200
AIエージェントとPhysical AIが拓く製造業の変革(ハノーバーメッセリキャップ)
iotcomjpadmin
0
210
トークン最適化のためのユーザーストーリー分析 / User Story Analysis for Token Optimization
oomatomo
0
160
作る力から、見極める力へ — AI時代に広がるエンジニアの価値と役割
rince
0
410
デジタル・デザイン:次の50年を描く「進化する青写真」
y150saya
0
800
きのこカンファレンス2026_肩書きを外したとき私は誰か
yamasatimi
1
130
知見・人・API・DB・予算 ─ ナイナイ尽くしだった人事データ整備 with dbt、5年間の学び
ken6377
1
140
アラート調査向けAIエージェントの本番導入とその後/AI Agents for Alert Investigation: Production Deployment and After
taddy_919
1
330
“全部コピーしない”ファイルデータの活用 : — FSx for ONTAP × S3 Tables × Icebergで作るメタデータカタログ
yoshiki0705
0
340
Docker Desktop不要の時代が来る? WSL標準の「wslc」で Linuxコンテナを動かしてみた.
ueponx
0
600
AI・ロボティクスと自動化社会 / AI, Robotics, and the Automated Society
ks91
PRO
0
150
Featured
See All Featured
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
The SEO identity crisis: Don't let AI make you average
varn
0
510
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
Skip the Path - Find Your Career Trail
mkilby
1
160
How Software Deployment tools have changed in the past 20 years
geshan
0
34k
4 Signs Your Business is Dying
shpigford
187
22k
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
250
Done Done
chrislema
186
16k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
380
The agentic SEO stack - context over prompts
schlessera
0
840
エンジニアに許された特別な時間の終わり
watany
107
250k
The Curious Case for Waylosing
cassininazir
1
420
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にテストデータを作る必要がある
テスト駆動開発から見えるもの • テストを書くことが大変...とおもったら ◦ 設計を見直すいい機会
おわり