PHPackages                             chaplean/unit-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. chaplean/unit-bundle

ActiveSymfony-bundle[Testing &amp; Quality](/categories/testing)

chaplean/unit-bundle
====================

Contains utilities for functional and unit tests

v8.0.0(7y ago)02047MITPHPPHP &gt;=7.1

Since Apr 21Pushed 6y agoCompare

[ Source](https://github.com/chaplean/unit-bundle)[ Packagist](https://packagist.org/packages/chaplean/unit-bundle)[ RSS](/packages/chaplean-unit-bundle/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (25)Versions (86)Used By (7)

Getting Started With ChapleanUnitBundle
=======================================

[](#getting-started-with-chapleanunitbundle)

Prerequisites
=============

[](#prerequisites)

This version of the bundle requires Symfony 2.8+.

Installation
============

[](#installation)

1. Composer
-----------

[](#1-composer)

```
composer require chaplean/unit-bundle

```

2. AppKernel.php
----------------

[](#2-appkernelphp)

Add

```
    $bundles[] = new Chaplean\Bundle\UnitBundle\ChapleanUnitBundle();
    $bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
    $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();

```

3. Import configuration
-----------------------

[](#3-import-configuration)

##### 3.1. Import unit file config in `config_test.yml`

[](#31-import-unit-file-config-in-config_testyml)

```
imports:
    - { resource: '@ChapleanUnitBundle/Resources/config/config.yml' }
```

##### 3.2. Configure mock (optional)

[](#32-configure-mock-optional)

In `config_test.yml`

```
chaplean_unit:
    mocked_service:
```

Example class:

```
class MockService implements MockedServiceOnSetUpInterface
{
    /**
     * @return void
     */
    public static function getMockedServices()
    {
        $knpPdf = \Mockery::mock('Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator');
        $knpPdf->shouldReceive('getOutputFromHtml')->andReturn('example');
        $knpPdf->shouldReceive('getOutput')->andReturn('example');

        $mocks['knp_snappy.pdf'] = $knpPdf;

        $client = \Mockery::mock(Client::class);
        $client->shouldReceive('request')->andReturn(new Response());

        $mocks['guzzle.client.sor_api'] = $client;

        return $mocks;
    }
}
```

##### 3.3. Add parameter (optional)

[](#33-add-parameter-optional)

Open `app/config/parameters*` files

Add and change the default value. The `false` value disable the loading of datafixtures.

```
parameters:
    ...
    data_fixtures_namespace: App\Bundle\RestBundle\
```

Role Provider
=============

[](#role-provider)

You can use phpunit's `@dataProvider` to automaticaly run a test with a list of different values. We can use this to test a route against different roles with a single unit test. To acheive this we will need to

1. list the roles and how to log as a user of that role
2. create a dataProvider giving for each role the expectations we want (usually a http code)
3. write the test using the @dataProvider

1. Listing the roles
--------------------

[](#1-listing-the-roles)

Add in your `parameters_test.yml` a `test_roles` dict as following:

```
parameters:
    # Dictionnary where the key is the name of the role (displayed when a
    # failure happens), and the value is the reference to an entity used
    # to do the login (the entity is given to LogicalTestCase::authenticate()).
    test_roles:
        NotLogged: ''
        User: 'user-1'
        Admin: 'user-2'
```

2. Create a dataProvider
------------------------

[](#2-create-a-dataprovider)

Add a provider in your test class:

```
class ExampleTest extends FunctionalTestCase
{
    /**
     * @return array
     */
    public function rolesMustBeLoggedProvider()
    {
        return $this->rolesProvider(
            // rolesProvider is an utility to map your expectations with the
            // configured roles. It takes an array with the roles as keys and
            // your expectations as values.
            array(
                'NotLogged' => Response::HTTP_FORBIDDEN,
                'User'      => Response::HTTP_OK,
                'Admin'     => Response::HTTP_OK,
            )
        );
    }

    /**
     * @return array
     */
    public function rolesWithDifferentExpectations()
    {
        return $this->rolesProvider(
            // You can also give different expectations, see 3. Create a unittest
            // testWithDifferentExpectations to see how it translates in the test
            // function signature.
            array(
                'NotLogged' => Response::HTTP_FORBIDDEN,
                'User'      => array(Response::HTTP_OK),
                'Admin'     => array(Response::HTTP_OK, 'other expectation),
            )
        );
    }

    /**
     * @return array
     */
    public function rolesWithExtraRoles()
    {
        return $this->rolesProvider(
            array(
                'NotLogged' => Response::HTTP_FORBIDDEN,
                'User'      => Response::HTTP_OK,
                'Admin'     => Response::HTTP_OK,
            ),
            // You can also provide extra roles, thoses are added to the list
            // of default roles. Like with regular roles you provide the role
            // name as key and then the expectations as value, but the first
            // expectation must be the user to use to log in as.
            array(
                'SpecialCase' => array('user-3', Response::HTTP_OK)
            )
        );
    }
}
```

3. Create a unittest
--------------------

[](#3-create-a-unittest)

Write unittests using the previous dataProvider

```
class ExampleTest extends FunctionalTestCase
{
    // Data provider ommited, see previous section

    /**
     * @dataProvider rolesMustBeLoggedProvider
     *
     * @param string  $user
     * @param integer $expectedCode
     *
     * @return void
     */
    public function testRouteMustBeLogged($user, $expectedCode)
    {
        $client = $this->createClientWith($user);
        $client->request('/protected/url');

        $response = $client->getResponse();
        $this->assertEquals($expectedCode, $response->getStatusCode());
    }

    /**
     * @dataProvider rolesWithDifferentExpectations
     *
     * @param string  $client
     * @param integer $expectedCode
     * @param string  $otherExpectation
     *
     * @return void
     */
    public function testWithDifferentExpectations($client, $expectedCode, $otherExpectation = null)
    {
        // $otherExpectation is not defined for every value in the provider so we must default to null
    }
}
```

Custom printer
==============

[](#custom-printer)

If you want use a custom printer add `printerClass` attribute with `Chaplean\Bundle\UnitBundle\TextUI\ResultPrinter` value in `phpunit.xml`

```

         printerClass="Chaplean\Bundle\UnitBundle\TextUI\ResultPrinter"

```

[See an overview](https://asciinema.org/a/u4d6NsZAifpGRlMYhPjq5La6N)

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 62.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~17 days

Recently: every ~94 days

Total

83

Last Release

2592d ago

Major Versions

v4.2.2 → v5.0.02016-12-20

v4.2.3 → v5.2.52017-06-12

v5.2.7 → v6.0.02017-08-02

v3.0.7 → v7.0.02017-12-06

7.3.0 → v8.0.02019-04-09

PHP version history (4 changes)v1.0.0PHP &gt;=5.3.2

v1.6.1PHP &gt;=5.5.9

v4.0.0PHP &gt;=7.0.8

v8.0.0PHP &gt;=7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/6bfb5e4a3dde826e4cd3f92d24fdfbdce1415c77f3edd38fc3420b4988977e13?d=identicon)[HugoDumazeau](/maintainers/HugoDumazeau)

![](https://www.gravatar.com/avatar/8084ee0162a85df3b3f49230b8c0dde81108195caa541bb00642cbd050e88b25?d=identicon)[hudumazeau](/maintainers/hudumazeau)

---

Top Contributors

[![mdevlamynck](https://avatars.githubusercontent.com/u/4378377?v=4)](https://github.com/mdevlamynck "mdevlamynck (92 commits)")[![valentin-chaplean](https://avatars.githubusercontent.com/u/15136254?v=4)](https://github.com/valentin-chaplean "valentin-chaplean (34 commits)")[![tomchaplean](https://avatars.githubusercontent.com/u/5412926?v=4)](https://github.com/tomchaplean "tomchaplean (21 commits)")

### Embed Badge

![Health badge](/badges/chaplean-unit-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/chaplean-unit-bundle/health.svg)](https://phpackages.com/packages/chaplean-unit-bundle)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[behat/behat

Scenario-oriented BDD framework for PHP

4.0k96.8M2.0k](/packages/behat-behat)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[ec-cube/ec-cube

EC-CUBE EC open platform.

78527.0k1](/packages/ec-cube-ec-cube)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k39.1M32.1k](/packages/orchestra-testbench)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
