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
Mockeryを読んでみたよ
Search
chiroruxx
January 18, 2023
Technology
400
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Mockeryを読んでみたよ
第148回 PHP勉強会@東京のLTで使用したスライドです。
chiroruxx
January 18, 2023
More Decks by chiroruxx
See All by chiroruxx
Contextとはなにか
chiroruxx
1
520
初心者エンジニアから中級者エンジニアになるためにオススメの1冊
chiroruxx
0
170
Laravelのパッケージ全部紹介する
chiroruxx
2
210
Gopher のための「自由な話し合い」ワークショップ
chiroruxx
0
63
PHPをGoで動かす
chiroruxx
0
130
Goを使ってTDDを体験しよう!
chiroruxx
1
1.4k
今ならできる!PhpStormプラグイン開発
chiroruxx
0
140
Go Connectへの想い
chiroruxx
0
250
eBPF with PHPをさわる
chiroruxx
0
220
Other Decks in Technology
See All in Technology
AIコーディングはじめました
miso
0
130
リアクションエンジニア ― 小さなリアクションから、話しかけやすい組織をつくる ―
ryuguyana
1
110
Flutter Webを「SDK」として配る — 50以上のゲームを支える埋め込みアーキテクチャ
masayukisuda
0
170
E2Eってつらくないですか?E2Eの管理コストを下げる手段とその実践
akaboshinit
6
2.9k
Devin プロモコードの受け取りと適用(2026.10.27)
kinopeee
1
820
Consistency for mere mortals - A gentle guide to consistency
ufried
0
120
1週間、Opus封印でお願いします
kintotechdev
1
430
Adversary Infrastructure Hunting101
disconinja
0
190
慶應義塾大学 AI実践05 論文執筆の仕組み化
keio_smilab
PRO
4
3.2k
[FECONF-2026] 신대륙을 향한 카카오페이증권 프론트엔드 항해기 (이주승)
l1ghthouse
0
350
あなたのDartコードは丸見えかもしれない ~reFlutterで暴くFlutterアプリの中身と多層防御~
zozotech
PRO
0
740
「作れる」と「リリースできる」の間にあったもの── PdMを外して見えた、PdMがやるべき仕事/Buildable ≠ Shippable: What Removing the PM Taught Me About the PM’s Job
yappli_developers
0
420
Featured
See All Featured
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
450
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
GraphQLとの向き合い方2022年版
quramy
50
15k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
50
10k
Speed Design
sergeychernyshev
33
2.2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
My Coaching Mixtape
mlcsv
0
370
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.7k
Odyssey Design
rkendrick25
PRO
2
910
Transcript
Mockeryを 読んでみたよ 2023/01/18 第148回 PHP勉強会@東京
コードリーディングの歴史 第146回PHP勉強会 ▪ 柚⼝ ましろう さん 第147回PHP勉強会 ▪ y_sone さん
流れにのっていくしかない!! 🌊
> whoami ▪ 名前: ちひろ/ちろ/前⽥ ▪ Twitter: @chiroruxxxx ▪ 所属:
東京都⽴産業技術⼤学院⼤学 産業技術専攻 情報アーキテクチャコース 追川PBL ▪ テーマ: スクラムの理論と実践
Mockery とは ▪ モック(テストダブル)を作ってくれるライブラリ ▪ ユニットテストとかで便利
サンプルコード $user = Mockery::mock(User::class); $user->shouldReceive('getFullName') ->andReturn('テスト太郎'); $decorator = new Decorator();
$this->assertSame( '** テスト太郎 **', $decorator->decorate($user) );
サンプルコード $user = Mockery::mock(User::class); $user->shouldReceive('getFullName') ->andReturn('テスト太郎'); $decorator = new Decorator();
$this->assertSame( '** テスト太郎 **', $decorator->decorate($user) ); モックしたクラスは Userクラス︖
サンプルコード $user = Mockery::mock(User::class); $user->shouldReceive('getFullName') ->andReturn('テスト太郎'); $decorator = new Decorator();
$this->assertSame( '** テスト太郎 **', $decorator->decorate($user) ); モックしたクラスは Userクラス︖ コンストラクタで 引数が必要だけど︖
サンプルコード $user = Mockery::mock(User::class); $user->shouldReceive('getFullName') ->andReturn('テスト太郎'); $decorator = new Decorator();
$this->assertSame( '** テスト太郎 **', $decorator->decorate($user) ); モックしたクラスは Userクラス︖ コンストラクタで 引数が必要だけど︖ こんなメソッド ないけど︖
いったい何者なんだ・・・!!
コードを読んで追っていくぞ 💪
今回の⽬的 ▪ Mockery::mock(User::class) で作成されるクラスが何者なのかを追ってく ▪ 今回紹介しない話も、ブログに書いています – あしたからがんばる - Mockeryでのモックの作り⽅を調べてみた
コードを読む ▪ PhpStorm + XDebug で1⾏ずつ状態を確認しながら読める ▪ 設定がちょっとだけ⾯倒 – PHPerKaigi
2023で・・・?
None
さっそく読んでいきます
クラス定義の読み込み ▪ ファイルを読み込んでる ▪ 展開してみると・・・? $code = file_get_contents(__DIR__ . '/../Mock.php');
クラス定義の読み込み $code = ' <?php class Mock implements MockInterface {
// ... public function shouldReceive(...$methodNames) { // ... ';
クラス定義の読み込み $code = ' <?php class Mock implements MockInterface {
// ... public function shouldReceive(...$methodNames) { // ... '; 💡 モックでメソッドが使える
クラスの継承 ▪ Userクラスを継承するように書き換えてる $code = str_replace( "implements MockInterface", "extends ¥¥"
. $className . " implements MockInterface", $code );
クラスの継承 $code = ' <?php class Mock extends ¥App¥Models¥User implements
MockInterface { // ... public function shouldReceive(...$methodNames) { // ... '; 💡 User 型として扱える
クラスのロード ▪ ええい! eval じゃい! ▪ ただの⽂字列から実⾏可能な状態に eval("?>" . $definition->getCode());
クラスのロード ▪ クラスはできた!!インスタンスはまだ作ってない <?php class Mock extends ¥App¥Models¥User implements MockInterface
{ // ... public function shouldReceive(...$methodNames) { // ...
インスタンスの作成 ▪ リフレクションクラスを使って 💡コンストラクタ実⾏無しでインスタンスを作 成 ▪ ・・・⿊魔術だ!! $reflectionClass = $this->getReflectionClass($className);
return $reflectionClass->newInstanceWithoutConstructor();
インスタンスの作成
まとめ ▪ モックしたクラスは User クラス? – User クラスを継承しているのでUser型として扱える ▪ shouldRecieve
なんてメソッドは User クラスにないけど? – User クラスを継承した先で定義しているので使える ▪ コンストラクタで引数が必要だけど? – リフレクションを使ってコンストラクタ無しで⽣成してる
まとめ ▪ みんなが使ってるあのライブラリも、蓋を開ければ案外ゴリ押し ▪ みんなも気軽にゴリ押しでライブラリを作ってみては?