Skip to content

Commit 1980493

Browse files
committed
minor #12101 Add documentation for polyfill provided by the PHPUnitBridge (jderusse)
This PR was merged into the 4.4 branch. Discussion ---------- Add documentation for polyfill provided by the PHPUnitBridge Provides documentaiton for the new features of PHPUnitBridge Fixes #12092 Commits ------- d1e0bae Add documentation for polyfill provided by the PHPUnitBridge
2 parents 96b755e + d1e0bae commit 1980493

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

components/phpunit_bridge.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ 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+
* Provides polyfills for methods that are not available in older version of
32+
PHPUnit
33+
34+
* Provide namespaced class name for older version of PHPUnit
35+
36+
* Makes compatible testSuite with both PHPUnit 8 and previous version by
37+
removing typehint on `setUp` and `tearDown` methods.
38+
3139
Installation
3240
------------
3341

@@ -371,6 +379,76 @@ Running the following command will display the full stack trace:
371379
372380
$ SYMFONY_DEPRECATIONS_HELPER='/Doctrine\\Common\\ClassLoader is deprecated\./' ./vendor/bin/simple-phpunit
373381
382+
Testing with multiple version of PHPUnit
383+
----------------------------------------
384+
385+
Use Case
386+
~~~~~~~~
387+
388+
When testing a library that have to be compatible with serveral version of PHP
389+
at the same time (like Symfony does), because of dependencies, the test suite
390+
have to be tested by differentes version of PHPUnit. Unfortunatly, writing a
391+
code compatible with a too wide range of version is not possible. ie:
392+
- several function in PHPUnit 8 are deprecated but the replacements methods
393+
didn't exists int PHPUnit 4.
394+
- PHPUnit 8 added return typehint in method ``setUp(): void`` which is not
395+
compatible with PHP 5.5.
396+
- PHPUnit switch to namespaced class starting from PHPUnit 6. Tests have to
397+
handle both case with/without namespaces.
398+
399+
Polyfill for the methods
400+
~~~~~~~~~~~~~~~~~~~~~~~~
401+
402+
When using the command ``simple-phpunit``, PHPUnit Bridge injects pollyfills
403+
for most of methods from the class ``TestCase`` and ``Assert`` in order to
404+
write Test cases even compatible with PHPUnit 4. Some of those methods are
405+
``expectException``, ``expectExcpetionMessage``, ``expectExceptionCode``,
406+
``createPartialMock``, ``assertEqualsWithDelta``, ``assertContainsEquals``, ...
407+
408+
Remove void return typehint
409+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
410+
411+
When running the command ``simple-phpunit`` with the env variable
412+
``SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT=1`` the PHPUnit bridge will alterate
413+
the code of PHPUnit to remove the return typehint (introduced in PHPUnit 8)
414+
from methods ``setUp``, ``tearDown``, ``setUpBeforeClass`` and
415+
``tearDownAfterClass``. Thuse allows you to write a Test compatible with both
416+
PHP 5 and PHPUnit 8.
417+
418+
An alternative, is to use the trait :class:`Symfony\Bridge\PhpUnit\SetUpTearDownTrait`.
419+
This trait will provide the right signature for the methods ``setUp``, ``tearDown``,
420+
``setUpBeforeClass`` and ``tearDownAfterClass`` and delegates the call to the methods
421+
``doSetUp``, ``doTearDown``, ``doSetUpBeforeClass`` and ``doTearDownAfterClass``::
422+
423+
use PHPUnit\Framework\TestCase;
424+
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
425+
426+
class MyTest extends TestCase
427+
{
428+
use SetUpTearDownTrait;
429+
430+
private $state;
431+
432+
private function doSetup()
433+
{
434+
$this->state = 'demo';
435+
}
436+
437+
protected function doSetup(): void
438+
{
439+
// Visibility and return typehint of method is free.
440+
}
441+
}
442+
443+
444+
Use namespaced class
445+
~~~~~~~~~~~~~~~~~~~~
446+
447+
The PHPUnit bridge adds namespaced class aliases for most of PHPUnit class
448+
declared in the old fashion way (ie. ``PHPUnit_Framework_Assert``), allowing
449+
you to always use the namespaced class declaration even when the test is
450+
executed with PHPUnit 4.
451+
374452
Time-sensitive Tests
375453
--------------------
376454

0 commit comments

Comments
 (0)