pear config-set auto_discover 1 pear install pear.phpunit.de/PHPUnit
class TestModel { public $num; public $str; public function setAttributes($i, $s) {} /* @return: true, false, */ public function saveData() {return false;} /* @return: true, false, */ public function loadData() {return false;} }
require_once 'PHPUnit/Autoload.php'; class TestModelTest extends PHPUnit_Framework_TestCase { function testTrue() { $this->assertTrue(true); } }
phpunit unit
PHPUnit 3.6.10 by Sebastian Bergmann. . Time: 0 seconds, Memory: 2.75Mb OK (1 test, 1 assertion)
<?php require_once 'PHPUnit/Autoload.php'; require_once dirname(__FILE__).'/../TestModel.php'; class TestModelTest extends PHPUnit_Framework_TestCase { // , function testStringCannotBeEmpty() { $model=new TestModel; $model->setAttributes(15,''); $this->assertFalse($model->saveData()); // , ! $model->setAttributes(15,'aaaa'); $this->assertTrue($model->saveData()); // } // 10<i<20 function testIntMustBeGreaterThanTenAdnSmallerThanTwenty() { $model=new TestModel; /* */ $model->setAttributes(2,'test1'); $this->assertFalse($model->saveData()); $model->setAttributes(10,'test2'); $this->assertFalse($model->saveData()); $model->setAttributes(20,'test3'); $this->assertFalse($model->saveData()); $model->setAttributes(25,'test4'); $this->assertFalse($model->saveData()); /* */ $model->setAttributes(15,'test5'); $this->assertTrue($model->saveData()); } // / function testSaveLoad() { $i=13; $str='test'; $model=new TestModel; $model->setAttributes($i,$str); $this->assertTrue($model->saveData()); // $fetchModel=new TestModel; $this->assertTrue($fetchModel->loadData()); // // $this->assertEquals($fetchModel->num,$i); $this->assertEquals($fetchModel->str,$str); } }
PHPUnit 3.6.10 by Sebastian Bergmann. FFF Time: 0 seconds, Memory: 2.75Mb There were 3 failures: 1) TestModelTest::testStringCannotBeEmpty Failed asserting that null is false. ... 2) TestModelTest::testIntMustBeGreaterThanTenAdnSmallerThanTwenty Failed asserting that null is false. ... 3) TestModelTest::testSaveLoad Failed asserting that null is true. ... FAILURES! Tests: 3, Assertions: 3, Failures: 3.
class TestModel { public $num; public $str; public $fname="file.txt"; public function setAttributes($i, $s) { $this->num=(int)$i; $this->str=$s; } public function saveData() { $h=fopen($this->fname,'w+'); $res=fputs($h, $this->str."\r\n".$this->num); fclose($h); return (bool)$res; } public function loadData() { $arr=file($this->fname); if ($arr==false) return false; list($this->str,$this->num)=$arr; return (bool)$arr; } }
There were 3 failures: 1) TestModelTest::testStringCannotBeEmpty Failed asserting that true is false. ... 2) TestModelTest::testIntMustBeGreaterThanTenAdnSmallerThanTwenty Failed asserting that true is false. ... 3) TestModelTest::testSaveLoad Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'test - -' +'test' FAILURES! Tests: 3, Assertions: 6, Failures: 3.
public function saveData() { if (!strlen($this->str)) return false; ...... }
public function saveData() { if (!strlen($this->str)) return false; if ($this->num<10 || $this->num>20) return false; ...... }
public function loadData() { $arr=file($this->fname, FILE_IGNORE_NEW_LINES); .... }
There was 1 failure: 1) TestModelTest::testIntMustBeGreaterThanTenAdnSmallerThanTwenty Failed asserting that true is false. TestModelTest.php:30 C:\Program Files\php\phpunit:46 FAILURES! Tests: 3, Assertions: 8, Failures: 1.
public function saveData() { if (!strlen($this->str)) return false; if ($this->num<=10 || $this->num>=20) return false; ...... }
Time: 0 seconds, Memory: 2.75Mb OK (3 tests, 11 assertions)
Source: https://habr.com/ru/post/138350/
All Articles