Gnu*_*cki 33
这是我自己的解决方案(我总结了在独立包中测试的所有过程):
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"
}
Run Code Online (Sandbox Code Playgroud)
请注意symfony/framework-bundle
我们对服务测试所需的依赖关系的使用.您当然可以降低在symfony核心上指定自己的实际依赖关系的依赖关系.
使用此文件,我可以处理命令(执行此操作)以构建我的包的供应商目录:
$ composer update
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
这些步骤是独立捆绑包中任何测试的标准步骤.
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');
}
}
Run Code Online (Sandbox Code Playgroud)
相应的config.yml
:
# Tests/config.yml
framework:
secret: test
session:
storage_id: session.storage.mock_file
my_own:
test: 2
Run Code Online (Sandbox Code Playgroud)
这是一个模拟会话的示例.不要忘了指定正确的架构配置中的节点,如果你想访问某些服务(如果不指定节点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());
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3555 次 |
最近记录: |