symfony2实例教程:
https://wusuopu.gitbooks.io/symfony2_tutorial/content/chapter08/README.html
lime测试框架:
http://symfony.com/legacy/doc/jobeet/1_2/zh_CN/08?orm=Propel
PHPUnit单元测试:
http://twpug.net/docs/symblog/docs/testing-unit-and-functional-phpunit.html
PHPUnit手册:
https://phpunit.de/manual/current/zh_cn/installation.html
PHPUnit初探:
http://www.php100.com/html/webkaifa/PHP/PHPyingyong/2011/0216/7534.html
symfony2-jobeet-tutorial:
https://github.com/happen-zhang/symfony2-jobeet-tutorial/blob/master/chapter-08/chapter-08.md
symfony2 cookbook:
http://wiki.jikexueyuan.com/project/symfony-cookbook/testing.html
一下是phpunit在symfony2的的配置文档
(1)phpunit的安装
去phpunit官网下载phpunit(https://phpunit.de/)安装包phpunit.phar,注意对应自己的php版本进行下载安装.
下载完成后要全局安装:(具体安装参考https://phpunit.de/manual/current/zh_cn/installation.html)
如果要全局安装 PHAR:
$ chmod +x phpunit.phar
$ sudo mv phpunit.phar /usr/local/bin/phpunit
$ phpunit --version
PHPUnit x.y.z by Sebastian Bergmann and contributors.
(2)安装完成后在symfony2进行配置(具体可参考https://wusuopu.gitbooks.io/symfony2_tutorial/content/chapter08/README.html)
PHPUnit对应的配置文件为app/phpunit.xml.dist
,这是个以.dist
能结尾的文件,可能需要将其复制一份到app/phpunit.xml
。它配置了一些测试参数,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="bootstrap.php.cache" >
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<!-- <php> <server name="KERNEL_DIR" value="/path/to/your/app/" /> </php> -->
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
注意文件中对应的路径和自己真实的路径要相同.
(3)编写单元测试样例
这里最主要的问题为在运行单元测试之前,要运行起整个环境.可参考demo
<?php namespace Topxia\Service\Test\Course; use Topxia\Service\User\Impl\UserServiceImpl; use Topxia\Service\Common\ServiceKernel; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class CourseServiceTest extends \PHPUnit_Framework_TestCase
{
public function setUp() {
require_once __DIR__.'/../../../../../app/AppKernel.php'; $kernel = new \AppKernel('prod', true); $kernel->boot(); // START: init service kernel $serviceKernel = ServiceKernel::create($kernel->getEnvironment(), $kernel->isDebug()); $serviceKernel->setParameterBag($kernel->getContainer()->getParameterBag()); $serviceKernel->setConnection($kernel->getContainer()->get('database_connection')); $serviceKernel->getConnection()->exec('SET NAMES UTF8'); }
public function testIndex()
{
$User = new UserServiceImpl(); $this->assertEquals(35, count($User->getUser("1"))); // $this->assertEquals('sdf', $User::getUser("90")); }
public function webIndex()
{
$client = WebTestCase::createClient(); $crawler = $client->request('GET', '/hello/Fabien'); $this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0); }
public function tearDown()
{
}
}
(4)运行单元测试
用命令行:
phpunit -c app src/Blogger/BlogBundle/Tests/Controller/PageControllerTest.php
或者 phpunit -c app