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
290
メルカリ CSE チームの 歩みと取り組み / Steps and Tries in the CSE team for Mercari
fivestar
2
880
Go in Corporate Solutions Engineering
fivestar
0
2.4k
いつまでPHP 5.x使ってるの
fivestar
1
1.9k
PHP のドキュメントを読んで PHP のことをもっと知ろう
fivestar
1
3.1k
CrocosSecurityBundleを Symfony3.0に対応した話
fivestar
0
230
PHP BLT だけど HTML5 の コンテンツモデルのお話
fivestar
0
2.3k
THE NEW "PERFECT PHP" WILL BE COMING SOON
fivestar
0
8k
Other Decks in Programming
See All in Programming
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
260
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
5
830
ドメインイベント増えすぎ問題
h0r15h0
2
390
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
210
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
800
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
2
410
return文におけるstd::moveについて
onihusube
1
1.2k
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Become a Pro
speakerdeck
PRO
26
5k
Automating Front-end Workflow
addyosmani
1366
200k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Gamification - CAS2011
davidbonilla
80
5.1k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Music & Morning Musume
bryan
46
6.2k
Producing Creativity
orderedlist
PRO
342
39k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
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