1
votes

I have installed Codeigniter 3 Release in my project then i enable session library in config/autolad.php but when i run my test, i always get the error "Unable to locate the specified class: Session.php".

this is my 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>

and this is my application/test/bootstrap.php to integrate 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;
    }
});

anybody can help me?


@gopakumar-gopalan, i did as you say, and here what i've got :

<?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

i see there are debug log say 'Session: Initialization under CLI aborted.', is this the cause for the error?

2
Check this out bro, they do disable session library through CLI. - Fery W

2 Answers

0
votes

For using Session library in codeigniter you need to load it either manually

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

or by auto loading. To auto load the session library, edit application/config/autoload.php

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

You maynot auto load database if you don't want the session to use database.

Next in application/config/config.php set the encryption key

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

and the session settings too...

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

Hope this will help.

0
votes

CodeIgniter3 does not support PHPUnit for application testing officially.

Try my ci-phpunit-test: http://kenjis.github.io/ci-phpunit-test/