phpunit无法在codeigniter中找到session.php

时间:2015-02-02 08:19:57

标签: php codeigniter session phpunit codeigniter-3

我在我的项目中安装了Codeigniter 3 Release然后我在config / autolad.php中启用会话库但是当我运行我的测试时,我总是收到错误“无法找到指定的类:Session.php”。

这是我的phpunit.xml

<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="application/tests/bootstrap.php">
    <testsuites>
        <testsuite name="TestSuite">
            <directory>application/tests</directory>
        </testsuite>
    </testsuites>
    <php>
        <const name="PHPUNIT_TEST" value="1" />
        <const name="PHPUNIT_CHARSET" value="UTF-8" />
        <server name="REMOTE_ADDR" value="0.0.0.0" />
    </php>
    <filter>
                <blacklist>
                        <directory suffix=".php">system</directory>
                        <!--directory suffix=".php">application/libraries</directory-->
                </blacklist>
    </filter>
</phpunit>

这是我的application / test / bootstrap.php集成codeigniter&amp; PHPUnit的

<?php

/*
 *---------------------------------------------------------------
 * OVERRIDE FUNCTIONS
 *---------------------------------------------------------------
 *
 * This will "override" later functions meant to be defined
 * in core\Common.php, so they throw erros instead of output strings
 */

function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
{
    throw new PHPUnit_Framework_Exception($message, $status_code);
}

function show_404($page = '', $log_error = TRUE)
{
    throw new PHPUnit_Framework_Exception($page, 404);
}

/*
 *---------------------------------------------------------------
 * BOOTSTRAP
 *---------------------------------------------------------------
 *
 * Bootstrap CodeIgniter from index.php as usual
 */

require_once dirname(__FILE__) . '/../../index.php';

/*
 * This will autoload controllers inside subfolders
 */ 
spl_autoload_register(function ($class) {
    foreach (glob(APPPATH.'controllers/**/'.ucfirst(strtolower($class).'.php')) as $controller) {
        require_once $controller;
    }
    //kreasi sendiri
    foreach (glob(APPPATH.'controllers/'.ucfirst(strtolower($class).'.php')) as $controller) {
        require_once $controller;
    }
});

任何人都可以帮助我吗?


@ gopakumar-gopalan,我按照你说的做了,在这里我得到了:

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>

INFO - 2015-02-03 04:52:10 --> Config Class Initialized
INFO - 2015-02-03 04:52:10 --> Hooks Class Initialized
DEBUG - 2015-02-03 04:52:10 --> UTF-8 Support Enabled
INFO - 2015-02-03 04:52:10 --> Utf8 Class Initialized
INFO - 2015-02-03 04:52:10 --> URI Class Initialized
DEBUG - 2015-02-03 04:52:10 --> No URI present. Default controller set.
INFO - 2015-02-03 04:52:10 --> Router Class Initialized
INFO - 2015-02-03 04:52:10 --> Output Class Initialized
INFO - 2015-02-03 04:52:10 --> Security Class Initialized
DEBUG - 2015-02-03 04:52:10 --> Global POST, GET and COOKIE data sanitized
INFO - 2015-02-03 04:52:10 --> Input Class Initialized
INFO - 2015-02-03 04:52:10 --> Language Class Initialized
INFO - 2015-02-03 04:52:10 --> Loader Class Initialized
INFO - 2015-02-03 04:52:10 --> Helper loaded: url_helper
INFO - 2015-02-03 04:52:10 --> Helper loaded: date_helper
DEBUG - 2015-02-03 04:52:10 --> Session: Initialization under CLI aborted.
INFO - 2015-02-03 04:52:10 --> Database Driver Class Initialized
INFO - 2015-02-03 04:52:10 --> Controller Class Initialized
INFO - 2015-02-03 04:52:10 --> Model Class Initialized
INFO - 2015-02-03 04:52:10 --> Model Class Initialized
INFO - 2015-02-03 04:52:10 --> Helper loaded: form_helper
INFO - 2015-02-03 04:52:10 --> File loaded: /var/www/sitahta.local/public_html/application/views/core/header.php
INFO - 2015-02-03 04:52:10 --> File loaded: /var/www/sitahta.local/public_html/application/views/core/footer.php
INFO - 2015-02-03 04:52:10 --> File loaded: /var/www/sitahta.local/public_html/application/views/core/alert.php
INFO - 2015-02-03 04:52:10 --> File loaded: /var/www/sitahta.local/public_html/application/views/login/index.php
INFO - 2015-02-03 04:52:10 --> Final output sent to browser
DEBUG - 2015-02-03 04:52:10 --> Total execution time: 0.0990

我看到有调试日志说'会话:CLI中的初始化已中止。',这是导致错误的原因吗?

2 个答案:

答案 0 :(得分:0)

要在codeigniter中使用会话库,您需要手动加载

$this->load->library('session');

或通过自动加载。要自动加载会话库,请修改application/config/autoload.php

$autoload['libraries'] = array('database','session');

如果您不希望会话使用数据库,则不能自动加载database

接下来在application/config/config.php中设置加密密钥

$config['encryption_key'] = 'your-encryption-key';

和会话设置......

$config['sess_cookie_name']     = 'my_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = TRUE;
$config['sess_use_database']    = FALSE; // TRUE If you use DB for storing session variables
$config['sess_table_name']      = 'my_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

希望这会有所帮助。

答案 1 :(得分:0)

CodeIgniter3不支持正式进行应用程序测试的PHPUnit。

试试我的ci-phpunit-test:http://kenjis.github.io/ci-phpunit-test/