Permalink
Browse files

analyses added

  • Loading branch information...
1 parent 62a345e commit 65f0ec05326f3a6e2e965e31d3f07d793e7bbbae @DavertMik DavertMik committed Sep 14, 2012
View
@@ -30,6 +30,7 @@ use Symfony\Component\Console\Application,
$app = new Application('Codeception', Codeception\Codecept::VERSION);
$app->add(new Codeception\Command\Build('build'));
$app->add(new Codeception\Command\Run('run'));
+ $app->add(new Codeception\Command\Analyze('analyze'));
$app->add(new Codeception\Command\Bootstrap('bootstrap'));
$app->add(new Codeception\Command\GenerateCept('generate:cept'));
$app->add(new Codeception\Command\GenerateCest('generate:cest'));
View
@@ -21,6 +21,7 @@ use Symfony\Component\Console\Application,
$app = new Application('Codeception', Codeception\Codecept::VERSION);
$app->add(new Codeception\Command\Build('build'));
$app->add(new Codeception\Command\Run('run'));
+ $app->add(new Codeception\Command\Analyze('analyze'));
$app->add(new Codeception\Command\Bootstrap('bootstrap'));
$app->add(new Codeception\Command\GenerateCept('generate:cept'));
$app->add(new Codeception\Command\GenerateCest('generate:cest'));
@@ -112,6 +112,8 @@ public function __call($method, $arguments) {
if ($this->scenario->running()) {
$class = get_class($this);
throw new \RuntimeException("Call to undefined method $class::$method");
+ } else {
+ $this->scenario->action($method, $arguments);
}
}
}
@@ -0,0 +1,126 @@
+<?php
+namespace Codeception\Command;
+
+use Symfony\Component\Console\Application;
+use Symfony\Component\Console\Input\InputDefinition;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Yaml\Yaml;
+use \Symfony\Component\Console\Helper\DialogHelper;
+
+class Analyze extends Base
+{
+
+ protected $methodTemplate = <<<EOF
+
+ /**
+ * Stub method used in a test file.
+ *
+ * Expected Usage:
+ *
+ * ``` php
+ * %s
+ * ```
+ */
+ public function %s(%s) {
+ \$this->debugSection("Empty","This action is empty");
+ }
+
+}
+EOF;
+
+ public function getDescription() {
+ return 'Analyzes for non-existent methods and adds them to corresponding helper';
+ }
+
+ protected function configure()
+ {
+ $this->setDefinition(array(
+ new \Symfony\Component\Console\Input\InputArgument('suite', InputArgument::REQUIRED, 'suite to analyze'),
+ new \Symfony\Component\Console\Input\InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use specified config instead of default'),
+ ));
+ parent::configure();
+ }
+
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $suite = $input->getArgument('suite');
+
+ $output->writeln('Warning: this command may affect your Helper classes');
+
+ $config = \Codeception\Configuration::config($input->getOption('config'));
+ $suiteconf = \Codeception\Configuration::suiteSettings($suite, $config);
+
+ $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
+ $suiteManager = new \Codeception\SuiteManager($dispatcher, $suite, $suiteconf);
+
+ if (isset($suiteconf['bootstrap'])) {
+ if (file_exists($suiteconf['path'] . $suiteconf['bootstrap'])) {
+ require_once $suiteconf['path'] . $suiteconf['bootstrap'];
+ }
+ }
+
+ $suiteManager->loadTests();
+ $tests = $suiteManager->getSuite()->tests();
+
+ $dialog = $this->getHelperSet()->get('dialog');
+
+ $helper = $this->matchHelper();
+ if (!$helper) {
+ $output->writeln("<error>No helpers for suite $suite is defined. Can't append new methods</error>");
+ return;
+ }
+
+ if (!file_exists($helper_file = \Codeception\Configuration::helpersDir(). $helper.'.php')) {
+ $output->writeln("<error>Helper class $helper.php doesn't exist</error>");
+ return;
+ }
+
+ $replaced = 0;
+ $analyzed = 0;
+
+ foreach ($tests as $test) {
+ if (!($test instanceof \Codeception\TestCase\Cept)) continue;
+ $analyzed++;
+ $test->testCodecept(false);
+ $scenario = $test->getScenario();
+
+ foreach ($scenario->getSteps() as $step) {
+ if ($step->getName() == 'Comment') continue;
+ $action = $step->getAction();
+
+ if (isset(\Codeception\SuiteManager::$actions[$action])) continue;
+ if (!$dialog->askConfirmation($output, "<question>\nAction '$action' is missing. Do you want to add it to helper class?\n</question>\n", false)) continue;
+
+ $example = sprintf('$I->%s(%s);', $action, $step->getArguments(true));
+
+ $args = array_map(function ($a) { return '$arg'.$a; }, range(1, count($step->getArguments())));
+
+ $stub = sprintf($this->methodTemplate, $example, $action, implode(', ', $args));
+
+ $contents = file_get_contents($helper_file);
+ $contents = preg_replace('~}(?!.*})~ism', $stub, $contents);
+ file_put_contents($helper_file, $contents);
+
+ $output->writeln("Action '$action' added to helper $helper");
+ $replaced++;
+
+ }
+ }
+
+ $output->writeln("<info>Analisys finished. $analyzed tests analyzed. $replaced methods added</info>");
+ }
+
+ private function matchHelper()
+ {
+ $modules = array_keys(\Codeception\SuiteManager::$modules);
+ foreach ($modules as $module) {
+ if (preg_match('~Helper$~', $module)) {
+ return $module;
+ }
+ }
+ }
+}
@@ -1,103 +1,103 @@
-<?php
-namespace Codeception\Command;
-
-use Symfony\Component\Console\Application;
-use Symfony\Component\Console\Input\InputDefinition;
-use Symfony\Component\Console\Input\InputOption;
-use Symfony\Component\Console\Input\InputArgument;
-use Symfony\Component\Console\Input\InputInterface;
-use Symfony\Component\Console\Output\OutputInterface;
-
-
-class GenerateScenarios extends Base
-{
- protected function configure()
- {
- $this->setDefinition(array(
- new \Symfony\Component\Console\Input\InputArgument('suite', InputArgument::REQUIRED, 'suite from which tests should be generated'),
- new \Symfony\Component\Console\Input\InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use specified config instead of default'),
- new \Symfony\Component\Console\Input\InputOption('path', 'p', InputOption::VALUE_REQUIRED, 'Use specified path as destination instead of default'),
- new \Symfony\Component\Console\Input\InputOption('single-file', '', InputOption::VALUE_NONE, 'Render all scenarios to only one file'),
- new \Symfony\Component\Console\Input\InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Specify output format: html or text (default)'),
- new \Symfony\Component\Console\Input\InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use specified config instead of default'),
-
- ));
- parent::configure();
- }
-
- public function getDescription()
- {
- return 'Generates text representation for all scenarios';
- }
-
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $suite = $input->getArgument('suite');
-
- $config = \Codeception\Configuration::config($input->getOption('config'));
- $suiteconf = \Codeception\Configuration::suiteSettings($suite, $config);
-
- if ($input->getOption('path')) {
- $path = $input->getOption('path');
- } else {
- $path = \Codeception\Configuration::dataDir() . 'scenarios';
- }
-
- @mkdir($path);
-
- if (!is_writable($path)) {
- throw new \Codeception\Exception\Configuration("Path for logs is not writable. Please, set appropriate access mode for log path.");
- }
-
- $path = $path . DIRECTORY_SEPARATOR . $suite;
-
- if ($input->getOption('single-file')) {
- file_put_contents($path . '.txt', '');
- } else {
- @mkdir($path);
- }
-
- $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
-
- $suiteManager = new \Codeception\SuiteManager($dispatcher, $suite, $suiteconf);
-
- if (isset($suiteconf['bootstrap'])) {
- if (file_exists($suiteconf['path'] . $suiteconf['bootstrap'])) {
- require_once $suiteconf['path'] . $suiteconf['bootstrap'];
- }
- }
-
- $suiteManager->loadTests();
- $tests = $suiteManager->getSuite()->tests();
-
- if ($input->getOption('format')) {
- $format = $input->getOption('format');
- } else {
- $format = 'text';
- }
-
- foreach ($tests as $test) {
- if (!($test instanceof \Codeception\TestCase\Cept)) continue;
- $test->testCodecept(false);
- $features = $test->getScenarioText($format);
- $name = $this->underscore(substr($test->getFileName(), 0, -8));
-
- if ($input->getOption('single-file')) {
- file_put_contents($path . '.txt', $features . PHP_EOL, FILE_APPEND);
- $output->writeln("* $name rendered");
- } else {
- file_put_contents($path . DIRECTORY_SEPARATOR . $name . '.txt', $features);
- $output->writeln("* $name generated");
- }
- }
- }
-
- private function underscore($name)
- {
- $name = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1_\\2', $name);
- $name = preg_replace('/([a-z\d])([A-Z])/', '\\1_\\2', $name);
- $name = str_replace(array('/','\\'),array('.','.'), $name);
- return $name;
- }
-
-}
+<?php
+namespace Codeception\Command;
+
+use Symfony\Component\Console\Application;
+use Symfony\Component\Console\Input\InputDefinition;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+
+class GenerateScenarios extends Base
+{
+ protected function configure()
+ {
+ $this->setDefinition(array(
+ new \Symfony\Component\Console\Input\InputArgument('suite', InputArgument::REQUIRED, 'suite from which tests should be generated'),
+ new \Symfony\Component\Console\Input\InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use specified config instead of default'),
+ new \Symfony\Component\Console\Input\InputOption('path', 'p', InputOption::VALUE_REQUIRED, 'Use specified path as destination instead of default'),
+ new \Symfony\Component\Console\Input\InputOption('single-file', '', InputOption::VALUE_NONE, 'Render all scenarios to only one file'),
+ new \Symfony\Component\Console\Input\InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Specify output format: html or text (default)'),
+ new \Symfony\Component\Console\Input\InputOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use specified config instead of default'),
+
+ ));
+ parent::configure();
+ }
+
+ public function getDescription()
+ {
+ return 'Generates text representation for all scenarios';
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $suite = $input->getArgument('suite');
+
+ $config = \Codeception\Configuration::config($input->getOption('config'));
+ $suiteconf = \Codeception\Configuration::suiteSettings($suite, $config);
+
+ if ($input->getOption('path')) {
+ $path = $input->getOption('path');
+ } else {
+ $path = \Codeception\Configuration::dataDir() . 'scenarios';
+ }
+
+ @mkdir($path);
+
+ if (!is_writable($path)) {
+ throw new \Codeception\Exception\Configuration("Path for logs is not writable. Please, set appropriate access mode for log path.");
+ }
+
+ $path = $path . DIRECTORY_SEPARATOR . $suite;
+
+ if ($input->getOption('single-file')) {
+ file_put_contents($path . '.txt', '');
+ } else {
+ @mkdir($path);
+ }
+
+ $dispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
+
+ $suiteManager = new \Codeception\SuiteManager($dispatcher, $suite, $suiteconf);
+
+ if (isset($suiteconf['bootstrap'])) {
+ if (file_exists($suiteconf['path'] . $suiteconf['bootstrap'])) {
+ require_once $suiteconf['path'] . $suiteconf['bootstrap'];
+ }
+ }
+
+ $suiteManager->loadTests();
+ $tests = $suiteManager->getSuite()->tests();
+
+ if ($input->getOption('format')) {
+ $format = $input->getOption('format');
+ } else {
+ $format = 'text';
+ }
+
+ foreach ($tests as $test) {
+ if (!($test instanceof \Codeception\TestCase\Cept)) continue;
+ $test->testCodecept(false);
+ $features = $test->getScenarioText($format);
+ $name = $this->underscore(substr($test->getFileName(), 0, -8));
+
+ if ($input->getOption('single-file')) {
+ file_put_contents($path . '.txt', $features . PHP_EOL, FILE_APPEND);
+ $output->writeln("* $name rendered");
+ } else {
+ file_put_contents($path . DIRECTORY_SEPARATOR . $name . '.txt', $features);
+ $output->writeln("* $name generated");
+ }
+ }
+ }
+
+ private function underscore($name)
+ {
+ $name = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\\1_\\2', $name);
+ $name = preg_replace('/([a-z\d])([A-Z])/', '\\1_\\2', $name);
+ $name = str_replace(array('/','\\'),array('.','.'), $name);
+ return $name;
+ }
+
+}
Oops, something went wrong.

0 comments on commit 65f0ec0

Please sign in to comment.