Skip to content

Could not use the model which the default controller has been created in the testcase #42

Open

Description

@shelverizr

I want to reuse some model, so I wrote the code like this

<?php
/**
* 模型基础测试类
* 用于设置一些公共的自动加载功能
*/
abstract class CIModelTestCase extends CITestCase
{
    protected $CI;

    public function __construct()
    {
        parent::__construct();
    }

    public static function setUpBeforeClass()
    {
        $CI =& get_instance();
        $CI->load->model('category');
        $CI->load->model('flow_record');
        $CI->load->model('order');
        $CI->load->model('mall_m');
        $CI->load->model('cart');
        $CI->load->database();
    }

    public static function tearDownAfterClass()
    {
        $CI =& get_instance();
        $CI->db->close();
    }
}

But I found that some model were useless. When I hack the Loader.php in the function model():

foreach ($this->_ci_model_paths as $mod_path)
{
    if ( ! file_exists($mod_path.'models/'.$path.$model.'.php'))
    {
        continue;
    }

    require_once($mod_path.'models/'.$path.$model.'.php');
    $this->_ci_models[] = $name;
    $CI->$name = new $model();
        // added this line
    var_dump($name, new $model());
    return $this;
}

The result:

1

Model ‘cart’, ‘order’, ‘mall_m’ were not in the ‘stubmodel’.

But if I change the default controller from

$route['default_controller'] = 'mall';

To

$route['default_controller'] = 'supermarket';

The result:

2

This time, the model ‘goods’ is not found in the ‘stubmodel’, but model ‘cart’, ‘order’, ‘mall_m’ were appeared.

How Should I fix it?

By the way:

Mall.php(only construct)

class Mall extends CI_Controller
{
    public $data;
    public $user;
    function __construct()
    {
        //调用父类的构造函数
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('session');
        $this->load->model('cart');
        $this->load->model('order');
        $this->load->model("mall_m");//User_model模型类实例化对象

        $user=$this->mall_m->user_selecet(array('username'=>$this->session->username,'password'=>$this->session->password));

    }
}

Supermarket.php(only construct)

<?php
/**
* 超市控制器
*/
class Supermarket extends CI_Controller
{
    // 超市所属分类id
    const SUPERMARKET_TYPE_ID = 212;

    function __construct()
    {
        // 调用父类方法
        parent::__construct();
        // 加载商品分类类
        $this->load->model('category');
        // 加载商品类
        $this->load->model('goods');
        // 加载数据库
        $this->load->database();
    }
}

CategoryTest.php(A simple testcase)

<?php

require_once(__DIR__ . '/../CIModelTestCase.php');

/**
* 栏目类测试类
*/
class CategoryTest extends CIModelTestCase
{
    public function test_get_all_children()
    {
        $this->assertEquals($this->CI->category->get_all_children('212'), $this->CI->db->query("SELECT * FROM typelist WHERE reid='212'")->result());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions