carlescliment/handy-tests-bundle

为Symfony 2测试提供的便捷工具包

1.0.0 2014-04-07 17:01 UTC

This package is not auto-updated.

Last update: 2024-09-14 13:07:39 UTC


README

Build Status

这是我在日常测试中使用的工具集合,以简化测试过程。请随意将这些工具集用于您的项目中!

安装

1. 更新您的供应商

将以下行添加到您的 composer.json

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

执行 php composer.phar update carlescliment/handy-tests-bundle

2. 在 app/AppKernel.php 中加载该包

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

工具包

表截断器

许多人都使用数据集来为每个测试填充适当的数据。但是,有时模式非常复杂,重新加载大型的数据集文件会变得非常慢。

在这些情况下,您可能更倾向于采用更细粒度的方法,即创建所需的实例,测试完成后从数据库中删除它们。

表截断器允许您截断表(仅限MySQL)。

use BladeTester\HandyTestsBundle\Model\TableTruncator;

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

Factory Girl

Factory Girl 允许您轻松实例化和持久化实体。从单个位置实例化和持久化对象有助于减少重复,并允许在不会产生噪音的情况下构建具有默认值的复杂实例。

这是工厂的一个示例

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;
    }
}

一旦编写了您的工厂,请将其注册为带标签的服务,以便在测试中使其可用。

文件:services.yml

imports:
    - { resource: factories.yml }

parameters:
    # ....

services:
    # Your other stuff

文件:factories.yml

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

然后,在您的测试中,您可以干净地创建 Person 实例

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

或者在您的测试中扩展 HandyTestCase(稍后介绍)会更容易

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

便捷测试用例

这是一个提供上述所有功能以及更多功能的 TestCase。只需将其继承到您的功能测试用例中即可。

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);
    }

}

致谢

贡献和反馈

请随意提供此包的反馈。贡献将非常受欢迎。