symfony2 用phpunit進行單元測試

listwebit發表於2015-11-09

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

相關文章