PHPã§TDD&CIã¯ã¼ã¯ã·ã§ãããJenkins + PHP ã®å種ãã©ã°ã¤ã³ãã¼ãè³æ
ã¯ããã«
ãã®è³æã¯ãPHPã§TDD&CIã¯ã¼ã¯ã·ã§ããã http://atnd.org/events/16626 㧠@yamashiro ãçºè¡¨ããããã®è³æã ãã
ã¯ã¼ã¯ã·ã§ããåå è
ãããªãã¦ãè¨äºèªãã ãã§å®çµããããã«ã¯æ¸ãã¦ãããã
æ¦è¦ã¨ãã¦ã¯ãJenkins ã使ã£ã¦ PHP ã®ã¦ã³ã³ã¬ã¬ã·ã¼ãªã³ã¼ããããã«ç¶ºéºã«ãã¦è¡ããã¨ãããã¨ã説æããã
èªç»èªè³ã ãã© PHPMD ã¨ã PHPCPD ã®ä½¿ãæ¹ã®èª¬æã®è³æã¨ãã¦ãããããã¤ã±ã¦ãè³æã«ãªã£ã¦ãã¨æãã¾ãããã¾ãã
ãã®è¨äºã«æ¸ããã¦ããã¨ã¯ãå²ã¨Template for Jenkins Jobs for PHP Projectsã¨ãã¶ã£ã¦ããã©ããã©ã°ã¤ã³ãçµã£ã¦ããã®ã¨ãä¸åä¸åã®ãã©ã°ã¤ã³ã«ã¤ãã¦è§£èª¬ãã¾ãå®éã«ã¨ã©ã¼ãèµ·ããã¨ãã«ã©ãããã°ããã®ãæ¸ããã
Java 㨠Jenkins ã®ã¤ã³ã¹ãã¼ã«ã¨JENKINS_HOME
ã¯ã¼ã¯ã·ã§ããã§ã¯ Java 㨠Jenkins ã¯åå è ã¯ã¤ã³ã¹ãã¼ã«ãã¦ãã¦ãã¨ãã話ã«ãªã£ã¦ããã¯ãã ããç°¡åã« Java 㨠Jenkins ã®ã¤ã³ã¹ãã¼ã«ãèµ·åã«ã¤ãã¦èª¬æããã
- Java ã®ã¤ã³ã¹ãã¼ã«
- ã°ã°ãã«ã¹ï¼ãã説æãã ãã¨å²ã¨ã ããã£ã¦ãããâ¦ï¼ãOracleã®ãµã¤ãããé©å½ã«ããã°ããããããªãã§ãããã
- Jenkins ã®ã¤ã³ã¹ãã¼ã«
- ä¸çªç°¡åãªã®ã¯ãhttp://mirrors.jenkins-ci.org/war/latest/ 㧠war ãåå¾ãã¦ãjava -jar jenkins.war ã§èµ·åãã¦çµäº
- Jenkins ã®èµ·åã®ç¢ºèªã¨ JENKINS_HOME ã®ç¢ºèª
- ãã©ã¦ã¶ã§ http://localhost:8080 ãã²ããã¦jenkinsæ°ã®é¡ãè¦ãããæå
- Top ç»é¢ã§ãJenkinsã®ç®¡çã-[ã·ã¹ãã ã®è¨å®] ã§è¡¨ç¤ºããã[ãã¼ã ãã£ã¬ã¯ããª]ãã以éã®èª¬æã§è¨ãã¨ããã® JENKINS_HOME ã¨ãã
- 以ä¸ã§ã³ãã³ãã©ã¤ã³ã§ãããã説æããã®ã§ãç°å¢å¤æ° JENKINS_HOME ãæå®ãã¦ãããã»ãããã
Jenkins ã®é«åº¦ã¨ãããç´°ããã¤ã³ã¹ãã¼ã«æ¹æ³ã¯ã°ã°ãå
¬å¼wikiãyumã§ã¤ã³ã¹ãã¼ã«ãã¦ã5åã§åºæ¥ãJenkinså°å
¥ ã¨ãåç
§
以ä¸ã®èª¬æã§ã¯ãjenkinsã localhost:8080 ã§èµ·åãã¦ããã¨ãåæã¨ãã¦ããããä»ã®ãµã¼ãã¼ã«ãã£ããããã¼ããããã¦ããããå ´åã¯èªã¿æ¿ãããã¨ã
å種 PHP å¨ãã®ãã¼ã«ã®ã¤ã³ã¹ãã¼ã«
PHP å¨ãã®ãã¼ã«ãã¤ã³ã¹ãã¼ã«ããããã«ãçæ§ããªãã¿ã® PEAR ã³ãã³ãã使ã£ã¦ãã£ã¨ã¤ã³ã¹ããã¼ã«ããã¾ãããã§ã«å ¥ã£ã¦ããããªãã®ã¯é©å®ããã©ã¼ã¹ã§ãããªãã¦ããã³ãã³ããæãã¦ãããªãã¦ããã§ãããã©ã¼ã¹ãæãã¦ãã ããã
pear channel-discover pear.phing.info pear channel-discover pear.pdepend.org pear channel-discover pear.phpmd.org pear channel-discover pear.phpunit.de pear channel-discover components.ez.no pear channel-discover pear.symfony-project.com pear install phing/phing pear install pdepend/PHP_Depend pear install phpmd/PHP_PMD pear install phpunit/phpcpd pear install PHPDocumentor pear install --alldeps phpunit/PHPUnit
ããã¦ãããã¼ã«ã¯ããã¨ã§ã説æããã以ä¸ã®éã
- Phing (Java ã® Apache Ant ã¨ãããã«ããã¼ã«ã® PHP ã¯ãã¼ã³)
- PHP_Depend (å®ã¯ä»åã¯ããã®èª¬æã¯ããªãããä¸ã§èª¬æããPHPMDãä¾åãã¦ãã®ã§ãããã±ã¼ã¸éã®ä¾åæ§ã¨ãã調ã¹ããããã¼ã«ï¼
- PHPMD (PHPã®ã³ã¼ãã®
ã¯ã½ããã¦ãªãé¨åãæ¢ããã¼ã«) - PHPCPD (PHPã®ã³ã¼ãã®
ã¢ãã³ããéè¤é¨åãæ¢ããã¼ã«ï¼ - PHPDocumentor (ã³ã¡ã³ãããPHPDocãåºåããããã®ãã¼ã«)
- PHPUnit ï¼è¨ããã¨ããã PHP ã®ãã¹ããã¼ã«)
ãã¼ã«ã«å¯¾å¿ãã Jenkins ãã©ã°ã¤ã³ã®ã¤ã³ã¹ãã¼ã«
Jenkins ãã©ã°ã¤ã³ã®ã¤ã³ã¹ãã¼ã«ã¯ãJenkinsã®ç»é¢ãããã§ããããä»åã¯ç°¡ç´ åã®ããã« jenkins-cli ã¨ããã³ãã³ãã©ã¤ã³ãã jenkins ããããä»çµã¿ã使ãã決ãã¦ç»é¢ããã®ã¤ã³ã¹ããã¼ã«ã®èª¬æãããã©ãããã£ãããã§ã¯ãªããæãã¦ãªãã決ãã¦ãªãã
ä¸å¿ãç»é¢ããã®ã¤ã³ã¹ãã¼ã«æ¹æ³ãç°¡åã«èª¬æãã¦ããã¨ãJenkinsã®ãã¼ã ãã[Jenkinsã®ç®¡ç]-[ãã©ã°ã¤ã³ã®ç®¡ç]-[å©ç¨å¯è½]ã¿ãã表示ãã¦è¡¨ç¤ºããããã©ã°ã¤ã³ä¸è¦§ããå¿ è¦ãªãã©ã°ã¤ã³ã®ãã§ãã¯ããã¯ã¹ãå ¥ããä¸çªä¸ã®ãã¤ã³ã¹ãã¼ã«ããã¿ã³ãã¯ãªãã¯ããã
ã³ãã³ãã©ã¤ã³ããã®å ´åãä¸è¨ã®ã³ãã³ããå®è¡ãã¦æ¬²ãããæåã® wget ã¯ãèªåã® jenkinsãµã¼ãã¼ãã jenkins-cli.jar ããã¦ã³ãã¼ããã¦ããããã§ã«ã©ããã«ç½®ãã¦ãã£ããããã使ãã°ãããã¾ããwget ã¯é©å®ãcurlã¨ãã«èªã¿æ¿ãããã¨ãããWindowsã§jenkinsã®äººï¼ããã®ï¼Jenkins+PHP+Windowsã¨ãè¨ã®éã£ããªã¼ãWindowsã®äººã¯æåã§ãã¦ã³ãã¼ãããã°ãããããããå¤åã
jenkins-cli.jar ã使ã£ã¦å種ãã©ã°ã¤ã³ãjenkinsã«ã¤ã³ã¹ããã¼ã«ãã¦ãæå¾ã« jenkins ãåèµ·åãã¦ããã
wget -O jenkins-cli.jar http://localhost:8080/jnlpJars/jenkins-cli.jar java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin phing java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin dry java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin pmd java -jar jenkins-cli.jar -s http://localhost:8080 install-plugin clover java -jar jenkins-cli.jar -s http://localhost:8080 safe-restart
ã¤ã³ã¹ããã¼ã«ãã¦ãããã©ã°ã¤ã³ã¯ä»¥ä¸ã®ã¨ãããPHPUnit ã®ãã©ã°ã¤ã³ããªãããã«è¦ããã®ã¯ãPHPUnit 㯠jenkins ãæ¨æºã§ãµãã¼ããã¦ãã JUnit å½¢å¼ã® xml ãåºåãããã
- phing (Phingã Jenkins ããããã¯ããããã®ãã©ã°ã¤ã³ï¼
- dry (PHPCPD ã®çµæã jenkins ããè¦ãããã®ãã©ã°ã¤ã³ããªã㧠phpcpdã£ã¦ååã®ãã©ã°ã¤ã³ãããªãã®ãå°ä¸æéã»ã©(ry ï¼
- pmd (PHPMD ã®çµæã jenkins ããè¦ãããã®ãã©ã°ã¤ã³ãã¡ãªã¿ã« pmd ã¨ãã java ã®ãã¼ã«ã¨äºææ§ã®ãã xml ã PHPMD ãåºåãã)
- cloverphp ï¼cloverã¨ããxdebugã§åºåãããã«ãã¬ãã¸æ å ±ã表示ããããã®ãã¼ã«ã®ãã©ã°ã¤ã³ãã¯ã¼ã¯ã·ã§ããã®éã«èª¬æã§ãããæªããâ¦)
ãµã³ãã«ã®ã¸ã§ãã®æ°è¦ä½æã¨ãµã³ãã«ã®ã³ã¼ãã®
ç¶ãã¦ããã§ã«åãç¨æãã Jenkins ã¸ã§ãã®ãã¦ã³ãã¼ããã¦é ç½®ã¨ããããåãç¨æããå種ãã¼ã«ãJenkinsãã©ã°ã¤ã³ã®è¡¨ç¤ºã確èªããããã®æºåãè¡ãã
ä»åããã¹ãç¨ãªã®ã§ã¸ã§ãã®ååã¯ci_workshop_jobã¨ããååã¨ãããã
ã¾ãã¯æ°è¦ã¸ã§ãã®ä½æãJenkinsã¯ã¸ã§ãã®åå¨ãªã©ãã©ãå¤æãããã¨ããã¨ã$JENKINS_HOME/jobs/
ã¸ã§ãã®è¨å®ã®GUIã§è¨å®ããé
ç®ã¯ãconfig.xmlã«æ¸ãããã¦ãããã
ãã®ä»çµãç¥ã£ã¦ããã°ãã¹ã¯ãªããã§config.xmlã®ãã³ãã¬ã¼ããæ°ããã¸ã§ãã®å ´æã«ã³ãã¼ãã¦ãæ°ããã¸ã§ãã®config.xml ã®å 容ãsedã¨ãã§æ¸ãæãã¦ãåã ã®ã¸ã§ãåºæã®å¤ã«å¤æ´ãã¦ãããã£ã¨ã¸ã§ããä½ããã¨ãã§ãããï¼
話ãæ»ãã¦ãJenkinsã®ãã®è¦å®éãã«ãµã³ãã«ã®ã¸ã§ããæã£ã¦ãããã
ã¨ããã£ããå
ã®è¨äºãæ¸ããã¨æã£ããã ãã©ãã¾ã£ããWindowsã®äººã®ãã¨ãèãã¦ããªãã£ãããWindowsã®äººã¯ã³ãã³ãããã£ã¦ããã¨ãé©å½ã«èªã¿æ¿ãã¦å®è¡ãã¦ãã ãããåããã£ããããã£ã¨ãå¤åã
git å ¥ã£ã¦ã人ã®å ´å
cd $JENKINS_HOME/jobs/ git clone git://github.com/yamashiro/ci_workshop_job.git java -jar jenkins-cli.jar -s http://localhost:8080 reload-configuration
å ¥ã£ã¦ãªã人ã®å ´å(tmpã¯é©å½ãªãã£ã¬ã¯ããª)
cd ~/tmp wget -O ci_workshop_job.tar.gz https://github.com/yamashiro/ci_workshop_job/tarball/master tar zxvf ci_workshop_job.tar.gz mv yamashiro-ci_workshop_job-3b7fcfc $JENKINS_HOME/jobs/ci_workshop_job/ java -jar jenkins-cli.jar -s http://localhost:8080 reload-configuration
ãã®ç¶æ
ã§ã¸ã§ã³ãã³ã¹ã®ç»é¢ãè¦ã¦ã以ä¸ã®ããã«ãci_workshop_job ã¨ããã¸ã§ããä½ããã¦ããã°æåã§ããã
ãã®ç¶æ
ã§ã¯ã¸ã§ãã¯æå¹ã«ãªã£ã¦ããªãã®ã§ãã¸ã§ãã®ãªã³ã¯ãã¯ãªãã¯ãã¦ãããã«ãå®è¡ããã¯ãªãã¯ãã¦ã¸ã§ããæå¹ã«ãã¤ã¤ãã«ããå®è¡ãã¦ã¿ãã
ãã«ãå®è¡ããçµæãé»è²ããªããã®ãªã³ã¯ãè¸ãã çµæã以ä¸ã®ããã«ãªã£ã¦ããã°æåã®ç¬¬ä¸æ©ã¯æå
ãµã³ãã«ã®ã¸ã§ãã®èª¬æ㨠build.xml ã¨ã®æ¯è¼ããã¼ã«ãä½ãããã
次ã«ãµã³ãã«ã®ã¸ã§ãã®èª¬æã¨ãµã³ãã«ã® build.xml ã¨ã®æ¯è¼ã説æãã¦ãããbuild.xml 㯠buildãã¼ã«ã§ãã Phing ãç¨ããå®è¡ã®ããã®è¨å®ãã¡ã¤ã«ã§ããã
jenkins ã®ã¸ã§ãã®è¨å®ã確èªããã«ã¯ãåã¸ã§ãã®ç»é¢ãã[è¨å®]ãªã³ã¯ãã¯ãªãã¯ããã°è¯ãã
Phing ã®è¨å®
jenkinsã®ãã«ã㧠Phing ã使ãã¨ããè¨å®ã¯ãè¨å®ç»é¢ä¸æ®µããã«ãã®é
ç®ã«Phingã®å¼ã³åºãã¨ããé
ç®ã§è¡ããã¦ããï¼ç»é¢ã¯ãé«åº¦ãªè¨å®ããæ¼ããå¾ã®è¡¨ç¤ºï¼ã
ãã¡ããæ°ãããã«ã対象ãä½ãã¨ãã¯ããã«ãæé ã®è¿½å ãã [Ant ã®å¼ã³åºã]ã[ã·ã§ã«ã®å®è¡]ãªã©ãé¸ã¶ããã®ã¨ãã« Phing ã®ãã©ã°ã¤ã³ã足ããã¦ããã°[Phingã®å®è¡]ãé¸æãããã¨ãã§ããã
ããã¾ã§ã®ã¯ã¼ã¯ã·ã§ããã®æé ããã£ãçµæã$JENKINS_HOME/jobs/ci_workshop_job/workspace/build.xmlãããã¯ãã§ããã
ç¾å¨ãPhingã®è¨å®ã®[ãã«ããã¡ã¤ã«]ã空ã§ããããããã©ã«ãã§workspaceã®build.xmlã¨ããååã®ãã¡ã¤ã«ã使ãããã§ããããã¡ã¤ã«åãªã©ãå¤ãããã¨ãã¯ããã®è¨å®ããããã
ã¾ããã¿ã¼ã²ããã空ã§ãããã空ã§ããå ´åãbuild.xmlã®
<project name="ci_workshop" basedir="." default="all">
ã® default ã¨ããã¿ã¼ã²ãããé¸ã°ãããjenkinsã§ã¿ã¼ã²ãããæå®ãããå ´åã¯ããã®è¨å®é ç®ã§è¨å®ãããã¿ã¼ã²ããã®è©³ãã説æã«ã¤ãã¦ã¯çããPhingã®ããã¥ã¡ã³ããèªãã§ãã ããã
ã¡ãªã¿ã«ä»åã®build.xmlã«ã¯ãall ã¨ããå ¨ã¦ã®ã¿ã¼ã²ããã«ä¾åãã¤ã¾ãå ¨ã¦ã®ã¿ã¼ã²ããã«ä¾åããã¿ã¹ã¯ãç¨æããã¦ãã¦ãå ¨ã¦ã®å¾è¿°ãã phpcpd ã phpmd ã®ã¿ã¹ã¯ãå ¨ã¦å®è¡ãããã
<target name="all" depends="phpcpd,phpmd,phpunit,phpdoc"> </target>
æå¾ã«ãããããã£ã¨ããé ç®ããããããã㯠build.xml å ã«ã
<property name="outputDir" value="."/>
ã¨ããé
ç®ããã£ã¦ãä»åã®ä¾ã§ã¯ phpunitã®çµæã®åºåå
ã決ãã¦ããã®ã ãããã®ããããã£ã Jenkins ã§åããã¨ãã ãä¸æ¸ãããã¨è¨ã£ãå ´åã«ä½¿ãã
PHPCPD ã®è¨å®ã¨ build.xml
PHPCPDã¯ã³ã¼ãã®éè¤ããã§ãã¯ããããã®ãã¼ã«ã§ããã
å
·ä½çã«ã¯ä»¥ä¸ã®ç»é¢ã®[éè¤ã³ã¼ã:2åã®è¦å]ãã¯ãªãã¯ããã¨
次ã®ããã«ã³ã¼ããéè¤ãã¦ãç®æããããã
ããã«ãªã³ã¯ãã¯ãªãã¯ããã¨ã³ã¼ãã®ã©ã®ç®æããJenkinsã®ç»é¢ã§è¦ããã¨ãã§ããã
build.xml ã¯ä»¥ä¸ã®ã¨ããã«ãªã£ã¦ããã
<target name="phpcpd"> <phpcpd minTokens="10"> <fileset dir="./phpfiles/"> <include name="**/**/*.php"/> </fileset> <formatter type="pmd" outfile="${outputDir}/cpd.xml"/> </phpcpd> </target>
build.xmlå ã®åé ç®ã«ã¤ãã¦ã¯ç´°ãã説æããªãããfilesetãªã©ã§å¯¾è±¡ã¨ãããã¡ã¤ã«ãæå®ãããããããªã®ã§ããã®ãã¡ã¤ã«ã ãã¯é¤å¤ãããã¨ãã¯ãfilesetå ã«excludeãªã©ãæå®ãã¦è¨å®ããã
PHPCPD ã®è¨å®ã¯dryãã©ã°ã¤ã³ãæ£ããã¤ã³ã¹ãã¼ã«ããã¦ããã°ãã«ãå¾ã®å¦çã®[éè¤ã³ã¼ãåæã®éè¨]ã§è¡ã
éè¨ãããã¡ã¤ã«ã®è¨å®ã¯ãbuild.xmlã®phpcpdã¿ã¹ã¯ä¸ã«ãã formatter ã® outputfile ã¨åããããéè¦åº¦ã®é¾å¤ã®è¨å®ã¯ãPHPCPDãåºåããã¨ã©ã¼ã®ãã¡ã¤ã«ã«ã¯ã
<duplication lines="18" tokens="44"> <file path="/Users/Shared/Jenkins/Home/jobs/ci_workshop_job/workspace/./phpfiles/Foo.php" line="8"/>
ã¨ããããã«éè¤ãã¦ãè¡æ°ã¨ãéè¤ãã¦ããã¼ã¯ã³æ°ãåºåãããã®ã ãããã®lineã®æ°ãã©ããããããªãããå¤ã§è¦åã¨ãããã¨ããè¨å®ã§ãããHighã¯èµ¤ãNormalã¯é»è²ã§è¡¨ç¤ºãããã
é«åº¦ãªè¨å®ã®å
容ã¯ãã¾ã俺ãææ¡ãã¦ãªãã®ã§çç¥ã誰ãæãã¦ããï½
PHPMD ã®è¨å®ã¨ build.xml
PHPMDã¯ã³ã¼ãå
ã®ä½¿ããã¦ããªãå¤æ°ãè¦åããããè¤éãããã¯ã©ã¹ãè¦åãããããã
å
·ä½çã«ã¯ä»¥ä¸ã®ç»é¢ã®[PMD:3åã®è¦å]ã®ãªã³ã¯ãã¯ãªãã¯ãã¦
次ã®ããã«ã«ã¼ã«ã«ãã£ã¦ããªãè¦åã表示ã§ããã
åãä¸çªä½¿ãã®ã¯ãè¦åãã¿ãã§ãã¡ã¤ã«ä¸è¦§ã表示ããã¦
ããã«åãã¡ã¤ã«ã®ã¨ã©ã¼ã®ç®æãè¦ã
ãã®å ´åã ã¨ããã©ã¡ã¼ã¿ã¼ã¨ãã¦æ¸¡ããã¦ããã®ã«ã¡ã½ããå ã§ä½¿ã£ã¦ããªãã®ã§ã¨ã©ã¼ã«ãªã£ã¦ããã
build.xml ã¯ä»¥ä¸ã®ã¨ããã«ãªã£ã¦ããã
<target name="phpmd"> <phpmd rulesets="ruleset.xml"> <fileset dir="./phpfiles"> <exclude name="exclude.php"/> </fileset> <formatter type="xml" outfile="${outputDir}/pmd.xml"/> </phpmd> </target>
<?xml version="1.0"?> <ruleset name="Hyper mANAGE PHPMD ruleset" xmlns="http://pmd.sf.net/ruleset/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"> <rule ref="rulesets/codesize.xml"> <exclude name="TooManyMethods" /> </rule> <rule ref="rulesets/codesize.xml/TooManyMethods"> <properties> <property name="maxmethods" value="15" /> </properties> </rule> <rule ref="rulesets/design.xml" /> <rule ref="rulesets/naming.xml"> <exclude name="ShortVariable" /> <exclude name="LongVariable" /> </rule> <rule ref="rulesets/unusedcode.xml" /> </ruleset>
åºæ¬çã«ã¯PHPMDã§ç¨æãã¦ããã«ã¼ã«ã»ãããèªã¿è¾¼ãã§ãã ãã ããShortVariableã®è¦åãªã©ã¯åºãã¨ãããã®ã§ãexcludeã§æå®ãã¦ããã
ç´°ããè¨å®ãªã©ã®èª¬æã¯ããªãããããã§ã¯ãTooManyMethodsã®è¦åã®ã¡ã½ããæ°ã®ãããå¤ãªã©ãè¡ãªã£ã¦ããã
èªåã§ä½æããã«ã¼ã«ã®è¨å®ãªã©ãããã§è¡ãã
PHPMD ã®è¨å®ã¯pmdãã©ã°ã¤ã³ãæ£ããã¤ã³ã¹ãã¼ã«ããã¦ããã°ãã«ãå¾ã®å¦çã®[PMDè¦åã®éè¨]ã§è¡ã
éè¨ãããã¡ã¤ã«ã®è¨å®ã¯ãformatterã®outputfileã¨åãããã
é«åº¦ãªè¨å®ã®å
容ã¯ãã¾ã俺ãææ¡ãã¦ãªãã®ã§çç¥ã誰ãæãã¦ããï½
PHPUnit ã®è¨å®ã¨ build.xml
PHPUnit ã¯ããããããªãèªåãã¹ãããã¦ããã®çµæãåºåããã
å
·ä½çã«ã¯ä»¥ä¸ã®ç»é¢ã®[ãã¹ãçµæ]ã®ãªã³ã¯ãã¯ãªãã¯ãã¦
次ã®ããã«å¤±æãããã¹ãã表示ã§ããã
ãªã³ã¯ããã©ãã°å¤±æãããã¹ãã®è©³ç´°ãªé
ç®ã表示ã§ãã
build.xml ã¯ä»¥ä¸ã®ã¨ããã«ãªã£ã¦ããã
<target name="phpunit"> <phpunit> <formatter type="xml" outfile="${outputDir}/phpunit.xml"/> <batchtest> <fileset dir="./phpfiles/"> <include name="**/*Test.php"/> </fileset> </batchtest> </phpunit> </target>
PHPUnit ã®è¨å®ã¯ Jenkins ã«ããã©ã«ãã§å
¥ã£ã¦ã JUnit ã®é
ç®ãè¨å®ããã°ããããã«ãå¾ã®å¦çã®[JUnitãã¹ãçµæã®éè¨]ã§è¡ãã
ãã¹ãçµæXMLã®è¨å®ã¯ãformatterã®outputfileã¨åãããã
PHPDoc ã®è¡¨ç¤ºã¨ build.xml
PHPDoc ã¯ãPHPãã¡ã¤ã«ã«
/**
* ãã®ã¯ã©ã¹ã¯ã»ãã»ããããã¯ã©ã¹ã§ã
* @author yamahsiro
*/
class HogeHoge {
ã¨æ¸ãã¦ããã¨ããã HTML å½¢å¼ã«æ´å½¢ãã¦ããããã¼ã«ã§ããã
åºåããã HTML ã Jenkinsæ°ããè¦ããã«ã¯ãJavadocã®åºåã¨ãã¦å¦çãã¦ãã¾ããã
build.xml ã¯ä»¥ä¸ã®ã¨ããã«ãªã£ã¦ããã
<target name="phpdoc"> <mkdir dir="phpdoc" /> <phpdoc title="API Documentation" destdir="./phpdoc" output="HTML:Smarty:PHP"> <fileset dir="./phpfiles"> </fileset> </phpdoc> </target>
PHPDocumentor ã«ãã£ã¦åºåãããå
ã®è¨å®ããJenkinsã§ããã©ã«ãã§å
¥ã£ã¦ãã Javadoc ã®é
ç®ãè¨å®ããã°ããããã«ãå¾ã®å¦çã®[Javadocã®ä¿å]ã§è¡ãã
ããã§ã®è¨å®é ç®ã¨ãdestdir ãããããã
ãã®çµæãPHPDocumentorã§åºåããããã¡ã¤ã«ããåã¸ã§ãã®ãªã³ã¯ã®[ããã¥ã¡ã³ã]ã§ãªã³ã¯ããããã¨ã«ãªãããã ããurlã«javadocãå
¥ãæ°æã¡æªãä»æ§ã«ãªããã
ã¨ã©ã¼ã®è§£æ¶æ¹æ³ã«ã¤ãã¦
ããã¾ã§èª¬æããåãã¼ã«ããã©ã°ã¤ã³ã§åºã¦ããã¨ã©ã¼ãå®éã«è§£æ¶ãã¦è¡ãããã¡ãªã¿ã«ãæãå ¥ãããã¡ã¤ã«ã«ã¯å ¨ã¦ãã¹ããåå¨ãã¦ãããããããã¹ãã®ãªãã³ã¼ãã§PHPCPDã®è¦åãPHPMDã®è¦åãããå ´åã¯ãå ã«ãã¹ããæ¸ãã¦ããæãå ¥ããã®ãæ£ããã¬ã¬ã·ã¼ã³ã¼ãã¨ã®æ¦ãæ¹ã¨è¨ãããã
ä¿®æ£ããã«ããã£ã¦ã¯ãå®ã¯ Jenkins ã¯ç´æ¥ã¯å©ç¨ããªããã¨ãããããããã®ä¿®æ£ã§ã¯ãã¾ã Jenkins ã¯å©ç¨ããªããæ¬æ¥ã¯ãJenkins ã¯æ¥ã åãã¦ãã¦ãä¾ãã°ã³ãããã®ãã³ã«ãã¹ããPHPMDãåãããã«ãã¦ãåä½ããªãã³ã¼ãããè¦åãã§ããããªã³ã¼ããæ¤ç¥ããããã«ä½¿ãããã ã
ä»åã¯ããã¹ãç¨ã« Jenkins ã® workspace ã«ã¯ç´æ¥ãã¡ã¤ã«ãç½®ãããã«ããããæ¬æ¥ã§ããã°ãSCMãããã¡ã¤ã«ãåã£ã¦ãã¦ããã¤ãSCMã®å¤æ´ãæ¤ç¥ãã¦ãã¹ããPHPMDã®ãã«ããè¡ãã»ããè¯ãã¨æããããSCMã®è¨å®ã¨ãã«ãã®è¨å®ã¯[ã½ã¼ã¹ã³ã¼ã管çã·ã¹ãã ]ã¨[ãã«ãã»ããªã¬]ã§è¡ãã
Jenkinsã®ãã©ã°ã¤ã³ãå ¥ãããã¨ã§gitãmercurialãªã©ã¨ãã£ãSCMã使ããã¨ãã§ããã
ç¹°ãè¿ãæ¸ããã以ä¸ã®èª¬æã§ã¯ã¯ã¼ã¯ã·ã§ããã®ç°¡ç´ åã®ããç´æ¥ Jenkins ã® workspace ãããã£ã¦ããããæ¬æ¥ã¯ Jenkins ã® workspace ã¯ç´æ¥ããããªãã§ãä¾ãã°ãã¼ã«ã«ã®éçºç°å¢ã§è½ã¡ã¦ããã¹ããä¿®æ£ãããã¼ã«ã«ã§ç¢ºèªããã³ããããããã®ã³ããããããªã¬ã¨ã㦠Jenkins ã®ãã«ããèµ°ãã¨ãããã¨ãç解ãã¦ããã¦æ¬²ããã
PHPUnitã®ä¿®æ£
ä½ã¯ã¨ãããããã¹ããè½ã¡ã¦ããç¶æ
ã¯ã¾ããã®ã§ããã®ä¸çªã«ãã¹ããä¿®æ£ããã
ä»åã®ä¾ã§ã¯ä»¥ä¸ã®ãã¹ããè½ã¡ã¦ããã
/** @test */ public function plusSimple2() { $this->assertSame(3, $this->foo->plus(1, 2)); }
plusã¨ããã¡ã½ããã¯ããã®åã®ã¨ãã足ãç®ãã¤ã¾ãã1+2=3ã¨ããå¦çãããã¡ã½ããã®ã¤ããã§ããã
ã¨ã©ã¼ã®å
容ã¯ä»¥ä¸ã®ã¨ããã« Jenkins ã§è¡¨ç¤ºããã¦ããã
FooTest::plusSimple2 Failed asserting that <integer:2> is identical to <integer:3>.
3ãäºæãã¦ããã®ã«ã2ãå®éã«ã¯è¿ã£ã¦ããã¨è¨ããã¦ããã
ãã¹ããå®è¡ããããã«ãworkspaceã«ç§»åãã¦PHPUnitã³ãã³ããå®è¡ãããã
cd $JENKINS_HOME/jobs/ci_workshop_job/workspace/phpfiles phpunit FooTest.php
ããããã¨ä»¥ä¸ã®ãããªè¡¨ç¤ºã«ãªãã¯ãã§ããã
PHPUnit 3.5.13 by Sebastian Bergmann. .F. Time: 0 seconds, Memory: 5.25Mb There was 1 failure: 1) FooTest::plusSimple2 Failed asserting that <integer:2> is identical to <integer:3>. /Users/Shared/Jenkins/Home/jobs/ci_workshop_job/workspace/phpfiles/FooTest.php:17 FAILURES! Tests: 3, Assertions: 7, Failures: 1.
Foo.phpã®plusã¡ã½ãããè¦ãã¨
function plus($a, $b) { return $a * $b; }
ã¨ãªã£ã¦ãããåç´ãªè©±ã§plusãªããºãªã®ã«ãæãç®ã«ãªã£ã¦ãããFoo.php ã®è©²å½ã®ç®æã® * ã + ã«å¤ãã¦å度 PHPUnit ã®ã³ãã³ããå®è¡ãããã
cd $JENKINS_HOME/jobs/ci_workshop_job/workspace/phpfiles phpunit FooTest.php
ä»åº¦ã¯
OK (3 tests, 7 assertions)
ã¨å ¨ã¦ã®ãã¹ããéã£ããã¨ã確èªã§ããã ããã
ï¼æ¬æ¥ã§ããã°ãã®ã³ã¼ããã³ããããã¦Jenkinsãåä½ããã®ãå¾
ã¤ãï¼
ãã®ç¶æ
㧠Jenkins ãåããã¨ããã¹ãã®ã¨ã©ã¼ãç¡ããªã£ã¦ãããçµæç»é¢ãéããªã£ã¦ãããã¨ã確èªã§ããã ããã
PHPCPD ã®ãããå¤ã®è¨å®ã¨è¦åã®ä¿®æ£
次ã«ãPHPCPDã®è¦åãæ¶ããããã®åã«ããã®ããã¸ã§ã¯ãã§ã¯ PHPCPD ã®è¦åãã§ããã¨ã©ã¼ã¨ããããã¤ã¾ãå ç¨ã®ç»é¢ã®ããã«éãç»é¢ã«ã¯ãªããªãããã«ãããã
ãã®ããã«ã¯ PHPCPD ã®é«åº¦ãªè¨å®ç»é¢ã§ã[ãã«ãçµæã®é¾å¤]ãè¨å®ãããããã®ãã® High ãããã¤ãã£ãããã¨ã Normal ããã£ããã¼ã¨ããæ°è¦ã«å¢ãããã¼ã¨ãç´°ããè¨å®ã§ããããåç´ã«ãå ¨ã¦ã®åè¨ã0ããå¤ãã¨ããã¨ããè¨å®ãããããããã
é話ä¼é¡ãã¬ã¬ã·ã¼ã¨æ¦ã£ã¦ãããã ã¨ããããPHPCPDã¨ãå¾ã§èª¬æããPHPMDã®è¦åã0ã«ãªãã®ã¯é ãæªæ¥ãããããªããã ã¨ãããä¾ãã°é¾å¤ãããç¨åº¦ã®æ°ã§è¨å®ããã®ãããããã¾ãã«å¤ããã¦è¦åãããããªãææ¨ã¯æå³ããªãã
ãããã¯ãæ°ããä½ãåºã hoge/ ãã©ã«ã以ä¸ã¯è¦å0ãç®æãã¨ããã®ãã¢ãªã ï¼åã®ããã¸ã§ã¯ãã§ã¯ããã ï¼ããã®å ´å㯠hoge ãã©ã«ã以ä¸ã ããå®è¡ããã¸ã§ããä½ãã®ãããã¨æãã
ãã¦ãã®ç¶æ 㧠Jenkins ãå度ãã«ãããã¨é»è²ããªãã¯ãã§ããã
ä»åº¦ã¯ PHPCPD ã®è¦åãæ¶ãããæ¶ãåã«Jenkins以å¤ããè¦åãè¦ãããã«ãããï¼ãªãã©ããªãã©ãè¨ããæ¬æ¥ã¯Jenkinsã®ã¯ã¼ã¯ã¹ãã¼ã¹ã§ã¯ä½æ¥ããªãï¼ã
PHPCPDã®ã³ãã³ããç´æ¥å©ãã¦ãããã®ã ããé¾å¤ã®æ å ±ã¨ããåæ£ãããªãããã«ããPhingçµç±ã§ PHPCPD ãå©ãã¦ã¿ããã
cd $JENKINS_HOME/jobs/ci_workshop_job/workspace/ phing phpcpd
ä½ããã¦ãããã¨ããã¨ãphing ã«ããã©ã«ãã®build.xmlå ã®phpcpdã¿ã¹ã¯ãå®è¡ããã¨ããå½ä»¤ãè¡ã£ã¦ããã
Foo.phpã以ä¸ã®ã¨ããã«ãªã£ã¦ãã¦ã
class Foo { function thisIsCopyAndPastePrograming($val) { $val = $val + 1; if ($val === 1) { return "1"; } else if ($val === 2) { return "2"; } else if ($val === 3) { return "3"; } else if ($val === 4) { return "4"; } else if ($val === 5) { return "5"; } else if ($val === 6) { return "6"; } else if ($val === 7) { return "7"; } return "Foo"; }
Hoo.phpã以ä¸ã«ãªã£ã¦ãã¦
class Hoge { function thisIsCopyAndPastePrograming($val) { if ($val === 1) { return "1"; } else if ($val === 2) { return "2"; } else if ($val === 3) { return "3"; } else if ($val === 4) { return "4"; } else if ($val === 5) { return "5"; } else if ($val === 6) { return "6"; } else if ($val === 7) { return "7"; } return "Hoge"; }
çµæã¨ãã¦ãbuidl.xmlã®outputfileã®è¨å®éãã«cpd.xmlã¨ãããã¡ã¤ã«ãåºæ¥ä¸ãã£ã¦ããã®ã§ç¢ºèªããã¨
<duplication lines="12" tokens="41"> <file path="/Users/Shared/Jenkins/Home/jobs/ci_workshop_job/workspace/./phpfiles/Foo.php" line="11"/> <file path="/Users/Shared/Jenkins/Home/jobs/ci_workshop_job/workspace/./phpfiles/Hoge.php" line="4"/> <codefragment> if ($val === 1) { return "1"; } else if ($val === 2) { return "2"; } else if ($val === 3) { return "3"; } else if ($val === 4) { return "4"; } else if ($val === 5) { return "5"; } else if ($val === 6) { return "6"; </codefragment> </duplication>
ã¨ãããããªåºåã«ãªã£ã¦ããããºã§ãããJenkinsã®ç»é¢ã§ã確èªã§ãããããã«ãFoo.phpã®11è¡ç®ããã¨Hoge.phpã®4è¡ç®ããã12è¡éè¤ãã¦ãã¨ããåºåã§ããã
ãããä¿®æ£ããããPHPCPDã®ä¿®æ£ã¯åã¯ããã¾ããã¿ã¼ã³ããªãã¦æ¥½ãªããããªããã¨æããæ°ãããã¡ã¤ã«ã«å ±éãã¦ãå¦çãæ¸ãããéè¤ãã¦ãäºã¤ä»¥ä¸ã®ã³ã¼ãã®è¦ªã¯ã©ã¹ãåä¸ãªã®ã§ããã°ããã®ã³ã¼ãã親ã³ã¼ãã«æã£ã¦ãããå ã®ã³ã¼ããããããå¼ã³åºãããã«ããã°ããã¨æãã
ä»åã®å ´åã¯ãä¾ãã°æ¬¡ã®ãããªã³ã¼ã IntToStr.php ã¨ãããããªãã¡ã¤ã«ãç¨æãã
class IntToStr { static function intToStrBetween1_7($val) { if ($val === 1) { return "1"; } else if ($val === 2) { return "2"; } else if ($val === 3) { return "3"; } else if ($val === 4) { return "4"; } else if ($val === 5) { return "5"; } else if ($val === 6) { return "6"; } else if ($val === 7) { return "7"; } } }
Foo.php 㨠Hoge.php 㧠require_once ãã¦å¼ã³åºãããã«å¤æ´ããã°ãã
require_once 'IntToStr.php'; class Foo { function thisIsCopyAndPastePrograming($val) { $val = $val + 1; $val = IntToStr::intToStrBetween1_7($val); return $val ? $val : "Foo"; }
ãã®æãä¾ãã°ã$val = $val + 1;ãã®è¡ããã¹ãã¦æ¸ãã®ãå¿ãã¦ãã¾ã£ãã¨ãããã
ãã®å ´åã§ãFooTest.phpã¨HogeTest.phpã¨ããPHPUnitã®ãã¹ãããããã¨ã©ã¼ã«ãªãã®ã§å®å¿ãã¦éè¤ã³ã¼ããæ¸ããã¨ãããªãã¡ã¯ã¿ãªã³ã°ãè¡ããã¨ãã§ããã
ä¿®æ£ãçµãã£ãããã¹ããåãããphpunit.xmlã確èªãã¨ã©ã¼ã0ã«ãªã£ã¦ãã®ã確èªããããéãéãè¨ããä»åã¯workspaceã§ä½æ¥ãã¦ãããé常ã§ããã°ãã¨ã©ã¼ããªããã¨ã確èªããã³ãããããJenkinsã§ç¢ºèªãããã
cd $JENKINS_HOME/jobs/ci_workshop_job/workspace/ phing phpunit
ãã®ç¶æ
㧠Jenkins ãåããã¨ãPHPCPDã®ã¨ã©ã¼ãç¡ããªããçµæç»é¢ãéããªã£ã¦ãããã¨ã確èªã§ããã ããã
ä½è«ã§ããããå®è£
ãå¢ããã®ã§æ¬æ¥ã§ããã°ãIntToStr.php ã«å¯¾ãããã¹ããæ¸ãããã¨ãã¨ã®HogeTest.phpã¨FooTest.phpã¯ãªãã¡ã¯ã¿ãªã³ã°ãã¹ãã ã¨èããããããããã¹ãããªãã¡ã¯ã¿ãªã³ã°ããã¨ãã«ãã¹ããæ£ãããã¨ã証æããã«ã¯ã©ãããããããã§ãããããã¹ãã®ãã¹ãã(ry
PHPMD ã®ãããå¤ã®è¨å®ã¨UsedParameterè¦åã®ä¿®æ£ãpmdåºåå½¢å¼ã®å¤æ´
次ã«ãPHPMDã®è¦åãæ¶ããããã®åã«ããã®ããã¸ã§ã¯ãã§ã¯ PHPMD ã®è¦åãã§ããã¨ã©ã¼ã¨ããããã¤ã¾ãå ç¨ã®ç»é¢ã®ããã«éãç»é¢ã«ã¯ãªããªãããã«ãããã
ãã®è¨å®ã¯ãããã»ã©ã® PHPCPD ã®è¨å®ã¨ã»ã¼ä¸ç·ãªã®ã§ã詳ããè¨å®ã®æ¹æ³ã¯ã¯ã¶ããPMDã®é«åº¦ãªè¨å®ã§[ãã«ãçµæã®é¾å¤]ã§å ¨ã¦ã®åè¨ã®é¾å¤ã0ã«ãããã
PHPMDã®è¦åã¯æ²¢å±±ç¨®é¡ãããã®ã§ãå¤æ´æ¹æ³ãå ¨ã¦èª¬æã¯ããªãããä¾ãã°UnusedFormalParameterã®ã¨ã©ã¼ã¯ãParameterã«æ¸¡ããã¦ãå¤æ°ãã¡ã½ããå ã§ä½¿ã£ã¦ããªãã¨ããã¨ã©ã¼ãªã®ã§ä»¥ä¸ã®ã¨ã©ã¼ãåºãã¡ã½ãã
function hasUnUsedParameterMethod($unuseParameter, $useParameter) { return $useParameter; }
ããã使ã£ã¦ãªããã©ã¡ã¼ã¿ã¼ãåé¤ããã°è¯ãã
function hasUnUsedParameterMethod($useParameter) { return $useParameter; }
ãã®ç¶æ 㧠phpmd ã phing çµç±ã§å®è¡ããã¨
cd $JENKINS_HOME/jobs/ci_workshop_job/workspace/ phing phpcpd
pmd.xmlã«ã¨ã©ã¼ãåºåããã¦ãããããpmd.xml ã¯å²ã¨è¦ã¥ããããé常ã®ããã¸ã§ã¯ãã§ããã¨è¦åã®æ°ãå¤ããªããã¨ãå¤ãã®ã§ãå®è¡ç°å¢ã«ãã£ã¦ãpmdã®åºåå½¢å¼ãå¤ããããã«ããããbuild.xmlã®å é ã®ã»ãã« pmdformat ã¨ããããããã£ã足ããã
<property name="outputDir" value="."/> <property name="pmdformat" value="html"/>
次ã«ãphpmd ã® formatter ã pmdformatã使ãããã«æ¸ãæããã
<formatter type="${pmdformat}" outfile="${outputDir}/pmd.${pmdformat}"/>
ããã§ãphing phpmdããå®è¡ããã¨ãworkspaceã«pmd.htmlãåºåããããå²ã¨äººéã«ã¯è¦ããããªã£ãã¯ãã§ãããUnusedFormalParameterã®è¦åã¯ç¡ããªã£ã¦ããã®ãåããã§ãããã
ãã ãJenkinsã®PMDãã©ã°ã¤ã³ã§ã¯htmlå½¢å¼ã§ã¯æ±ããªãã®ã§ãããããã£ã®è¨å®ã以ä¸ã®ããã«pmdformat=xmlã¨ä¸æ¸ããããã
ããã§ãã¹ããå®è¡ããã¨ã¨ã©ã¼ã«ãªãã®ã§ã
cd $JENKINS_HOME/jobs/ci_workshop_job/workspace/ phing phpunit
ãã¹ããã¡ã¤ã«ã®ã»ããä¿®æ£ãã¦ããã
ã§ãé常ã§ããã°ã³ããããã¦ãããã
CyclomaticComplexity è¦åã®ä¿®æ£
次ã®è¦åã¯ãHoge.phpãCyclomaticComplexityã¨è¨ããã¦æããã¦ãããããã¯ã³ã¼ããè¤éãããã¨ããè¦åã§ããããã®è¦åãã¡ã½ããå ã®å®è¡ãã¹ã®å¤ããè¦åããNPathComplexityãªã©ã®è¦åã¯ãä¾ãã° ifæã foræãã¡ã½ããã«åãåºããããã¯ã©ã¹åãã¦å§è²ããã®ãããã¨æããããæ¹ã¯ããã¤ããããã
Hoge.phpã¯ä»¥ä¸ã®ã¨ããã«ãªã£ã¦ããã
function complexMethod($array) { foreach($array as $val) { if ($val === 3) { if (true) { if (true) {} if (true) {} return $val; } } } if (true) { while (false) { if (true) {} } } if (true) { while (false) { if (true) {} } } return "complexMethod"; }
ã¨ããããforæãã¡ã½ããåãã¦å¼ã³åºãæ¹å¼ã«å¤ãã¦ã¿ãã
function complexMethod($array) {
$val = $this->foreachMethod($array);
if (true) { while (false) { if (true) {} } }
if (true) { while (false) { if (true) {} } }
return $val ? $val : "complexMethod";
}
private function foreachMethod($array) {
foreach($array as $val) {
if ($val === 3) {
if (true) {
if (true) {}
if (true) {}
return $val;
}
}
}
}
ããã§ãCyclomaticComplexityã®è¦åã¯æ¶ããã¯ãã§ããã
ããããã¹ããå®è¡ãã¦ç¢ºèªããããæ¬æ¥ã§ããã°ã³ããããã¦ãããã
TooManyMethodsã®è¦åãæ¶ã(@SuppressWarningsãã¼ã¸ã§ã³ï¼
ãã¦æå¾ã«TooManyMethodsã®è¦åãæ¶ãããTooManyMethodsã®è¦åãåºã¦ããã®ã¯CommonUtilã§ããã
CommonUtilã¯å ±éã§ä½¿ãããUtilãªã®ã§ãTooManyMethodsãªã®ã¯ãããããªãã¨ãããã
ãªã®ã§è¦åãç¡è¦ããããã«ãããã@SuppressWarningsã¨ããã¢ããã¼ã·ã§ã³ã使ãã°è¦åãç¡è¦ãããã¨ãã§ãããTooManyMethodsã¯ã¯ã©ã¹ã«ã¤ãè¦åãªã®ã§ã¯ã©ã¹ã®ã³ã¡ã³ãã«ãUnusedFormalParameterãç¡è¦ãããã£ããã¡ã½ããã«ã¤ãè¦åãªã®ã§ã¡ã½ããã®ã³ã¡ã³ãã«å ¥ããã
å ·ä½çã«ã¯ä»¥ä¸ã®ã¨ããã¨ãªãã
/** * @SuppressWarnings(PHPMD.TooManyMethods) * */ class CommonUtil { function hoge1() {}
ãã㧠TooManyMethods ã®ã¨ã©ã¼ãæ¶ããå ¨ã¦ã®ãã¹ãã¨ã©ã¼ãPHPCPDã®è¦åãPHPMDã®è¦åãæ¶ããã¸ã§ãã®ç¶æ ãéãç¶æ ã«ãªã£ãã¯ãã§ããã
xdebug ã®ã¤ã³ã¹ãã¼ã«ã¨è¨å®ãcloverã§ã®ã«ãã¬ãã¸çã®è¡¨ç¤º
ãã¦ãã¿ãªããããããã¯ãããã¹ããã¼ã«ã¨ãã§ãã¹ãã§ããããã«ãªã£ã¦ãããã«ãã¬ãã¸çã¯ãããããããæ®éããã§ããããåã¯ããã ã
xdegug 㨠clover ã¨ãããã©ã°ã¤ã³ã使ãã° Jenkins ã§ã«ãã¬ãã¸çã表示ãããã¨ãã§ããã
ã¾ã㯠xdebug ã®ã¤ã³ã¹ãã¼ã«æ¹æ³ã ãç°å¢ã«ãã£ã¦è²ã
ã ããã°ã°ãã«ã¹ã¨ããâ¦ï½
peclã§ã¤ã³ã¹ãã¼ã«ããã®ãããããããªãã§ããããããããã¯æ®éã«ã½ã¼ã¹ãããã£ã¦ãã¦ãã«ããããã
Windowsã¯dllããããããªã
php.iniã§xdebugã®soãªããdllãèªã¿è¾¼ãè¨å®ãæ¸ã
/usr/local/lib/php.ini ã¨ãã/etc/php.iniã¨ãC:\Xampp\php\php.ini ã
zend_extension = "/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
ãªãã
zend_extension = "C:\xampp\php\xdebug.dll"
ã足ãã°ãããã
ããã¦ãbuild.xmlãæ¸ãæãã¦ãPHPUnitã§ã®ãã¹ãã®åã«Coverageæ å ±ã®äºååºåã¨coverageã®formatãããã°è¯ããã©ããã¥ã¼ãã¼3åéã¯ããã³ã°ã®ããã«ããã§ã« coverage_build.xml ã¨ããã®ãç¨æãã¦ããã
coverage_build.xml ã使ããããã« Phing ã®è¨å®ãæ¸ãæããã
coverage_build.xmlã®å 容ã¯ä»¥ä¸ã®ã¨ããã
<target name="phpunit"> <mkdir dir="clover" /> <coverage-setup database="${outputDir}/coverage.db"> <fileset dir="./phpfiles/"> <exclude name="**/*Test.php"/> </fileset> </coverage-setup> <phpunit codecoverage="true"> <formatter type="xml" outfile="${outputDir}/phpunit.xml"/> <formatter type="clover" outfile="clover.xml"/> <batchtest> <fileset dir="./phpfiles/"> <include name="**/*Test.php"/> </fileset> </batchtest> </phpunit> <coverage-report outfile="clover.xml"> <report todir="clover" /> </coverage-report> </target>
coverage-setupã§ã«ãã¬ãã¸ãåå¾ãããã¡ã¤ã«ç¾¤ã®äºåæ
å ±ãåå¾ããphpunitã¿ã¹ã¯ã®codecoverage="true"ã§PHPUnitã§ã«ãã¬ãã¸æ
å ±ãåéããããã«ãã
ãã®åºåããããã¡ã¤ã«ãéè¨ã§ããããã«ãJenkinsã§cloverãã©ã°ã¤ã³ã®è¨å®ãè¡ãã
coverage_build.xml ã«æ¸ãã outfile 㨠report ã® todir ã®è¨å®ã使ã£ã¦ã[Clover ã«ãã¬ãã¸ã¬ãã¼ããéè¨]ã®è¨å®ãä¸è¨ã®ããã«è¡ãã
ããã§ã¸ã§ããå®è¡ããã¨ãåã¸ã§ãã®ã¡ãã¥ã¼ã«ã [Clover HTMLã«ãã¬ãã¸ã¬ãã¼ã]ã®ãªã³ã¯ã表示ããã
ã¯ãªãã¯ããã¨ã«ãã¬ãã¸çã表示ãããã
ä½è«ã ãã©ãã«ãã¬ãã¸çã£ã¦ããã¾ãé«ããã®ãç®æãã¦ããã¡ã ã¨æããã§ãããã ãã©ãã¨ãããã«ãã¬ãã¸çãé«ãã»ãããããã ããå°ãã¥ã¤ãããå¤ãé«ããã¦ãã£ã¦ã ãã ãé«ããªããªãã¨è¦åã¨ããã¨ããã«ãã¬ãã¸çãåå¾ãã¦ãplotã£ã¦ããJenkinsãã©ã°ã¤ã³ãããã®ã§ãã«ãã¬ãã¸çã®å¤åã表示ããã¨ããããããªãã§ããããã
å°ãã¿
ãªããã ãã£ã¨å°ãã¿æ¸ãã¦ãã
PHPUnit ã§ã®å®è¡ã¨ Phing 㧠phpunit ãå ¨é¨åãããã¨ãã®åä½ãéã件
PHPUnitã§ã®ãã¹ãã¯éããã ãã©ãPhingã§phpunitã¿ã¹ã¯ãåããããéããªãã¨ããéã®ã±ã¼ã¹ã¨ããããã
ä¾ãã°åãPHPUnitã§ã¯éãã®ã«ãPhingã§éããªãã¦ã¯ã¾ã£ãã®ã¯ global ãªå¤ãè¦ã¦ãã¨ãã«ãPHP CLIã®ãã°ããããã ãã©ãglobal ã§åç §ãããã¨ãã¦ãè¦ããªãã¦ãåã ã®ãã¹ããã¡ã¤ã«ã« global $hoge; ã¨ãæ¸ãã¨ãã¹ãéãã¨ãâ¦
ãããã¯éã«Phingã§ã¯éãã®ã«ãPHPUnitã§åã ã®ãã¹ãããã¨ãã¡â¦ã¨ãã¯ãä»ã®ãã¹ã㧠require_once ããã¦ã¦ããã®ãããã§ãã¹ããéãã¨ããããã¿ã¼ã³ã
ããããã©ãã«ãããã«ã¯ãjobãç´°ããããã®ãããã¨æã£ã¦ããã
ãã®ããã¥ã¡ã³ãã§ãæ¸ãããã©ãJenkinsã¯CUIããã触ããããã®ã§ãjenkins-cliã¨ãããããè¨å®ãã¡ã¤ã«ã¯ config.xml ã¨ããã®ããããã°ããã ãã ããæ°ããä½æ¥ãããã¨ãã«ã¯ã¹ã¯ãªããã§æ°ãã job ãã¤ããã¨ããã¨æããã
å ±éãã¡ã¤ã«ã¨ãã ã PHPMD ã®ã«ã¼ã«ãå¤ãã
TooManyMethods ã®è¦åãæ¶ãã£ã¦èª¬æã®ã¨ãã㧠@SuppressWarnings ãå ¥ããã£ã¦ãã説æãããããã
å ±éãã¡ã¤ã«ã¨ã㯠@SuppressWarnings ã§äºè¶³ããã¨æããã ãã©ãTestãã¡ã¤ã«ã¨ãã£ã¦ãã³ã¼ãã®ç¶ºéºãã¨ãèããªããããªãã
ãã®å ´åã¯ãphpmdã®ã¿ã¹ã¯ãåãã¦ã«ã¼ã«ãã¡ã¤ã«ããããã°ããã¨æãããbuild.xml ã§
<target name="phpmd"> <phpmd rulesets="ruleset.xml"> <fileset dir="./phpfiles"> <exclude name="exclude.php"/> <exclude name="**/**/*Test.php"/> </fileset> <formatter type="xml" outfile="${outputDir}/pmd.xml"/> </phpmd> </target> <target name="phpmd_testfile"> <phpmd rulesets="ruleset_testfile.xml"> <fileset dir="./phpfiles"> <include name="**/**/*Test.php"/> </fileset> <formatter type="xml" outfile="${outputDir}/pmd_testfile.xml"/> </phpmd> </target>
ã¿ãããªæãã§ããã¤ã³ãã¯ã«ã¼ã«ãã¡ã¤ã«ãéãã£ã¦ãã¨ã¨ãoutfileãéããã¨ãã¡ãªã¿ã« Jenkins ã® PMD ã®è¨å®ã§ãpmdãã¡ã¤ã«ã®è¨å®ãä»åã®ä¾ã§ã¯pmd*.xmlã«ãã¦ããããããã®build.xmlã®è¨å®ã§ãã¾ãåããã
ã¹ãã¼ã¸ã³ã°ç°å¢ã«ãããã¤ã㦠Selenium ã¨ãã§ãã¹ã
ãããåã®ããã¸ã§ã¯ãã§ã¯ã§ãã¦ãªãã®ã§å¦æ³ãªãã ãã©ãCI ã®ããã»ã¹ã®ä¸ã«ããã¹ãéã£ãããã¹ãã¼ã¸ã³ã°ç°å¢ã¨ãã«ãããã¤ã㦠Selenium ã¨ãã§çµåãã¹ãã¨ãããã¨ãã§ããããPhing使ã£ã¦ãããããå¥ã®ãã«ããã¼ã«ä½¿ã£ã¦ããããã
å²ã¨ãã£ããå 㯠PHP é¢ä¿ãªãããã
PHPMD ã§ããã¸ã§ã¯ãç¬èªã®ã«ã¼ã«ãæ¸ã
é·ããªãã®ã§å¾ã§å¥ã¨ã³ããªã«ãããåºæ¬ã¯ruleset ã«æ°ããã«ã¼ã«ã®è¨å®ãæ¸ããæ°ããRuleã¯ã©ã¹ãä½ããããã§å®äºã
æ¢åã®ã«ã¼ã«ã®ã¯ã©ã¹ãè¦ãã°å²ã¨ç°¡å