PHPUnit and 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 = '[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];
    }

    /**
     *  @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

你可能感兴趣的:(PHPUnit and PHP)