Unit test library on top of PHP Unit.
Note: PHP 7.1 or higher is required.
- Install composer.
- Run
composer require fabstract/unit-test
.
This class is used for separating unit tests by which methods they were written for. Suppose you have the following class:
namespace MyPackage;
class Calculator
{
public function add($a, $b) {
return $a + $b;
}
public function multiply($a, $b) {
return $a * $b;
}
}
Now in order to write tests for these two methods:
- Create a directory, preferably the same name with the class name.
- Inside that directory, create a class for each method you want to test.
- Name the class as
methodName
+MethodTest
. For exampleAddMethodTest
. - Extend the class from
MethodTestBase
.
Note: Your method should be camelcase.
Here is what it looks like:
namespace MyPackage\Calculator;
class AddMethodTest extends \Fabstract\Component\UnitTest\MethodTestBase
{
public function testOneAndOneEqualsTwo() {
$arguments = [1, 1];
$result = $this->call(new Calculator(), $arguments);
$this->assertEquals(2, $result);
}
public function testOneAndZeroEqualsOne() {
$arguments = [1, 0];
$result = $this->call(new Calculator(), $arguments);
$this->assertEquals(1, $result);
}
}
and
namespace MyPackage\Calculator;
class MultiplyMethodTest extends \Fabstract\Component\UnitTest\MethodTestBase
{
public function testOneAndOneEqualsOne() {
$arguments = [1, 1];
$result = $this->call(new Calculator(), $arguments);
$this->assertEquals(1, $result);
}
public function testOneAndZeroEqualsZero() {
$arguments = [1, 0];
$result = $this->call(new Calculator(), $arguments);
$this->assertEquals(0, $result);
}
}
You can add as many unit tests as you want, without worrying about a method's tests getting mixed up with other method's tests.