seegno/test-bundle

2.0.1 2017-03-20 17:16 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:05:18 UTC


README

Latest Version Build Status Code Climate Test Coverage License

简介

此Bundle提供基础类以辅助设置测试数据库和数据固定。

安装

1. 使用composer下载SeegnoTestBundle

运行以下命令安装SeegnoTestBundle

$ composer require --dev seegno/test-bundle

2. 启用bundle

在kernel中启用bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    // ...

    if (in_array($this->getEnvironment(), array('test'))) {
        // ...
        $bundles[] = new Seegno\TestBundle\SeegnoTestBundle();
    }
}

3. 为测试准备您的应用程序

集成(功能)

请在config_test.yml中添加以下配置

seegno_test:
    database:
        driver: ORM # Types available: ORM (SQL) and ODM (MongoDB)

警告!!

在您的config_test.yml文件中配置一个不同的数据库用于测试非常重要,因为在集成测试中所有数据都将从数据库中清除。

使用方法

单元测试

use Seegno\TestBundle\TestCase\BaseTestCase;

class SomeClassTest extends BaseTestCase
{
    //...
}

可用功能

$this->getFaker(); // Get a faker instance.
$this->setReflectionProperty($class, $propertyName, $propertyValue); // Set a class property using reflection.

集成测试

use Seegno\TestBundle\TestCase\IntegrationTestCase;

class SomeClassTest extends IntegrationTestCase
{
    //...
}

可用功能

$this->getContainer(); // Get an instance of the dependency injection container.
$this->getSession(); // Get session.
$this->get('SERVICE'); // Get services.
$this->initializeDatabase(); // Initialize test database (SQL or MongoDB).

Web测试

use Seegno\TestBundle\TestCase\WebTestCase;

class SomeClassTest extends WebTestCase
{
    //...
}

可用功能

$this->authenticateUser($client, $user, $credentials, $roles, $firewall); // Authenticate a user.
$this->getClient(); // Get client that simulates a browser and makes requests to a Kernel object.
$this->assertResponseFields($response, $object, $groups); // Assert that object properties keys are in the response.

运行测试

在运行测试之前,请确保测试数据库已更新。

php app/console doctrine:database:drop --env=test --force
php app/console doctrine:database:create --env=test
php app/console doctrine:schema:create --env=test

要在本地机器上运行测试,只需使用phpunit命令即可

phpunit