Phakeで簡単 モックオブジェクト作成

Phakeで簡単 モックオブジェクト作成

2012.03.27 PHP勉強会@東京 #58

Ff822949384025796e11fd7f681df060?s=128

Katsuhiro OGAWA

July 02, 2012
Tweet

Transcript

  1. PhakeͰ؆୯ ϞοΫΦϒδΣΫτ࡞੒ Katsuhiro OGAWA @fivestr 2012.03.27 - PHPษڧձ@౦ژ #58

  2. About Me খ઒ ༤େ - OGAWA Katsuhiro @fivestr גࣜձࣾΫϩίε ձࣾͰ͸ߴڮͬͯݺ͹Εͯ·͢

    ύʔϑΣΫτPHPॻ͖·ͨ͠ 5݄ʹ΋PHPͷຊग़͢Έ͍ͨͰ͢ (৭ʑະఆ) OOPɺMVCɺDDD 2
  3. Agenda ελϒͱϞοΫ Phakeͱ͸ Phake API PhakeͷΠϯετʔϧ 3

  4. ςετ͕஗͍ʁ DB΍IOɺWeb APIͳͲ΁ͷΞΫηεΛ൐͏ॲཧͷςε τͬͯͲ͏ͯ͠·͔͢ʁ DBΞΫηεͯͨ͠Βςετ͕஗͘ͳͬͨΓ͢Δ͠ɺWeb APIͩͱωοτϫʔΫʹͭͳ͕ͬͯͳ͍ͱςετͰ͖ͳ͍ Ͱ͢ΑͶ ͦΜͳͱ͖͸ελϒͷग़൪Ͱ͢ 4

  5. ελϒͱ͸ લड़ͷΑ͏ͳ֎෦ͷԿ͔΁ͷΞΫηεͷ୅ΘΓʹఆ਺Λ ฦ͢Α͏ʹͨ͠΋ͷ͕ελϒ stub out - ΋Έফ͢ ैདྷ:ɹɹreturn $webservice->api($id); ελϒ:ɹreturn

    [‘id’ => ‘1’, ‘name’ => ‘takahashi’]; ఆ਺ʹஔ͖׵͑Δ 5
  6. αϯϓϧ FacebookΫϥε͸ Facebook͕ఏڙ͢Δ PHP SDKɻ api()ϝιουͰAPI΁ ΞΫηε ClientΫϥε͸ FacebookΦϒδΣΫ τΛ࢖ͬͯσʔλΞΫ

    ηε 6
  7. ελϒͰࠩ͠ସ͑ FacebookΫϥεͷ api()ϝιουΛ্ॻ͖ ͯ͠ఆ਺Λฦ͢Α͏ʹ ͢Ε͹ɺWeb API΁ͷ ΞΫηεΛͳͤ͘Δ ্ҐϞδϡʔϧ ԼҐϞδϡʔϧ 7

  8. ελϒͷ׆༻ ελϒΛ࢖͏ͱɺDB΍Web APIͱ͍ͬͨɺ֎෦ͷԿ͔΁ ͷґଘΛ੾Γ཭͢͜ͱ͕Մೳ ֎෦ͷԿ͔΁ͷΞΫηε͕Ͱ͖ͳ͍ঢ়ଶͰ΋ɺΞΫηε ॲཧΛελϒʹஔ͖׵͑Δ͜ͱͰ্ҐϞδϡʔϧͷςε τ͕Մೳ 8

  9. ϞοΫΦϒδΣΫτͱ͸ ϞοΫΦϒδΣΫτ͸ελϒͷҰछ ελϒ͕ԿΒ͔ͷಈ࡞ͷ୅༻඼Ͱ͔͠ͳ͍ͷʹରͯ͠ɺ ϞοΫΦϒδΣΫτ͸্ҐϞδϡʔϧ͕ԼҐϞδϡʔϧ Λਖ਼͘͠࢖༻͍ͯ͠Δ͔Λςετ͢ΔͨΊͷ΋ͷ 9

  10. ϞοΫΦϒδΣΫτͷ׆༻ DBʹอଘ͢ΔΑ͏ͳ໭Γ஋Λ൐Θͳ͍ϝιουΛελϒ Խ͍ͨ͠ͱ͖͸ɺ໭Γ஋͕ԿͰ͋Δ͔Ͱ͸ͳ͘ɺϝιο υࣗମ͕ݺͼग़͞Ε͍ͯΔ͜ͱΛ֬ೝ͢Δ 10

  11. ϞοΫΦϒδΣΫτͷ࡞Γํ ୯ͳΔελϒͱҧͬͯɺϞοΫΦϒδΣΫτ͸ϝιου ͕ݺͼग़͞Εͨ͜ͱΛςετ͠ͳ͚Ε͹ͳΒͳ͍ ϞοΫΦϒδΣΫτϑϨʔϜϫʔΫΛ࢖ͬͯϞοΫΦϒ δΣΫτΛ࡞੒͢Δͷ͕Ұൠత ୅දతͳ΋ͷ͕PHPUnitඪ४ͷPHPUnit_MockObject ࠓճ঺հ͢ΔPhake΋ϑϨʔϜϫʔΫͷ1ͭ 11

  12. Phake ௚ײతʹ࢖͑ΔϞοΫΦϒδΣΫτϑϨʔϜϫʔΫ JavaͷMockito͕ϕʔε PHPUnitʹ΋ରԠ 12

  13. PHPUnit_MockObject 13 ;

  14. Phake 14 ;

  15. Phake Phake::mock ϞοΫΦϒδΣΫτͷ࡞੒ Phake::when ελϒϝιουͷ࡞੒ Phake::verify ϝιου͕ਖ਼͘͠ݺͼग़͞Ε͔ͨ 15

  16. Phake::mock Phake::mock( $classname [, $arg1[, $... ]] ) ࢦఆͨ͠Ϋϥε໊ΛݩʹϞοΫΦϒδΣΫτΛ࡞੒ ୈ2Ҿ਺Ҏ͕߱ίϯετϥΫλͷҾ਺

    16
  17. Phake::when Phake::when( $mock ) ->ελϒԽ͍ͨ͠ϝιου(ظ଴͢ΔҾ਺) ->ݺͼग़͞ΕͨΒͲ͏ͳΔ͔() ελϒԽ͍ͨ͠ϝιουͱҾ਺Λͦͷ··هड़ Phake::when($mock)->foo(Phake::anyParameters()) foo()ϝιου͕Ҿ਺໰Θͣݺͼग़͞ΕΔ 17

  18. Phake::when - Answers thenReturn( $value ) $value Λฦ͢ thenThrow( $exception

    ) $exception Λεϩʔ thenCallParent() parentݺͼग़͠ 18
  19. Phake::verify Phake::verify( $mock [, $mode ] ) ->ݕূ͢Δϝιου(ظ଴͢ΔҾ਺) Phake::verify($mock, Phake::times($n))->foo()

    foo()ϝιου͕$nճݺͼग़͞Εͨ͜ͱΛݕূ Phake::verifyNoIntaraction($mock) $mockΦϒδΣΫτͷελϒϝιου͕Կ΋ݺͼग़͞ Εͳ͔ͬͨ͜ͱΛݕূ 19
  20. Partial Mocks ௨ৗ Phake::mock() ͰϞοΫΦϒδΣΫτΛ࡞੒͢Δ ͱɺ͢΂ͯͷϝιου͕ελϒԽ͞ΕΔ Ұ෦ͷϝιουͷΈελϒԽ͍ͨ͠৔߹͸ύʔγϟϧ ϞοΫͱͯ͠࡞੒͢Δ ύʔγϟϧϞοΫΦϒδΣΫτ͸جຊతʹ͸ϝιου͕ ͦͷ··ݺͼग़͞ΕΔ͕ɺPhake::when()Ͱ෦෼తʹελ

    ϒϝιουʹஔ͖׵͑Δ͜ͱ͕Մೳ 20
  21. 21

  22. 22

  23. Partial Mocks ύʔγϟϧϞοΫ͕ඞཁʹͳΔΑ͏Ͱ͋Ε͹ɺ෦෼తઃ ܭΛݟ௚͍͍͢ػձ͔΋ ύʔγϟϧϞοΫͰελϒԽͨ͠෦෼ΛผͷΫϥεͱ͠ ͯநग़ͯ͠ɺϞοΫΦϒδΣΫτͰஔ͖׵͑Δํ͕ͨͿ Μૉ௚ͰΘ͔Γ΍͍͢ͱࢥ͍·͢ 23

  24. Installation pear config-set auto_discover 1 pear install pear.digitalsandwich.com/phake include_pathΛઃఆͯ͠Phake.phpΛಡΈࠐΉ͚ͩ 24

  25. ·ͱΊ PhakeΛ࢖͏ͱ௚ײతʹϞοΫΦϒδΣΫτΛ࡞ΕΔ ࢥߟͷྲྀΕʹԊͬͯεϜʔζʹϞοΫΦϒδΣΫτ͕ ࡞ΕΔͨΊςετ͕ḿΔ PHPUnitͷMockObjectͰे෼ͳΒແཧʹ࢖Θͳ͍͍ͯ͘ Μ͡Όͳ͍Ͱ͔͢Ͷ…… 25

  26. Links http://phake.digitalsandwich.com/ https://github.com/mlively/Phake http://fivestar.hatenablog.com/ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ 26