Weitere ähnliche Inhalte Ähnlich wie PHPUnit 入門介紹 (20) PHPUnit 入門介紹31. Person.php
<?php
class Person
{
protected $_name = '';
public function __construct($name)
{
$this->_name = (string) $name;
}
public function sayHelloTo($name)
{
return 'Hello, ' . $name . '. I am ' . $this->_name . '.';
}
}
55. Add.php
<?php
class Add
{
protected $_num1 = 0, $_num2 = 0;
public function setNum1($num)
{
$this->_num1 = (int) $num;
}
public function setNum2($num)
{
$this->_num2 = (int) $num;
}
public function getResult()
{
return $this->_num1 + $this->_num2;
}
}
62. AddTest.php
<?php
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'Add.php';
class AddTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider provider
*/
public function testAdd($num1, $num2, $expection)
{
$add = new Add();
$add->setNum1($num1);
$add->setNum2($num2);
$this->assertEquals($expection, $add->getResult());
}
public function provider()
{
return array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
); // 可以用迴圈或讀檔的方式去產生大量的測試資料
}
}
64. # phpunit AddTest.php
PHPUnit 3.3.17 by Sebastian Bergmann.
...F
Time: 0 seconds
There was 1 failure:
1) testAdd(AddTest) with data set #3 (1, 1, 3)
Failed asserting that <integer:2> matches expected value
<integer:3>.
xxxxxxxxAddTest.php:15
FAILURES!
Tests: 4, Assertions: 4, Failures: 1.
72. ExceptionTest.php
<?php
require_once 'PHPUnit/Framework.php';
class ExceptionTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException InvalidArgumentException
*/
public function testException()
{
}
}
# phpunit ExceptionTest.php
PHPUnit 3.3.17 by Sebastian Bergmann.
F
Time: 0 seconds
There was 1 failure:
1) testException(ExceptionTest)
Expected exception InvalidArgumentException
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
88. FixtureTest.php
<?php
require_once 'PHPUnit/Framework.php';
class ArrayTest extends PHPUnit_Framework_TestCase
{
protected $fixture;
protected function setUp()
{
$this->fixture = array();
}
public function testNewArrayIsEmpty()
{
$this->assertEquals(0, sizeof($this->fixture));
}
public function testArrayContainsAnElement()
{
$this->fixture[] = 'Element';
$this->assertEquals(1, sizeof($this->fixture));
}
}
89. FixtureTest.php
<?php
require_once 'PHPUnit/Framework.php';
class ArrayTest extends PHPUnit_Framework_TestCase
{
protected $fixture;
protected function setUp()
{
$this->fixture = array();
}
public function testNewArrayIsEmpty()
{
$this->assertEquals(0, sizeof($this->fixture));
}
public function testArrayContainsAnElement()
{
$this->fixture[] = 'Element';
$this->assertEquals(1, sizeof($this->fixture));
}
protected function tearDown()
{
unset($this->fixture);
}
}
127. MySuite.php
<?php
require_once 'MyTest.php';
class MySuite extends PHPUnit_Framework_TestSuite
{
public static function suite()
{
return new self('MyTest');
}
protected function setUp()
{
$this->sharedFixture = 'something';
}
protected function tearDown()
{
$this->sharedFixture = null;
}
}
137. StubTest.php
<?php
require_once 'PHPUnit/Framework.php';
class SomeClass // 給 Mock Object 用的類別宣告,不一定要有實際內容
{
public function doSomething()
{
}
}
class StubTest extends PHPUnit_Framework_TestCase
{
public function testStub()
{
$stub = $this->getMock('SomeClass'); // 建立 Mock Object
$stub->expects($this->any()) // 不論呼叫幾次
->method('doSomething') // 指定方法
}
}
138. StubTest.php
<?php
require_once 'PHPUnit/Framework.php';
class SomeClass // 給 Mock Object 用的類別宣告,不一定要有實際內容
{
public function doSomething()
{
}
}
class StubTest extends PHPUnit_Framework_TestCase
{
public function testStub()
{
$stub = $this->getMock('SomeClass'); // 建立 Mock Object
$stub->expects($this->any()) // 不論呼叫幾次
->method('doSomething') // 指定方法
->will($this->returnValue('foo')); // 指定回傳值
}
}
139. StubTest.php
<?php
require_once 'PHPUnit/Framework.php';
class SomeClass // 給 Mock Object 用的類別宣告,不一定要有實際內容
{
public function doSomething()
{
}
}
class StubTest extends PHPUnit_Framework_TestCase
{
public function testStub()
{
$stub = $this->getMock('SomeClass'); // 建立 Mock Object
$stub->expects($this->any()) // 不論呼叫幾次
->method('doSomething') // 指定方法
->will($this->returnValue('foo')); // 指定回傳值
$this->assertEquals('foo', $stub->doSomething());
}
}
141. Subject.php
<?php
class Subject
{
protected $observers = array();
public function attach(Observer $observer)
{
$this->observers[] = $observer;
}
public function doSomething()
{
$this->notify('something');
}
protected function notify($arg)
{
foreach ($this->observers as $observer) {
$observer->update($arg);
}
}
}
143. SubjectTest.php
<?php
require_once 'PHPUnit/Framework.php';
require_once 'Subject.php';
class Observer // 雖然實際的 Observer 未完成,但我們還是要給它一個類別宣告
{
public function update($arg) {}
}
class SubjectTest extends PHPUnit_Framework_TestCase
{
public function testUpdateIsCalledOnce()
{
// 建立一個 Observer 的 Mock Object
$observer = $this->getMock('Observer');
// 預期 Observer::update 方法應該只跑一次
// 而傳入 update 方法的參數值為 something
$observer->expects($this->once())
->method('update')
->with($this->equalTo('something'));
}
}
144. SubjectTest.php
<?php
require_once 'PHPUnit/Framework.php';
require_once 'Subject.php';
class Observer // 雖然實際的 Observer 未完成,但我們還是要給它一個類別宣告
{
public function update($arg) {}
}
class SubjectTest extends PHPUnit_Framework_TestCase
{
public function testUpdateIsCalledOnce()
{
// 建立一個 Observer 的 Mock Object
$observer = $this->getMock('Observer');
// 預期 Observer::update 方法應該只跑一次
// 而傳入 update 方法的參數值為 something
$observer->expects($this->once())
->method('update')
->with($this->equalTo('something'));
$subject = new Subject();
$subject->attach($observer);
// 我們預測這裡會呼叫 Observer::update() 一次
$subject->doSomething();
}
}
154. # phpunit --coverage-html ./report SubjectTest.php
PHPUnit 3.3.17 by Sebastian Bergmann.
.
Time: 0 seconds
OK (1 test, 1 assertion)
Generating code coverage report, this may take a moment.
155. # phpunit --coverage-html ./report SubjectTest.php
PHPUnit 3.3.17 by Sebastian Bergmann.
.
Time: 0 seconds
OK (1 test, 1 assertion)
Generating code coverage report, this may take a moment.