Skip to main content
Drupal Unit Testing / TDD course with PHPUnit
Course

Drupal Unit Testing / TDD course with PHPUnit

Metadrop's Drupal PHPUnit course covers unit testing, test-driven development (TDD), and automated testing for PHP developers building on Drupal.
Duration

We will give a review of the different tests that can be integrated into PHP using the PHPUnit Framework. Discovering what needs to be covered at the lower levels of development and how to execute them.

This course can be complemented with practical cases in workshop mode in subsequent days on projects indicated by the applicant.

Requirements

  • Knowledge of PHP (medium)
  • Knowledge of Object Orientation (medium)
  • Terminal handling (basic)
  • Have PHP / Composer installed
  • Have an IDE installed (PHPStorm, Netbeans, Visual Studio Code)

What you will learn

By the end of the course you will be able to write and run PHPUnit tests for PHP code and apply test-driven development on your own project.
Set up a local PHPUnit environment with Composer and your IDE
Write tests with assertions following naming conventions and standards
Configure PHPUnit through its XML configuration file
Manage dependencies between tests, fixtures and exception testing
Isolate external dependencies using mocks and stubs
Apply TDD: write the test before the code and refactor with tests
Test protected and private methods and attributes, and static methods

Course modules

Introduction

We review the different tests that can be integrated into PHP using the PHPUnit framework, and what needs to be covered at the lower levels of development.

Local work environment setup

Preparing the machine to run tests: installing PHPUnit with Composer and configuring the IDE to launch them and read the results.

  1. Composer
  2. IDE configuration

Fundamentals

The building blocks of a test: assertions, how to combine several of them, and the conventions that keep a test suite readable.

  1. Introduction to assertions
  2. Using multiple assertions
  3. Correcting bugs through testing
  4. Naming conventions and standards

PHPUnit XML configuration

Where PHPUnit reads its settings from and how to adapt the XML configuration file to the project.

Test dependencies, fixtures and exceptions

Controlling the state a test needs: dependencies between tests, set-up and tear-down, fixtures, and checking that exceptions are thrown.

  1. Dependencies between tests
  2. SetUp and TearDown
  3. Test fixtures
  4. Exceptions test

Mocks and stubs

Isolating the code under test from its external dependencies using mock objects, dependency injection and the Mockery framework.

  1. Creating mock objects to avoid external dependencies
  2. Dependency injection
  3. Mockery - Framework

TDD - Test Driven Development

Turning testing into a development method: writing the test before the code and using it to drive and refactor the implementation.

  1. Writing tests before code
  2. Targeted development test
  3. Code refactor with tests

Advanced test

Reaching the parts of a class that are not directly accessible: protected and private methods and attributes, and static methods.

  1. Protected / private methods
  2. Protected / private attributes
  3. Static methods

Conclusions

Recap of what has been covered and how to carry on applying it to the project's own code.

Answers are generated automatically by AI and may not be accurate.