`
sillycat
  • 浏览: 2539228 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

PHPUnit and PHP

    博客分类:
  • PHP
 
阅读更多
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
分享到:
评论

相关推荐

    pear及phpUnit的安装教程

    PEAR(PHP Extension and Application Repository)是PHP的一个扩展和应用程序仓库,它提供了一系列的PHP类库和工具,用于帮助开发者构建可重用的代码库。另一方面,phpUnit是PHP编程语言的一个单元测试框架,它是...

    phpunit配置及使用

    首先,为了安装 `phpunit`,我们需要先安装 PEAR (PHP Extension and Application Repository),它是 PHP 的一个包管理器。将 `go-pear.phar` 文件放到 PHP 的安装目录,然后通过命令行执行以下步骤: 1. 打开...

    Mockery一个简单而灵活的PHP框架模拟对象用于在PHPUnit中进行单元测试

    Mockery是PHP编程环境中的一款强大的模拟对象框架,它专门设计用于单元测试,特别是与PHPUnit集成时。Mockery的出现使得开发者能够更容易地创建模拟对象,这些对象可以替代真实依赖,以便在测试过程中隔离代码,专注...

    安装pear和phpunit

    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 ...

    phpunit-util:帮助程序包,可简化对PHPUnit的测试

    所需PHP版本PHP 7.0以上安装composer require --dev amphp/phpunit-util用法&lt;?phpnamespace Foo ;use Amp \ ByteStream ;use Amp \ PHPUnit \ AsyncTestCase ;use Amp \ Socket ;class BarTest extends ...

    phpunit-and-selenium-course:udemy课程PHPUnit在2020年的实践— PHP开发人员的单元和Selenium测试

    【标题】:在这个标题中,我们关注的是"phpunit-and-selenium-course",这表明这是一个关于使用PHPUnit进行单元测试和Selenium进行Web自动化测试的课程,特别针对的是2020年的实践。PHPUnit是PHP编程语言中最广泛...

    Real-World Solutions for Developing High-Quality PHP Frameworks and Applications

    《Real-World Solutions for Developing High-Quality PHP Frameworks and Applications》是一本专为PHP开发者设计的实战指南,它深入探讨了如何构建高质量的PHP框架和应用程序。这本书对于那些热爱PHP并希望提升...

    PHP单元测试利器 PHPUNIT深入用法(三)第1/2页

    在PHP单元测试领域,PHPUnit是一个不可或缺的工具,尤其对于确保代码质量、促进持续集成和敏捷开发至关重要。在本文中,我们将深入探讨PHPUnit的两个高级概念:Annotations(注解)及其在自动化测试框架生成中的应用...

    PHP Objects Patterns and Practice(5th)

    《PHP Objects Patterns and Practice》是PHP编程领域的一本经典著作,尤其在第五版中,作者深入探讨了如何在实际开发中有效地使用面向对象编程(OOP)和设计模式。本书旨在帮助开发者提升PHP编程技能,使其能构建更...

    PHP使用phpunit进行单元测试示例

    本文实例讲述了PHP使用phpunit进行单元测试。分享给大家供大家参考,具体如下: 1. linux服务器上安装phpunit wget https://phar.phpunit.de/phpunit.phar ...PHPUnit 5.6.1 by Sebastian Bergmann and contrib

    CentOS环境下安装PHPUnit的方法分析

    首先,安装PHPUnit前需要确保已经安装了PEAR(PHP Extension and Application Repository),它是PHP的一个包管理器。可以通过以下命令下载并安装PEAR: ```bash wget http://pear.php.net/go-pear.phar /usr/local...

    php5.3.10包

    PHP 5.3.10 包含的PEAR(PHP Extension and Application Repository)是一个PHP的包管理和分发框架。PEAR提供了一套标准,使得开发、发布和安装PHP库变得更加简单。它包括了用于构建、测试和发布PHP软件的工具,以及...

    pear包安装phpunit的方法

    PEAR(PHP Extension and Application Repository)是PHP的一个框架和组件分发系统,它提供了一种标准的方式来安装和管理PHP的可重用组件。通过PEAR,我们可以方便地获取和安装像PHPUnit这样的第三方库。 安装...

    PHP Reactive Programming

    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 ...

    Pro.PHP.Patterns.Frameworks.Testing.and.More.2008

    PHPUnit是PHP中最广泛使用的单元测试框架,书中可能会详细介绍如何使用PHPUnit进行测试编写,确保代码质量。 4. 面向对象编程(OOP):PHP自5.0版本开始强化了OOP特性,如类、对象、继承、封装、多态等。书中会深入...

Global site tag (gtag.js) - Google Analytics