Wanted to share a little configuration I just finished making for some Peridot and Leo -based tests which work with Propel models.
A bunch of these tests write data to the database using the Propel models, and I need a clean slate DB before each test. I also need a clean slate HTTP session. Here’s some code ( from peridot.php ) that uses transactions to achieve that:
class MyScope extends Scope {
function __construct( $emitter ) {
$emitter->on( 'peridot.execute', function() {
$this->conn = Propel::getConnection();
} );
$emitter->on( 'test.start', function() {
self::destroySession();
// have to rollback and begin here.
// it was unreliable when I rolled back on test.end
$this->conn->rollback();
$this->conn->beginTransaction();
} );
}
private static function destroySession() {
session_start();
$_SESSION =[];
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
session_destroy();
}
}
return function($emitter) {
$gmsScope = new GmsScope( $emitter );
//add this scope into every suite
$emitter->on('suite.start', function($test) use ($gmsScope) {
$test->getScope()->peridotAddChildScope($gmsScope);
});
};