Beginning PHPUnit
- 2. Jace Ju / jaceju /
PHP Smarty
Plurk: http://www.plurk.com/jaceju >////<
- 8. • PHPUnit
• PHPUnit
•
•
- 25. 1 + 2 + 3 + ... + N = ?
- 35. Math.php
<?php
class Math
{
Math::sum public static function sum($min, $max)
{
$sum = 0;
for ($i = $min; $i <= $max; $i++) {
$sum += $i;
}
return $sum;
}
}
- 36. Math.php
//
TEST_MODE if (defined('TEST_MODE')) {
}
- 37. Math.php
//
if (defined('TEST_MODE')) {
// Test 1
$result = Math::sum(1, 10);
1 10 if (55 !== $result) {
echo "Test 1 failed!n";
} else {
echo "Test 1 OK!n";
}
}
- 38. Math.php
//
if (defined('TEST_MODE')) {
// Test 1
$result = Math::sum(1, 10);
if (55 !== $result) {
echo "Test 1 failed!n";
} else {
echo "Test 1 OK!n";
}
// Test 2
$result = Math::sum(1, 100);
if (5050 !== $result) {
1 100 echo "Test 2 failed!n";
} else {
echo "Test 2 OK!n";
}
}
- 42. run_test.php
<?php
define('TEST_MODE', true);
require_once __DIR__ . '/library/Math.php';
- 56. JUnit
http://www.junit.org/
- 58. # pear channel-discover pear.symfony-project.com
# pear install symfony/YAML
# pear channel-discover pear.phpunit.de
# pear channel-discover components.ez.no
# pear install -o phpunit/phpunit
- 63. project
├── application
├── library
│ └── Math.php
└── tests
├── application
( tests )
└── library
- 66. project
├── application
├── library
│ └── Math.php
└── tests
├── application
└── library
library
MathTest.php └── MathTest.php
- 70. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
class MathTest
{
}
- 71. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
PHPUnit class MathTest extends PHPUnit_Framework_TestCase
TestCase {
}
- 72. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum()
{
}
}
- 73. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum()
test {
}
}
- 74. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
test public function testSum()
{
CamelCase
}
}
- 75. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum()
{
$this->assertEquals(55, Math::sum(1, 10));
PHPUnit $this->assertEquals(5050, Math::sum(1, 100));
assertions }
}
- 76. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum()
{
$this->assertEquals(55, Math::sum(1, 10));
$this->assertEquals(5050, Math::sum(1, 100));
}
}
- 77. MathTest.php
<?php
require_once __DIR__ . '/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum()
{
$this->assertEquals(55, Math::sum(1, 10));
$this->assertEquals(5050, Math::sum(1, 100));
}
}
- 87. MathTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum()
MathTest {
$this->assertEquals(55, Math::sum(1, 10 ));
}
}
- 88. MathTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum($expected, $min, $max)
{
$this->assertEquals(55, Math::sum(1, 10 ));
}
}
- 89. MathTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum($expected, $min, $max)
{
$this->assertEquals($expected, Math::sum($min, $max));
}
}
- 90. MathTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum($expected, $min, $max)
{
$this->assertEquals($expected, Math::sum($min, $max));
}
public function myDataProvider()
public {
return array(
array(55, 1, 10),
array(5050, 1, 100)
);
}
}
- 91. MathTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
public function testSum($expected, $min, $max)
{
$this->assertEquals($expected, Math::sum($min, $max));
}
public function myDataProvider()
{
return array(
array(55, 1, 10),
array(5050, 1, 100)
);
}
}
- 92. MathTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/Math.php';
class MathTest extends PHPUnit_Framework_TestCase
{
/**
PHPUnit * @dataProvider myDataProvider
@dataProvider */
annotation public function testSum($expected, $min, $max)
data provider {
$this->assertEquals($expected, Math::sum($min, $max));
}
public function myDataProvider()
{
return array(
array(55, 1, 10),
array(5050, 1, 100)
);
}
}
- 99. 1 + 2 + 3 + ... + N
=
- 101. Math.php
<?php
Math.php class Math
{
public static function sum($min, $max)
{
$sum = 0;
for ($i = $min; $i <= $max; $i++) {
$sum += $i;
}
return $sum;
}
}
- 102. Math.php
<?php
class Math
{
public static function sum($min, $max)
{
$sum = 0;
return $sum;
}
}
- 103. Math.php
<?php
class Math
{
public static function sum($min, $max)
{
$sum = $min + $max * $max / 2;
return $sum;
}
}
- 105. # phpunit tests/library/MathTest
PHPUnit 3.5.15 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 6.00Mb
There was 1 failure:
1) MathTest03::testSum
Failed asserting that <integer:51> matches expected <integer:
55>.
/path/to/tests/library/MathTest.php:7
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
- 106. # phpunit tests/library/MathTest
PHPUnit 3.5.15 by Sebastian Bergmann.
F
Time: 0 seconds, Memory: 6.00Mb
There was 1 failure:
1) MathTest03::testSum
Failed asserting that <integer:51> matches expected <integer:
55
51
55>.
/path/to/tests/library/MathTest.php:7
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
- 107. Math.php
<?php
class Math
{
/**
*
*/
public static function sum($min, $max)
{
$sum = $min + $max * $max / 2;
return $sum;
}
}
- 108. Math.php
<?php
class Math
{
/**
*
*/
public static function sum($min, $max)
{
$sum = ($min + $max) * $max / 2;
return $sum;
}
}
- 112. SQL Select
ORM Framework
- 116. • DbSelect
•
‣ from SELECT FROM
- 117. • DbSelect
•
‣ from SELECT FROM
‣ cols SELECT *
- 118. Example 1
$select = new DbSelect();
echo $select->from(‘table’);
SELECT * FROM table
- 119. Example 2
$select = new DbSelect();
echo $select->from(‘table’)->cols(array(
‘col_a’, ‘col_b’));
SELECT col_a, col_b FROM table
- 122. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
}
- 123. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
public function testFrom()
{
from
}
}
- 124. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
public function testFrom()
{
$select = new DbSelect();
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
}
- 126. DbSelect.php
<?php
class DbSelect
{
public function from($table)
from
{
}
}
- 127. DbSelect.php
<?php
class DbSelect
{
public function from($table)
{
}
public function __toString()
__toString
{
}
}
- 128. DbSelect.php
<?php
class DbSelect
{
public function from($table)
{
}
public function __toString()
{
return 'SELECT * FROM test';
}
}
- 131. DbSelect.php
<?php
class DbSelect
{
public function from($table)
{
}
public function __toString()
{
table
return 'SELECT * FROM ' . $this->_table;
}
}
- 132. DbSelect.php
<?php
class DbSelect
{
$_table protected $_table = 'table';
public function from($table)
{
}
public function __toString()
{
return 'SELECT * FROM ' . $this->_table;
}
}
- 133. DbSelect.php
<?php
class DbSelect
{
protected $_table = 'table';
public function from($table)
{
if (!preg_match('/[0-9a-z]+/i', $table)) {
throw new IllegalNameException('Illegal Table Name');
from
}
setter
$this->_table = $table;
return $this;
}
public function __toString()
{
return 'SELECT * FROM ' . $this->_table;
}
}
- 135. Design → Write Test → Coding
Direction → Find Target → Fire
- 137. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
DbSelectTest.php public function testFrom()
{
$select = new DbSelect();
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
}
- 138. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
public function testFrom()
{
$select = new DbSelect();
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
public function testCols()
{
cols $select = new DbSelect();
$select->from('test')->cols(array(
'col_a',
'col_b',
));
$this->assertEquals('SELECT col_a, col_b FROM test',
$select->__toString());
}
}
- 139. DbSelect.php
<?php
DbSelect.php class DbSelect
{
protected $_table = 'table';
public function from($table)
{
if (!preg_match('/[0-9a-z]+/i', $table)) {
throw new IllegalNameException('Illegal Table Name');
}
$this->_table = $table;
return $this;
}
public function __toString()
{
return 'SELECT * FROM ' . $this->_table;
}
}
- 140. DbSelect.php
<?php
class DbSelect
{
protected $_table = 'table';
public function from($table)
{
if (!preg_match('/[0-9a-z]+/i', $table)) {
throw new IllegalNameException('Illegal Table Name');
}
$this->_table = $table;
return $this;
}
cols public function cols($cols)
{
$this->_cols = (array) $cols;
return $this;
}
public function __toString()
{
return 'SELECT * FROM ' . $this->_table;
}
}
- 141. DbSelect.php
<?php
class DbSelect
{
protected $_table = 'table';
$_cols protected $_cols = '*';
public function from($table)
{
if (!preg_match('/[0-9a-z]+/i', $table)) {
throw new IllegalNameException('Illegal Table Name');
}
$this->_table = $table;
return $this;
}
public function cols($cols)
{
$this->_cols = (array) $cols;
return $this;
}
public function __toString()
{
return 'SELECT * FROM ' . $this->_table;
}
}
- 142. DbSelect.php
<?php
class DbSelect
{
protected $_table = 'table';
protected $_cols = '*';
public function from($table)
{
if (!preg_match('/[0-9a-z]+/i', $table)) {
throw new IllegalNameException('Illegal Table Name');
}
$this->_table = $table;
return $this;
}
public function cols($cols)
{
$this->_cols = (array) $cols;
return $this;
}
public function __toString()
{
$_cols $cols = implode(', ', (array) $this->_cols);
* return 'SELECT ' . $cols . ' FROM ' . $this->_table;
}
}
- 147. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
DbSelectTest.php class DbSelectTest extends PHPUnit_Framework_TestCase
{
public function testFrom()
�� {
$select = new DbSelect();
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
public function testCols()
{
$select = new DbSelect();
$select->from('test')->cols(array(
'col_a',
'col_b',
));
$this->assertEquals('SELECT col_a, col_b FROM test',
$select->__toString());
}
}
- 148. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
public function testFrom()
{
$select = new DbSelect();
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
new DbSelect() public function testCols()
{
$select = new DbSelect();
$select->from('test')->cols(array(
'col_a',
'col_b',
));
$this->assertEquals('SELECT col_a, col_b FROM test',
$select->__toString());
}
}
- 151. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
public function testFrom()
{
$select->from('test');
$this->assertEquals('SELECT * FROM test',
new DbSelect() $select->__toString());
}
public function testCols()
{
$select->from('test')->cols(array('col_a', 'col_b'));
$this->assertEquals('SELECT col_a, col_b FROM test',
$select->__toString());
}
}
- 152. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
fixture protected $_select;
public function testFrom()
{
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
public function testCols()
{
$select->from('test')->cols(array('col_a', 'col_b'));
$this->assertEquals('SELECT col_a, col_b FROM test',
$select->__toString());
}
}
- 153. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
protected $_select;
protected function setUp()
{
setUp $this->_select = new DbSelect();
fixture }
public function testFrom()
{
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
public function testCols()
{
$select->from('test')->cols(array('col_a', 'col_b'));
$this->assertEquals('SELECT col_a, col_b FROM test',
$select->__toString());
}
}
- 154. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
protected $_select;
protected function setUp()
{
$this->_select = new DbSelect();
}
public function testFrom()
{
$select->from('test');
$this->assertEquals('SELECT * FROM test',
$select->__toString());
}
public function testCols()
{
$select->from('test')->cols(array('col_a', 'col_b'));
$this->assertEquals('SELECT col_a, col_b FROM test',
$select->__toString());
}
protected function tearDown()
{
$this->_select = null;
tearDown
}
fixture }
- 155. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
protected $_select;
protected function setUp()
{
$this->_select = new DbSelect();
}
public function testFrom()
{
$this->_select->from('test');
$this->assertEquals('SELECT * FROM test',
$this->_select->__toString());
$select }
$this->_select
public function testCols()
{
$this->_select->from('test')->cols(array('col_a', 'col_b'));
$this->assertEquals('SELECT col_a, col_b FROM test',
$this->_select->__toString());
}
protected function tearDown()
{
$this->_select = null;
}
}
- 166. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
DbSelectTest.php class DbSelectTest extends PHPUnit_Framework_TestCase
{
// ... ...
}
- 167. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
// ... ...
public function testIllegalTableName()
{
try {
$this->_select->from('test WHERE id = 1');
}
catch (IllegalNameException $e) {
throw $e;
}
}
}
- 168. DbSelectTest.php
<?php
require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
class DbSelectTest extends PHPUnit_Framework_TestCase
{
// ... ...
/**
PHPUnit * @expectedException IllegalNameException
@expectedException */
annotation public function testIllegalTableName()
{
try {
$this->_select->from('test WHERE id = 1');
}
catch (IllegalNameException $e) {
throw $e;
}
}
}
- 169. # phpunit tests/library/DbSelectTest
PHPUnit 3.5.15 by Sebastian Bergmann.
.F.
Time: 0 seconds, Memory: 6.00Mb
There was 1 failure:
1) DbSelectTest::testIllegalTableName
IllegalNameException Expected exception IllegalNameException
FAILURES!
Tests: 3, Assertions: 3, Failures: 1.
- 171. DbSelectTest.php
<?php
DbSelect.php class DbSelect
{
// ... ...
public function from($table)
{
if (!preg_match('/[0-9a-z]+/i', $table)) {
throw new Exception('Illegal Table Name: ' . $table);
}
$this->_table = $table;
return $this;
}
// ... ...
}
- 172. DbSelectTest.php
<?php
class DbSelect
{
// ... ...
public function from($table)
{
if (!preg_match('/[0-9a-z]+/i', $table)) {
throw new Exception('Illegal Table Name: ' . $table);
}
$this->_table = $table;
return $this;
}
// ... ...
}
- 173. DbSelectTest.php
<?php
class DbSelect
{
// ... ...
public function from($table)
{
^ $ if (!preg_match('/^[0-9a-z]+$/i', $table)) {
throw new Exception('Illegal Table Name: ' . $table);
}
$this->_table = $table;
return $this;
}
// ... ...
}
- 181. project
├── application
├── library
└── tests
phpunit.xml
tests
└── phpunit.xml
- 183. phpunit.xml
<phpunit colors=”true”>
root tag
</phpunit>
- 184. phpunit.xml
<phpunit colors=”true”>
test suites <testsuite name="Application Test Suite">
<directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
</phpunit>
- 185. phpunit.xml
# phpunit -c tests/phpunit.xml
PHPUnit 3.5.15 by Sebastian Bergmann.
....
Time: 0 seconds, Memory: 6.00Mb
colors=”true” OK (4 tests, 4 assertions)
- 186. phpunit.xml
<phpunit colors=”true” bootstrap=”./bootstrap.php”>
<testsuite name="Application Test Suite">
PHP <directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
</phpunit>
- 187. bootstrap.php
<?php
define('PROJECT_PATH', realpath(dirname(__DIR__)));
set_include_path(implode(PATH_SEPARATOR, array(
realpath(PROJECT_PATH . '/library'),
realpath(PROJECT_PATH . '/application'),
get_include_path()
)));
function autoload($className)
{
$className = str_replace('_', '/', $className);
require_once "$className.php";
}
spl_autoload_register('autoload');