2011-05-23 161 views

回答

3

不幸的是,CakePHP不能與PHPUnit配合使用。 CakePHP已經切換到使用SimpleTest,您將有兩種選擇之一,重構測試以使用SimpleTest或修改核心以使用PHPUnit。

但是應該說它是Mark Story has stated that CakePHP 2.0 will use PHPUnit它的測試框架,所以如果你可以等到那時候可能是最好的選擇。

CakePHP 1.3 Book on Testing

+0

謝謝!我目前沒有測試,所以我仍然可以在兩者之間進行選擇。我會花一些時間試着看看我能否讓CakePHP使用PHPUnit核心。 – 2011-05-24 09:17:16

+0

SimpleTest總是有問題,特別是模型的測試數據。 – hongster 2011-06-03 12:54:47

+0

我想修改我的1.3核心以使用PHPUnit。任何線索如何做到這一點? – 2014-07-02 14:15:26

5

這是比較容易的。我使用作曲家安裝的蛋糕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" 
       } 
      } 
     } 
    ] 
} 

然後在測試目錄中的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的方式做到這一點。例如,如果你的控制器被命名爲日曆您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; 
    } 
} 

同爲模特,供應商類別等。對我很好。