PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Codeception/Command/GeneratePageObject.php

https://github.com/pmcjury/Codeception
PHP | 86 lines | 63 code | 15 blank | 8 comment | 2 complexity | 5b5d53885bc855268cedeabc77d3b79b MD5 | raw file
  1. <?php
  2. namespace Codeception\Command;
  3. use Codeception\Configuration;
  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. use Codeception\Lib\Generator\PageObject as PageObjectGenerator;
  10. /**
  11. * Generates PageObject. Can be generated either globally, or just for one suite.
  12. * If PageObject is generated globally it will act as UIMap, without any logic in it.
  13. *
  14. * * `codecept g:page Login`
  15. * * `codecept g:page Registration`
  16. * * `codecept g:page acceptance Login`
  17. */
  18. class GeneratePageObject 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, 'Either suite name or page object name)'),
  26. new InputArgument('page', InputArgument::OPTIONAL, 'Page name of pageobject to represent'),
  27. new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
  28. ));
  29. parent::configure();
  30. }
  31. public function getDescription() {
  32. return 'Generates empty PageObject class';
  33. }
  34. public function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. $suite = $input->getArgument('suite');
  37. $class = $input->getArgument('page');
  38. if (!$class) {
  39. $class = $suite;
  40. $suite = null;
  41. }
  42. $conf = $suite
  43. ? $this->getSuiteConfig($suite, $input->getOption('config'))
  44. : $this->getGlobalConfig($input->getOption('config'));
  45. $className = $this->getClassName($class);
  46. $filename = $suite
  47. ? $this->pathToSuitePageObject($conf, $className)
  48. : $this->pathToGlobalPageObject($conf, $className);
  49. $gen = new PageObjectGenerator($conf, $class);
  50. $res = $this->save($filename, $gen->produce());
  51. if (!$res) {
  52. $output->writeln("<error>PageObject $filename already exists</error>");
  53. exit;
  54. }
  55. $output->writeln("<info>PageObject was created in $filename</info>");
  56. }
  57. protected function pathToGlobalPageObject($config, $class)
  58. {
  59. $path = $this->buildPath(Configuration::projectDir().$config['paths']['tests'].'/_pages/', $class);
  60. $filename = $this->completeSuffix($class, 'Page');
  61. $this->introduceAutoloader(Configuration::projectDir().$config['paths']['tests'].DIRECTORY_SEPARATOR.$config['settings']['bootstrap'],'Page','_pages');
  62. return $path.$filename;
  63. }
  64. protected function pathToSuitePageObject($config, $class)
  65. {
  66. $path = $this->buildPath($config['path'].'/_pages/', $class);
  67. $filename = $this->completeSuffix($class, 'Page');
  68. $this->introduceAutoloader($config['path'].DIRECTORY_SEPARATOR.$config['bootstrap'],'Page','_pages');
  69. return $path.$filename;
  70. }
  71. }