PHPackages                             carlescliment/handy-tests-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. carlescliment/handy-tests-bundle

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

carlescliment/handy-tests-bundle
================================

A handy toolkit for testing in Symfony 2

1.0.0(12y ago)642.8k↓13.5%5Apache2PHPPHP &gt;=5.3.2

Since Jun 27Pushed 11y ago1 watchersCompare

[ Source](https://github.com/carlescliment/handy-tests-bundle)[ Packagist](https://packagist.org/packages/carlescliment/handy-tests-bundle)[ Docs](https://github.com/carlescliment/handy-tests-bundle)[ RSS](/packages/carlescliment-handy-tests-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (0)

HandyTestsBundle
================

[](#handytestsbundle)

[![Build Status](https://camo.githubusercontent.com/51d2e435cb04c2d42ed6b2d76b5ed4e8401fdc19080ac7145ff968269d52a50c/68747470733a2f2f7472617669732d63692e6f72672f6361726c6573636c696d656e742f68616e64792d74657374732d62756e646c652e706e67)](https://travis-ci.org/carlescliment/handy-tests-bundle)

This is a collection of the tools I use daily to make testing easier. Feel free to use this toolset in your projects!

Installation
------------

[](#installation)

### 1. Update your vendors

[](#1-update-your-vendors)

Add this line to your `composer.json`

```
"require": {
    "carlescliment/handy-tests-bundle": "dev-master"
}

```

Execute `php composer.phar update carlescliment/handy-tests-bundle`

### 2. Load the bundle in `app/AppKernel.php`

[](#2-load-the-bundle-in-appappkernelphp)

```
if ('test' === $this->getEnvironment()) {
    $bundles[] = new BladeTester\HandyTestsBundle\BladeTesterHandyTestsBundle();
}

```

The Toolkit
-----------

[](#the-toolkit)

### The table truncator

[](#the-table-truncator)

Many of you use fixtures to fill the database with appropriate data each test. But, sometimes, the schema is so complex that it gets very slow to always reload a big fixture file.

In these cases, you will probably prefer a more fine-grained approach, in which you create the instances you want, removing them from the database after the test is finished.

The table truncator allows you to -obvious- truncate tables (MySQL only).

```
use BladeTester\HandyTestsBundle\Model\TableTruncator;

$tables = array('table1', 'table2', 'table3');
TableTruncator::truncate($tables, $entity_manager);

```

### The Factory Girl

[](#the-factory-girl)

The Factory Girl allows you to easily instantiate and persist entities. Instantiating and persisting objects from a single place helps removing duplication and allows building complex instances with default values without generating noise.

This is an example of a factory:

```
namespace Your\OwnBundle\Factory;

use Doctrine\Common\Persistence\ObjectManager;
use BladeTester\HandyTestsBundle\Model\FactoryInterface;

use Your\OwnBundle\Entity\Person;

class PersonFactory implements FactoryInterface {

    private $om;

    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }

    public function getName()
    {
        return 'Person';
    }

    public function build(array $attributes)
    {
        $name = isset($attributes['name']) ? $attributes['name'] : 'Factorized name';
        $surname = isset($attributes['surname']) ? attributes['surname'] : 'Factorized surname';
        $age = isset($attributes['age']) ? $attributes['age'] : null;
        $person = new Person;
        $person->setName($name);
        $person->setSurname($surname);
        $person->setAge($age);
        return $person;
    }

    public function create(array $attributes)
    {
        $person = $this->build($attributes);
        $this->om->persist($person);
        $this->om->flush();
        return $person;
    }
}

```

Once you have written your factory, register it as a tagged service in order to make it available from your tests.

file: services.yml

```
imports:
    - { resource: factories.yml }

parameters:
    # ....

services:
    # Your other stuff

```

file: factories.yml

```
services:
    your_vendor.handy_test.person_factory:
        class: Your\OwnBundle\Factory\PersonFactory
        arguments: ["@doctrine.orm.entity_manager"]
        tags:
            - { name: handy_tests.factory }

```

Then, in your test, you can create Person instances cleanly:

```
$factory_girl = $client->getKernel()->getContainer()->get('handy_tests.factory_girl')
$person = $factory_girl->create('Person');

```

Or even easier if you extend the HandyTestCase in your tests (see later):

```
$person = $this->create('Person');

```

### The Handy Test Case

[](#the-handy-test-case)

This is a TestCase that provides all the features described above and a little more. Just inherit it in your functional test cases.

```
namespace Your\Bundle\Tests\Controller;

use BladeTester\HandyTestsBundle\Model\HandyTestCase;

class FooControllerTest extends HandyTestCase {

    public function setUp() {
        parent::setUp(); // for annonymous users
        parent::setUp(array("PHP_AUTH_USER" => "test_user", "PHP_AUTH_PW"   => "test_password",)); // for basic http authentication
    }

    /**
     * @test
     */
    public function handyFeatures()
    {
        // Use factories to build or create entities.
        $persisted_entity = $this->create('ComplexEntity', array('name' => 'sampleName'));
        $not_persisted_entity = $this->build('ComplexEntity', array('name' => 'sampleName'));

        // Use the router instead of concrete paths
        $crawler = $this->visit('accout_show', array('id' => $account->getId()));

        // Perform XML HTTP requests
        $route_data = array('id' => 666); // to be used in the router
        $request_data = array('foo' => 'bar'); // things that go in $_POST or $_GET
        $crawler = $this->asyncRequest('my_service_route', $route_data, $request_data, 'POST');

        // debug the screen being displayed
        $this->printContents();

        // A handy entity manager
        $this->em->getRepository('...');

        // A handy client
        $this->client->click($link);

        // truncate tables
        $this->truncateTables(array('table_foo', 'table_bar'));

        // test your events properly without triggering them from interfaces
        $this->dispatchEvent('account.delete', $account_event);
    }

}

```

Credits
-------

[](#credits)

- Author: [Carles Climent](https://github.com/carlescliment)
- Contributor: [Fran Moreno](https://github.com/franmomu)
- Contributor: [Pedro Nofuentes](https://github.com/pedronofuentes)

Contribute and feedback
-----------------------

[](#contribute-and-feedback)

Please, feel free to provide feedback of this bundle. Contributions will be much appreciated.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 95% 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 ~142 days

Total

3

Last Release

4424d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c2a85fbd3df7cb8270d08d62ca42ba012b02cad5ff2a146dcc544ca658f92689?d=identicon)[carlescliment](/maintainers/carlescliment)

---

Top Contributors

[![carlescliment](https://avatars.githubusercontent.com/u/1255401?v=4)](https://github.com/carlescliment "carlescliment (76 commits)")[![franmomu](https://avatars.githubusercontent.com/u/720690?v=4)](https://github.com/franmomu "franmomu (2 commits)")[![pedronofuentes](https://avatars.githubusercontent.com/u/954396?v=4)](https://github.com/pedronofuentes "pedronofuentes (2 commits)")

---

Tags

testing

### Embed Badge

![Health badge](/badges/carlescliment-handy-tests-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/carlescliment-handy-tests-bundle/health.svg)](https://phpackages.com/packages/carlescliment-handy-tests-bundle)
```

###  Alternatives

[phpunit/phpunit

The PHP Unit Testing framework.

20.0k910.7M134.8k](/packages/phpunit-phpunit)[phpunit/php-code-coverage

Library that provides collection, processing, and rendering functionality for PHP code coverage information.

8.9k892.4M1.5k](/packages/phpunit-php-code-coverage)[mockery/mockery

Mockery is a simple yet flexible PHP mock object framework

10.7k497.0M23.6k](/packages/mockery-mockery)[behat/behat

Scenario-oriented BDD framework for PHP

4.0k96.8M2.0k](/packages/behat-behat)[symfony/phpunit-bridge

Provides utilities for PHPUnit, especially user deprecation notices management

2.5k201.2M4.2k](/packages/symfony-phpunit-bridge)[brianium/paratest

Parallel testing for PHP

2.5k118.8M754](/packages/brianium-paratest)

PHPackages © 2026

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