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
470
初心者エンジニアから中級者エンジニアになるためにオススメの1冊
chiroruxx
0
150
Laravelのパッケージ全部紹介する
chiroruxx
2
160
Gopher のための「自由な話し合い」ワークショップ
chiroruxx
0
56
PHPをGoで動かす
chiroruxx
0
120
Goを使ってTDDを体験しよう!
chiroruxx
1
1.4k
今ならできる!PhpStormプラグイン開発
chiroruxx
0
130
Go Connectへの想い
chiroruxx
0
250
eBPF with PHPをさわる
chiroruxx
0
210
Other Decks in Technology
See All in Technology
[2026 Oracle Technical Deep Dive] AI時代のデータベース基盤をどう選ぶ? Exadata Database Serviceの選択肢と使い分け (2026年9月17日開催)
oracle4engineer
PRO
0
210
[2026 Oracle Technical Deep Dive] AI時代のアプリケーションを支えるCloud NativeとJava EEモダナイゼーション (2026年9月17日開催)
oracle4engineer
PRO
0
110
IR Today: Theory, Practice, and Agents
dtunkelang
0
340
生成AIを使って「人が」考える技術 ― AI時代の人機共想と実践ノウハウ|UNITT AC2026
ishiirikie
0
540
SREでアラート疲れを 解決しよう!
kairim0
1
120
The kernel report
ennael
PRO
1
230
検証フェーズはAIの回答を判断するための学習機会
toru_kubota
2
470
The seven pitfalls of AI (revised version)
ufried
0
230
AIは爆速なのに、私が詰まっていた話 ― 音声入力と鳴くマスコットでボトルネックを削る
yama3133
0
490
2026-09-26 Platform Engineering Kaigi 2026 インフラとアプリの境界線と委譲の設計 / Drawing the Infra and App Line
masasuzu
0
620
【データ横丁主催】AI Agentがコンテキストを使って仕事をした後、何が残るのか― 組織の経験を次の判断に引き継ぐ「Agent Memory」
shisyu_gaku
2
320
顧客の成果創出とプロダクトの成長を 両立するためのFDE
sansantech
PRO
0
650
Featured
See All Featured
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
590
How to make the Groovebox
asonas
2
2.5k
Navigating Team Friction
lara
192
16k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.3k
Git: the NoSQL Database
bkeepers
PRO
433
67k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.3k
Crafting Experiences
bethany
1
370
Paper Plane (Part 1)
katiecoart
PRO
2
11k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
530
A better future with KSS
kneath
240
18k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
550
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 クラスを継承した先で定義しているので使える ▪ コンストラクタで引数が必要だけど? – リフレクションを使ってコンストラクタ無しで⽣成してる
まとめ ▪ みんなが使ってるあのライブラリも、蓋を開ければ案外ゴリ押し ▪ みんなも気軽にゴリ押しでライブラリを作ってみては?