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
130
Goで学ぶSOLID原則
fivestar
0
280
メルカリ CSE チームの 歩みと取り組み / Steps and Tries in the CSE team for Mercari
fivestar
2
870
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
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
220
flutterkaigi_2024.pdf
kyoheig3
0
150
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Better Code Design in PHP
afilina
PRO
0
130
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Jakarta EE meets AI
ivargrimstad
0
140
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Being A Developer After 40
akosma
87
590k
Become a Pro
speakerdeck
PRO
25
5k
Building Applications with DynamoDB
mza
90
6.1k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Visualization
eitanlees
145
15k
The Invisible Side of Design
smashingmag
298
50k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
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