braincrafted/testing-bundle

Tools for testing Symfony2 bundles

Installs: 11 758

Dependents: 4

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 1

Open Issues: 0

Type:symfony-bundle

v0.4.2 2016-02-15 14:43 UTC

This package is auto-updated.

Last update: 2024-03-20 09:10:38 UTC


README

Handcrafted in Vienna by Florian Eckerstorfer.

About

This bundle currently provides an abstract class to better isolate functional tests of Symfony2 applications. The WebTestCase class drops the schema, recreates it and loads all fixture files. Currently DoctrineFixturesBundle is a required dependency of this bundle.

At some point this bundle may contain other useful classes, services and helpers related to testing.

Installation

You need to add bundle to your composer.json file:

{
    "require-dev": {
        "braincrafted/testing-bundle": "@stable"
    }
}

Tip: You should replace @stable with a specific version.

Compatibility

TestingBundle is developed to always work with the latest version of Symfony. The following table helps you choose the right version of TestingBundle.

Symfony version TestingBundle version
v2.3.* v0.1.*
v2.4.*-v2.5.* v0.2.*-v0.3.*
v2.6+ v0.4+

The master branch has been updated to be compatible with Symfony 2.3. If you are using Symfony <2.3 you can use the 0.1 branch.

Add the bundle to your kernel (only activate the bundle in the dev and test environment, you don't need to have it activated in the production environment):

// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        // ...
        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Braincrafted\Bundle\TestingBundle\BraincraftedTestingBundle($this);
            // ...
        }
        // ...
    }
    // ...
}

Usage

The test cases that you want isolate must extend Braincrafted\Bundle\TestingBundle\Test\WebTestCase.

// AcmeDemoBundle/Tests/DemoTest.php
namespace AcmeDemoBundle\Tests;

use Braincrafted\Bundle\TestingBundle\Test\WebTestCase;

class DemoTest extends WebTestCase
{
    // ...
}

By default WebTestCase provides a setUp() and a tearDown() method that boot respectively shut down the kernel. However, if you have your own setUp() and/or tearDown() methods in your test case you need to manually do this.

// AcmeDemoBundle/Tests/DemoTest.php
namespace AcmeDemoBundle\Tests;

use Braincrafted\Bundle\TestingBundle\Test\WebTestCase;

class DemoTest extends WebTestCase
{
    public function setUp()
    {
        $this->setUpKernel();
        // ...
    }

    public function tearDown()
    {
        $this->tearDownKernel();
        // ...
    }
}

If you require a client in your test case, you can use the createClient() method:

$client = $this->createClient();

createClient() will call setUpKernel() when no kernel is available at this point.

You can also access the dependency injection container of the kernel:

$container = $this->getContainer();

Render Crawler HTML

The WebTestCase class also has an nice helper method that returns the HTML code of a crawler. You can use it in all test cases that subclass Braincrafted\Bundle\TestingBundle\Test\WebTestCase:

echo $this->renderCrawlerHtml($crawler);

Testing Translation Keys

BraincraftedTestingBundle installs an alternative translator that is only activated in the test environment. This translator returns the translation key instead of the translated text. That way you can use the translation keys in your functional tests instead of the translated text.

Change Log

Version 0.4.2 (15 February 2016)

  • NoTranslator implements empty setConfigCacheFactory() method for compatibility

Version 0.4.1 (20 April 2015)

  • NoTranslator returns message catalogue

Version 0.4 (6 February 2015)

  • Add compatibility with Symfony 2.6

Version 0.3.3 (6 February 2015)

  • Update to composer.json to reflect that the 0.3 branch is not compatible with Symfony 2.6.

Version 0.3.2 (26 October 2014)

  • #10 Try to find AppKernel.php in a standard path before falling back to KERNEL_DIR constant (by florianeckerstorfer)

Version 0.3.1 (25 August 2014)

  • Fixed type hint for WebTestCase::createClient() method
  • Pass $options to WebTestCase::createKernel() in WebTestCase::createClient()

Version 0.3 (18 November 2013)

  • Changed namespace to Braincrafted.

License

The MIT License (MIT)

Copyright (c) 2012-2015 Florian Eckerstorfer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.