Unit Testing Testing individual units of code to determine if they are fit for use. units The smallest testable part of an application. Functions Classes Methods
PHPUnit • xUnit-style library by Sebastian Bergmann • Well integrated and well documented • Installable via Composer, PEAR, and Phar Features: • Mocking • Assertions • Setup/teardown • Annotations • Data providers • Code coverage • Selenium integration • Etc.
Demo: AWS SDK for PHP 1. Configuring and running PHPUnit 2. Interpreting test output 3. IDE Conveniences 4. Examining code coverage 5. Real test examples 6. Update some tests https://github.com/aws/aws-sdk-php
Best Practices • Test one unit at a time • Make each test independent • Mock external state and services • Name your tests after their intention • Use good OOP and DRY principles
Best Practices • Test one unit at a time • Make each test independent • Mock external state and services • Name your tests after their intention • Use good OOP and DRY principles • Write your code to be testable
Dependency Injection Dependency Injection is where components are given their dependencies through their constructors, methods, or fields. class
Mailer
{
protected
$transport;
public
function
__construct(
TransportInterface
$transport
)
{
$this-‐>transport
=
$transport;
}
}
“ ”
Continuous Integration (CI) • Integrate code often and automate your build and test processes • Continuous Integration Systems Jenkins CI • Travis CI • Xinc phpUnderControl • Bamboo • More tools: phpmd, phpcpd, phpcs (see http://phpqatools.org)
TDD & BDD TDD – Test-driven development • Tests are written before the implementation • Short-development cycle; Agile methods • Test-writing is part of the design process BDD – Behavior-driven development • Extends TDD; focuses on acceptance testing • Business requirements define behaviors • Behat or phpspec are the main PHP tools
People to Follow • Sebastian Bergmann – @s_bergmann • Martin Fowler – @martinfowler • Miško Hevery – @mhevery • Chris Hartjes – @grmpyprogrammer • Konstantin Kudryashov – @everzet • Jeff Carouth – @jcarouth