Skip to content

Commit e43e0ff

Browse files
committed
Merge branch '4.4'
* 4.4: Add documentation for polyfill provided by the PHPUnitBridge
2 parents 0fa6551 + afe6ef8 commit e43e0ff

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

components/phpunit_bridge.rst

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ It comes with the following features:
2828
constraints to apply; 2. running tests in parallel when a test suite is split
2929
in several phpunit.xml files; 3. recording and replaying skipped tests;
3030

31+
* It allows to create tests that are compatible with multiple PHPUnit versions
32+
(because it provides polyfills for missing methods, namespaced aliases for
33+
non-namespaced classes, etc.).
34+
3135
Installation
3236
------------
3337

@@ -367,6 +371,83 @@ Running the following command will display the full stack trace:
367371
368372
$ SYMFONY_DEPRECATIONS_HELPER='/Doctrine\\Common\\ClassLoader is deprecated\./' ./vendor/bin/simple-phpunit
369373
374+
Testing with Multiple PHPUnit Versions
375+
--------------------------------------
376+
377+
When testing a library that has to be compatible with several versions of PHP,
378+
the test suite cannot use the latest versions of PHPUnit because:
379+
380+
* PHPUnit 8 deprecated several methods in favor of other methods which are not
381+
available in older versions (e.g. PHPUnit 4);
382+
* PHPUnit 8 added the ``void`` return type to the ``setUp()`` method, which is
383+
not compatible with PHP 5.5;
384+
* PHPUnit switched to namespaced classes starting from PHPUnit 6, so tests must
385+
work with and without namespaces.
386+
387+
Polyfills for the Unavailable Methods
388+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389+
390+
.. versionadded:: 4.4
391+
392+
This feature was introduced in Symfony 4.4.
393+
394+
When using the ``simple-phpunit`` script, PHPUnit Bridge injects polyfills for
395+
most methods of the ``TestCase`` and ``Assert`` classes (e.g. ``expectException()``,
396+
``expectExcpetionMessage()``, ``assertContainsEquals``, etc.). This allows writing
397+
test cases using the latest best practices while still remaining compatible with
398+
older PHPUnit versions.
399+
400+
Removing the Void Return Type
401+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
402+
403+
.. versionadded:: 4.4
404+
405+
This feature was introduced in Symfony 4.4.
406+
407+
When running the ``simple-phpunit`` script with the ``SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT``
408+
environment variable set to ``1``, the PHPUnit bridge will alter the code of
409+
PHPUnit to remove the return type (introduced in PHPUnit 8) from ``setUp()``,
410+
``tearDown()``, ``setUpBeforeClass()`` and ``tearDownAfterClass()`` methods.
411+
This allows you to write a test compatible with both PHP 5 and PHPUnit 8.
412+
413+
Alternatively, you can use the trait :class:`Symfony\Bridge\PhpUnit\SetUpTearDownTrait`,
414+
which provides the right signature for the ``setUp()``, ``tearDown()``,
415+
``setUpBeforeClass()`` and ``tearDownAfterClass()`` methods and delegates the
416+
call to the ``doSetUp()``, ``doTearDown()``, ``doSetUpBeforeClass()`` and
417+
``doTearDownAfterClass()`` methods::
418+
419+
use PHPUnit\Framework\TestCase;
420+
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
421+
422+
class MyTest extends TestCase
423+
{
424+
use SetUpTearDownTrait;
425+
426+
private $state;
427+
428+
private function doSetup()
429+
{
430+
$this->state = 'demo';
431+
}
432+
433+
protected function doSetup(): void
434+
{
435+
// visibility and return type-hint of method is free
436+
}
437+
}
438+
439+
Using Namespaced PHPUnit Classes
440+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
441+
442+
.. versionadded:: 4.4
443+
444+
This feature was introduced in Symfony 4.4.
445+
446+
The PHPUnit bridge adds namespaced class aliases for most of the PHPUnit classes
447+
declared without namespaces (e.g. ``PHPUnit_Framework_Assert``), allowing you to
448+
always use the namespaced class declaration even when the test is executed with
449+
PHPUnit 4.
450+
370451
Time-sensitive Tests
371452
--------------------
372453

0 commit comments

Comments
 (0)