PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Codeception/Command/GeneratePhpUnit.php

https://github.com/pmcjury/Codeception
PHP | 65 lines | 41 code | 16 blank | 8 comment | 1 complexity | 6554667b9f19b9a7b8d5486864f0460c MD5 | raw file
  1. <?php
  2. namespace Codeception\Command;
  3. use Codeception\Lib\Generator\PhpUnit as PhpUnitGenerator;
  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 as in classical PHPUnit.
  11. *
  12. * * `codecept g:phpunit unit UserTest`
  13. * * `codecept g:phpunit unit User`
  14. * * `codecept g:phpunit unit "App\User`
  15. *
  16. */
  17. class GeneratePhpUnit extends Command
  18. {
  19. use Shared\FileSystem;
  20. use Shared\Config;
  21. protected function configure()
  22. {
  23. $this->setDefinition(array(
  24. new InputArgument('suite', InputArgument::REQUIRED, 'suite where tests will be put'),
  25. new InputArgument('class', InputArgument::REQUIRED, 'class name'),
  26. new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
  27. ));
  28. parent::configure();
  29. }
  30. public function getDescription() {
  31. return 'Generates empty PHPUnit test without Codeception additions';
  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. $path = $this->buildPath($config['path'], $class);
  39. $filename = $this->completeSuffix($this->getClassName($class), 'Test');
  40. $filename = $path.$filename;
  41. $gen = new PhpUnitGenerator($config, $class);
  42. $res = $this->save($filename, $gen->produce());
  43. if (!$res) {
  44. $output->writeln("<error>Test $filename already exists</error>");
  45. exit;
  46. }
  47. $output->writeln("<info>Test was created in $filename</info>");
  48. }
  49. }