定制 tps/util-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tps/util-bundle

Composer 安装命令:

composer require tps/util-bundle

包简介

generate phpunit-templates for services, including mocked dependencies

README 文档

README

Build Status Coverage Status

Installation

Require the bundle via composer:

composer require "tps/util-bundle":"dev-master"

Or add to composer.json:

"require": {
    [...]
    "tps/util-bundle": "dev-master"
},

Activate in AppKernel.php:

$bundles = [
    [...]
    new Tps\UtilBundle\TpsUtilBundle()
]

Generate unit-tests from service classes

From time to time it happens that a dev is confronted with a brownfield service that has no unit-test, and wants to fix that. If the service has a lot of dependencies in the constructor, preparing the mocks for that can be really annoying (yes, the clean solution is to split the service to smaller parts with nice and easy-to-mock constructor). Anyways, if you are in a situation where you want to add a proper test for a monster-service, this tool can be ver handy.

Lets say you have a simple service like this:

<?php
namespace Tps\UtilBundle\Tests\Fixtures;

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\Form\Form;

class ExampleClass {
    /**
     * @var EntityManager
     */
    private $testForm;
    /**
     * @var TwigEngine
     */
    private $templating;
    /**
     * @var string
     */
    private $primitiveParameter;

    public function __construct(Form $testForm, TwigEngine $templating, $primitiveParameter)
    {
        $this->testForm = $testForm;
        $this->templating = $templating;
        $this->primitiveParameter = $primitiveParameter;
    }
    [...]

To generate a base template for a service test, run the command

app/console tps:util:generate-service-test 'Tps\UtilBundle\Tests\Fixtures\ExampleClass'

Generated output will be:

<?php

namespace Tps\UtilBundle\Tests\Tests\Fixtures;

use PHPUnit_Framework_MockObject_MockObject;
use Tps\UtilBundle\Tests\Fixtures\ExampleClass;
use Symfony\Component\Form\Form;
use Symfony\Bundle\TwigBundle\TwigEngine;

class ExampleClassTest extends \PHPUnit_Framework_TestCase
{
   /**
    * @var PHPUnit_Framework_MockObject_MockObject|\Symfony\Component\Form\Form
    */
    private $formMock;
   /**
    * @var PHPUnit_Framework_MockObject_MockObject|\Symfony\Bundle\TwigBundle\TwigEngine
    */
    private $twigEngineMock;
   /**
    * @var int|string|boolean|array
    */
    private $primitiveParameter = null;

   /**
    * @var PHPUnit_Framework_MockObject_MockObject|\Tps\UtilBundle\Tests\Fixtures\ExampleClass
    */
    private $exampleClass;

    public function setUp()
    {
        $this->formMock = $this->getMockBuilder(Form::class)
            ->disableOriginalConstructor()->getMock();
        $this->twigEngineMock = $this->getMockBuilder(TwigEngine::class)
            ->disableOriginalConstructor()->getMock();

        $this->exampleClass = new ExampleClass(
            $this->formMock,
            $this->twigEngineMock,
            $this->primitiveParameter
        );
    }
}

统计信息

  • 总下载量: 36.73k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-05-16