“Redirecting” database calls

Published on and tagged with cakephp  php

For the test suite I wanted to “redirect” all database calls to the test database. But how to do that without touching the application code? Well, I could solve it with a little hack. I dynamically add a constructor to the code of database.php and execute that code with the eval function:

$config = file_get_contents(CONFIGS.'database.php');
$config = str_replace('<?php', '', $config);
$config = str_replace('?>', '', $config);
$config = str_replace('}', 'function __construct() {$this->default = $this->test;}}', $config);

It is not very elegant, but it does the job ;-)

© daniel hofstetter. Licensed under a Creative Commons License