atoum/atoum-bundle

围绕 atoum 单元测试框架的包

安装次数: 352,728

依赖关系: 11

建议者: 0

安全: 0

星标: 45

关注者: 19

分支: 25

开放问题: 14

类型:symfony-bundle

2.0.0 2017-07-19 20:42 UTC

README

Build Status

此包提供了将 atoum(由 mageekguy 开发的简单、现代且直观的 PHP 单元测试框架)与 Symfony2 集成的(非常)简单方法。

安装

1 - 使用 composer

{
    "require": {
        "atoum/atoum-bundle": "^1.4"
    }
}

在大多数情况下,您不需要在生产环境中使用 AtoumBundle。

{
    "require-dev": {
        "atoum/atoum-bundle": "^1.4"
    }
}

2 - 命令

AtoumBundle 提供了 Symfony 命令。您可以在特定的包上运行 atoum 测试。

2-a 注册到内核

您必须在 AppKernel 上定义 AtoumBundle。

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
    //.....
    $bundles[] = new atoum\AtoumBundle\AtoumAtoumBundle();
}

2-b 配置

在配置中定义您的包(如果您只想在测试环境中使用它,请仅在 config_test.yml 中使用它)

atoum:
    bundles:
        # note that the full name, including vendor, is required
        AcmeFooBundle: ~ # FooBundle is defined with directories Tests/Units, Tests/Controller
        MeBarBundle:
            directories: [Tests/Units, Tests/Functional, ...]

2-c 命令行使用

然后您可以使用

$ php app/console atoum FooBundle --env=test # launch tests of FooBundle
$ php app/console atoum FooBundle BarBundle --env=test # launch tests of FooBundle and BarBundle
$ php app/console atoum acme_foo --env=test # launch tests of bundle where alias is acme_foo
$ php app/console atoum --env=test # launch tests from configuration.

简单用法

使您的测试类继承包中的 atoum\AtoumBundle\Test\Units\Test 类。

如果您不使用 composer,请务必使用您喜欢的加载方法(require、autoload 等)加载此类。

<?php

// src/Acme/MyBundle/Tests/Units/Entity/HelloWorld.php

namespace Acme\MyBundle\Tests\Units\Entity;
// if you don't use a bootstrap file, (or composer) you need to require the application autoload
//require __DIR__ . '/../../../../../../app/autoload.php';

// use path of the atoum.phar as bellow if you don't want to use atoum via composer
//require_once __DIR__ . '/../../../../../vendor/mageekguy.atoum.phar';

use atoum\AtoumBundle\Test\Units;

class helloWorld extends Units\Test
{
}

Web 测试用例

您可以轻松创建一个内核环境

<?php

require __DIR__ . '/../../../../../../../app/autoload.php';

use atoum\AtoumBundle\Test\Units;

class helloWorld extends Units\WebTestCase
{
    public function testMyTralala()
    {
        $client = $this->createClient();
    }
}

命令测试用例

您也可以轻松测试一个命令

<?php

namespace My\Bundle\FoobarBundle\Tests\Units\Command;

use atoum\AtoumBundle\Test\Units as AtoumBundle;
use mageekguy\atoum;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

// Assuming that this command will display "Success" if succes, and returns a boolean
use My\Bundle\FoobarBundle\Command\FoobarCommand as Base;

class FoobarCommand extends AtoumBundle\CommandTestCase
{
    public function testExecute()
    {
        $this
            ->given(
                $command = new Base()
            )
            ->if($commandTester = $this->createCommandTester($command))
            ->then
                ->boolean($commandTester->execute())
                    ->isTrue()
                ->string($commandTester->getDisplay())
                    ->contains("Success")
        ;
    }
}

已知问题

  • 无法找到 AppKernel 的路径,请覆盖 getKernelDirectory 方法并将路径添加到您的 app 目录中。

测试控制器

您可以使用 ControllerTest 类(它继承自 WebTestCase - 每个文件必须对应于一个 Symfony2 控制器)来测试您的控制器。

<?php

namespace vendor\FooBundle\Tests\Controller;

use atoum\AtoumBundle\Test\Units\WebTestCase;
use atoum\AtoumBundle\Test\Controller\ControllerTest;

class BarController extends ControllerTest
{
    public function testGet()
    {
        $this
            ->request(array('debug' => true))
                ->GET('/demo/' . uniqid())
                    ->hasStatus(404)
                    ->hasCharset('UTF-8')
                    ->hasVersion('1.1')
                ->POST('/demo/contact')
                    ->hasStatus(200)
                    ->hasHeader('Content-Type', 'text/html; charset=UTF-8')
                    ->crawler
                        ->hasElement('#contact_form')
                            ->hasChild('input')->exactly(3)->end()
                            ->hasChild('input')
                                ->withAttribute('type', 'email')
                                ->withAttribute('name', 'contact[email]')
                            ->end()
                            ->hasChild('input[type=submit]')
                                ->withAttribute('value', 'Send')
                            ->end()
                            ->hasChild('textarea')->end()
                        ->end()
                        ->hasElement('li')
                            ->withContent('The CSRF token is invalid. Please try to resubmit the form.')
                            ->exactly(1)
                        ->end()
                        ->hasElement('title')
                            ->hasNoChild()
                        ->end()
                        ->hasElement('meta')
                            ->hasNoContent()
                        ->end()
                        ->hasElement('link')
                            ->isEmpty()
                        ->end()
        ;
    }
}

测试表单类型

您可以使用 FormTestCase 类测试您的表单类型,如官方 Symfony 2 文档所示。但官方文档适用于 PHPUnit 测试框架,因此这里首先提供 atoum 的翻译示例。

<?php

namespace Acme\DemoBundle\Tests\Form;

use Acme\DemoBundle\Entity\TestEntity;
use atoum\AtoumBundle\Test\Form;
use Acme\DemoBundle\Form\TestEntityType as MyTypeToTest;

class TestEntityType extends Form\FormTestCase{

    public function testToutCourt()
    {
        $formData = array(
            'texte1' => 'test 1',
            'texte2' => 'test 2',
        );

        $type = new MyTypeToTest();
        $form = $this->factory->create($type);

        $object = new TestEntity();
        $object->fromArray($formData);

        // submit the data to the form directly
        $form->submit($formData);

        $this->boolean($form->isSynchronized())->isTrue();
        $this->variable($object)->isEqualTo($form->getData());

        $view = $form->createView();
        $children = $view->children;

        foreach (array_keys($formData) as $key) {
            $this->array($formData)->hasKey($key);
        }
    }

}

Faker 数据

AtoumBundle 集成了 Faker 库。

在您的测试类中,您可以使用 faker 断言访问 Faker\Generator 实例。

public function testMyAmazingFeature()
{
    //.....
    $randomName = $this->faker->name;

    $dateTimeBetweenYesterdayAndNow = $this->faker->dateTimeBetween('-1 day', 'now');
    //.....
}

有关使用方法,请参阅 Faker 的文档