集成PHPUnit和CakePHP 1.3

问题描述

| 我一直在寻找可以帮助我将PHPUnit与CakePHP集成的教程。希望也使用Selenium测试,因此更喜欢PHPUnit。 我一直在尝试按照http://cakebaker.42dh.com/2006/03/22/selenium/上的教程进行操作,但似乎无法正常工作。有什么好的教程吗? 谢谢!     

解决方法

不幸的是,CakePHP并非旨在与PHPUnit一起使用。 CakePHP已切换为使用SimpleTest,您将有以下两种选择之一:重构测试以与SimpleTest一起使用,或修改核心以使用PHPUnit。 但是,应该说Mark Story声明CakePHP 2.0将在其测试框架中使用PHPUnit,因此,如果您愿意等到那时,那可能是最好的选择。 CakePHP 1.3测试书籍     ,这比较容易。我使用composer安装中的cake 1.3。这就是我的composer.json的样子:
{
    \"config\": {
        \"vendor-dir\": \"vendors/composer\"
    },\"require\": {
        \"phpunit/phpunit\": \"3.7.*\",\"cakephp/cakephp-1.3\": \"1.3\",},\"repositories\": [
        {
            \"type\": \"package\",\"package\": {
                \"name\": \"cakephp/cakephp-1.3\",\"version\": \"1.3\",\"source\": {
                    \"url\": \"https://github.com/cakephp/cakephp.git\",\"type\": \"git\",\"reference\": \"1.3\"
                }
            }
        }
    ]
}
然后在tests目录中的phpunit bootstrap.php文件:
<?php
include(\'../vendors/composer/autoload.php\');
include(\'../webroot/index.php\');
这与phpunit.xml的目录相同:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<phpunit
     bootstrap=\"bootstrap.php\"

     backupStaticAttributes=\"false\"

     cacheTokens=\"false\"
     colors=\"false\"
     convertErrorsToExceptions=\"true\"
     convertNoticesToExceptions=\"true\"
     convertWarningsToExceptions=\"true\"
     forceCoversAnnotation=\"false\"
     mapTestClassNameToCoveredClassName=\"false\"
     printerClass=\"PHPUnit_TextUI_ResultPrinter\"

     processIsolation=\"false\"
     stopOnError=\"false\"
     stopOnFailure=\"false\"
     stopOnIncomplete=\"false\"
     stopOnSkipped=\"false\"
     testSuiteLoaderClass=\"PHPUnit_Runner_StandardTestSuiteLoader\"

     strict=\"false\"
     verbose=\"false\"
    >

    <testsuites>
        <testsuite name=\"AllTests\">
        <directory>.</directory>
        </testsuite>
    </testsuites>

    <filter>
        <blacklist>
            <directory suffix=\".php\"></directory>
            <file></file>
            <exclude>
                <directory suffix=\".php\"></directory>
                <file></file>
            </exclude>
        </blacklist>
        <whitelist processUncoveredFilesFromWhitelist=\"true\">
            <directory suffix=\".php\"></directory>
            <file></file>
            <exclude>
                <directory suffix=\".php\"></directory>
                <file></file>
            </exclude>
        </whitelist>
    </filter>
</phpunit>
不要忘记在测试设置中加载应用程序类。你可以做到cakephp的方式。例如,如果您的控制器命名为calendar,则calendarTest.php可能类似于:
<?php

/**
 * Class ComponentsCommonTest
 * @property calendarController $calendarController
 */
class CalendarTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var calendarController $calendarController
     */
    private $calendarController;

    function setUp()
    {
        App::import(\'Core\',array(\'View\',\'Controller\',\'Model\',\'Router\'));
        App::import(\'Controller\',\'Calendar\');
        $this->calendarController =& new CalendarController();
        $this->calendarController->constructClasses();
        $this->calendarController->layout = null;
    }
}
型号,供应商类别等相同。对我来说很棒。