PHPUnit and PHP

PHPUnitandPHP

1ChecktheversionofmyPHP

>php--version

PHP5.5.30(cli)(built:Oct23201517:21:45)

Copyright(c)1997-2015ThePHPGroup

ZendEnginev2.5.0,Copyright(c)1998-2015ZendTechnologies

InstallitGlobal

>wgethttps://phar.phpunit.de/phpunit.phar

>chmod+xphpunit.phar

>sudomvphpunit.phar/usr/local/bin/phpunit

>phpunit--version

PHPUnit5.1.3bySebastianBergmannandcontributors.

Installinproject

wgetthepharfileandthenrunitlikethis

>phpphpunit.phar--version

Oh,Iamusingcomposer,soImayneedtoaddphpunitfromthere.

Addthedependencytomycomposer.json

{

"require":{

"aws/aws-sdk-php":"3.0.6",

"predis/predis":"1.0.1"

},

"require-dev":{

"phpunit/phpunit":"5.1.*",

"phpunit/dbunit":">=1.2",

"phpunit/php-invoker":"*"

}

}

>phpcomposer.pharupdate

ButitcomplainaboutmyPHPversion.

UpgradethePHPVersion

>wgethttp://ar2.php.net/distributions/php-5.6.16.tar.bz2

makemeinstallsomethingherehttp://www.xquartz.org/

Thereisalotoferrormessagesthere,soItrythiswayfromherehttp://php-osx.liip.ch/

>curl-shttp://php-osx.liip.ch/install.sh|bash-s5.6

>sudoln-s/usr/local/php5/opt/php

ItworksafterIupgradethephpversion.

>phpcomposer.pharupdate

>php--version

PHP5.6.16(cli)(built:Dec26201518:37:18)

2RecallMyPHPKnowledge

PHPwebprogramming

classfoo{

var$foo;

var$bar;

functionfoo(){

$this->foo=‘Foo’;

$this->bar=array(‘Bar1’,‘Bar2’,‘Bar3’);

}

}

3SetUpPHPUnit

Hereishowtheprojectsetupcomposer.json

{

"autoload":{

"psr-0":{

"PHPUnitEventDemo":"src/"

}

},

"require":{

},

"require-dev":{

"phpunit/phpunit":"5.1.*",

"phpunit/dbunit":">=1.2",

"phpunit/php-invoker":"*"

}

}

Thesourcecodessrc/PHPUnitEventDemo/Event.php

<?php

namespacePHPUnitEventDemo;

classEvent

{

public$id;

public$name;

public$start_date;

public$end_date;

public$deadline;

public$attendee_limit;

public$attendees=array();

publicfunction__construct($id,$name,$start_date,$end_date,$deadline,$attendee_limit)

{

$this->id=$id;

$this->name=$name;

$this->start_date=$start_date;

$this->end_date=$end_date;

$this->deadline=$deadline;

$this->attendee_limit=$attendee_limit;

}

publicfunctionreserve($user)

{

//使用者報名

$this->attendees[$user->id]=$user;

}

publicfunctiongetAttendeeNumber()

{

returnsizeof($this->attendees);

}

publicfunctionunreserve($user)

{

//使用者取消報名

unset($this->attendees[$user->id]);

}

}

?>

src/PHPUnitEventDemo/User.php

<?php

namespacePHPUnitEventDemo;

classUser

{

public$id;

public$name;

public$email;

publicfunction__construct($id,$name,$email)

{

$this->id=$id;

$this->name=$name;

$this->email=$email;

}

}

?>

MytestClassasfollow:tests/EventTest.php

<?php

classEventTestextendsPHPUnit_Framework_TestCase

{

publicfunctiontestReserve()

{

$eventId=1;

$eventName='活動1';

$eventStartDate='2014-12-2418:00:00';

$eventEndDate='2014-12-2420:00:00';

$eventDeadline='2014-12-2323:59:59';

$eventAttendeeLimit=10;

$event=new\PHPUnitEventDemo\Event($eventId,$eventName,$eventStartDate,

$eventEndDate,$eventDeadline,$eventAttendeeLimit);

$userId=1;

$userName='User1';

$userEmail='[email protected]';

$user=new\PHPUnitEventDemo\User($userId,$userName,$userEmail);

//使用者報名活動

$event->reserve($user);

$expectedNumber=1;

//預期報名人數

$this->assertEquals($expectedNumber,$event->getAttendeeNumber());

//報名清單中有已經報名的人

$this->assertContains($user,$event->attendees);

return[$event,$user];

}

/**

*@dependstestReserve

*/

publicfunctiontestUnreserve($objs)

{

//測試使用者取消報名

$event=$objs[0];

$user=$objs[1];

//使用者取消報名

$event->unreserve($user);

$unreserveExpectedCount=0;

//預期報名人數

$this->assertEquals($unreserveExpectedCount,$event->getAttendeeNumber());

//報名清單中沒有已經取消報名的人

$this->assertNotContains($user,$event->attendees);

}

}

?>

AndhereisthestepsIgorunthetestclass:

IalreadyhavePHP

>php--version

PHP5.6.16(cli)(built:Dec26201518:37:18)

Copyright(c)1997-2015ThePHPGroup

ZendEnginev2.6.0,Copyright(c)1998-2015ZendTechnologies

withZendOPcachev7.0.6-dev,Copyright(c)1999-2015,byZendTechnologies

withXdebugv2.2.5,Copyright(c)2002-2014,byDerickRethans

>curl-sShttps://getcomposer.org/installer|php

>phpcomposer.pharinstall

Runthetestclass

>phpunit--bootstrapvendor/autoload.phptests/EventTest

CreatethisfilethereandWearegoodtogophpunit.xml

<phpunitbootstrap="vendor/autoload.php">

<testsuites>

<testsuitename="money">

<directory>tests</directory>

</testsuite>

</testsuites>

</phpunit>

SingleCommandwillrunallthetests

>phpunit

References:

https://phpunit.de/manual/current/zh_cn/installation.html

https://phpunit.de/manual/current/zh_cn/writing-tests-for-phpunit.html

http://www.openfoundry.org/tw/tech-column/9326-phpunit-testing

https://phpunit.de/manual/current/en/organizing-tests.html

https://github.com/ymhuang0808/PHPUnit-Event-Demo/tree/start

相关推荐