【问题标题】:phpunit unable to locate session.php in codeigniterphpunit 无法在 codeigniter 中找到 session.php
【发布时间】:2015-04-01 03:37:27
【问题描述】:

我已经在我的项目中安装了 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 和 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 下的初始化中止。”,这是错误的原因吗?

【问题讨论】:

  • Check this 兄弟,他们确实通过 CLI 禁用了会话库。

标签: php codeigniter session phpunit codeigniter-3


【解决方案1】:

要在 codeigniter 中使用 Session 库,您需要手动加载它

$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;

希望这会有所帮助。

【讨论】:

  • 是的,我一直在 autoload.php 中启用会话库,并且我尝试手动加载它。但似乎没有效果。 :(
  • 能否启用强度为 4 的日志并再次运行查看日志文件中的内容?
【解决方案2】:

CodeIgniter3 官方不支持 PHPUnit 进行应用测试。

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

【讨论】:

    猜你喜欢
    • 2019-10-23
    • 2015-07-02
    • 2018-07-28
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    • 2017-12-11
    • 2015-02-24
    相关资源
    最近更新 更多