【问题标题】:Integrating PHPUnit with CakePHP 1.3将 PHPUnit 与 CakePHP 1.3 集成
【发布时间】:2011-08-31 01:29:11
【问题描述】:

我一直在寻找帮助我将 PHPUnit 与 CakePHP 集成的教程。也希望使用 Selenium 测试,所以更喜欢 PHPUnit。

我一直在尝试按照http://cakebaker.42dh.com/2006/03/22/selenium/ 上的教程进行操作,但似乎无法正常工作。有什么好的教程吗?

谢谢!

【问题讨论】:

    标签: unit-testing cakephp phpunit cakephp-1.3


    【解决方案1】:

    这相对容易。我使用作曲家安装的 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;
        }
    }
    

    模型、供应商类别等也是如此。非常适合我。

    【讨论】:

      【解决方案2】:

      不幸的是,CakePHP 不是为与 PHPUnit 一起工作而设计的。 CakePHP 已切换到使用SimpleTest,您将有两种选择之一,重构您的测试以使用 SimpleTest 或修改核心以使用 PHPUnit。

      但是应该声明Mark Story has stated that CakePHP 2.0 will use PHPUnit 是它的测试框架,所以如果你可以等到那时,那可能是最好的选择。

      CakePHP 1.3 Book on Testing

      【讨论】:

      • 谢谢!我目前没有测试,这就是为什么我仍然可以在两者之间进行选择。我会花一些时间尝试看看是否可以让 CakePHP 使用 PHPUnit 核心。
      • SimpleTest 总是有问题的,尤其是模型的测试数据。
      • 我想修改我的 1.3 核心以使用 PHPUnit。任何线索如何做到这一点?
      猜你喜欢
      • 2020-01-29
      • 2012-09-24
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 2010-10-05
      • 2016-11-05
      • 1970-01-01
      相关资源
      最近更新 更多