I need to test a PHP Web-Application. The Application is one block of procedural code (in index.php), controlled by session variables. Determined by control-flow, other files are included that are procedural blocks of php, too (xy.inc) and modify session variables. E.g.:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: text/html; charset=utf-8");
session_name("some_name");
session_start();
$_SESSION['some_key'] = 'some_value';
if($_SESSION['key1'] == 1){
//include some file that in the end sets $_SESSION['key1'] = 0
}
else if($_SESSION['key2'] == 1){
//Do something
$_SESSION['key2'] = 0;
$_SESSION['keyX'] = 'someOtherControlValue';
}
//....Lots of code
$SESSION['whatever'] = 'justAnotherValue';
//..more code, more $_SESSION checks and so on
Refactoring is no option (at the moment?), so I'm just trying to get at least some basic tests running, without interfering with the coder's "programming style".
Is there a way of accessing (and modifying) the $_SESSION properties in acceptance tests? I want to use Codeception for my tests (but I'm open for alternatives).
I want to achieve something like (pseudo)
// setup test db etc.
get('/');
assert(title == 'the title');
// change some control values
$_SESSION['myDatabase'] = 'myTestdb';
$_SESSION['SomeKeyForFuctionIWantTest'] = 'myValue';
get('/');
assert($_SESSION['SomeKeyForFuctionIWantTest'] == 'myValue');
assert(getSomeElementCreatedByFunction() != null);
assert(somethingIsInTheDatabase);
I have little experience in php and session-management (and I'm coding in OOP), can anyone help me out?
side-note: There is no framework, mvc or anything else involved
I found a relatively simple and very dirty hack for this.
When testing I'm not calling '/'(index.php) directly, but a test page. The test page sets my variables as needed (maybe need a "safe" way to inject my test vars here..).
# defined in /test.php - otherwise change path in include index
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type: text/html; charset=utf-8");
session_name("my_session_name");
session_start();
$_SESSION['TESTME'] = 'bla';
session_write_close();
session_destroy();
include 'index.php';
?>
In index.php I can see $_SESSION['TESTME'] == 'bla'.
Fight fire with fire I guess..