Skip to content

Commit a449964

Browse files
author
Davert
committed
interactive console introduced
1 parent 792415a commit a449964

9 files changed

Lines changed: 147 additions & 377 deletions

File tree

codecept

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use Symfony\Component\Console\Application,
3030
$app->add(new Codeception\Command\Build('build'));
3131
$app->add(new Codeception\Command\Run('run'));
3232
$app->add(new Codeception\Command\Analyze('analyze'));
33+
$app->add(new Codeception\Command\Console('console'));
3334
$app->add(new Codeception\Command\Bootstrap('bootstrap'));
3435
$app->add(new Codeception\Command\GenerateCept('generate:cept'));
3536
$app->add(new Codeception\Command\GenerateCest('generate:cest'));

src/Codeception/Codecept.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Codecept
4848
'colors' => false,
4949
'log' => true,
5050
'coverage' => false,
51-
'defer-flush' => false,
51+
'defer-flush' => false
5252
);
5353

5454
public function __construct($options = array()) {
@@ -138,6 +138,14 @@ public function getOptions() {
138138
return $this->options;
139139
}
140140

141+
/**
142+
* @return EventDispatcher
143+
*/
144+
public function getDispatcher()
145+
{
146+
return $this->dispatcher;
147+
}
148+
141149
public static function checkLastVersion()
142150
{
143151
if (! class_exists('SimpleXMLElement')) {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
namespace Codeception\Command;
3+
4+
use Codeception\AbstractGuy;
5+
use Codeception\Event\Suite;
6+
use Codeception\Scenario;
7+
use Codeception\SuiteManager;
8+
use Codeception\TestCase\Cept;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
class Console extends Base {
15+
16+
protected $test;
17+
protected $codecept;
18+
protected $suite;
19+
20+
21+
protected function configure()
22+
{
23+
$this->setDefinition(array(
24+
new InputArgument('suite', InputArgument::REQUIRED, 'suite to be executed'),
25+
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
26+
new InputOption('colors', '', InputOption::VALUE_NONE, 'Use colors in output'),
27+
));
28+
parent::configure();
29+
}
30+
31+
public function getDescription()
32+
{
33+
return 'Launches interactive test console';
34+
}
35+
36+
public function execute(InputInterface $input, OutputInterface $output)
37+
{
38+
$suiteName = $input->getArgument('suite');
39+
$this->output = $output;
40+
41+
$config = \Codeception\Configuration::config($input->getOption('config'));
42+
$settings = \Codeception\Configuration::suiteSettings($suiteName, $config);
43+
44+
$options = $input->getOptions();
45+
$options['debug'] = true;
46+
$options['steps'] = true;
47+
48+
$this->codecept = new \Codeception\Codecept($options);
49+
$dispatcher = $this->codecept->getDispatcher();
50+
$suiteManager = new SuiteManager($dispatcher, $suiteName, $settings);
51+
$this->suite = $suiteManager->getSuite();
52+
$this->test = new Cept($dispatcher, array('name' => 'interactive', 'file' => 'interactive'));
53+
54+
$guy = $settings['class_name'];
55+
$scenario = new Scenario($this->test);
56+
$I = new $guy($scenario);
57+
58+
$this->listenToSignals();
59+
60+
$output->writeln("<info>Interactive console started for suite $suiteName</info>");
61+
$output->writeln("<info>Try Codeception commands without writing a test</info>");
62+
$output->writeln("<info>type 'exit' to leave console</info>");
63+
64+
$dispatcher->dispatch('suite.before', new Suite($this->suite, $this->codecept->getResult(), $settings));
65+
$dispatcher->dispatch('test.parsed', new \Codeception\Event\Test($this->test));
66+
$dispatcher->dispatch('test.before', new \Codeception\Event\Test($this->test));
67+
68+
$output->writeln("\n\n\$I = new {$settings['class_name']}(\$scenario);");
69+
$scenario->run();
70+
71+
$this->executeCommands($output, $I, $settings['bootstrap']);
72+
$dispatcher->dispatch('test.after', new \Codeception\Event\Test($this->test));
73+
$dispatcher->dispatch('suite.after', new Suite($this->suite));
74+
75+
$output->writeln("<info>Bye-bye!</info>");
76+
}
77+
78+
79+
protected function executeCommands(OutputInterface $output, AbstractGuy $I, $bootstrap)
80+
{
81+
$dialog = $this->getHelperSet()->get('dialog');
82+
83+
if (file_exists($bootstrap)) require $bootstrap;
84+
85+
do {
86+
$command = $dialog->ask($output, '$I->');
87+
if ($command == 'exit') return;
88+
if ($command == '') continue;
89+
try {
90+
eval("\$I->$command;");
91+
} catch (\PHPUnit_Framework_AssertionFailedError $fail) {
92+
$output->writeln("<error>fail</error> ".$fail->getMessage());
93+
} catch (\Exception $e) {
94+
$output->writeln("<error>error</error> ".$e->getMessage());
95+
}
96+
97+
} while (true);
98+
}
99+
100+
protected function listenToSignals()
101+
{
102+
if (function_exists('pcntl_signal')) {
103+
declare(ticks = 1);
104+
pcntl_signal(SIGINT, function () {});
105+
pcntl_signal(SIGTERM, function () {});
106+
}
107+
}
108+
109+
}

src/Codeception/Configuration.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,29 @@ public static function projectDir()
9999
public static function suiteSettings($suite, $config)
100100
{
101101
if (!in_array($suite, self::$suites)) throw new \Exception("Suite $suite was not loaded");
102+
103+
$defaults = array(
104+
'class_name' => 'NoGuy',
105+
'modules' => isset($config['modules']) ? $config['modules'] : array(),
106+
'bootstrap' => false,
107+
'suite_class' => '\PHPUnit_Framework_TestSuite',
108+
'colors' => true,
109+
'memory_limit' => '1024M',
110+
'path' => '',
111+
'error_level' => 'E_ALL & ~E_STRICT & ~E_DEPRECATED'
112+
);
113+
102114
$globalConf = $config['settings'];
103115
$globalConf['coverage'] = isset($config['coverage'])
104116
? $config['coverage']
105117
: array();
106118

107-
$moduleConf = array('modules' => isset($config['modules']) ? $config['modules'] : array());
108119
$path = $config['paths']['tests'];
109120

110121
$suiteConf = file_exists(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.yml") ? Yaml::parse(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.yml") : array();
111122
$suiteDistconf = file_exists(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.dist.yml") ? Yaml::parse(self::$dir . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . "$suite.suite.dist.yml") : array();
112123

113-
$settings = self::mergeConfigs($globalConf, $moduleConf);
124+
$settings = self::mergeConfigs($globalConf, $defaults);
114125
$settings = self::mergeConfigs($settings, $suiteDistconf);
115126
$settings = self::mergeConfigs($settings, $suiteConf);
116127

src/Codeception/SuiteManager.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,10 @@ class SuiteManager {
2727
protected $testcaseClass = 'Codeception\TestCase';
2828
protected $printer = null;
2929

30-
31-
protected $defaults = array(
32-
'class_name' => 'NoGuy',
33-
'modules' => array('enabled' => array(), 'config' => array()),
34-
'bootstrap' => false,
35-
'suite_class' => '\PHPUnit_Framework_TestSuite',
36-
'colors' => true,
37-
'memory_limit' => '1024M',
38-
'path' => '',
39-
'error_level' => 'E_ALL & ~E_STRICT & ~E_DEPRECATED'
40-
);
41-
4230
protected $settings = array();
4331

4432
public function __construct(EventDispatcher $dispatcher, $name, $settings) {
45-
$this->settings = array_merge($this->defaults, $settings);
33+
$this->settings = $settings;
4634
$this->dispatcher = $dispatcher;
4735
$this->suite = $this->createSuite($name);
4836
$this->path = $settings['path'];
@@ -196,12 +184,4 @@ public function loadTests()
196184
public function getSuite() {
197185
return $this->suite;
198186
}
199-
200-
protected function getClassesFromFile($file)
201-
{
202-
$loaded_classes = get_declared_classes();
203-
require_once $file;
204-
$extra_loaded_classes = get_declared_classes();
205-
return array_diff($extra_loaded_classes,$loaded_classes);
206-
}
207187
}

tests/cli/GeneratePhpUnitCept.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
$I = new CliGuy($scenario);
3-
$I->wantTo('generate sample Test');
3+
$I->wa1ntTo('generate sample Test');
44
$I->amInPath('tests/data/sandbox');
55
$I->executeCommand('generate:phpunit dummy Dummy');
66
$I->seeFileFound('DummyTest.php');

tests/log/cli-2013-02-28

Lines changed: 0 additions & 88 deletions
This file was deleted.

0 commit comments

Comments
 (0)