我需要直接在独立包中进行一些功能测试。我不想测试 Controller ,只想测试真实服务之间的一些交互。
我想知道是否有标准/最佳方法来做到这一点。我用一种方法做到了,但想知道是否有更好的方法。
最佳答案
这是我自己的解决方案(我将所有测试过程总结在一个独立的包中):
<强>1。首先,一个好的 bundle 有它自己的 composer.json
来定义它的依赖:
{
"name": "my/own-bundle",
"type": "symfony-bundle",
"description": "Symfony2 bundle that provides ...",
"keywords": ["my","own"],
"license": "MIT",
"authors": [
{
"name": "John Doe",
"email": "john.doe@omg.wtf"
}
],
"require": {
"php": ">=5.3.2",
"symfony/framework-bundle": ">=2.3"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"psr-0": { "My\\OwnBundle": "" }
},
"target-dir": "My/OwnBundle",
"minimum-stability": "dev"
}
请注意对 symfony/framework-bundle
的依赖的使用,这是我们对服务进行测试所需要的。您当然可以通过指定您自己对 symfony 核心的真实依赖性来降低依赖性。
使用这个文件,我可以处理命令(执行)以构建我的包的供应商目录:
$ composer update
<强>2。然后,我设置我的 phpunit 配置文件:
<!-- phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="Tests/bootstrap.php"
>
<testsuites>
<testsuite name="MyOwnBundle Test Suite">
<directory>./Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
<强>3。然后,我在我的测试目录中为类的自动加载设置了 php bootstrap:
// Tests/bootstrap.php
$file = __DIR__.'/../vendor/autoload.php';
if (!file_exists($file))
{
$file = __DIR__.'/../../../../../../vendor/autoload.php';
if (!file_exists($file))
throw new RuntimeException('Install dependencies to run test suite.');
}
$autoload = require_once $file;
这些步骤对于独立包中的任何测试都是标准的。
<强>4。现在,我想模拟一个应用程序来对我的服务进行一些功能测试:
我需要一个内核类:
// Tests/AppKernel.php (you can define it in a subdirectory /Fixtures if you prefer)
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array();
if (in_array($this->getEnvironment(), array('test'))) {
$bundles[] = new Symfony\Bundle\FrameworkBundle\FrameworkBundle();
$bundles[] = new My\OwnBundle\MyOwnBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config.yml');
}
}
以及对应的config.yml
:
# Tests/config.yml
framework:
secret: test
session:
storage_id: session.storage.mock_file
my_own:
test: 2
这是一个带有 session 模拟的示例。如果你想访问一些服务,不要忘记指定正确的框架配置节点(如果你不指定节点session
,你就没有服务session
例如)。
<强>5。最后,我可以在我的测试类中检索我的服务,如下所示:
// Tests/Functional/Handling/Handler.php
namespace My\OwnBundle\Tests\Functional\Handling;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class HandlerTest extends WebTestCase
{
private $handler;
protected function setUp()
{
require_once __DIR__.'/../../AppKernel.php';
$kernel = new \AppKernel('test', true);
$kernel->boot();
$container = $kernel->getContainer();
$this->handler = $container->get('my_own.handling.handler');
}
public function testHandle()
{
$this->assert($this->handler->handle());
}
}
关于symfony - 独立 Symfony2 包中的功能测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27501321/