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
Phakeで簡単 モックオブジェクト作成
Search
Katsuhiro Ogawa
July 02, 2012
Programming
1
3.4k
Phakeで簡単 モックオブジェクト作成
2012.03.27 PHP勉強会@東京 #58
Katsuhiro Ogawa
July 02, 2012
Tweet
Share
More Decks by Katsuhiro Ogawa
See All by Katsuhiro Ogawa
新規プロダクト開発に伴う既存マイクロサービスのリアーキテクティングとその後
fivestar
1
140
Goで学ぶSOLID原則
fivestar
0
300
メルカリ CSE チームの 歩みと取り組み / Steps and Tries in the CSE team for Mercari
fivestar
2
890
Go in Corporate Solutions Engineering
fivestar
0
2.4k
いつまでPHP 5.x使ってるの
fivestar
1
1.9k
PHP のドキュメントを読んで PHP のことをもっと知ろう
fivestar
1
3.2k
CrocosSecurityBundleを Symfony3.0に対応した話
fivestar
0
240
PHP BLT だけど HTML5 の コンテンツモデルのお話
fivestar
0
2.4k
THE NEW "PERFECT PHP" WILL BE COMING SOON
fivestar
0
8k
Other Decks in Programming
See All in Programming
AHC041解説
terryu16
0
400
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
190
[JAWS-UG横浜 #80] うわっ…今年のServerless アップデート、少なすぎ…?
maroon1st
0
100
快速入門可觀測性
blueswen
0
500
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
170
Alba: Why, How and What's So Interesting
okuramasafumi
0
210
DMMオンラインサロンアプリのSwift化
hayatan
0
190
Jaspr Dart Web Framework 박제창 @Devfest 2024
itsmedreamwalker
0
150
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
630
ATDDで素早く安定した デリバリを実現しよう!
tonnsama
1
1.9k
Amazon Nova Reelの可能性
hideg
0
200
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
Featured
See All Featured
Building Applications with DynamoDB
mza
93
6.2k
What's in a price? How to price your products and services
michaelherold
244
12k
Navigating Team Friction
lara
183
15k
Become a Pro
speakerdeck
PRO
26
5.1k
Being A Developer After 40
akosma
89
590k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
Six Lessons from altMBA
skipperchong
27
3.6k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
GraphQLとの向き合い方2022年版
quramy
44
13k
GitHub's CSS Performance
jonrohan
1030
460k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Transcript
PhakeͰ؆୯ ϞοΫΦϒδΣΫτ࡞ Katsuhiro OGAWA @fivestr 2012.03.27 - PHPษڧձ@౦ژ #58
About Me খ ༤େ - OGAWA Katsuhiro @fivestr גࣜձࣾΫϩίε ձࣾͰߴڮͬͯݺΕͯ·͢
ύʔϑΣΫτPHPॻ͖·ͨ͠ 5݄ʹPHPͷຊग़͢Έ͍ͨͰ͢ (৭ʑະఆ) OOPɺMVCɺDDD 2
Agenda ελϒͱϞοΫ Phakeͱ Phake API PhakeͷΠϯετʔϧ 3
ςετ͕͍ʁ DBIOɺWeb APIͳͲͷΞΫηεΛ͏ॲཧͷςε τͬͯͲ͏ͯ͠·͔͢ʁ DBΞΫηεͯͨ͠Βςετ͕͘ͳͬͨΓ͢Δ͠ɺWeb APIͩͱωοτϫʔΫʹͭͳ͕ͬͯͳ͍ͱςετͰ͖ͳ͍ Ͱ͢ΑͶ ͦΜͳͱ͖ελϒͷग़൪Ͱ͢ 4
ελϒͱ લड़ͷΑ͏ͳ֎෦ͷԿ͔ͷΞΫηεͷΘΓʹఆΛ ฦ͢Α͏ʹͨ͠ͷ͕ελϒ stub out - Έফ͢ ैདྷ:ɹɹreturn $webservice->api($id); ελϒ:ɹreturn
[‘id’ => ‘1’, ‘name’ => ‘takahashi’]; ఆʹஔ͖͑Δ 5
αϯϓϧ FacebookΫϥε Facebook͕ఏڙ͢Δ PHP SDKɻ api()ϝιουͰAPI ΞΫηε ClientΫϥε FacebookΦϒδΣΫ τΛͬͯσʔλΞΫ
ηε 6
ελϒͰࠩ͠ସ͑ FacebookΫϥεͷ api()ϝιουΛ্ॻ͖ ͯ͠ఆΛฦ͢Α͏ʹ ͢ΕɺWeb APIͷ ΞΫηεΛͳͤ͘Δ ্ҐϞδϡʔϧ ԼҐϞδϡʔϧ 7
ελϒͷ׆༻ ελϒΛ͏ͱɺDBWeb APIͱ͍ͬͨɺ֎෦ͷԿ͔ ͷґଘΛΓ͢͜ͱ͕Մೳ ֎෦ͷԿ͔ͷΞΫηε͕Ͱ͖ͳ͍ঢ়ଶͰɺΞΫηε ॲཧΛελϒʹஔ͖͑Δ͜ͱͰ্ҐϞδϡʔϧͷςε τ͕Մೳ 8
ϞοΫΦϒδΣΫτͱ ϞοΫΦϒδΣΫτελϒͷҰछ ελϒ͕ԿΒ͔ͷಈ࡞ͷ༻Ͱ͔͠ͳ͍ͷʹରͯ͠ɺ ϞοΫΦϒδΣΫτ্ҐϞδϡʔϧ͕ԼҐϞδϡʔϧ Λਖ਼͘͠༻͍ͯ͠Δ͔Λςετ͢ΔͨΊͷͷ 9
ϞοΫΦϒδΣΫτͷ׆༻ DBʹอଘ͢ΔΑ͏ͳΓΛΘͳ͍ϝιουΛελϒ Խ͍ͨ͠ͱ͖ɺΓ͕ԿͰ͋Δ͔Ͱͳ͘ɺϝιο υࣗମ͕ݺͼग़͞Ε͍ͯΔ͜ͱΛ֬ೝ͢Δ 10
ϞοΫΦϒδΣΫτͷ࡞Γํ ୯ͳΔελϒͱҧͬͯɺϞοΫΦϒδΣΫτϝιου ͕ݺͼग़͞Εͨ͜ͱΛςετ͠ͳ͚ΕͳΒͳ͍ ϞοΫΦϒδΣΫτϑϨʔϜϫʔΫΛͬͯϞοΫΦϒ δΣΫτΛ࡞͢Δͷ͕Ұൠత දతͳͷ͕PHPUnitඪ४ͷPHPUnit_MockObject ࠓճհ͢ΔPhakeϑϨʔϜϫʔΫͷ1ͭ 11
Phake ײతʹ͑ΔϞοΫΦϒδΣΫτϑϨʔϜϫʔΫ JavaͷMockito͕ϕʔε PHPUnitʹରԠ 12
PHPUnit_MockObject 13 ;
Phake 14 ;
Phake Phake::mock ϞοΫΦϒδΣΫτͷ࡞ Phake::when ελϒϝιουͷ࡞ Phake::verify ϝιου͕ਖ਼͘͠ݺͼग़͞Ε͔ͨ 15
Phake::mock Phake::mock( $classname [, $arg1[, $... ]] ) ࢦఆͨ͠Ϋϥε໊ΛݩʹϞοΫΦϒδΣΫτΛ࡞ ୈ2ҾҎ͕߱ίϯετϥΫλͷҾ
16
Phake::when Phake::when( $mock ) ->ελϒԽ͍ͨ͠ϝιου(ظ͢ΔҾ) ->ݺͼग़͞ΕͨΒͲ͏ͳΔ͔() ελϒԽ͍ͨ͠ϝιουͱҾΛͦͷ··هड़ Phake::when($mock)->foo(Phake::anyParameters()) foo()ϝιου͕ҾΘͣݺͼग़͞ΕΔ 17
Phake::when - Answers thenReturn( $value ) $value Λฦ͢ thenThrow( $exception
) $exception Λεϩʔ thenCallParent() parentݺͼग़͠ 18
Phake::verify Phake::verify( $mock [, $mode ] ) ->ݕূ͢Δϝιου(ظ͢ΔҾ) Phake::verify($mock, Phake::times($n))->foo()
foo()ϝιου͕$nճݺͼग़͞Εͨ͜ͱΛݕূ Phake::verifyNoIntaraction($mock) $mockΦϒδΣΫτͷελϒϝιου͕Կݺͼग़͞ Εͳ͔ͬͨ͜ͱΛݕূ 19
Partial Mocks ௨ৗ Phake::mock() ͰϞοΫΦϒδΣΫτΛ࡞͢Δ ͱɺͯ͢ͷϝιου͕ελϒԽ͞ΕΔ Ұ෦ͷϝιουͷΈελϒԽ͍ͨ͠߹ύʔγϟϧ ϞοΫͱͯ͠࡞͢Δ ύʔγϟϧϞοΫΦϒδΣΫτجຊతʹϝιου͕ ͦͷ··ݺͼग़͞ΕΔ͕ɺPhake::when()Ͱ෦తʹελ
ϒϝιουʹஔ͖͑Δ͜ͱ͕Մೳ 20
21
22
Partial Mocks ύʔγϟϧϞοΫ͕ඞཁʹͳΔΑ͏Ͱ͋Εɺ෦తઃ ܭΛݟ͍͍͢ػձ͔ ύʔγϟϧϞοΫͰελϒԽͨ͠෦ΛผͷΫϥεͱ͠ ͯநग़ͯ͠ɺϞοΫΦϒδΣΫτͰஔ͖͑Δํ͕ͨͿ ΜૉͰΘ͔Γ͍͢ͱࢥ͍·͢ 23
Installation pear config-set auto_discover 1 pear install pear.digitalsandwich.com/phake include_pathΛઃఆͯ͠Phake.phpΛಡΈࠐΉ͚ͩ 24
·ͱΊ PhakeΛ͏ͱײతʹϞοΫΦϒδΣΫτΛ࡞ΕΔ ࢥߟͷྲྀΕʹԊͬͯεϜʔζʹϞοΫΦϒδΣΫτ͕ ࡞ΕΔͨΊςετ͕ḿΔ PHPUnitͷMockObjectͰेͳΒແཧʹΘͳ͍͍ͯ͘ Μ͡Όͳ͍Ͱ͔͢Ͷ…… 25
Links http://phake.digitalsandwich.com/ https://github.com/mlively/Phake http://fivestar.hatenablog.com/ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ 26