Skip to content

Unit Testing Tool

TashfiquZaman edited this page Sep 26, 2020 · 2 revisions

PHP Unit Testing with "PHPUnit"

Description

We are writing our code on PHP so we need to know how to write unit tests. Ideally have PHP and Composer installed, but a brief overview of installing these will be covered. PHPUnit supports the declaration of explicit dependencies between test methods. Such dependencies do not define the order in which the test methods are to be executed but they allow the returning of an instance of the test fixture by a producer and passing it to the dependent consumers.

    1. A producer is a test method that yields its unit under test as return value.
    1. A consumer is a test method that depends on one or more producers and their return values.

Example

?`php use PHPUnit\Framework\TestCase;

class StackTest extends TestCase { public function testPushAndPop() { $stack = []; $this->assertSame(0, count($stack));

    array_push($stack, 'foo');
    $this->assertSame('foo', $stack[count($stack)-1]);
    $this->assertSame(1, count($stack));

    $this->assertSame('foo', array_pop($stack));
    $this->assertSame(0, count($stack));
}

}`