You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Daniel Siepmann 55f72aac20
TASK: Add some info to readme
3 years ago
Classes TASK: Last changes for first talk 4 years ago
Resources/Private/CodeExamples/Tests/Unit TASK: Update to TYPO3 CMS 9.5 and PHPUnit 8.0 3 years ago
.gitignore TASK: Last changes for first talk 4 years ago
composer.json TASK: Update to TYPO3 CMS 9.5 and PHPUnit 8.0 3 years ago
readme.rst TASK: Add some info to readme 3 years ago


Testing Talk

This is about automated testing, for PHP.

Everyone is testing already, by hand. This involves:

* Unit testing

* Functional testing

* Acceptance testing

* Browser testing

* …

All of the above is already done by you, so … NO PANIC!

This is "The Hitchhiker's Guide to […]" testing.

Table of contents:

.. contents:: :local:


Execute the following::

git clone
cd testing-talk

Clean everything::

rm -rf composer.lock vendor web Tests phpunit.xml.dist infection.json.dist Results

Installation development dependencies using composer::

composer install

This also includes PHPUnit, see: composer.json

Check installation::

./vendor/bin/phpunit --version

This would not work yet, as we do not have any tests::

./vendor/bin/phpunit Tests/Unit/




.. note::

The concrete PHPUnit version also depends on your current PHP Version.

Create first test

Hands on! Let's write a first basic test.

E.g. a small model with a bit logic::

mkdir -p Tests/Unit/Domain/Model
cp Resources/Private/CodeExamples/Tests/Unit/Domain/Model/AddressTest.php \

Execute the first test::

./vendor/bin/phpunit Tests/Unit/

Execute with colors::

./vendor/bin/phpunit --color Tests/Unit/

Execute with info about executed tests::

./vendor/bin/phpunit --color --debug Tests/Unit/

What's in the test?

#. We have one PHP class `AddressTest`.

#. The class and file ends with `Test`.

#. Two public methods.

#. The methods are annotated with `@test`.

#. Methods create an instance of the class to test.

#. Methods call an `assert*()` method.

Create test for controller

Introduction to mocking

What is mocking, or a mock?

In object-oriented programming, mock objects are simulated objects that mimic the
behavior of real objects in controlled ways.

A programmer typically creates a mock object to test the behavior of some other
object, in much the same way that a car designer uses a crash test dummy to
simulate the dynamic behavior of a human in vehicle impacts.




Example mock:

.. code-block:: php


use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;

class Test extends TestCase
public function someTest()
$viewMock = $this->getMockBuilder(ViewInterface::class)->getMock();

->with('frontendUser', $frontendUserMock);


Add the test

We want to test the controller now::

mkdir -p Tests/Unit/Controller
cp Resources/Private/CodeExamples/Tests/Unit/Controller/FrontendUserControllerTest.php \

Execute all tests::

./vendor/bin/phpunit --color --debug Tests/Unit/

Alternative output

Used as "agile" output::

./vendor/bin/phpunit Tests/Unit/ --color --testdox-html Results/testdox.html
xdg-open Results/testdox.html

Used in CI to parse results::

./vendor/bin/phpunit --log-junit Results/junit.xml Tests/Unit

html Coverage
Used to check which methods still need testing::

./vendor/bin/phpunit --coverage-html Results/Coverage/ --whitelist Classes Tests/Unit
xdg-open Results/Coverage/index.html

Benefits of tests

#. Detect new bugs.

#. Make sure the same bug does not occur a 2nd time.

#. Reproduce bug.

#. Speed up development.

#. Show how to use the written code.

#. Allow co-worker, in pull request, to see what you expect.
And how you understood the feature-request.

#. Write code without working system, by using tests instead.

#. Allow more secure refactoring.

#. Forces to write clean code.

Automate test execution

Existing tests are great. If they are executed.

Tests which exist are code, if they are not executed, they are dead code.

Tests costs money, so get the money back by executing tests.

The easiest way is to have an CI (=Continuous Integration).


* Jenkins

* Travis

* Gitlab CI

* Bitbucket Pipelines

* Bamboo

* Circle CI

* …


Use `phpunit.xml.dist`::

cp Resources/Private/Configs/phpunit.xml.dist phpunit.xml.dist



Code Coverage

Most of the time counts only number of executed lines.

This helps to find untested code, nothing more!
100% covered lines does not mean you are testing all circumstances,
just every line at least once.


.. code-block:: php


if ($var1 || $var2) {
echo 'test';


Will have 100% if all lines are executed, that is even if we do not provide `$var2`.
We have to test the possible cases, not only all lines.





Is not:


Is: change risk anti pattern score
Combines complexity and test coverage.

Different kinds of tests



Lists: Acid tests, Unit testing, A/B testing, Acceptance testing, Ad hoc testing,
Agile testing, All-pairs testing, API testing, Black-box testing & White-box
testing, Boundary testing, Cloud testing, Compatibility testing, Component-based
usability testing, …

Unit Tests

What we did above. White box test of small pieces of code.

Functional Tests

Involves multiple code parts, database, file system and further components, e.g. web

Acceptance Tests

Tests from user view, e.g. via browser.

Mutation testing

Tests how easy it is to break test::

cp Resources/Private/Configs/infection.json.dist infection.json.dist






Start writing tests, small unit tests.

Automate execution of tests.


Further reading





* Source code of open source projects, like TYPO3: