BaseCommandRunner.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. use Codeception\Util\Stub;
  3. use Codeception\Application;
  4. use Symfony\Component\Console\Tester\CommandTester;
  5. class BaseCommandRunner extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @var \Codeception\Command\Base
  9. */
  10. protected $command;
  11. public $filename = "";
  12. public $content = "";
  13. public $output = "";
  14. public $config = [];
  15. public $saved = [];
  16. protected $commandName = 'do:stuff';
  17. protected function execute($args = [], $isSuite = true)
  18. {
  19. $app = new Application();
  20. $app->add($this->command);
  21. $default = \Codeception\Configuration::$defaultConfig;
  22. $default['paths']['tests'] = __DIR__;
  23. $conf = $isSuite
  24. ? \Codeception\Configuration::suiteSettings('unit', $default)
  25. : $default;
  26. $this->config = array_merge($conf, $this->config);
  27. $commandTester = new CommandTester($app->find($this->commandName));
  28. $args['command'] = $this->commandName;
  29. $commandTester->execute($args, ['interactive' => false]);
  30. $this->output = $commandTester->getDisplay();
  31. }
  32. protected function makeCommand($className, $saved = true, $extraMethods = [])
  33. {
  34. if (!$this->config) {
  35. $this->config = [];
  36. }
  37. $self = $this;
  38. $mockedMethods = [
  39. 'createFile' => function ($file, $output) use ($self, $saved) {
  40. if (!$saved) {
  41. return false;
  42. }
  43. $self->filename = $file;
  44. $self->content = $output;
  45. $self->log[] = ['filename' => $file, 'content' => $output];
  46. $self->saved[$file] = $output;
  47. return true;
  48. },
  49. 'getGlobalConfig' => function () use ($self) {
  50. return $self->config;
  51. },
  52. 'getSuiteConfig' => function () use ($self) {
  53. return $self->config;
  54. },
  55. 'createDirectoryFor' => function ($path, $testName) {
  56. $path = rtrim($path, DIRECTORY_SEPARATOR);
  57. $testName = str_replace(['/', '\\'], [DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR], $testName);
  58. return pathinfo($path . DIRECTORY_SEPARATOR . $testName, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR;
  59. },
  60. 'getSuites' => function () {
  61. return ['shire'];
  62. },
  63. 'getApplication' => function () {
  64. return new \Codeception\Util\Maybe;
  65. }
  66. ];
  67. $mockedMethods = array_merge($mockedMethods, $extraMethods);
  68. $this->command = Stub::construct(
  69. $className,
  70. [$this->commandName],
  71. $mockedMethods
  72. );
  73. }
  74. protected function assertIsValidPhp($php)
  75. {
  76. $temp_file = tempnam(sys_get_temp_dir(), 'CodeceptionUnitTest');
  77. file_put_contents($temp_file, $php);
  78. exec('php -l ' . $temp_file, $output, $code);
  79. unlink($temp_file);
  80. $this->assertEquals(0, $code, $php);
  81. }
  82. }