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