Unit Test - jubairzaman/E-repair GitHub Wiki

PHP unit ---Unit Testing Framework

OverView

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.

Writing Tests for PHPUnit

Example 2.1 shows how we can write tests using PHPUnit that exercise PHP’s array operations. The example introduces the basic conventions and steps for writing tests with PHPUnit:

The tests for a class Class go into a class ClassTest.

ClassTest inherits (most of the time) from PHPUnit\Framework\TestCase.

The tests are public methods that are named tests*.

Alternatively, you can use the @test annotation in a method’s docblock to mark it as a test method.

Inside the test methods, assertion methods such as assertSame() (see Assertions) are used to assert that an actual value matches an expected value.

Example

Test Dependencies

Unit Tests are primarily written as a good practice to help developers identify and fix bugs, to refactor code and to serve as documentation for a unit of software under test. To achieve these benefits, unit tests ideally should cover all the possible paths in a program. One unit test usually covers one specific path in one function or method. However, a test method is not necessarily an encapsulated, independent entity. Often there are implicit dependencies between test methods, hidden in the implementation scenario of a test.

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.

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

Example

Testing PHP Errors, Warnings, and Notices

By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception. Among other benefits, this makes it possible to expect that a PHP error, warning, or notice is triggered in a test When testing code that uses PHP built-in functions such as fopen() that may trigger errors it can sometimes be useful to use error suppression while testing. This allows you to check the return values by suppressing notices that would lead to an exception raised by PHPUnit’s error handler.

Testing Output

Sometimes you want to assert that the execution of a method, for instance, generates an expected output (via echo or print, for example). The PHPUnit\Framework\TestCase class uses PHP’s Output Buffering feature to provide the functionality that is necessary for this.

Example 2.14 shows how to use the expectOutputString() method to set the expected output. If this expected output is not generated, the test will be counted as a failure.

Error Output

Whenever a test fails PHPUnit tries its best to provide you with as much context as possible that can help to identify the problem.