/** * Define constants after including files. */ function prepareTemplate(Text_Template $template) { $template->setVar(array('constants' => '')); $template->setVar(array('wp_constants' => PHPUnit_Util_GlobalState::getConstantsAsString())); parent::prepareTemplate($template); }
/** * Performs custom preparations on the process isolation template. * * @param Text_Template $template * * @author Felix Gilcher <*****@*****.**> * @since 1.0.2 */ protected function prepareTemplate(Text_Template $template) { parent::prepareTemplate($template); // FIXME: workaround for php unit bug (https://github.com/sebastianbergmann/phpunit/pull/1338) $template->setVar(array('dataName' => "'.(" . var_export($this->myDataName, true) . ").'")); // FIXME: if we have full composer autoloading we can remove this // we need to restore the included files even without global state, since otherwise // the agavi test class files would be missing. // We can't write include()s directly since Agavi possibly get's bootstrapped later // in the process (but before the test instance is created) and if we'd load any // files which are being loaded by the bootstrap process chaos would ensue since // the bootstrap process uses plain include()s without _once $fileAutoloader = sprintf(' spl_autoload_register(function($name) { $classMap = %s; if(isset($classMap[$name])) { include($classMap[$name]); } }); ', var_export($this->getDependendClasses(), true)); // these constants are either used by out bootstrap wrapper script // (AGAVI_TESTING_ORIGINAL_PHPUNIT_BOOTSTRAP) or can be used by the user's // bootstrap script (AGAVI_TESTING_IN_SEPERATE_PROCESS) $constants = sprintf(' define("AGAVI_TESTING_IN_SEPERATE_PROCESS", true); define("AGAVI_TESTING_ORIGINAL_PHPUNIT_BOOTSTRAP", %s); ', var_export(isset($GLOBALS["__PHPUNIT_BOOTSTRAP"]) ? $GLOBALS["__PHPUNIT_BOOTSTRAP"] : null, true)); $isolatedTestSettings = array('environment' => $this->getIsolationEnvironment(), 'defaultContext' => $this->getIsolationDefaultContext(), 'clearCache' => $this->getClearCache(), 'bootstrap' => $this->doBootstrap()); $globals = sprintf(' $GLOBALS["AGAVI_TESTING_CONFIG"] = %s; $GLOBALS["AGAVI_TESTING_ISOLATED_TEST_SETTINGS"] = %s; $GLOBALS["__PHPUNIT_BOOTSTRAP"] = %s; ', var_export(AgaviConfig::toArray(), true), var_export($isolatedTestSettings, true), var_export(__DIR__ . '/scripts/IsolatedBootstrap.php', true)); if (!$this->preserveGlobalState) { $template->setVar(array('included_files' => $fileAutoloader, 'constants' => $constants, 'globals' => $globals)); } else { // HACK: oh great, text/template doesn't expose the already set variables, but we need to modify // them instead of overwriting them. So let's use the reflection to the rescue here. $reflected = new ReflectionObject($template); $property = $reflected->getProperty('values'); $property->setAccessible(true); $oldVars = $property->getValue($template); $template->setVar(array('included_files' => $fileAutoloader, 'constants' => $oldVars['constants'] . PHP_EOL . $constants, 'globals' => $oldVars['globals'] . PHP_EOL . $globals)); } }
/** * Define constants after requires/includes * * See http://kpayne.me/2012/07/02/phpunit-process-isolation-and-constant-already-defined/ * for more details * * @param \Text_Template $template */ public function prepareTemplate(\Text_Template $template) { $template->setVar(array('globals' => '$GLOBALS[\'__PHPUNIT_BOOTSTRAP\'] = \'' . $GLOBALS['__PHPUNIT_BOOTSTRAP'] . '\';')); parent::prepareTemplate($template); }
/** * Performs custom preparations on the process isolation template. * * @param Text_Template $template * * @author Felix Gilcher <*****@*****.**> * @since 1.0.2 */ protected function prepareTemplate(Text_Template $template) { parent::prepareTemplate($template); $vars = array('agavi_environment' => '', 'agavi_default_context' => '', 'agavi_clear_cache' => 'false'); if (null !== ($env = $this->getIsolationEnvironment())) { $vars['agavi_environment'] = $env; } if (null !== ($ctx = $this->getIsolationDefaultContext())) { $vars['agavi_default_context'] = $ctx; } if ($this->getClearCache()) { $vars['agavi_clear_cache'] = 'true'; // literal strings required for proper template rendering } $template->setVar($vars); $templateFile = $this->getTemplateFile(); if (null !== $templateFile) { $template->setFile($templateFile); } }