visithor/visithor-bundle

Symfony 的 Visithor Bundle

安装数: 19,942

依赖项: 3

建议者: 0

安全性: 0

星标: 16

关注者: 4

分支: 0

开放性问题: 0

类型:symfony-bundle

v0.2.1 2016-06-01 14:04 UTC

README

Build Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version

Symfony Bundle 为 PHP 包 visithor,这是一个提供您以简单且痛苦的方式测试应用程序路由与特定 HTTP 状态码的库。

请阅读 Visithor 文档,以便了解此项目的最终目的以及您应该如何创建您的配置文件。

安装

您需要做的就是将此包添加到您的 composer 中的 require-dev 块中,然后您将能够测试您的应用程序。

'require-dev':
    ...
    
    'visithor/visithor-bundle': '~0.1'

然后您必须更新您的依赖项。

php composer.phar update

集成

此 Bundle 将项目集成到您的 Symfony 项目中。这意味着它将在您的项目控制台添加所有命令,因此当您执行 app/console 时,您将看到 visithor:* 命令。

php app/console visithor:go --env=test

配置

此 Bundle 在定义 URL 时提供了额外功能。现在您可以使用路由名称和参数数组来定义您的路由。

defaults:
    #
    # This value can be a simple HTTP Code or an array of acceptable HTTP Codes
    # - 200
    # - [200, 301]
    #
    http_codes: [200, 302]

urls:
    #
    # By default, is there is no specified HTTP Code, then default one is used
    # as the valid one
    #
    - http://google.es
    - http://elcodi.io
    
    #
    # There are some other formats available as well
    #
    - [http://shopery.com, 200]
    - [http://shopery.com, [200, 302]]
    
    #
    # This Bundle adds some extra formats
    #
    - [store_homepage, 200]
    - [[store_category_products_list, {'slug': 'women-shirts', 'id': 1}], 200]
    - [[store_category_products_list, {'slug': 'another-name', 'id': 1}], 302]
    - [[store_homepage, {_locale: es}]]

环境

也许您需要在 Visithor 测试路由之前准备您的环境,对吗?准备您的数据库,加载数据库固定值,以及您需要使测试安装工作的一切。

默认情况下,VisithorBundle 已经有一个简单实现并已正常工作。此实现负责构建数据库和创建您的模式。

php app/console doctrine:database:create
php app/console doctrine:schema:update

它还负责在测试完成后销毁您的测试数据库。

php app/console doctrine:database:drop --force

如果您想扩展此行为,例如加载某些固定值,那么您需要实现自己的实现,或者扩展此实现。

要实现自己的,您应该定义一个名为 visitor.environment_builder 的服务,该服务实现接口 Visithor\Bundle\Environment\Interfaces\EnvironmentBuilderInterface

如果您查看此接口,您将看到您需要定义两个方法。第一个方法用于设置环境,将在测试套件的开始时仅调用一次。第二个方法将拆除该环境(例如删除数据库)。

use Symfony\Component\HttpKernel\KernelInterface;
use Visithor\Bundle\Environment\Interfaces\EnvironmentBuilderInterface;

/**
 * Class EnvironmentBuilder
 */
class EnvironmentBuilder implements EnvironmentBuilderInterface
{
    /**
     * Set up environment
     *
     * @param KernelInterface $kernel Kernel
     *
     * @return $this Self object
     */
    public function setUp(KernelInterface $kernel)
    {
        //
    }

    /**
     * Tear down environment
     *
     * @param KernelInterface $kernel Kernel
     *
     * @return $this Self object
     */
    public function tearDown(KernelInterface $kernel)
    {
        //
    }

    /**
     * Get authenticated user
     *
     * @param string $role Role
     *
     * @return mixed User for authentication
     */
    public function getAuthenticationUser($role)
    {
        //
    }
}

这是完全覆盖默认实现的方式,但如果您只想扩展它,那么要简单得多。看看这个例子。

namespace Elcodi\Common\VisithorBridgeBundle\Visithor;

use Doctrine\DBAL\Connection;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\HttpKernel\KernelInterface;
use Visithor\Bundle\Environment\SymfonyEnvironmentBuilder;

/**
 * Class EnvironmentBuilder
 */
class EnvironmentBuilder extends SymfonyEnvironmentBuilder
{
    /**
     * Set up environment
     *
     * @param KernelInterface $kernel Kernel
     *
     * @return $this Self object
     */
    public function setUp(KernelInterface $kernel)
    {
        parent::setUp($kernel);

        $this
            ->executeCommand('doctrine:fixtures:load', [
                '--fixtures' => $kernel
                        ->getRootDir() . '/../src/Elcodi/Fixtures',
            ])
            ->executeCommand('elcodi:templates:load')
            ->executeCommand('elcodi:templates:enable', [
                'template' => 'StoreTemplateBundle',
            ])
            ->executeCommand('elcodi:plugins:load')
            ->executeCommand('assets:install')
            ->executeCommand('assetic:dump');
    }
}

要调用某些命令,您可以使用受保护的 executeCommand 方法,但请记住调用父方法以初始化应用程序并调用现有代码。

角色

您肯定会需要测试您的私有路由。当然,这是一个常见的需求,此 Bundle 满足了这一需求 :)

让我们检查我们的简单安全文件。

security:

    providers:
        in_memory:
            memory: ~

    firewalls:
        default:
            provider: in_memory
            http_basic: ~
            anonymous: ~

    access_control:
        - { path: ^/admin, roles: ROLE_ADMIN }
        - { path: ^/superadmin, roles: ROLE_SUPERADMIN }

然后,让我们看看我们的 Visithor 配置。

urls:
    - ['/', 200]
    - ['/admin', 200, {'role': 'ROLE_ADMIN', 'firewall': 'default'}]
    - ['/superadmin', 403, {'role': 'ROLE_ADMIN', 'firewall': 'default'}]

在这种情况下,所有路由都在默认防火墙 default 下。

路由 admin_route 由访问角色 ROLE_ADMIN 保护,并且因为我们正在对此角色进行测试,所以我们将收到 200。

路由 superadmin_route 由访问角色 ROLE_SUPERADMIN 保护,但在这个例子中,我们再次使用角色 ROLE_ADMIN 进行测试,所以我们将收到 403 代码。

当然,您可以将防火墙定义为全局选项。只有当角色和防火墙选项都定义时,您的路由才会应用安全性。

defaults:
    options:
        firewall: default
urls:
    - ['/', 200]
    - ['/admin', 200, {'role': 'ROLE_ADMIN'}]
    - ['/superadmin', 403, {'role': 'ROLE_ADMIN'}]

因为您需要验证真实用户才能使其工作,所以您可以在自己的 EnvironmentBuilder 实现中返回此用户。确保您的测试环境已经准备好进行测试。

让我们通过一个实际例子来看一下这种方法的一个实现。

/**
 * Get authenticated user
 *
 * @param string $role Role
 *
 * @return mixed User for authentication
 */
public function getAuthenticationUser($role)
{
    return 'ROLE_ADMIN' === $role
        ? $this
            ->adminUserRepository
            ->findOneBy([
                'email' => 'admin@admin.com'
            ])
        : null;
}

如您所见,接收到的参数是你打算测试的角色,因此你可以根据该值切换用户。