carlescliment/handy-tests-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

carlescliment/handy-tests-bundle

Composer 安装命令:

composer require --dev carlescliment/handy-tests-bundle

包简介

A handy toolkit for testing in Symfony 2

关键字:

README 文档

README

Build Status

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

Installation

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

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

The Toolkit

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

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

Contribute and feedback

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

carlescliment/handy-tests-bundle 适用场景与选型建议

carlescliment/handy-tests-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44.6k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2013 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「testing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 carlescliment/handy-tests-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 carlescliment/handy-tests-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 carlescliment/handy-tests-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 44.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: Apache2
  • 更新时间: 2013-02-28