- 浏览: 2539228 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
PHPUnit and PHP
1 Check the version of my PHP
> php --version
PHP 5.5.30 (cli) (built: Oct 23 2015 17:21:45)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
Install it Global
> wget https://phar.phpunit.de/phpunit.phar
> chmod +x phpunit.phar
> sudo mv phpunit.phar /usr/local/bin/phpunit
> phpunit --version
PHPUnit 5.1.3 by Sebastian Bergmann and contributors.
Install in project
wget the phar file and then run it like this
> php phpunit.phar --version
Oh, I am using composer, so I may need to add phpunit from there.
Add the dependency to my composer.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": "*"
}
}
> php composer.phar update
But it complain about my PHP version.
Upgrade the PHP Version
http://sillycat.iteye.com/blog/2149513
http://sillycat.iteye.com/blog/2223621
> wget http://ar2.php.net/distributions/php-5.6.16.tar.bz2
make me install something here http://www.xquartz.org/
http://sillycat.iteye.com/blog/2076067
There is a lot of error messages there, so I try this way from here http://php-osx.liip.ch/
> curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6
> sudo ln -s /usr/local/php5 /opt/php
It works after I upgrade the php version.
> php composer.phar update
> php --version
PHP 5.6.16 (cli) (built: Dec 26 2015 18:37:18)
2 Recall My PHP Knowledge
PHP web programming
http://sillycat.iteye.com/blog/768664
http://sillycat.iteye.com/blog/769110
http://sillycat.iteye.com/blog/770369
class foo{
var $foo;
var $bar;
function foo(){
$this->foo = ‘Foo’;
$this->bar = array(‘Bar1’, ‘Bar2’, ‘Bar3’);
}
}
http://sillycat.iteye.com/blog/2194084
3 Set Up PHPUnit
Here is how the project set up composer.json
{
"autoload" : {
"psr-0": {
"PHPUnitEventDemo": "src/"
}
},
"require": {
},
"require-dev": {
"phpunit/phpunit": "5.1.*",
"phpunit/dbunit": ">=1.2",
"phpunit/php-invoker": "*"
}
}
The source codes src/PHPUnitEventDemo/Event.php
<?php
namespace PHPUnitEventDemo;
class Event
{
public $id;
public $name;
public $start_date;
public $end_date;
public $deadline;
public $attendee_limit;
public $attendees = array();
public function __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;
}
public function reserve($user)
{
// 使用者報名
$this->attendees[$user->id] = $user;
}
public function getAttendeeNumber()
{
return sizeof($this->attendees);
}
public function unreserve($user)
{
// 使用者取消報名
unset($this->attendees[$user->id]);
}
}
?>
src/PHPUnitEventDemo/User.php
<?php
namespace PHPUnitEventDemo;
class User
{
public $id;
public $name;
public $email;
public function __construct($id, $name, $email)
{
$this->id = $id;
$this->name = $name;
$this->email = $email;
}
}
?>
My test Class as follow: tests/EventTest.php
<?php
class EventTest extends PHPUnit_Framework_TestCase
{
public function testReserve()
{
$eventId = 1;
$eventName = '活動1';
$eventStartDate = '2014-12-24 18:00:00';
$eventEndDate = '2014-12-24 20:00:00';
$eventDeadline = '2014-12-23 23:59:59';
$eventAttendeeLimit = 10;
$event = new \PHPUnitEventDemo\Event($eventId, $eventName, $eventStartDate,
$eventEndDate, $eventDeadline, $eventAttendeeLimit);
$userId = 1;
$userName = 'User1';
$userEmail = 'user1@openfoundry.org';
$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];
}
/**
* @depends testReserve
*/
public function testUnreserve($objs)
{
// 測試使用者取消報名
$event = $objs[0];
$user = $objs[1];
// 使用者取消報名
$event->unreserve($user);
$unreserveExpectedCount = 0;
// 預期報名人數
$this->assertEquals($unreserveExpectedCount, $event->getAttendeeNumber());
// 報名清單中沒有已經取消報名的人
$this->assertNotContains($user, $event->attendees);
}
}
?>
And here is the steps I go run the test class:
I already have PHP
>php --version
PHP 5.6.16 (cli) (built: Dec 26 2015 18:37:18)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
>curl -sS https://getcomposer.org/installer | php
>php composer.phar install
Run the test class
>phpunit --bootstrap vendor/autoload.php tests/EventTest
Create this file there and We are good to go phpunit.xml
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="money">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Single Command will run all the tests
> 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
1 Check the version of my PHP
> php --version
PHP 5.5.30 (cli) (built: Oct 23 2015 17:21:45)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
Install it Global
> wget https://phar.phpunit.de/phpunit.phar
> chmod +x phpunit.phar
> sudo mv phpunit.phar /usr/local/bin/phpunit
> phpunit --version
PHPUnit 5.1.3 by Sebastian Bergmann and contributors.
Install in project
wget the phar file and then run it like this
> php phpunit.phar --version
Oh, I am using composer, so I may need to add phpunit from there.
Add the dependency to my composer.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": "*"
}
}
> php composer.phar update
But it complain about my PHP version.
Upgrade the PHP Version
http://sillycat.iteye.com/blog/2149513
http://sillycat.iteye.com/blog/2223621
> wget http://ar2.php.net/distributions/php-5.6.16.tar.bz2
make me install something here http://www.xquartz.org/
http://sillycat.iteye.com/blog/2076067
There is a lot of error messages there, so I try this way from here http://php-osx.liip.ch/
> curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6
> sudo ln -s /usr/local/php5 /opt/php
It works after I upgrade the php version.
> php composer.phar update
> php --version
PHP 5.6.16 (cli) (built: Dec 26 2015 18:37:18)
2 Recall My PHP Knowledge
PHP web programming
http://sillycat.iteye.com/blog/768664
http://sillycat.iteye.com/blog/769110
http://sillycat.iteye.com/blog/770369
class foo{
var $foo;
var $bar;
function foo(){
$this->foo = ‘Foo’;
$this->bar = array(‘Bar1’, ‘Bar2’, ‘Bar3’);
}
}
http://sillycat.iteye.com/blog/2194084
3 Set Up PHPUnit
Here is how the project set up composer.json
{
"autoload" : {
"psr-0": {
"PHPUnitEventDemo": "src/"
}
},
"require": {
},
"require-dev": {
"phpunit/phpunit": "5.1.*",
"phpunit/dbunit": ">=1.2",
"phpunit/php-invoker": "*"
}
}
The source codes src/PHPUnitEventDemo/Event.php
<?php
namespace PHPUnitEventDemo;
class Event
{
public $id;
public $name;
public $start_date;
public $end_date;
public $deadline;
public $attendee_limit;
public $attendees = array();
public function __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;
}
public function reserve($user)
{
// 使用者報名
$this->attendees[$user->id] = $user;
}
public function getAttendeeNumber()
{
return sizeof($this->attendees);
}
public function unreserve($user)
{
// 使用者取消報名
unset($this->attendees[$user->id]);
}
}
?>
src/PHPUnitEventDemo/User.php
<?php
namespace PHPUnitEventDemo;
class User
{
public $id;
public $name;
public $email;
public function __construct($id, $name, $email)
{
$this->id = $id;
$this->name = $name;
$this->email = $email;
}
}
?>
My test Class as follow: tests/EventTest.php
<?php
class EventTest extends PHPUnit_Framework_TestCase
{
public function testReserve()
{
$eventId = 1;
$eventName = '活動1';
$eventStartDate = '2014-12-24 18:00:00';
$eventEndDate = '2014-12-24 20:00:00';
$eventDeadline = '2014-12-23 23:59:59';
$eventAttendeeLimit = 10;
$event = new \PHPUnitEventDemo\Event($eventId, $eventName, $eventStartDate,
$eventEndDate, $eventDeadline, $eventAttendeeLimit);
$userId = 1;
$userName = 'User1';
$userEmail = 'user1@openfoundry.org';
$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];
}
/**
* @depends testReserve
*/
public function testUnreserve($objs)
{
// 測試使用者取消報名
$event = $objs[0];
$user = $objs[1];
// 使用者取消報名
$event->unreserve($user);
$unreserveExpectedCount = 0;
// 預期報名人數
$this->assertEquals($unreserveExpectedCount, $event->getAttendeeNumber());
// 報名清單中沒有已經取消報名的人
$this->assertNotContains($user, $event->attendees);
}
}
?>
And here is the steps I go run the test class:
I already have PHP
>php --version
PHP 5.6.16 (cli) (built: Dec 26 2015 18:37:18)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
>curl -sS https://getcomposer.org/installer | php
>php composer.phar install
Run the test class
>phpunit --bootstrap vendor/autoload.php tests/EventTest
Create this file there and We are good to go phpunit.xml
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="money">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
Single Command will run all the tests
> 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
发表评论
-
PHP Command Line Tool
2019-08-17 05:06 385PHP Command Line Tool Recently ... -
Code SonarQube 2019(2)PostgreSQL as Database
2019-07-24 05:32 416Code SonarQube 2019(2)PostgreSQ ... -
Code SonarQube 2019(1)Installation with default H2
2019-07-23 10:42 763Code SonarQube 2019(1)Installat ... -
Auth Solution(3)JWT in Java and PHP Sample
2019-05-03 07:33 305Auth Solution(3)JWT in Java and ... -
Flarum BBS System(2)Docker the BBS
2018-11-20 03:31 642Flarum BBS System(2)Docker the ... -
Flarum BBS System(1)Installation and Introduction
2018-11-18 14:29 493Flarum BBS System(1)Installatio ... -
Grav CMS System(5)Multiple Domains and Certs
2018-11-15 01:48 524Grav CMS System(5)Multiple Doma ... -
Grav CMS System(4)Multiple Domain Sites in HAProxy and HTTPS
2018-11-14 21:33 570Grav CMS System(4)Multiple Doma ... -
Grav CMS System(3)Docker Nginx and PHP
2018-11-14 00:21 453Grav CMS System(3)Docker Nginx ... -
Grav CMS System(1)Install PHP7 on MAC
2018-10-23 21:53 611Grav CMS System(1)Install PHP7 ... -
Laravel PHP Framework(1)Introduction and Installation
2018-08-22 02:47 977Laravel PHP Framework(1)Introdu ... -
JSON Log in PHP and Cloud Watch(1)JSON Format in PHP
2017-12-30 05:31 595JSON Log in PHP and Cloud Watch ... -
PHPRedis Library
2017-03-30 00:32 671PHPRedis Library We can instal ... -
PHP Call Wget Command
2017-03-24 23:35 663PHP Call Wget Command In my PH ... -
PHP XML Writer
2017-03-10 03:59 1445PHP XML Writer PHP XML writer ... -
PHP Redis Client and Cluster
2017-02-16 03:34 641PHP Redis Client and Cluster S ... -
PHP Redis Client and Replica
2017-02-16 01:11 587PHP Redis Client and Replica W ... -
PHP HTTP Library and Error Handling
2017-02-11 06:19 634PHP HTTP Library and Error Hand ... -
PHP ENV and HTTP Extension
2017-01-12 01:04 785PHP ENV and HTTP Extension We ... -
PHP Connect Redis Driver
2016-11-23 00:29 572PHP Connect Redis Driver I was ...
相关推荐
PEAR(PHP Extension and Application Repository)是PHP的一个扩展和应用程序仓库,它提供了一系列的PHP类库和工具,用于帮助开发者构建可重用的代码库。另一方面,phpUnit是PHP编程语言的一个单元测试框架,它是...
首先,为了安装 `phpunit`,我们需要先安装 PEAR (PHP Extension and Application Repository),它是 PHP 的一个包管理器。将 `go-pear.phar` 文件放到 PHP 的安装目录,然后通过命令行执行以下步骤: 1. 打开...
Mockery是PHP编程环境中的一款强大的模拟对象框架,它专门设计用于单元测试,特别是与PHPUnit集成时。Mockery的出现使得开发者能够更容易地创建模拟对象,这些对象可以替代真实依赖,以便在测试过程中隔离代码,专注...
In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '8.0/no DST' instead in PEAR\Validate.php on ...
所需PHP版本PHP 7.0以上安装composer require --dev amphp/phpunit-util用法<?phpnamespace Foo ;use Amp \ ByteStream ;use Amp \ PHPUnit \ AsyncTestCase ;use Amp \ Socket ;class BarTest extends ...
【标题】:在这个标题中,我们关注的是"phpunit-and-selenium-course",这表明这是一个关于使用PHPUnit进行单元测试和Selenium进行Web自动化测试的课程,特别针对的是2020年的实践。PHPUnit是PHP编程语言中最广泛...
《Real-World Solutions for Developing High-Quality PHP Frameworks and Applications》是一本专为PHP开发者设计的实战指南,它深入探讨了如何构建高质量的PHP框架和应用程序。这本书对于那些热爱PHP并希望提升...
在PHP单元测试领域,PHPUnit是一个不可或缺的工具,尤其对于确保代码质量、促进持续集成和敏捷开发至关重要。在本文中,我们将深入探讨PHPUnit的两个高级概念:Annotations(注解)及其在自动化测试框架生成中的应用...
《PHP Objects Patterns and Practice》是PHP编程领域的一本经典著作,尤其在第五版中,作者深入探讨了如何在实际开发中有效地使用面向对象编程(OOP)和设计模式。本书旨在帮助开发者提升PHP编程技能,使其能构建更...
本文实例讲述了PHP使用phpunit进行单元测试。分享给大家供大家参考,具体如下: 1. linux服务器上安装phpunit wget https://phar.phpunit.de/phpunit.phar ...PHPUnit 5.6.1 by Sebastian Bergmann and contrib
首先,安装PHPUnit前需要确保已经安装了PEAR(PHP Extension and Application Repository),它是PHP的一个包管理器。可以通过以下命令下载并安装PEAR: ```bash wget http://pear.php.net/go-pear.phar /usr/local...
PHP 5.3.10 包含的PEAR(PHP Extension and Application Repository)是一个PHP的包管理和分发框架。PEAR提供了一套标准,使得开发、发布和安装PHP库变得更加简单。它包括了用于构建、测试和发布PHP软件的工具,以及...
PEAR(PHP Extension and Application Repository)是PHP的一个框架和组件分发系统,它提供了一种标准的方式来安装和管理PHP的可重用组件。通过PEAR,我们可以方便地获取和安装像PHPUnit这样的第三方库。 安装...
Test your reactive PHP code using PHPUnit Analyze PHP source code and apply a custom set of rules by building a CLI tool About the Author Martin Sikora has been professionally programming since 2006 ...
PHPUnit是PHP中最广泛使用的单元测试框架,书中可能会详细介绍如何使用PHPUnit进行测试编写,确保代码质量。 4. 面向对象编程(OOP):PHP自5.0版本开始强化了OOP特性,如类、对象、继承、封装、多态等。书中会深入...