peridot-php/peridot-httpkernel-plugin

Peridot 插件,用于简化 HttpKernel 应用程序的测试

1.1.0 2014-10-24 14:18 UTC

This package is not auto-updated.

Last update: 2024-09-10 02:07:37 UTC


README

Build Status HHVM Status

使用 HttpKernelPeridot 轻松测试 HttpKernel 应用程序。

一些基于 HttpKernel 的框架

##使用方法

我们建议通过 composer 将此插件安装到您的项目中

$ composer require --dev peridot-php/peridot-httpkernel-plugin:~1.0

您可以通过您的 peridot.php 文件注册插件。

<?php
use Evenement\EventEmitterInterface;
use Peridot\Plugin\HttpKernel\HttpKernelPlugin;

return function(EventEmitterInterface $emitter) {
    //the second argument expects an HttpKernelInterface or a function that returns one
    HttpKernelPlugin::register($emitter, include __DIR__ . '/app.php');
};

通过注册插件,您的 Peridot 测试现在将有一个可用的 $client 属性

<?php
describe('Api', function() {
    describe('/info', function() {
        it('should return info about Peridot', function() {
            $this->client->request('GET', '/info');
            $response = $this->client->getResponse();
            $info = json_decode($response->getContent());
            assert($info->project == "Peridot", "project should be Peridot");
            assert($info->description == "Event driven testing framework", "description should describe Peridot");
            assert($info->styles == "BDD, TDD", "styles should be BDD, TDD");
        });
    });

    describe('/author', function() {
        it('should return info about the author', function() {
            $this->client->request('GET', '/author');
            $author = json_decode($this->client->getResponse()->getContent());
            assert($author->name == "Brian Scaturro", "author name should be on response");
            assert($author->likes == "pizza", "author should like pizza");
        });
    });
});

成功了!

如果不想在所有测试中都使用客户端,没问题。

###按测试使用

像任何其他 peridot 范围 一样,您可以根据测试或套件混合使用此插件提供的 HttpKernelScope

<?php
use Peridot\Plugin\HttpKernel\HttpKernelScope;

describe('Api', function() {

    //here we manually mixin the http kernel scope
    $scope = new HttpKernelScope(include __DIR__ . '/../app.php');
    $this->peridotAddChildScope($scope);

    describe('/author', function() {
        it('should return info about the author', function() {
            $this->client->request('GET', '/author');
            $author = json_decode($this->client->getResponse()->getContent());
            assert($author->name == "Brian Scaturro", "author name should be on response");
            assert($author->likes == "pizza", "author should like pizza");
        });
    });
});

###配置客户端属性名称

如果 $this->client 对您来说有点太通用,范围和插件都接受一个可选的最后一个参数,允许您设置此值。

HttpKernelPlugin::register($emitter, include __DIR__ . '/app.php', "browser");
$scope = new HttpKernelScope($application, "browser");

您的测试现在变为

<?php
use Peridot\Plugin\HttpKernel\HttpKernelScope;

describe('Api', function() {

    describe('/author', function() {
        it('should return info about the author', function() {
            $this->browser->request('GET', '/author');
            $author = json_decode($this->browser->getResponse()->getContent());
            assert($author->name == "Brian Scaturro", "author name should be on response");
            assert($author->likes == "pizza", "author should like pizza");
        });
    });
});

##示例规范

此存储库附带一个示例 Silex 应用程序,它使用此插件进行测试。

要测试使用此插件的示例,请运行以下命令

$ vendor/bin/peridot -c app/peridot.php app/specs/api.spec.php

要测试手动添加范围的示例,请运行此命令

$ vendor/bin/peridot app/specs/no-plugin.spec.php

##运行插件测试

$ vendor/bin/peridot specs/