这是我自己的解决方案(我总结了在独立包中测试的所有过程):
1. 首先,好的捆绑包有自己的特色composer.json
定义其依赖项:
{
"name": "my/own-bundle",
"type": "symfony-bundle",
"description": "Symfony2 bundle that provides ...",
"keywords": ["my","own"],
"license": "MIT",
"authors": [
{
"name": "John Doe",
"email": "[email protected] /cdn-cgi/l/email-protection"
}
],
"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
例如)。
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());
}
}