提问者:小点点

使用枚举的CakePHP测试的变通方法?


我尝试为CakePHP项目创建单元测试。在创建夹具和一些测试后,我遇到了问题:

警告:列类型枚举(开,关)不存在 /app/cake/2.4.7/Cake/Model/Datasource/DboSource.php行3081

...

应用程序错误:调用处理程序方法异常'PDOExcture'与消息'SQLSTATE[42S22]:列未找到: 1054未知的列'状态'在'字段列表'在 /app/cake/2.4.7/Cake/Model/Datasource/DboSource.php:2923

我知道CakePHP不支持enum,但我无法更改数据类型。是否有任何解决方法可以让单元测试与枚举一起运行?


共1个答案

匿名用户

我遇到了同样的问题。为了解决这个问题,我创建了一个子类,它扩展了CakeTestFixture类,并覆盖了表的创建,以将所有枚举字段重新映射到字符串。

<?php

class MyTestFixture extends CakeTestFixture {

  /**
   * Maps enum fields in the database to strings with a length of 64
   */
  function create(&$db) {
    foreach($this->fields as $name => &$field) {
      if( strstr($field['type'], "enum") !== false ) {
        $field['type'] = 'string';
        $field['length'] = 64;
      }
    }
    parent::create($db);
  }
}

要使用它,只需在fixture中扩展这个类,而不是CaketTestFixture。

<?php

App::uses('MyTestFixture', 'WhereverYouPutIt');

class MyTableFixture extends MyTestFixture {
    public $import = array('model' => 'MyTable');
}