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.9k
フレームワークが生み出す負債や複雑さに対して、PHPUnitと付き合っていく
stwile
1
2.8k
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
0
150
Other Decks in Technology
See All in Technology
Control Planeで育てるBtoB SaaSの認証基盤 - SRE NEXT 2026
pokohide
1
2.6k
「早く出す」より「事業に効く」 ── 顧客の業務サイクルから逆算するAI時代の二重ループ開発と「変化の設計者」 / devsumi2026
rakus_dev
1
400
Amazon EVS で VCF 9.0 / 9.1 のサポート開始まとめ
mtoyoda
0
310
脱金融のフューチャー・デザイン / Future Design Beyond Finance
ks91
PRO
0
160
LLMやAIエージェントをソフトウェアに組み込むプラクティス
shibuiwilliam
2
420
「AIに依存している」と 「AIを使いこなしている」の違い
k8yasuma
0
120
シンガポールで登壇してきます
yama3133
0
250
カードゲーム作りが教えてくれた プロダクトオーナーシップ
moritamasami
0
110
Webアプリ認証の全体像 / The Big Picture of Web App Authentication
kitano_yuichi
0
180
そのドキュメント、自動化しませんか?
yuksew
1
300
オブザーバビリティ、本当に活用できてる? 〜API連携×生成AIで成熟度を自動評価〜
dmmsre
1
3.6k
OPENLOGI Company Profile for engineer
hr01
1
74k
Featured
See All Featured
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
260
Become a Pro
speakerdeck
PRO
31
6k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Designing Powerful Visuals for Engaging Learning
tmiket
1
450
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
410
Automating Front-end Workflow
addyosmani
1370
210k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
610
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
170
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にテストデータを作る必要がある
テスト駆動開発から見えるもの • テストを書くことが大変...とおもったら ◦ 設計を見直すいい機会
おわり