You are on page 1of 36

PHP Unit

Coverage

➔ What is Unit Testing


➔ Components of PHP Unit
➔ How to use PHP Unit in CakePHP
➔ Demo
Software Development Life Cycle

➔ Requirement Analysis
➔ Design
➔ Implementation
➔ Testing
➔ Deployment
➔ Maintenance
What is Unit Testing?

➔ Is a way of testing a unit


◆ Smallest piece of code that can be logically isolated in a
system
◆ Has few inputs and single output
◆ Individual program, function, procedure, methods
➔ Is a process in which small units of code are tested against
expected results
➔ Validate that each unit of the software performs as design.
Benefits

➔ Easier to refactor the code to the point of testability and


understandability
➔ Increases confidence in changing/maintaining code
➔ Code are more reusable
➔ Development is faster
➔ Cost is lesser
➔ Easy debugging
➔ Codes are more reliable
➔ Higher probability of bug free code
➔ Help reduce regressions of the code over time
How to create a Unit Test Cases

1. Use a UnitTest Framework to develop automated test cases.


2. Approaches:
a. Test-Driven Development (TDD)
- Development process is driven by testing
- Syntax : uses proper PHP code in the testing files
a. Behaviour-Driven Development (BDD)
- Development process is driven by testing
- Syntax : uses human-readable sentences to describe
the behaviour of each feature and can be understood
by non-technical stakeholders
TDD
BDD
PHPUnit

➔ Is a programmer-oriented testing framework created by


Sebastian Bergmann
➔ Takes a small portions of code which are called unit and test
them one by one
➔ It is used for unit testing of PHP projects
➔ Is the best-known testing framework for writing UnitTest in PHP
➔ Can be used via the command line
Benefits of PHPUnit

➔ It provides a handy TestCase class that can extend


according to the needs
➔ Allows the developers to use pre-written assertion
methods
➔ Is a framework independent library for unit testing.
➔ De facto standard in the PHP world
➔ Supported by default by all PHP IDEs
➔ A lot of documentation
Benefits of PHPUnit

➔ Reduction in development time


➔ Facilitates change
➔ Simplifies Integration
➔ Enhance Design
➔ Measure of Completion
Writing Test Using PHPUnit

➔ Test for both the successes and failures


➔ Test methods need descriptive names
◆ Ex: testProductCanBeAddToBasket
◆ Prefixed with test
◆ Public
◆ Should contain at least 1 assertion
◆ Should be independent to one another
➔ Test classes will be named after the method being tested
◆ Class name should end with word Test and extend
CakeTestCase
Example
<?php
class ExampleTest extends TestCase {
public function setUp() {
// set up the class here
}

public function testSomething() {


// write test assertion here
}

public function tearDown() {


// clean up
}
?>
PHP Unit Key Terminology

➔ Function
➔ Parameter
➔ Return Value
➔ Expected Results
➔ Assert
➔ Fixture
➔ Mock / Stub
➔ Set up
➔ Tear Down
Function

In programming, a named section of a program that


performs a specific task.
Function
Parameter

A parameter is a special kind of variable in computer


programming language that is used to pass information
between functions
Parameters
Return Value

In most programming languages, the return statement is


either return or return value, where value is a variable or
other information coming back from the function.
Return Value
Expected Result

Expected result is the correct result you are expecting


based on the requirement/s.
Fixture

➔ array that is stored in the $stack variable


➔ Test data
➔ Fixture provides table schema and records to fill the table.
➔ Each table is a separate fixture.
➔ Schema and Records can be inside the fixture or imported
from development data
➔ Fixture tables are created in CakeTestCase::start()
➔ Before each test method, fixture tables are populated with
records in fixture.
➔ After each test method, fixture tables are truncated.
➔ Fixture tables are dropped in CakeTestCase::end()
Importance of Fixture
➔ Fixtures allow you to avoid having to mock out your database
connections.
➔ Fixtures allow for known data, making your tests invulnerable to
application data anomalies.
➔ Easily create data to cover any cases you want.
Assertion

➔ It’s a way we test our code


➔ To assert that something is valid
➔ Used to test for expected behaviour of units
➔ Best practice to have one assertion for each test
➔ Example:
◆ assertEmpty($result)
◆ assertEquals($expected , $result)
◆ assertFalse($result)
◆ assertTrue($result)
◆ AssertFileExists - used to check that a specified file exists what is expected

https://phpunit.de/manual/6.5/en/appendixes.assertions.html
Stubs

➔ Replacing an object with a test double that


(optionally) returns configured return values is
referred to as stubbing
➔ Replace a real component on which the SUT
depends
Set Up and Tear Down

In PHPUnit testing we have the setUp() method that


is called before every test. And we have the
tearDown() method that is called after every test.

We use the setUp() method to initialise variables,


open file connection etc. that will prepare the
environment for the test. And after completing the
test we use the tearDown() method to unset
variables, close file connection etc.
Set Up and Tear Down
How to run test
php app/Console/cake.php test app <name of test class>

You might also like