Skip to content

Commit

Permalink
Unit tests load and subclass Session #353.
Browse files Browse the repository at this point in the history
Unit tests to reproduce the problem raised in #353.
In addition to testing load by libary or load by driver we are also extending by prefixed sub-class.
Added new mack application folder.
Added Session Subclass mock.
Fix bootstrap to point APPPATH to the mock application folder.
Including IC_Session class more than once in the same unit test requires runInSeparateProcess and preserveGlobalState disabled
which also causes all knowledge of bootstrap to dissapear so we need to cater for that.
  • Loading branch information
nickl- committed Sep 4, 2012
1 parent b9effd0 commit 7102c63
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 20 deletions.
2 changes: 1 addition & 1 deletion tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Path constants
defined('PROJECT_BASE') OR define('PROJECT_BASE', realpath($dir.'/../').'/');
defined('BASEPATH') OR define('BASEPATH', PROJECT_BASE.'system/');
defined('APPPATH') OR define('APPPATH', PROJECT_BASE.'application/');
defined('APPPATH') OR define('APPPATH', PROJECT_BASE.'tests/mocks/application/');
defined('VIEWPATH') OR define('VIEWPATH', PROJECT_BASE.'');

// Get vfsStream either via PEAR or composer
Expand Down
98 changes: 79 additions & 19 deletions tests/codeigniter/libraries/Session_test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php
if (!class_exists('CI_TestCase') && print_r(get_included_files()))
require_once __DIR__.'/../../Bootstrap.php';
defined('UTF8_ENABLED') OR define('UTF8_ENABLED', true);

/**
* Session driver library unit test
Expand Down Expand Up @@ -32,39 +35,50 @@ public function set_up()
$obj = new stdClass;
$classes = array(
'config' => 'cfg',
'load' => 'load',
'input' => 'in'
'load' => 'load',
'input' => 'in'
);
foreach ($classes as $name => $abbr) {
$class = $this->ci_core_class($abbr);
$obj->$name = new $class;
}
$this->ci_instance($obj);
$config = $this->setUpConfig();

$name = $this->getName();
if (empty($name))
$this->setName('test_session_library');
else
$this->session = new Mock_Libraries_Session($config);
}

public function setUpConfig()
{
// Attach session instance locally
$config = array(
'sess_encrypt_cookie' => FALSE,
'sess_use_database' => FALSE,
'sess_table_name' => '',
'sess_expiration' => 7200,
'sess_encrypt_cookie' => FALSE,
'sess_use_database' => FALSE,
'sess_table_name' => '',
'sess_expiration' => 7200,
'sess_expire_on_close' => FALSE,
'sess_match_ip' => FALSE,
'sess_match_ip' => FALSE,
'sess_match_useragent' => TRUE,
'sess_cookie_name' => 'ci_session',
'cookie_path' => '',
'cookie_domain' => '',
'cookie_secure' => FALSE,
'cookie_httponly' => FALSE,
'sess_time_to_update' => 300,
'time_reference' => 'local',
'cookie_prefix' => '',
'encryption_key' => 'foobar',
'sess_valid_drivers' => array(
'sess_cookie_name' => 'ci_session',
'cookie_path' => '',
'cookie_domain' => '',
'cookie_secure' => FALSE,
'cookie_httponly' => FALSE,
'sess_time_to_update' => 300,
'time_reference' => 'local',
'cookie_prefix' => '',
'encryption_key' => 'foobar',
'sess_valid_drivers' => array(
'Mock_Libraries_Session_native',
'Mock_Libraries_Session_cookie'
'Mock_Libraries_Session_cookie'
)
);
$this->session = new Mock_Libraries_Session($config);
$this->ci_set_config($config);
return $config;
}

/**
Expand Down Expand Up @@ -401,5 +415,51 @@ public function test_sess_destroy()
$this->session->native->sess_destroy();
$this->assertNull($this->session->native->userdata($key));
}

/**
* Test the session loaded as a library and
* overwritten with a prefixed subclass.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_session_library()
{
$this->ci_instance->session = null;

$config = $this->setUpConfig();
$obj = new stdClass;
$class = $this->ci_core_class('cfg');
$obj->config = new $class;
$class = $this->ci_core_class('loader');
$obj->load = new $class;
$class = $this->ci_core_class('in');
$obj->input = new $class;
$this->ci_instance($obj);

$this->ci_set_config('subclass_prefix', 'Prefix_');

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

$this->assertInstanceOf('Prefix_Session', $this->ci_instance->session);
}

/**
* Test the session loaded as a driver and
* overwritten with a prefixed subclass.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_session_driver()
{
$this->ci_instance->session = null;

$this->ci_set_config('subclass_prefix', 'Prefix_');

$this->ci_instance->load->driver('session');

$this->assertInstanceOf('Prefix_Session', $this->ci_instance->session);
}
}

17 changes: 17 additions & 0 deletions tests/mocks/application/libraries/Session/prefix_session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Mock library testing extending Session
* driver/library with prefixed subclass
*/
class Prefix_Session extends CI_Session {
/**
* Simulate new page load
*/
public function reload()
{
$this->_flashdata_sweep();
$this->_flashdata_mark();
$this->_tempdata_sweep();
}
}

0 comments on commit 7102c63

Please sign in to comment.