PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Codeception/Command/GenerateTest.php

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