protected function execute(InputInterface $input, OutputInterface $output) { $suite = $input->getArgument('suite'); $output->writeln('Warning: this command may affect your Helper classes'); $config = Configuration::config($input->getOption('config')); $suiteconf = Configuration::suiteSettings($suite, $config); $dispatcher = new EventDispatcher(); $suiteManager = new 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 = 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 Cept) { continue; } $analyzed++; $test->testCodecept(false); $scenario = $test->getScenario(); foreach ($scenario->getSteps() as $step) { if ($step->getName() == 'Comment') { continue; } $action = $step->getAction(); if (isset(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); $this->save($helper_file, $contents, true); $output->writeln("Action '{$action}' added to helper {$helper}"); $replaced++; } } $output->writeln("<info>Analysis finished. {$analyzed} tests analyzed. {$replaced} methods added</info>"); $output->writeln("Run the 'build' command to finish"); }
public function execute(InputInterface $input, OutputInterface $output) { $suite = ucfirst($input->getArgument('suite')); $actor = $input->getArgument('actor'); if ($this->containsInvalidCharacters($suite)) { $output->writeln("<error>Suite name '{$suite}' contains invalid characters. ([A-Za-z0-9_]).</error>"); return; } $config = \Codeception\Configuration::config($input->getOption('config')); if (!$actor) { $actor = $suite . $config['actor']; } $config['class_name'] = $actor; $dir = \Codeception\Configuration::testsDir(); if (file_exists($dir . $suite . '.suite.yml')) { throw new \Exception("Suite configuration file '{$suite}.suite.yml' already exists."); } $this->buildPath($dir . $suite . DIRECTORY_SEPARATOR, 'bootstrap.php'); // generate bootstrap $this->save($dir . $suite . DIRECTORY_SEPARATOR . 'bootstrap.php', "<?php\n// Here you can initialize variables that will be available to your tests\n", true); $actorName = $this->removeSuffix($actor, $config['actor']); // generate helper $this->save(\Codeception\Configuration::helpersDir() . $actorName . 'Helper.php', (new Helper($actorName, $config['namespace']))->produce()); $enabledModules = ['Cake\\Codeception\\Helper', 'App\\TestSuite\\Codeception\\' . $actorName . 'Helper']; if ('Unit' === $suite) { array_shift($enabledModules); } $conf = ['class_name' => $actorName . $config['actor'], 'modules' => ['enabled' => $enabledModules]]; $this->save($dir . $suite . '.suite.yml', Yaml::dump($conf, 2)); $output->writeln("<info>Suite {$suite} generated</info>"); }
protected function updateHelpers() { $helpers = Finder::create()->files()->name('*Helper.php')->in(\Codeception\Configuration::helpersDir()); foreach ($helpers as $helper_file) { $helper_body = file_get_contents($helper_file); $helper_body = preg_replace('~namespace Codeception\\\\Module;~', "namespace {$this->namespace}\\Codeception\\Module;", $helper_body); $this->save($helper_file, $helper_body, true); } }
public function testGuyWithSuffix() { $this->execute(array('suite' => 'shire', 'actor' => 'HobbitGuy')); $conf = \Symfony\Component\Yaml\Yaml::parse($this->content); $this->assertEquals('HobbitGuy', $conf['class_name']); $this->assertContains('HobbitHelper', $conf['modules']['enabled']); $helper = $this->log[1]; $this->assertEquals(\Codeception\Configuration::helpersDir() . 'HobbitHelper.php', $helper['filename']); $this->assertContains('class HobbitHelper extends \\Codeception\\Module', $helper['content']); }
public function execute(InputInterface $input, OutputInterface $output) { $name = ucfirst($input->getArgument('name')); $config = \Codeception\Configuration::config($input->getOption('config')); $file = \Codeception\Configuration::helpersDir() . "{$name}Helper.php"; $res = $this->save($file, (new Helper($name, $config['namespace']))->produce()); if ($res) { $output->writeln("<info>Helper {$file} created</info>"); } else { $output->writeln("<error>Error creating helper {$file}</error>"); } }
public function testBasic() { $this->execute(array('namespace' => 'MiddleEarth', '--force' => true)); $this->assertContains('adds namespaces to your Helper and Guy classes and Cepts', $this->output); $config = $this->log[0]; $this->assertContains('Config file updated', $this->output); $this->assertContains('namespace: MiddleEarth', $config['content']); $this->assertEquals(\Codeception\Configuration::projectDir() . 'codeception.yml', (string) $config['filename']); $helper = $this->log[1]; $this->assertContains('namespace MiddleEarth\\Codeception\\Module', $helper['content']); $this->assertRegExp('~' . \Codeception\Configuration::helpersDir() . '.*?Helper~', (string) $helper['filename']); // log[2] is helper, log[3] ... are helpers too $cept = $this->log[7]; $this->assertRegExp('~<?php use MiddleEarth\\\\.*Guy~', $cept['content']); $this->assertIsValidPhp($cept['content']); }
public function execute(InputInterface $input, OutputInterface $output) { $suite = lcfirst($input->getArgument('suite')); $actor = $input->getArgument('actor'); $config = \Codeception\Configuration::config($input->getOption('config')); if (!$actor) { $actor = ucfirst($suite) . $config['actor']; } $config['class_name'] = $actor; $dir = \Codeception\Configuration::testsDir(); if (file_exists($dir . $suite)) { throw new \Exception("Directory $suite already exists."); } if (file_exists($dir . $suite . '.suite.yml')) { throw new \Exception("Suite configuration file '$suite.suite.yml' already exists."); } $this->buildPath($dir . $suite . DIRECTORY_SEPARATOR, '_bootstrap.php'); // generate bootstrap $this->save($dir . $suite . DIRECTORY_SEPARATOR . '_bootstrap.php', "<?php\n// Here you can initialize variables that will for your tests\n", true ); $actorName = $this->removeSuffix($actor, $config['actor']); // generate helper $this->save( \Codeception\Configuration::helpersDir() . $actorName . 'Helper.php', (new Helper($actorName, $config['namespace']))->produce() ); $conf = array( 'class_name' => $actorName.$config['actor'], 'modules' => array( 'enabled' => array($actorName . 'Helper') ), ); $this->save($dir . $suite . '.suite.yml', Yaml::dump($conf, 2)); $output->writeln("<info>Suite $suite generated</info>"); }
protected static function autoloadHelpers() { Autoload::registerSuffix('Helper', \Codeception\Configuration::helpersDir()); }