Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
継続的にLaravelのUnitTestを書く上で 気をつけていること
Search
stwile
September 28, 2021
Technology
260
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
継続的にLaravelのUnitTestを書く上で 気をつけていること
stwile
September 28, 2021
Transcript
18 slides
継続的に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にテストデータを作る必要がある
テスト駆動開発から見えるもの • テストを書くことが大変...とおもったら ◦ 設計を見直すいい機会
おわり
More Decks by stwile
See All by stwile
Fat Controller は悪か? ~光のFat Controller・闇のガリController~
stwile
2
2k
フレームワークが生み出す負債や複雑さに対して、PHPUnitと付き合っていく
stwile
1
2.9k
TDD視点から見る、Laravel・Requestクラスの依存性
stwile
0
160
Other Decks in Technology
See All in Technology
Kiro アップデート 2026 年 11 月号 (11 月 8 日時点) | 目指せ Kiro マスター アップデート編
inariku
PRO
2
130
属人化したISMS運用をGitHubとAIで仕組み化した1年間の記録
sioncojp
4
2.7k
速くなるほど、深く考える AI時代のQAに学ぶ
kobayashiyorimitsu
0
240
AI時代における医療情報システムのセキュリティ / Security of Healthcare IT Systems in the AI Era
henryofficial
0
180
コピーオンライト型ファイルシステムと ランダム書き込み
sat
PRO
2
110
ラクスル会社紹介資料|エンジニア向け / RAKSUL Introduction for Engineers
raksulrecruiting
0
120
おうち環境でも「なるべく止めたくない!」 ― おうち環境の機器冗長化の理想と現実
yuutosi
1
440
異夢同船〜異なる夢でも同じ船でなんとかする〜
fortegp05
1
380
閉域網でのAPI公開の選択肢全部比較!
takano0131
2
340
お風呂場をサーバルームにしよう
yusuke427
0
220
変更差分を読むために環境を整備しよう
pipepasta
1
390
プロダクションコードにおける encoding/json から encoding/json/v2 へのスムーズな移行
sansantech
PRO
1
440
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
680
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
900
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
380
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Claude Code のすすめ
schroneko
67
230k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
560
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
750
The SEO Collaboration Effect
kristinabergwall1
1
620
Prompt Engineering for Job Search
mfonobong
0
590
Git: the NoSQL Database
bkeepers
PRO
433
67k