8ec727c1
曹明
初始化代码提交
|
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
<?php
namespace Codeception\Command;
use Codeception\Configuration;
use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Subscriber\Bootstrap as BootstrapLoader;
use Codeception\Subscriber\Console as ConsolePrinter;
use Codeception\SuiteManager;
use Codeception\Test\Interfaces\ScenarioDriven;
use Codeception\Test\Test;
use Codeception\Util\Maybe;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
/**
* Shows step by step execution process for scenario driven tests without actually running them.
*
* * `codecept dry-run acceptance`
* * `codecept dry-run acceptance MyCest`
* * `codecept dry-run acceptance checkout.feature`
* * `codecept dry-run tests/acceptance/MyCest.php`
*
*/
class DryRun extends Command
{
use Shared\Config;
use Shared\Style;
protected function configure()
{
$this->setDefinition(
[
new InputArgument('suite', InputArgument::REQUIRED, 'suite to scan for feature files'),
new InputArgument('test', InputArgument::OPTIONAL, 'tests to be loaded'),
]
);
parent::configure();
}
public function getDescription()
{
return 'Prints step-by-step scenario-driven test or a feature';
}
public function execute(InputInterface $input, OutputInterface $output)
{
$this->addStyles($output);
$suite = $input->getArgument('suite');
$test = $input->getArgument('test');
$config = $this->getGlobalConfig();
if (! Configuration::isEmpty() && ! $test && strpos($suite, $config['paths']['tests']) === 0) {
list(, $suite, $test) = $this->matchTestFromFilename($suite, $config['paths']['tests']);
}
$settings = $this->getSuiteConfig($suite);
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new ConsolePrinter([
'colors' => !$input->getOption('no-ansi'),
'steps' => true,
'verbosity' => OutputInterface::VERBOSITY_VERBOSE,
]));
$dispatcher->addSubscriber(new BootstrapLoader());
$suiteManager = new SuiteManager($dispatcher, $suite, $settings);
$moduleContainer = $suiteManager->getModuleContainer();
foreach (Configuration::modules($settings) as $module) {
$moduleContainer->mock($module, new Maybe());
}
$suiteManager->loadTests($test);
$tests = $suiteManager->getSuite()->tests();
$dispatcher->dispatch(Events::SUITE_INIT, new SuiteEvent($suiteManager->getSuite(), null, $settings));
$dispatcher->dispatch(Events::SUITE_BEFORE, new SuiteEvent($suiteManager->getSuite(), null, $settings));
foreach ($tests as $test) {
if ($test instanceof \PHPUnit_Framework_TestSuite_DataProvider) {
foreach ($test as $t) {
if ($t instanceof Test) {
$this->dryRunTest($output, $dispatcher, $t);
}
}
}
if ($test instanceof Test and $test instanceof ScenarioDriven) {
$this->dryRunTest($output, $dispatcher, $test);
}
}
$dispatcher->dispatch(Events::SUITE_AFTER, new SuiteEvent($suiteManager->getSuite()));
}
protected function matchTestFromFilename($filename, $tests_path)
{
$filename = str_replace(['//', '\/', '\\'], '/', $filename);
$res = preg_match("~^$tests_path/(.*?)/(.*)$~", $filename, $matches);
if (!$res) {
throw new \InvalidArgumentException("Test file can't be matched");
}
return $matches;
}
/**
* @param OutputInterface $output
* @param $dispatcher
* @param $test
*/
protected function dryRunTest(OutputInterface $output, EventDispatcher $dispatcher, Test $test)
{
$dispatcher->dispatch(Events::TEST_START, new TestEvent($test));
$dispatcher->dispatch(Events::TEST_BEFORE, new TestEvent($test));
try {
$test->test();
} catch (\Exception $e) {
}
$dispatcher->dispatch(Events::TEST_AFTER, new TestEvent($test));
$dispatcher->dispatch(Events::TEST_END, new TestEvent($test));
if ($test->getMetadata()->isBlocked()) {
$output->writeln('');
if ($skip = $test->getMetadata()->getSkip()) {
$output->writeln("<warning> SKIPPED </warning>" . $skip);
}
if ($incomplete = $test->getMetadata()->getIncomplete()) {
$output->writeln("<warning> INCOMPLETE </warning>" . $incomplete);
}
}
$output->writeln('');
}
}
|