Boost Your Symfony Skills with PHPUnit Bridge

The PHPUnit bridge provides utilities for PHPUnit, especially user deprecation notices management.

The PHPUnit Bridge provides utilities to report legacy tests and usage of deprecated code and helpers for mocking native functions related to time, DNS and class existence.

It comes with the following features:

  • Sets by default a consistent locale (C) for your tests (if you create locale-sensitive tests, use PHPUnit's setLocale() method);
  • Auto-register class_exists to load Doctrine annotations (when used);
  • It displays the whole list of deprecated features used in the application;
  • Displays the stack trace of a deprecation on-demand;
  • Provides a ClockMock, DnsMock and ClassExistsMock classes for tests sensitive to time, network or class existence;

Provides a modified version of PHPUnit that allows:

  1. separating the dependencies of your app from those of phpunit to prevent any unwanted constraints to apply;
  2. running tests in parallel when a test suite is split in several phpunit.xml files;
  3. recording and replaying skipped tests;
  • It allows to create tests that are compatible with multiple PHPUnit versions (because it provides polyfills for missing methods, namespaced aliases for non-namespaced classes, etc.).

Installation

$ composer require --dev symfony/phpunit-bridge

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

The PHPUnit bridge is designed to work with all maintained versions of Symfony components, even across different major versions of them. You should always use its very latest stable major version to get the most accurate deprecation report.

If you plan to write assertions about deprecations and use the regular PHPUnit script (not the modified PHPUnit script provided by Symfony), you have to register a new test listener called SymfonyTestsListener:

<!-- https://phpunit.de/manual/6.0/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.0/phpunit.xsd"
>

    <!-- ... -->

    <listeners>
        <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
    </listeners>
</phpunit>

Usage

This article explains how to use the PhpUnitBridge features as an independent component in any PHP application. Read the Testing article to learn about how to use it in Symfony applications.

Once the component is installed, a simple-phpunit script is created in the vendor/ directory to run tests. This script wraps the original PHPUnit binary to provide more features:

$ cd my-project/
$ ./vendor/bin/simple-phpunit

After running your PHPUnit tests, you will get a report similar to this one:

$ ./vendor/bin/simple-phpunit
  PHPUnit by Sebastian Bergmann.

  Configuration read from <your-project>/phpunit.xml.dist
  .................

  Time: 1.77 seconds, Memory: 5.75Mb

  OK (17 tests, 21 assertions)

  Remaining deprecation notices (2)

  getEntityManager is deprecated since Symfony 2.1. Use getManager instead: 2x
    1x in DefaultControllerTest::testPublicUrls from App\Tests\Controller
    1x in BlogControllerTest::testIndex from App\Tests\Controller

The summary includes:

Unsilenced

Reports deprecation notices that were triggered without the recommended @-silencing operator.

Legacy

Deprecation notices denote tests that explicitly test some legacy features.

Remaining/Other

Deprecation notices are all other (non-legacy) notices, grouped by message, test class and method.

If you don't want to use the simple-phpunit script, register the following PHPUnit event listener in your PHPUnit configuration file to get the same report about deprecations (which is created by a PHP error handler called DeprecationErrorHandler):

<!-- phpunit.xml.dist -->
<!-- ... -->
<listeners>
    <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>

Running Tests in Parallel

The modified PHPUnit script allows running tests in parallel by providing a directory containing multiple test suites with their own phpunit.xml.dist.

├── tests/
│   ├── Functional/
│   │   ├── ...
│   │   └── phpunit.xml.dist
│   ├── Unit/
│   │   ├── ...
│   │   └── phpunit.xml.dist
$ ./vendor/bin/simple-phpunit tests/

The modified PHPUnit script will recursively go through the provided directory, up to a depth of 3 subdirectories or the value specified by the environment variable SYMFONY_PHPUNIT_MAX_DEPTH, looking for phpunit.xml.dist files and then running each suite it finds in parallel, collecting their output and displaying each test suite's results in their own section.

Resources


Download details:

Author: symfony
Source: https://github.com/symfony/phpunit-bridge

License: MIT license

#php #symfony 

Boost Your Symfony Skills with PHPUnit Bridge
1.20 GEEK