Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Codeceptionを利用したWordPressプラグインテストことはじめ

 Codeceptionを利用したWordPressプラグインテストことはじめ

WordBench京都2016年3月号で発表したCodeceptionについてのスライドです。
スライド内で利用したサンプルプラグインやテストは以下に公開しています。

https://github.com/Ippey/cc-plugin

A73ed5cefbbd84abd2c5e14a5eb4a339?s=128

Ippei Sumida

March 19, 2016
Tweet

Transcript

  1. CodeceptionΛར༻ͨ͠
 WordPressϓϥάΠϯςετ
 ͜ͱ͸͡Ί

  2. ࣗݾ঺հ • ֯ాɹҰฏʢ͢Έͩɹ͍ͬ΃͍ʣ • PHP, iOS, Androidɹ։ൃ΍ͬͯ·͢ • CoderDojo੢ٶɾകాɹϝϯλʔ

  3. ࠓ೔ͷ໨ඪ • CodeceptionͷઃఆΛ͢Δ • ϓϥάΠϯͷςετέʔεΛ࡞੒͢Δ • ςετΛ࣮ߦ͢Δ

  4. ΞδΣϯμ 1. Codeceptionʹ͍ͭͯ 2. PhpStormʹ͍ͭͯ 3. WordPress։ൃ؀ڥߏங 4. SeleniumΠϯετʔϧɾઃఆɾىಈ

  5. ΞδΣϯμ 5. CodeceptionΠϯετʔϧɾઃఆ 6. σϞ༻WordPressϓϥάΠϯ࡞੒ 7. ςετϓϩάϥϜ࡞੒ 8. ςετ

  6. 1.Codeceptionʹ͍ͭͯ

  7. Codeceptionͱ͸ • PHPͰಈ࡞͢ΔɺςεςΟϯάϑϨʔϜϫʔΫ • Unit, Functional, Acceptanceςετ͕Ͱ͖Δ • CIͱ࿈ܞՄೳ

  8. Unitςετ • ͍ΘΏΔ୯ମςετ • ϝιουɺؔ਺୯Ґͷςετ • PHPUnitͱΑΓ΋ॻ͖΍͍͢

  9. Functionalςετ • ػೳςετ • ਖ਼௚͔ͭΘͳ͍ɻ • Ͱ΋͖ͬͱຊ࣭Λ͔ͭΊ͹໾ཱͭ͸ͣɻ

  10. Acceptanceςετ • ৼΔ෣͍ςετ • Selenium࿈ܞͰϒϥ΢βςετͰ͖Δɻ • APIͷςετ΋Ͱ͖Δɻ • ʢݸਓతʣCodeception͸͜ͷͨΊʹ͋Δɻ

  11. 2.PhpStormʹ͍ͭͯ

  12. PhpStormͱ͸ • PHP༻IDE • Eclipseͱൺ΂͍ͯܰɻ • Git,SVNΫϥΠΞϯτඪ४૷උɻ • Vagrant΋੍ޚՄೳɻ

  13. ͨͩ͠༗ྉ • ೥ֹ9,936ԁʙ • ຊՈɺαϜϥΠζϜ͔ΒߪೖՄೳɻ • ࠓͳΒαϜϥΠζϜ͔Βߪೖͷํ͕͍҆ʁ

  14. ࠓճ࢖༻͢Δ΋ͷ • WordPress (Apache, MySQL, PHP>=5.4, wp-cli) • PhpStorm •

    Codeception • Selenium standalone-server • Firefox
  15. 3.WordPress։ൃ؀ڥߏங

  16. wp-cliΠϯετʔϧ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh- pages/phar/wp-cli.phar

  17. wp-cliΠϯετʔϧʢMacʣ brew install wp-cli

  18. MySQLηοτΞοϓ mysql -uroot -p*** mysql
 create database wordpress;
 create database

    wordpress_test
 CREATE USER wordpress IDENTIFIED BY 'wordpress';
 CREATE USER wordpress@localhost IDENTIFIED BY 'wordpress';
 GRANT ALL PRIVILEGES ON *.* TO wordpress@localhost;
 GRANT ALL PRIVILEGES ON *.* TO wordpress;
 flush privileges;
  19. VCCWΠϯετʔϧ vagrant box add miya0001/vccw
 wget https://github.com/vccw-team/vccw/archive/2.19.0.zip
 unzip 2.19.0.zip
 cd

    vccw-2.19.0
 vagrant up
 
 ※ผϓϩδΣΫτͰvccw.devΛ࢖͍ͬͯΔ৔߹͸ɺϗετ໊Λม ߋ͍ͯͩ͘͠͞ɻ
  20. PluginηοτΞοϓ cd ***/www/wordpress/wp-content/plugins
 wp scaffold plugin cc-plugin
 cd cc-plugin
 ./bin/install-wp-tests.sh

    wordpress_test wordpress wordpress localhost

  21. 4.Selenium
 Πϯετʔϧɾઃఆ

  22. SeleniumηοτΞοϓ wget http://goo.gl/qTy1IB
 java -jar selenium-server-standalone-*.jar

  23. SeleniumηοτΞοϓʢMacʣ brew install selenium-server-standalone

  24. ※஫ҙ఺ • FirefoxɺSeleniumͱ΋࠷৽൛ʹ͢Δɻ • ͨ·ʹλΠϛϯάѱ͍ͱಈ͔ͳ͍৔߹͕͋Δɻ

  25. 5.Codeception
 Πϯετʔϧɾઃఆ

  26. CodeceptionηοτΞοϓ cd ***/www/wordpress/wp-content/plugins/cc-plugin
 wget http://codeception.com/codecept.phar
 php codecept.phar bootstrap

  27. CodeceptionηοτΞοϓ cd tests
 cp bootstrap.php _bootstrap.php

  28. CodeceptionηοτΞοϓ class_name: AcceptanceTester
 modules:
 enabled:
 - PhpBrowser:
 url: http://vccw.dev
 -

    \Helper\Acceptance
 
 env:
 firefox:
 modules:
 enabled:
 - WebDriver:
 url: http://vccw.dev
 browser: firefox
 - \Helper\Acceptance
 tests/acceptance.suite.yml
  29. 6.σϞ༻WordPress
 ϓϥάΠϯ࡞੒

  30. ϓϥάΠϯΛ͝༻ҙ͠·ͨ͠ɻ https://github.com/Ippey/cc-plugin

  31. ϓϥάΠϯΛ͝༻ҙ͠·ͨ͠ɻ CCϓϥάΠϯ
 ʢCodeceptionςετ༻ϓϥάΠϯʣ
 
 ɾػೳɿࢦఆͷ౎ಓ෎ݝͷळ٢ҰཡΛςʔϒϧදࣔ͠·͢ɻ
 ɾར༻ྫɿγϣʔτίʔυɻ[akiyoshi ژ౎෎]
 ɾඋߟɿळ٢ళฮҰཡ͸ΦʔϓϯσʔλԽ͍ͯ͠·͢ʂ

  32. 7.ςετϓϩάϥϜ࡞੒

  33. ςετϓϩάϥϜ࡞੒ɹ UnitTest cd ***/www/wordpress/wp-content/plugins/cc-plugin
 php codecept.phar generate:test unit AkiyoshiShopManager

  34. ςετϓϩάϥϜ࡞੒ɹ UnitTest private $manager;
 
 protected function _before()
 {
 $this->manager

    = new AkiyoshiShopManager();
 }
  35. ςετϓϩάϥϜ࡞੒ɹ UnitTest /**
 * ϓϥΠϕʔτϝιουΛݺͿͨΊͷReflectionMethodΛฦ͢
 *
 * @param $obj
 *

    @param $name
 *
 * @return ReflectionMethod
 */
 private function getPrivateMethod($obj, $name) {
 $method = new ReflectionMethod($obj, $name);
 $method->setAccessible(true);
 return $method;
 }
  36. ςετϓϩάϥϜ࡞੒ɹ UnitTest /**
 * getListByPrefectureͷςετ
 */
 public function testGetListByPrefecture() {


    $pref = "ژ౎෎";
 $method = $this->getPrivateMethod($this->manager, "getListByPrefecture");
 $list = $method->invoke($this->manager, array($pref));
 foreach ($list as $row) {
 $this->assertEquals($pref, $row["prefecture"]);
 }
 }
  37. ςετϓϩάϥϜ࡞੒ɹ AcceptanceTest cd ***/www/wordpress/wp-content/plugins/cc-plugin
 php codecept.phar generate:cest acceptance CcPlugin

  38. ςετϓϩάϥϜ࡞੒ɹ AcceptanceTest public function _before(AcceptanceTester $I)
 {
 $I->amOnPage("/archives/1");
 }

  39. ςετϓϩάϥϜ࡞੒ɹ AcceptanceTest public function testShowAkiyoshiShopList(AcceptanceTester $I)
 {
 $I->canSeeElement("#akiyoshi-list");
 }
 


    public function testShowKawaramachi(AcceptanceTester $I) {
 $I->canSee("Տݪொళ", "#akiyoshi-list");
 }

  40. 8.ςετ

  41. ςετ࣮ߦ
 UnitTest cd ***/www/wordpress/wp-content/plugins/cc-plugin
 php codecept.phar run unit

  42. ςετ࣮ߦԼ४උ
 AcceptanceTest ɾϓϥάΠϯΛ༗ޮʹ͢Δ
 ɾطଘϖʔδΛฤूɻ࠷ޙͷߦʹ[akiyoshi ژ౎෎]Λઃఆɻ

  43. ςετ࣮ߦ
 AcceptanceTest PHPBrowser cd ***/www/wordpress/wp-content/plugins/cc-plugin
 php codecept.phar run acceptance

  44. ςετ࣮ߦ
 AcceptanceTest Firefox cd ***/www/wordpress/wp-content/plugins/cc-plugin
 php codecept.phar run acceptance —env

    firefox
  45. ςετ࣮ߦ
 શ෦·ͱΊͯ cd ***/www/wordpress/wp-content/plugins/cc-plugin
 php codecept.phar run

  46. ΄͔ʹ΋

  47. CodeCeptionͷศརͳ࢖͍ํ ɾϓϥάΠϯҎ֎ͷςετʹ΋ར༻Մೳɻ
 ɹɹ࡞੒ͨ͠ςʔϚͷಠࣗ෦෼͕֤ը໘ʹ൓ө͞ΕͯΔ͔ʁ
 ɹɹΧελϜϑΟʔϧυͷ஋͕൓ө͞Ε͍ͯΔ͔ʁ
 
 ɾPHPϑϨʔϜϫʔΫ༻ͷϞδϡʔϧ͕༻ҙ͞Ε͍ͯΔɻ
 
 ɾ΋ͪΖΜTravisCI,CircleCIͳͲͷCIͱ࿈ܞՄೳɻ
 ɹˠ࣮ࡍʹ΍ͬͯΈΔɻ

  48. ࠂ஌

  49. DojoCon Japan 2016։࠵༧ఆ