Integrating PHPUnit with CakePHP 1.3

旧巷老猫 提交于 2019-12-01 04:24:56
Justin Yost

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!