Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

Total Downloads Latest Stable Version License

UnitTest

Unit test library on top of PHP Unit.

Installation

Note: PHP 7.1 or higher is required.

  1. Install composer.
  2. Run composer require fabstract/unit-test.

Usage

MethodTestBase

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 example AddMethodTest.
  • 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.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages