| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341 |
- #!/usr/bin/env php
- <?php
- /**
- * generates a config unit test class
- *
- * This generator is meant to test all possible configuration combinations
- * without having to write endless amounts of code manually.
- *
- * DANGER: Too many options/settings and too high max iteration setting may trigger
- * a fork bomb. Please save your work before executing this script.
- */
- include 'bootstrap.php';
- $options = array(
- 'main/opendiscussion' => array(
- array(
- 'setting' => true,
- 'tests' => array(
- array(
- 'type' => 'NotTag',
- 'args' => array(
- array(
- 'id' => 'opendiscussion',
- 'attributes' => array(
- 'disabled' => 'disabled',
- ),
- ),
- '$content',
- 'outputs enabled discussion correctly'
- ),
- ),
- ),
- 'affects' => array('view')
- ), array(
- 'setting' => false,
- 'tests' => array(
- array(
- 'type' => 'Tag',
- 'args' => array(
- array(
- 'id' => 'opendiscussion',
- 'attributes' => array(
- 'disabled' => 'disabled',
- ),
- ),
- '$content',
- 'outputs disabled discussion correctly'
- ),
- ),
- ),
- 'affects' => array('view')
- ),
- ),
- 'main/syntaxhighlighting' => array(
- array(
- 'setting' => true,
- 'tests' => array(
- array(
- 'type' => 'Tag',
- 'args' => array(
- array(
- 'tag' => 'link',
- 'attributes' => array(
- 'type' => 'text/css',
- 'rel' => 'stylesheet',
- 'href' => 'regexp:#css/prettify/prettify.css#',
- ),
- ),
- '$content',
- 'outputs stylesheet correctly',
- ),
- ), array(
- 'type' => 'Tag',
- 'args' => array(
- array(
- 'tag' => 'script',
- 'attributes' => array(
- 'type' => 'text/javascript',
- 'src' => 'regexp:#js/prettify.js#'
- ),
- ),
- '$content',
- 'outputs javascript correctly',
- ),
- ),
- ),
- 'affects' => array('view'),
- ), array(
- 'setting' => false,
- 'tests' => array(
- array(
- 'type' => 'NotTag',
- 'args' => array(
- array(
- 'tag' => 'link',
- 'attributes' => array(
- 'type' => 'text/css',
- 'rel' => 'stylesheet',
- 'href' => 'regexp:#css/prettify/prettify.css#',
- ),
- ),
- '$content',
- 'removes stylesheet correctly',
- ),
- ), array(
- 'type' => 'NotTag',
- 'args' => array(
- array(
- 'tag' => 'script',
- 'attributes' => array(
- 'type' => 'text/javascript',
- 'src' => 'regexp:#js/prettify.js#',
- ),
- ),
- '$content',
- 'removes javascript correctly',
- ),
- ),
- ),
- 'affects' => array('view'),
- ),
- ),
- );
- // endless loop protection, since we're working with a recursive function, creating factorial configurations
- define('MAX_ITERATIONS', 1000);
- // generate all possible combinations of configurations: (option * settings)!
- $i = 0;
- $configurations = array(array('options' => array(), 'tests' => array(), 'affects' => array()));
- $configurations = getConfigurations($options, $configurations, $i);
- $defaultOptions = parse_ini_file(PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini', true);
- $code = <<<'EOT'
- <?php
- /**
- * DO NOT EDIT: This file is automatically generated by configGenerator.php
- */
- class configTest extends PHPUnit_Framework_TestCase
- {
- private static $pasteid = '501f02e9eeb8bcec';
- private static $paste = array(
- 'data' => '{"iv":"EN39/wd5Nk8HAiSG2K5AsQ","v":1,"iter":1000,"ks":128,"ts":64,"mode":"ccm","adata":"","cipher":"aes","salt":"QKN1DBXe5PI","ct":"8hA83xDdXjD7K2qfmw5NdA"}',
- 'meta' => array(
- 'postdate' => 1344803344,
- 'opendiscussion' => true,
- ),
- );
- private $_model;
- private $_conf;
- public function setUp()
- {
- /* Setup Routine */
- $this->_conf = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
- if (!is_file($this->_conf . '.bak') && is_file($this->_conf))
- rename($this->_conf, $this->_conf . '.bak');
- $this->_model = zerobin_data::getInstance(array('dir' => PATH . 'data'));
- serversalt::setPath(PATH . 'data');
- $this->reset();
- }
- public function tearDown()
- {
- /* Tear Down Routine */
- rename($this->_conf . '.bak', $this->_conf);
- }
- public function reset($configuration = array())
- {
- $_POST = array();
- $_GET = array();
- $_SERVER = array();
- if ($this->_model->exists(self::$pasteid))
- $this->_model->delete(self::$pasteid);
- // generate configuration file
- if (count($configuration)) {
- @unlink($this->_conf);
- $c = fopen($this->_conf, 'a');
- foreach ($configuration as $section => $options) {
- fwrite($c, "[$section]" . PHP_EOL);
- foreach($options as $option => $setting) {
- if (is_string($setting)) {
- $setting = '"' . $setting . '"';
- } else {
- $setting = var_export($setting, true);
- }
- fwrite($c, "$option = $setting" . PHP_EOL);
- }
- fwrite($c, PHP_EOL);
- }
- fclose($c);
- }
- }
- EOT;
- foreach ($configurations as $key => $conf) {
- $options = var_export_min(array_replace_recursive($defaultOptions, $conf['options']), true);
- foreach ($conf['affects'] as $step) {
- $step = ucfirst($step);
- switch ($step) {
- case 'Create':
- // @todo
- continue;
- break;
- case 'Read':
- // @todo
- continue;
- break;
- case 'Delete':
- // @todo
- continue;
- break;
- // view
- default:
- $code .= <<<EOT
- /**
- * @runInSeparateProcess
- */
- public function test$step$key()
- {
- \$this->reset($options);
- ob_start();
- new zerobin;
- \$content = ob_get_contents();
- EOT;
- }
- foreach ($conf['tests'] as $tests) {
- foreach ($tests as $test) {
- $args = array();
- foreach ($test['args'] as $arg) {
- if ($arg == '$content') {
- $args[] = $arg;
- } else {
- $args[] = var_export_min($arg, true);
- }
- }
- $type = $test['type'];
- $args = implode(', ', $args);
- $code .= <<<EOT
- \$this->assert$type($args);
- EOT;
- }
- }
- $code .= <<<'EOT'
- }
- EOT;
- }
- }
- $code .= '}' . PHP_EOL;
- file_put_contents('configuration.php', $code);
- // recursive factorial function
- function getConfigurations(&$options, &$configurations, &$i) {
- if (++$i > MAX_ITERATIONS) {
- echo "max iterations reached, stopping", PHP_EOL;
- return $configurations;
- }
- echo "getConfigurations: iteration $i", PHP_EOL;
- $continue = list($path, $settings) = each($options);
- if ($continue === false) {
- return $configurations;
- }
- list($section, $option) = explode('/', $path);
- for ($c = 0, $max = count($configurations); $c < $max; ++$c) {
- if (!array_key_exists($section, $configurations[$c]['options'])) {
- $configurations[$c]['options'][$section] = array();
- }
- if (count($settings) == 0) {
- throw new Exception("Check your \$options: option $option has no settings!");
- }
- // set the first setting in the original configuration
- $setting = current($settings);
- addSetting($configurations[$c], $setting, $section, $option, $i);
- // create clones for each of the other settings
- while ($setting = next($settings)) {
- $clone = $configurations[$c];
- $configurations[] = addSetting($clone, $setting, $section, $option, $i);
- }
- reset($settings);
- }
- return getConfigurations($options, $configurations, $i);
- }
- function addSetting(&$configuration, &$setting, &$section, &$option, &$i) {
- if (++$i > MAX_ITERATIONS) {
- echo "max iterations reached, stopping", PHP_EOL;
- return $configuration;
- }
- echo "addSetting: iteration $i", PHP_EOL;
- if (
- array_key_exists($option, $configuration['options'][$section]) &&
- $configuration['options'][$section][$option] === $setting['setting']
- ) {
- $val = var_export_min($setting['setting'], true);
- throw new Exception("Endless loop or error in options detected: option '$option' already exists with setting '$val' in one of the configurations!");
- }
- $configuration['options'][$section][$option] = $setting['setting'];
- $configuration['tests'][$option] = $setting['tests'];
- foreach ($setting['affects'] as $affects) {
- if (!in_array($affects, $configuration['affects'])) {
- $configuration['affects'][] = $affects;
- }
- }
- return $configuration;
- }
- // by linus@flowingcreativity.net via php.net
- function var_export_min($var, $return = false) {
- if (is_array($var)) {
- $toImplode = array();
- foreach ($var as $key => $value) {
- $toImplode[] = var_export($key, true).' => '.var_export_min($value, true);
- }
- $code = 'array('.implode(', ', $toImplode).')';
- if ($return) {
- return $code;
- } else {
- echo $code;
- }
- } else {
- return var_export($var, $return);
- }
- }
|