phpunit与xdebug的使用

基本说明:

1.xdebug是程序猿在调试程序过程中使用的bug调试暴露工具

  windows下安装:

  1)下载php对应的dll文件,下载地址:https://xdebug.org/download.php

  2)在php.ini文件中做对应xdebug的配置,只需要在最后加上下面的代码

[Xdebug]
zend_extension="C:/xampp/php/ext/php_xdebug.dll"

  3)因为我是集成环境xampp的一键安装,不需要下载dll文件,在xampp内部已经存在,只需在php配置文件末尾添加对应配置即可,请不要在额外有extention;如果自行下载dll文件,注意下载与php版本与线程相匹配的文件;zend_extension这个名称也会随着php版本的不同而变化,如果遇到了相应问题,网络上有很多对应说明。

2.phpunit是测试人员单元测试工具,需要对程序员的开发代码进行每个类至每个方法的“断言”,常常程序员在开发过程中也会编写对应的测试用例代码,方便后期代码变动的测试。

  1)在集成环境xampp中,我的安装方式是pear命令行安装:

pear install phpunit/PHPUnit

  安装完之后,你会在php的根目录下看到pear.bat文件  

  安装完之后,最好设置phpunit为环境变量,这样就可以全局使用了,phpunit环境变量对应的目录就是pear.bat文件所在的目录  

  2)安装完之后我创建一个测试文件,代码如下:

<?php
//This is my first test
class MyFirstTest extends PHPUnit_Framework_TestCase{
  public function testFirst(){
    $stack = 7;
    $this->assertEquals(0,$stack);
  }
}

  3)创建完单元测试文件之后,让我们去命令行执行一下,以下是命令行显示:

C:\Users\v_lihuan1>phpunit F:\www\testCase.php
PHPUnit 3.7.21 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 2.00Mb

There was 1 failure:

1) MyFirstTest::testFirst
Failed asserting that 7 matches expected 0.

F:\www\testCase.php:6

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

  现在phpunit已经可以用了,至于断言的内容和代码的编写,我们只需要查看对应phpunit的方法文档即可

3.pdepend是一个PHP中静态代码分析的工具。它可以用来检查你的PHP项目中的代码规模和复杂程度.我也是第一次安装,如有不妥,请大家指教

执行命令行:pear install pdepend/PHP_Depend-beta ,出现以下问题

F:\www\xdebug>pear install pdepend/PHP_Depend-beta
Did not download optional dependencies: pecl/imagick, use --alldeps to download automatically
pdepend/PHP_Depend can optionally use package "pecl/imagick" (version >= 2.2.0b2)
downloading PHP_Depend-1.1.4.tgz ...
Starting to download PHP_Depend-1.1.4.tgz (179,584 bytes)
.....................done: 179,584 bytes
ERROR: failed to mkdir C:\php\pear\docs\PHP_Depend

F:\www\xdebug>

按照文中所说是缺少对应依赖pecl/imagick,需要先安装这个插件.我查到pear有可以将对应依赖一次全部执行的命令,如下所示:

F:\www\xdebug>pear install --alldeps pdepend/PHP_Depend-beta
downloading PHP_Depend-1.1.4.tgz ...
Starting to download PHP_Depend-1.1.4.tgz (179,584 bytes)
......................................done: 179,584 bytes
downloading imagick-3.4.3.tgz ...
Starting to download imagick-3.4.3.tgz (245,410 bytes)
...done: 239,218 bytes
install ok: channel://pear.pdepend.org/PHP_Depend-1.1.4
ERROR: unable to unpack C:\Users\V_LIHU~1\AppData\Local\Temp\pear\download\imagick-3.4.3.tgz

F:\www\xdebug>

如上所示:pecl/imagick解压失败,按照理解,也就是安装未成功😱

我又重新使用pear install  pecl/imagick和pecl  install imagick都未安装成功,并爆出异常如下所示:

C:\Windows\system32>pear install pecl/imagick
downloading imagick-3.4.3.tgz ...
Starting to download imagick-3.4.3.tgz (245,410 bytes)
...................................................done: 245,410 bytes
19 source files, building
WARNING: php_bin C:\xampp\php\php.exe appears to have a suffix .exe, but config variable php_suffix does not match
ERROR: The DSP imagick.dsp does not exist.

C:\Windows\system32>pecl install imagick
downloading imagick-3.4.3.tgz ...
Starting to download imagick-3.4.3.tgz (245,410 bytes)
.....................................done: 245,410 bytes
19 source files, building
WARNING: php_bin C:\xampp\php\php.exe appears to have a suffix .exe, but config variable php_suffix does not match
ERROR: The DSP imagick.dsp does not exist.

搜集了一下网上的资料,我决定不使用命令行安装了,下载exe文件执行安装,下载地址:

http://www.imagemagick.org/script/download.php

执行完之后,我去命令行查看pdepend 的安装版本情况如下:

C:\Users\v_lihuan1>pdepend --version
PHP_Depend 1.1.4 by Manuel Pichler


C:\Users\v_lihuan1>

按照上面显示,是成功了,接下来我在去做一下pdepend的使用

pdepend使用

1)生成程序的xml格式的summary report,我需要自行解析生成的文件

C:\Windows\system32>pdepend --summary-xml=F:\tmp\summary.xml F:\www\wxapiweb
PHP_Depend 1.1.4 by Manuel Pichler

Parsing source files:
..................................

 

2)生成两个svg矢量图

第一种类型:

C:\Windows\system32>pdepend --jdepend-chart=F:\tmp\testchart.svg F:\www\xdebug
PHP_Depend 1.1.4 by Manuel Pichler

Parsing source files:
..........                                                      10

Executing Dependency-Analyzer:
..                                                              45

Generating pdepend log files, this may take a moment.

Time: 00:02; Memory: 4.50Mb

C:\Windows\system32>

图片上的文字表述说明:https://pdepend.org/documentation/handbook/reports/abstraction-instability-chart.html

第二种矢量图片

C:\Windows\system32>pdepend --overview-pyramid=F:\tmp\testpyramid.svg F:\www\xdebug
PHP_Depend 1.1.4 by Manuel Pichler

Parsing source files:
..........                                                      10

Executing Coupling-Analyzer:
...                                                             66

Executing CyclomaticComplexity-Analyzer:
...                                                             68

Executing Inheritance-Analyzer:
                                                                10

Executing NodeCount-Analyzer:
..                                                              45

Executing NodeLoc-Analyzer:
..                                                              55

Generating pdepend log files, this may take a moment.

Time: 00:01; Memory: 6.25Mb

C:\Windows\system32>

图上参数表述说明参考:https://pdepend.org/documentation/handbook/reports/overview-pyramid.html

 

 

 

在使用的过程中遇到以下问题:

前提注意:

1.phpunit在环境变量中设置,使其可以全局使用

2.phpunit -version查看phpunit版本

3.phpunit可以将文件夹内所有的测试用例执行,并生成报告文件,这样更方便我们全局代码管理

F:\www\xdebug>phpunit --coverage-html /test1 test.php
PHPUnit 3.7.21 by Sebastian Bergmann.

.

Time: 5 seconds, Memory: 3.25Mb

OK (1 test, 1 assertion)

Generating code coverage report in HTML format ... done

F:\www\xdebug>
/test1是生成的报告文件的存放位置,test.php是对应要执行的测试用例,我们也可以将其设置为文件夹,那么文件夹下的所有测试用例都将被执行
posted @ 2017-12-07 17:11  芭菲雨  阅读(430)  评论(0编辑  收藏  举报