将 PHPUnit 与 CakePHP 1.3 集成

发布于 2024-11-09 11:32:47 字数 253 浏览 0 评论 0原文

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

我一直在尝试遵循 http://cakebaker.42dh.com/2006 上的教程/03/22/selenium/ 但似乎无法让它工作。有什么好的教程吗?

谢谢!

I have been looking for a tutorial to help me integrate PHPUnit with CakePHP. Looking to use Selenium tests too so prefer PHPUnit.

I have been trying to follow the tutorial on http://cakebaker.42dh.com/2006/03/22/selenium/ but cant seem to get it work. Any good tutorials out there?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-11-16 11:32:47

这相对容易。我使用来自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;
    }
}

模型、供应商类等相同。对我来说效果很好。

It's relatively easy. I use cake 1.3 from composer installation. This is how my composer.json looks like:

{
    "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"
                }
            }
        }
    ]
}

Then the phpunit bootstrap.php file in tests directory:

<?php
include('../vendors/composer/autoload.php');
include('../webroot/index.php');

This is phpunit.xml form the same dir:

<?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>

Don't forget to load your application classes in test setup. You can do it cakephp way. For example if your controller is named calendar your calendarTest.php may look like:

<?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;
    }
}

The same for models, vendor classes and so on. Works great for me.

兲鉂ぱ嘚淚 2024-11-16 11:32:47

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

不过需要说明的是,Mark Story 已声明 CakePHP 2.0 将使用 PHPUnit 因为它是测试框架,所以如果你能等到那时,这可能会是最好的选择。

CakePHP 1.3 测试书籍

Unfortunately CakePHP isn't designed to work together with PHPUnit. CakePHP has switched to using SimpleTest and you'll have one of two choices, refactor your tests to work with SimpleTest or modify the core to use PHPUnit.

However it should be stated that Mark Story has stated that CakePHP 2.0 will use PHPUnit for it's testing framework, so if you can aford to wait till then that may wind up being the best option.

CakePHP 1.3 Book on Testing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文