zoukankan      html  css  js  c++  java
  • Yii 1.1.x 单元测试

    代码若不整洁,只会越来越糟糕;代码写不好,公司要黄是迟早。

    Yii 的应用有两种,下面记录这两种应用的单元测试方法

    1. webApplication
    2. consoleApplication

    在protected ests下面放 bootstrap.php

    <?php
    date_default_timezone_set("Asia/Shanghai");
    // change the following paths if necessary
    $_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__) . '/../';
    set_include_path($_SERVER['DOCUMENT_ROOT']);
    error_reporting(E_ALL);
    defined('YII_DEBUG') or define('YII_DEBUG',true);
    
    // change the following paths if necessary
    $yii=dirname(__FILE__).'/../../../framework/yii.php';
    $config=dirname(__FILE__).'/../../protected/config/main.php';
    $console=dirname(__FILE__).'/../../protected/config/console.php';
    // specify how many levels of call stack should be shown in each log message
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
    require_once($yii);
    //require_once($config);
    //require_once($console);
    
    Yii::setPathOfAlias('application', $_SERVER['DOCUMENT_ROOT']);
    Yii::import("application.components.*");
    Yii::import('application.extensions.a.*');
    Yii::import('application.extensions.b.*');
    Yii::import('application.extensions.c.*');
    Yii::import('application.extensions.d.*');
    Yii::import('application.extensions.e.*');
    Yii::import('application.extensions.f.*');
    Yii::import('application.extensions.g.*');
    Yii::import("application.services.*");
    Yii::createWebApplication($config);
    //Yii::createConsoleApplication($console);
    

    研究一下午,PHP 的include 真的复杂,import 非常important,有了Bootstrap.php

    在tests下面的测试文件例如 ServicesTester.php

    第一行写
    require_once ("../bootstrap.php");

    然后就写MockClass 和 TestCase 就好了。

    注意:
    bootstrap 最后的两行,差别在于读取的配置文件不同,可能导致数据库连接配置的问题。
    命令行的项目 读的是console.php。
    Web项目读的是main.php。
    Yii:app()->db 读的位置取决于 读的配置文件,这一点容易出错。

  • 相关阅读:
    解决CentOS 7 history命令不显示操作记录的时间和用户身份问题
    CentOS7关闭selinux
    Centos7下添加开机自启动服务和脚本
    快速查看一个文件的权限 stat -c %a
    修改centos7系统语言
    sudo
    chsh命令 修改用户登录shell
    忘记root开机密码及怎样开启密码远程连接模式
    centos7系统中添加 pstree 命令
    vim 多行添加注释,取消注释
  • 原文地址:https://www.cnblogs.com/slankka/p/9158514.html
Copyright © 2011-2022 走看看