PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Codeception/Command/GenerateCest.php

https://github.com/pmcjury/Codeception
PHP | 63 lines | 45 code | 9 blank | 9 comment | 2 complexity | ea2532eb8516f8f85edeb9d0fc90a568 MD5 | raw file
  1. <?php
  2. namespace Codeception\Command;
  3. use Codeception\Lib\Generator\Cest as CestGenerator;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputOption;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. /**
  10. * Generates Cest (scenario-driven object-oriented test) file:
  11. *
  12. * * `codecept generate:cest suite Login`
  13. * * `codecept g:cest suite subdir/subdir/testnameCest.php`
  14. * * `codecept g:cest suite LoginCest -c path/to/project`
  15. * * `codecept g:cest "App\Login"`
  16. *
  17. */
  18. class GenerateCest extends Command
  19. {
  20. use Shared\FileSystem;
  21. use Shared\Config;
  22. protected function configure()
  23. {
  24. $this->setDefinition(array(
  25. new InputArgument('suite', InputArgument::REQUIRED, 'suite where tests will be put'),
  26. new InputArgument('class', InputArgument::REQUIRED, 'test name'),
  27. new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
  28. ));
  29. }
  30. public function getDescription() {
  31. return 'Generates empty Cest file in suite';
  32. }
  33. public function execute(InputInterface $input, OutputInterface $output)
  34. {
  35. $suite = $input->getArgument('suite');
  36. $class = $input->getArgument('class');
  37. $config = $this->getSuiteConfig($suite, $input->getOption('config'));
  38. $className = $this->getClassName($class);
  39. $path = $this->buildPath($config['path'], $class);
  40. $filename = $this->completeSuffix($className, 'Cest');
  41. $filename = $path.$filename;
  42. if (file_exists($filename)) {
  43. $output->writeln("<error>Test $filename already exists</error>");
  44. return;
  45. }
  46. $gen = new CestGenerator($class, $config);
  47. $res = $this->save($filename, $gen->produce());
  48. if (!$res) {
  49. $output->writeln("<error>Test $filename already exists</error>");
  50. return;
  51. }
  52. $output->writeln("<info>Test was created in $filename</info>");
  53. }
  54. }