Repository navigation
Expand file tree
/
Copy pathConsole.php
More file actions
131 lines (109 loc) · 4.59 KB
/
Copy pathConsole.php
File metadata and controls
131 lines (109 loc) · 4.59 KB
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
<?php
namespace Codeception\Command;
use Codeception\Actor;
use Codeception\Codecept;
use Codeception\Events;
use Codeception\Configuration;
use Codeception\Event\SuiteEvent;
use Codeception\Event\TestEvent;
use Codeception\Scenario;
use Codeception\SuiteManager;
use Codeception\TestCase\Cept;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Try to execute test commands in run-time. You may try commands before writing the test.
*
* `codecept console acceptance` - starts acceptance suite environment. If you use WebDriver you can manipulate browser with Codeception commands.
*/
class Console extends Base
{
protected $test;
protected $codecept;
protected $suite;
protected function configure()
{
$this->setDefinition(
array(
new InputArgument('suite', InputArgument::REQUIRED, 'suite to be executed'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
new InputOption('colors', '', InputOption::VALUE_NONE, 'Use colors in output'),
)
);
parent::configure();
}
public function getDescription()
{
return 'Launches interactive test console';
}
public function execute(InputInterface $input, OutputInterface $output)
{
$suiteName = $input->getArgument('suite');
$this->output = $output;
$config = Configuration::config($input->getOption('config'));
$settings = Configuration::suiteSettings($suiteName, $config);
$options = $input->getOptions();
$options['debug'] = true;
$options['steps'] = true;
$this->codecept = new Codecept($options);
$dispatcher = $this->codecept->getDispatcher();
$suiteManager = new SuiteManager($dispatcher, $suiteName, $settings);
$this->suite = $suiteManager->getSuite();
$this->test = new Cept($dispatcher, array('name' => 'interactive', 'file' => 'interactive'));
$scenario = new Scenario($this->test);
$guy = $settings['class_name'];
$I = new $guy($scenario);
$this->listenToSignals();
$output->writeln("<info>Interactive console started for suite $suiteName</info>");
$output->writeln("<info>Try Codeception commands without writing a test</info>");
$output->writeln("<info>type 'exit' to leave console</info>");
$output->writeln("<info>type 'actions' to see all available actions for this suite</info>");
$suiteEvent = new SuiteEvent($this->suite, $this->codecept->getResult(), $settings);
$dispatcher->dispatch(Events::SUITE_BEFORE, $suiteEvent);
$dispatcher->dispatch(Events::TEST_PARSED, new TestEvent($this->test));
$dispatcher->dispatch(Events::TEST_BEFORE, new TestEvent($this->test));
$output->writeln("\n\n\$I = new {$settings['class_name']}(\$scenario);");
$scenario->run();
$this->executeCommands($output, $I, $settings['bootstrap']);
$dispatcher->dispatch(Events::TEST_AFTER, new TestEvent($this->test));
$dispatcher->dispatch(Events::SUITE_AFTER, new SuiteEvent($this->suite));
$output->writeln("<info>Bye-bye!</info>");
}
protected function executeCommands(OutputInterface $output, Actor $I, $bootstrap)
{
$dialog = $this->getHelperSet()->get('dialog');
if (file_exists($bootstrap)) {
require $bootstrap;
}
do {
$command = $dialog->ask($output, '$I->', null, array_keys(SuiteManager::$actions));
if ($command == 'actions') {
$output->writeln("<info>" . implode(' ', array_keys(SuiteManager::$actions)));
continue;
};
if ($command == 'exit') {
return;
}
if ($command == '') {
continue;
}
try {
eval("\$I->$command;");
} catch (\PHPUnit_Framework_AssertionFailedError $fail) {
$output->writeln("<error>fail</error> " . $fail->getMessage());
} catch (\Exception $e) {
$output->writeln("<error>error</error> " . $e->getMessage());
}
} while (true);
}
protected function listenToSignals()
{
if (function_exists('pcntl_signal')) {
declare(ticks = 1);
pcntl_signal(SIGINT, SIG_IGN);
pcntl_signal(SIGTERM, SIG_IGN);
}
}
}