currentDatabase(),0,5) != 'tmpdb') { //echo "Re-creating temp database... "; self::create_temp_db(); //echo "done.\n"; } // This code is a bit misplaced; we want some way of the whole session being reinitialised... Versioned::reading_stage(null); singleton('DataObject')->flushCache(); $dbadmin = new DatabaseAdmin(); $dbadmin->clearAllData(); $this->fixture = new YamlFixture($fixtureFile); $this->fixture->saveIntoDatabase(); } // Set up email $this->originalMailer = Email::mailer(); $this->mailer = new TestMailer(); Email::set_mailer($this->mailer); } /** * Array of */ protected $fixtureDictionary; /** * Get the ID of an object from the fixture. * @param $className The data class, as specified in your fixture file. Parent classes won't work * @param $identifier The identifier string, as provided in your fixture file */ protected function idFromFixture($className, $identifier) { if($this->fixture) return $this->fixture->idFromFixture($className, $identifier); else user_error("You've called \$this->objFromFixture() but you haven't specified static \$fixture_file.\n" . "Ensure that static \"\$fixture_file = 'module/tests/fixturefile.yml';\" is specified in your " .get_class($this). " class.", E_USER_WARNING); } /** * Return all of the IDs in the fixture of a particular class name. * @return A map of fixture-identifier => object-id */ protected function allFixtureIDs($className) { if($this->fixture) return $this->fixture->allFixtureIDs($className); else user_error("You've called \$this->objFromFixture() but you haven't specified static \$fixture_file.\n" . "Ensure that static \"\$fixture_file = 'module/tests/fixturefile.yml';\" is specified in your " .get_class($this). " class.", E_USER_WARNING); } /** * Get an object from the fixture. * @param $className The data class, as specified in your fixture file. Parent classes won't work * @param $identifier The identifier string, as provided in your fixture file */ protected function objFromFixture($className, $identifier) { if($this->fixture) return $this->fixture->objFromFixture($className, $identifier); else user_error("You've called \$this->objFromFixture() but you haven't specified static \$fixture_file.\n" . "Ensure that static \"\$fixture_file = 'module/tests/fixturefile.yml';\" is specified in your " .get_class($this). " class.", E_USER_WARNING); } /** * Load a YAML fixture file into the database. * Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture * @param $fixtureFile The location of the .yml fixture file, relative to the site base dir */ function loadFixture($fixtureFile) { $parser = new Spyc(); $fixtureContent = $parser->load(Director::baseFolder().'/'.$fixtureFile); $this->fixture = new YamlFixture($fixtureFile); $this->fixture->saveIntoDatabase(); } function tearDown() { // Restore email configuration Email::set_mailer($this->originalMailer); $this->originalMailer = null; $this->mailer = null; } /** * Clear the log of emails sent */ function clearEmails() { return $this->mailer->clearEmails(); } /** * Search for an email that was sent. * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. * @param $to * @param $from * @param $subject * @param $content * @return An array containing the keys: 'type','to','from','subject','content', 'plainContent','attachedFiles','customHeaders','htmlContent',inlineImages' */ function findEmail($to, $from = null, $subject = null, $content = null) { return $this->mailer->findEmail($to, $from, $subject, $content); } /** * Assert that the matching email was sent since the last call to clearEmails() * All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression. * @param $to * @param $from * @param $subject * @param $content * @return An array containing the keys: 'type','to','from','subject','content', 'plainContent','attachedFiles','customHeaders','htmlContent',inlineImages' */ function assertEmailSent($to, $from = null, $subject = null, $content = null) { // To do - this needs to be turned into a "real" PHPUnit ass if(!$this->findEmail($to, $from, $subject, $content)) { $infoParts = ""; $withParts = array(); if($to) $infoParts .= " to '$to'"; if($from) $infoParts .= " from '$from'"; if($subject) $withParts[] = "subject '$subject'"; if($content) $withParts[] = "content '$content'"; if($withParts) $infoParts .= " with " . implode(" and ", $withParts); throw new PHPUnit_Framework_AssertionFailedError( "Failed asserting that an email was sent$infoParts." ); } } /** * Returns true if we are currently using a temporary database */ static function using_temp_db() { $dbConn = DB::getConn(); return $dbConn && (substr($dbConn->currentDatabase(),0,5) == 'tmpdb'); } static function kill_temp_db() { // Delete our temporary database if(self::using_temp_db()) { $dbConn = DB::getConn(); $dbName = $dbConn->currentDatabase(); if($dbName && DB::query("SHOW DATABASES LIKE '$dbName'")->value()) { // echo "Deleted temp database " . $dbConn->currentDatabase() . "\n"; $dbConn->dropDatabase(); } } } static function create_temp_db() { // Create a temporary database $dbConn = DB::getConn(); $dbname = 'tmpdb' . rand(1000000,9999999); while(!$dbname || $dbConn->databaseExists($dbname)) { $dbname = 'tmpdb' . rand(1000000,9999999); } $dbConn->selectDatabase($dbname); $dbConn->createDatabase(); $dbadmin = new DatabaseAdmin(); $dbadmin->doBuild(true, false, true); return $dbname; } } ?>