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
実践DIコンテナ
Search
Sota Sugiura
April 20, 2016
Technology
0
4.8k
実践DIコンテナ
DIコンテナを使って役に立った場面をご紹介
PHP BLT#4 (
http://phpblt.connpass.com/event/27655/
) 発表資料
Sota Sugiura
April 20, 2016
Tweet
Share
More Decks by Sota Sugiura
See All by Sota Sugiura
内製したSlack Appで頑張るIncident Response@Waroom Meetup #1 / Incident Response with Slack App in 10X
sota1235
0
1k
20220926_セキュリティチームの今_for_Drs._Prime_公開用.pdf
sota1235
0
63
再発防止策を考える技術 / #phpconsen
sota1235
10
3.6k
How to choose the best npm module for your team?
sota1235
9
530
Realtime Database for high traffic production application
sota1235
7
3.9k
Road to migrate JP Web as a microservice
sota1235
4
1.5k
インターフェース再入門 / Think Interface again
sota1235
6
10k
再発防止策を考える技術 #phpconfuk_rej
sota1235
1
1.1k
Update around Firebase #io18
sota1235
3
4.2k
Other Decks in Technology
See All in Technology
Qdrant を用いた検索改善施策の紹介 / Search Engineering Tech Talk 2024 Summer
visional_engineering_and_design
1
210
Grafana エコシステムの活用事例 on ABEMA
tetsuya28
4
260
組織デバイスのための効率的なアプリケーション更新戦略
kenchan0130
0
260
DenoでもViteしたい!インポートパスのエイリアスを指定してラクラクアプリ開発
bengo4com
2
2k
コード✕AIーソフトウェア開発者のための生成AI実践入門~
yuhattor
4
850
Vespaを利用したテクいベクトル検索
szdr
2
180
Low Latency Join Method for Distributed DBMS
yugabytejapan
0
180
プロダクト価値を考えるための情報透明化とチーム文化づくり
nyo_taro
1
150
業務ヒアリングと知識の呪い
tamai_63
0
290
【shownet.conf_】ShowNet 2024 ~ Inter * Network ~
shownet
PRO
0
540
AWSの初級者向けAI・ML資格『AWS Certified AI Practitioner』の傾向と対策/So You Want To Pass AWS Certified AI Practitioner
quiver
0
470
LINEヤフー新卒採用 コーディングテスト解説 アルゴリズム問題編
lycorp_recruit_jp
0
13k
Featured
See All Featured
Ruby is Unlike a Banana
tanoku
96
11k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
23k
Testing 201, or: Great Expectations
jmmastey
38
7k
A designer walks into a library…
pauljervisheath
202
24k
How to train your dragon (web standard)
notwaldorf
87
5.6k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
The Pragmatic Product Professional
lauravandoore
31
6.2k
Building Applications with DynamoDB
mza
90
6k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.6k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
RailsConf 2023
tenderlove
28
840
The Straight Up "How To Draw Better" Workshop
denniskardys
232
130k
Transcript
࣮ફ%*ίϯςφ PO-BSBWFM !TPUB 1)1#-5
ࣗݾհ w ਿӜᰜଠ 4PUB4VHJVSB w !TPUB w ͪͺʔྺ w
גࣜձࣾΞΠελΠϧ ʘ'PMMPXNFʗ
ΈΜͳʮ%*ศརͩͧʂʯ
ʮͲ͏͍͏෩ʹʜʁʯ
ͯ͜ͱͰ
%*ίϯςφͷΛ͠·͢ ͬͯΔͧͬͯํπΠολʔͯ͠Ͳ͏ͧ Ňŏ﹏ŏ
%*ͱ w %*%FQFOEFODZ*OKFDUJPO w ʮґଘੑͷೖʯͱݴ͏ͱ͔ݴΘͳ͍ͱ͔ w ৄ͍͠આ໌࣌ؒͷ߹্Χοτ ứʆ
%*ίϯςφͱ w %*ύλʔϯͰґଘղܾ͞ΕͨΠϯελϯεΛฦ ͯ͘͠ΕΔγϩϞϊ w ༷ʑͳ࣮͕͋Δ͕ɺ͜͜Ͱ-BSBWFMΛྫʹ
-BSBWFMʹ͓͚Δ %*ίϯςφ
%*$POUBJOFSPO-BSBWFM w ίϯετϥΫλͰλΠϓώϯτ͓͍ͯͨ͠Ϋϥε Λ%*ίϯςφ͕ೖͯ͘͠ΕΔ w ඞཁͳΫϥεʹ͞ΒʹඞཁͳΠϯελϯε͕͋ Ε࠶ؼతʹΠϯελϯεΛೖ͢Δ
%*$POUBJOFSPO-BSBWFM #SFBE
%*$POUBJOFSPO-BSBWFM #SFBE *OFFE #BDPO -FUUVDFBOE5PNBUP public function __construct( Bacon $bacon,
Lettuce $lettuce, Tomato $tomato ) { // }
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS (JWFZPV #SFBE *OFFE #BDPO -FUUVDFBOE5PNBUP #BDPO -FUUVDF 5PNBUP
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS *OFFE4BMU #SFBE #BDPO -FUUVDF 5PNBUP public function __construct(Salt
$salt) { // }
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS (JWFZPV *OFFE4BMU #SFBE #BDPO -FUUVDF 5PNBUP 4BMU
%*$POUBJOFSPO-BSBWFM %*$POUBJOFS /FFE /FFE /FFE #SFBE #BDPO -FUUVDF 5PNBUP 4BMU
/FFE
$MBTT#JOEJOH w %*ίϯςφʹ͋Β͔͡ΊΠϯελϯεΛಥͬࠐ ΊΔ
$MBTT#JOEJOH <?php /* ུ */ /** * */ public function
boot() { $this->app->bind( // ίϯετϥΫλͰλΠϓώϯτ͞ΕΔΫϥε໊ \App\Dinners\PotatoTips::class, function ($app) { // ࣮ࡍʹฦ͞ΕΔΠϯελϯε return new \App\Dinners\Blt; } ); }
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS $this->app->bind( \App\Dinners\PotatoTips::class, function ($app) {
return new \App\Dinners\Blt; } );
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS (JWFZPV #MU
$MBTT#JOEJOHT "QQMJDBUJPO *OFFE 1PUBUP5JQT %*$POUBJOFS (JWFZPV #MU ˞௨ৗͷλΠϓώϯτͱಉ༷ɺ ܧঝؔʹͳ͍ͱΤϥʔʹͳΓ·͢
͜͜·Ͱલ࠲
ຊ ͜ͷ%*ίϯςφ͕ʹཱͬͨ໘Λ͝հʂʂʂ
ͦͷ ࣮ͷࠩ͠ସ͑
Ғ͍ਓʮநʹґଘͤΑʯ w Α͋͘Δ%*ίϯςφͷ͍ํͱͯ͠நΫϥε ʹ۩ΫϥεΛ$MBTT#JOEJOH͢Δ
*OUFSGBDFBOE$MBTT interface Burger { public function addTopping($toppingName); } class Blt
implements Burger { public function addTopping($toppingName) { // } }
#JOE*OUFSGBDF /* in DI Container */ public function boot() {
// ΠϯλʔϑΣʔεʹ۩ΫϥεΛbind // BurgerΠϯλʔϑΣʔεΛλΠϓώϯτ͢Δͱ // BltΫϥε͕ೖ͞ΕΔ $this->app->bind(Burger::class, Blt::class); }
ಥવͷϦΫΤετ ʮԶ#-5͡Όͳͯ͘ςϦϠΩόʔΨʔ͕৯͍ͨʯ
ʮطଘίʔυΛॻ͖͑ͣʹ ཁʹ͍͑ͨʯ
ͦ͏ͩʂநͩʂ w ͜ͷ࣌ɺٻΊΒΕͯΔͷ#VSHFSΠϯλʔ ϑΣʔεΛ࣮ͨ͠৽ͨͳόʔΨʔ w ͭ·Γ෦తͳ࣮͕Ͳ͏มΘΖ͏ͱͦͷந Λ࣮͢Εࠩ͠ସ͕͑Մೳ
/FX$MBTT interface Burger { public function addTopping($toppingName); } class Blt
implements Burger { public function addTopping($toppingName) { // } } class Teriyaki implements Burger { public function addTopping($toppingName) { // } }
$IBOHF$MBTT#JOEJOH /* in DI Container */ public function boot() {
// ݩʑͷΫϥε // $this->app->bind(Burger::class, Blt::class); // ৽ࠩ͘͠͠ସ͑ΔΫϥεΛBinding $this->app->bind(Burger::class, Teriyaki::class); }
.JTTJPO$PNQMFUF 㱼ʆ w நͱ%*ίϯςφΛ͏·͘ѻͬͯطଘίʔυʹ खΛೖΕͣʹ৽͍͠ίʔυʹࠩ͠ସ͑Δ͜ͱ͕ Մೳ w %*ίϯςφ͚ͩͷͰͳ͍ΜͰ͕͢ศརͩΑ ͱ͍͏ʜͶʜ
ͦͷ ςετ࣮
ྫ͑͜ΜͳςετΛॻ͖͍ͨ
501ϖʔδʹΞΫηεͨ͠Β ͪΌΜͱϖʔδ͕දࣔ͞ΕΔ͔
"QQMJDBUJPO5FTUJOH w %#ʹ͍߹Θͤͯฦ͖ͬͯͨͷ͕501ϖʔδ ʹग़Δͣʜ w ͖ͪΜͱɺ͔ͭ%#͔ΒͷϨεϙϯε͕ࡌͬ ͯΕ0,
"QQMJDBUJPO5FTUJOH /** * TOPϖʔδʹΞΫηεͨ͠Β’username'ͷจࣈྻ͕͋Γɺ * HTTPεςʔλείʔυ͕200Ͱ͋Δ͜ͱ */ public function testAccessTopPage()
{ $this->visit('/top') ->see(‘username') ->assertResponseOk(); }
Έ w ςετέʔε࣮ߦ͞ΕΔʹ%#ΞΫηε͢Δͷ πϥΠ w ޭ༻ࣦഊ༻ͷσʔλΛέʔε͝ͱʹ༻ҙ͢Δ ͷπϥΠ w ςετલޙͰ%#ͷ͓আͱ͔πϥΠ
6TF%*$POUBJOFS ͦΕɺ%*ίϯςφͰղܾͰ͖·͢Α
$MBTT#JOEJOH w σʔλʹΞΫηεͯ͠ΔΫϥε%*ίϯςφ͔ Βೖ͞Ε͍ͯΔ w ςετத͚ͩσʔλΞΫηεΫϥεʹߜͬͯϞο ΫΫϥεʹ#JOEJOHͯ͠͠·͓̺͑
.BLJOH.PDL$MBTT // σʔλΞΫηεΫϥεͷϞοΫ࡞ $userMock = \Mockery::mock(\User::class); $userMock->shouldReceive(‘getUsers')->andReturn('username');
#JOEJOH.PDL$MBTT // ϞοΫΫϥεΛBinding $this->app->bind(\User::class, function () use ($userMock) { return
$userMock; });
5FTUJOH public function testAccessTopPage() { // σʔλΞΫηεΫϥεͷϞοΫ࡞ $userMock = \Mockery::mock(\User::class);
$userMock->shouldReceive('getUsers')->andReturn('username'); // ϞοΫΫϥεΛBinding $this->app->bind(\User::class, function () use ($userMock) { return $userMock; }); // ςετ࣮ߦ $this->visit('/top') ->see('username') ->assertResponseOk(); }
.JTTJPO$PNQMFUF 㶅㱿㶅 w ष͍ͷʹ֖ςετͷతΛՌͨͨ͢ΊʹΞϓ ϦέʔγϣϯͷҰ෦͚ͩϞοΫͰࠩ͠ସ͑Δ ͜ͱ͕Ͱ͖ͨʂ w ΞΫηεςετͰͳ͍ͯ͘Ζ͍Ζͳͱ͜ΖͰ Ԡ༻Մೳ w
"1*ΞΫηεͷϞοΫͱ͔
·ͱΊ w %*ίϯςφΫϥεͷґଘ͕ղܾ͞ΕͨΠϯε λϯεΛฦͯ͘͠ΕΔຐ๏ͷശ w ͏·͘པΕςετ࣮ͷࠩ͠ସ͕͑ͱͯ ؆୯@ ᵇʮЏ @ w
ΠέͯΔ%*ίϯςφϥΠϒϥϦҰഋ͋ΔͷͰ ֤ࣗ(PPHMFઌੜʂ
Ҏ্ʂ