Skip to content

Commit

Permalink
Code cleanup (PSR-2) and some refactoring
Browse files Browse the repository at this point in the history
- Basic syntax checking and cleanup
- Changed code for a little more consistency
  • Loading branch information
zbateson committed Jun 22, 2015
1 parent a428062 commit f11bfe9
Show file tree
Hide file tree
Showing 186 changed files with 860 additions and 817 deletions.
6 changes: 4 additions & 2 deletions src/Codeception/Actor.php
Expand Up @@ -3,6 +3,8 @@

use Codeception\Lib\Actor\Shared\Comment;
use Codeception\Lib\Actor\Shared\Friend;
use Codeception\Scenario;
use Codeception\Step\Executor;

abstract class Actor
{
Expand All @@ -14,7 +16,7 @@ abstract class Actor
*/
protected $scenario;

public function __construct(\Codeception\Scenario $scenario)
public function __construct(Scenario $scenario)
{
$this->scenario = $scenario;
$this->scenario->stopIfBlocked();
Expand Down Expand Up @@ -51,7 +53,7 @@ public function __call($method, $arguments)
*/
public function execute($callable)
{
$this->scenario->addStep(new \Codeception\Step\Executor($callable, []));
$this->scenario->addStep(new Executor($callable, []));
$callable();
return $this;
}
Expand Down
11 changes: 8 additions & 3 deletions src/Codeception/Codecept.php
@@ -1,7 +1,7 @@
<?php
namespace Codeception;

use Codeception\Exception\ConfigurationException as ConfigurationException;
use Codeception\Exception\ConfigurationException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -102,15 +102,20 @@ protected function loadExtensions($options)
$config = Configuration::config();
foreach ($config['extensions']['enabled'] as $extensionClass) {
if (!class_exists($extensionClass)) {
throw new ConfigurationException("Class `$extensionClass` is not defined. Autoload it or include into '_bootstrap.php' file of 'tests' directory");
throw new ConfigurationException(
"Class `$extensionClass` is not defined. Autoload it or include into "
. "'_bootstrap.php' file of 'tests' directory"
);
}
$extensionConfig = isset($config['extensions']['config'][$extensionClass])
? $config['extensions']['config'][$extensionClass]
: [];

$extension = new $extensionClass($extensionConfig, $options);
if (!$extension instanceof EventSubscriberInterface) {
throw new ConfigurationException("Class $extensionClass is not an EventListener. Please create it as Extension or Group class.");
throw new ConfigurationException(
"Class $extensionClass is not an EventListener. Please create it as Extension or Group class."
);
}
$this->extensions[] = $extension;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Codeception/Command/Bootstrap.php
@@ -1,5 +1,4 @@
<?php

namespace Codeception\Command;

use Codeception\Lib\Generator\Helper;
Expand Down Expand Up @@ -239,7 +238,10 @@ protected function createDirs()
@mkdir($this->dataDir);
@mkdir($this->supportDir);
@mkdir($this->envsDir);
file_put_contents($this->dataDir . '/dump.sql', '/* Replace this file with actual dump of your database */');
file_put_contents(
$this->dataDir . '/dump.sql',
'/* Replace this file with actual dump of your database */'
);
}

}
85 changes: 54 additions & 31 deletions src/Codeception/Command/Build.php
Expand Up @@ -36,55 +36,78 @@ public function getDescription()

protected function configure()
{
$this->setDefinition(
[
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]
);
$this->setDefinition([
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->buildActorsForConfig($input->getOption('config'));
}

protected function buildActorsForConfig($configFile)
private function buildActor(array $settings)
{
$config = $this->getGlobalConfig($configFile);
$suites = $this->getSuites($configFile);

$path = pathinfo($configFile);
$dir = isset($path['dirname']) ? $path['dirname'] : getcwd();
$actorGenerator = new ActorGenerator($settings);
$this->output->writeln(
'<info>' . Configuration::config()['namespace'] . '\\' . $actorGenerator->getActorName()
. "</info> includes modules: " . implode(', ', $actorGenerator->getModules())
);

$content = $actorGenerator->produce();

foreach ($config['include'] as $subConfig) {
$this->output->writeln("<comment>Included Configuration: $subConfig</comment>");
$this->buildActorsForConfig($dir . DIRECTORY_SEPARATOR . $subConfig);
}
$file = $this->buildPath(
Configuration::supportDir(),
$settings['class_name']) . $this->getClassName($settings['class_name']
);
$file .= '.php';
return $this->save($file, $content);
}

private function buildActions(array $settings)
{
$actionsGenerator = new ActionsGenerator($settings);
$this->output->writeln(
" -> {$settings['class_name']}Actions.php generated successfully. "
. $actionsGenerator->getNumMethods() . " methods added"
);

$content = $actionsGenerator->produce();

$file = $this->buildPath(Configuration::supportDir() . '_generated', $settings['class_name']);
$file .= $this->getClassName($settings['class_name']) . 'Actions.php';
return $this->save($file, $content, true);
}

private function buildSuiteActors($configFile)
{
$suites = $this->getSuites($configFile);
if (!empty($suites)) {
$this->output->writeln("<info>Building Actor classes for suites: " . implode(', ', $suites) . '</info>');
}
foreach ($suites as $suite) {
$settings = $this->getSuiteConfig($suite, $configFile);
$actionsGenerator = new ActionsGenerator($settings);
$contents = $actionsGenerator->produce();

$actorGenerator = new ActorGenerator($settings);
$file = $this->buildPath(Configuration::supportDir() . '_generated', $settings['class_name']) . $this->getClassName($settings['class_name']) . 'Actions.php';
$this->save($file, $contents, true);

$this->output->writeln('<info>' . Configuration::config()['namespace'] . '\\' . $actorGenerator->getActorName() . "</info> includes modules: " . implode(', ', $actorGenerator->getModules()));
$this->output->writeln(" -> {$settings['class_name']}Actions.php generated successfully. " . $actionsGenerator->getNumMethods() . " methods added");

$contents = $actorGenerator->produce();

$file = $this->buildPath(Configuration::supportDir(), $settings['class_name']) . $this->getClassName($settings['class_name']) . '.php';
if ($this->save($file, $contents)) {
$actorBuilt = $this->buildActor($settings);
$this->buildActions($settings);

if ($actorBuilt) {
$this->output->writeln("{$settings['class_name']}.php created.");
}

}
}

protected function buildActorsForConfig($configFile)
{
$config = $this->getGlobalConfig($configFile);

$path = pathinfo($configFile);
$dir = isset($path['dirname']) ? $path['dirname'] : getcwd();

foreach ($config['include'] as $subConfig) {
$this->output->writeln("<comment>Included Configuration: $subConfig</comment>");
$this->buildActorsForConfig($dir . DIRECTORY_SEPARATOR . $subConfig);
}
$this->buildSuiteActors($configFile);
}
}
9 changes: 3 additions & 6 deletions src/Codeception/Command/Clean.php
@@ -1,7 +1,6 @@
<?php
namespace Codeception\Command;


use Codeception\Configuration;
use Codeception\Util\FileSystem;
use Symfony\Component\Console\Command\Command;
Expand All @@ -27,11 +26,9 @@ public function getDescription()

protected function configure()
{
$this->setDefinition(
[
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]
);
$this->setDefinition([
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]);
parent::configure();
}

Expand Down
27 changes: 12 additions & 15 deletions src/Codeception/Command/Console.php
@@ -1,5 +1,4 @@
<?php

namespace Codeception\Command;

use Codeception\Codecept;
Expand All @@ -8,8 +7,6 @@
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Lib\Console\Output;
use Codeception\Lib\Di;
use Codeception\Lib\ModuleContainer;
use Codeception\Scenario;
use Codeception\SuiteManager;
use Codeception\TestCase\Cept;
Expand Down Expand Up @@ -37,13 +34,11 @@ class Console extends Command

protected function configure()
{
$this->setDefinition(
[
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'),
]
);
$this->setDefinition([
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();
}
Expand Down Expand Up @@ -87,9 +82,11 @@ public function execute(InputInterface $input, OutputInterface $output)
->initConfig();

$scenario = new Scenario($this->test);
if (isset($config["namespace"])) $settings['class_name'] = $config["namespace"] .'\\' . $settings['class_name'];
$actor = $settings['class_name'];
$I = new $actor($scenario);
if (isset($config["namespace"])) {
$settings['class_name'] = $config["namespace"] .'\\' . $settings['class_name'];
}
$actor = $settings['class_name'];
$I = new $actor($scenario);

$this->listenToSignals();

Expand Down Expand Up @@ -130,7 +127,7 @@ protected function executeCommands(InputInterface $input, OutputInterface $outpu
if ($command == 'actions') {
$output->writeln("<info>" . implode(' ', $this->actions));
continue;
};
}
if ($command == 'exit') {
return;
}
Expand All @@ -139,7 +136,7 @@ protected function executeCommands(InputInterface $input, OutputInterface $outpu
}
try {
$value = eval("return \$I->$command;");
if ($value and !is_object($value)) {
if ($value && !is_object($value)) {
codecept_debug($value);
}
} catch (\PHPUnit_Framework_AssertionFailedError $fail) {
Expand Down
15 changes: 6 additions & 9 deletions src/Codeception/Command/GenerateCept.php
Expand Up @@ -23,13 +23,11 @@ class GenerateCept extends Command

protected function configure()
{
$this->setDefinition(
[
new InputArgument('suite', InputArgument::REQUIRED, 'suite to be tested'),
new InputArgument('test', InputArgument::REQUIRED, 'test to be run'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]
);
$this->setDefinition([
new InputArgument('suite', InputArgument::REQUIRED, 'suite to be tested'),
new InputArgument('test', InputArgument::REQUIRED, 'test to be run'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]);
}

public function getDescription()
Expand All @@ -56,5 +54,4 @@ public function execute(InputInterface $input, OutputInterface $output)
}
$output->writeln("<info>Test was created in $full_path</info>");
}

}
}
12 changes: 5 additions & 7 deletions src/Codeception/Command/GenerateCest.php
Expand Up @@ -24,13 +24,11 @@ class GenerateCest extends Command

protected function configure()
{
$this->setDefinition(
[
new InputArgument('suite', InputArgument::REQUIRED, 'suite where tests will be put'),
new InputArgument('class', InputArgument::REQUIRED, 'test name'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]
);
$this->setDefinition([
new InputArgument('suite', InputArgument::REQUIRED, 'suite where tests will be put'),
new InputArgument('class', InputArgument::REQUIRED, 'test name'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]);
}

public function getDescription()
Expand Down
17 changes: 8 additions & 9 deletions src/Codeception/Command/GenerateEnvironment.php
Expand Up @@ -23,12 +23,10 @@ class GenerateEnvironment extends Command

protected function configure()
{
$this->setDefinition(
[
new InputArgument('env', InputArgument::REQUIRED, 'Environment name'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]
);
$this->setDefinition([
new InputArgument('env', InputArgument::REQUIRED, 'Environment name'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]);
}

public function getDescription()
Expand All @@ -40,9 +38,10 @@ public function execute(InputInterface $input, OutputInterface $output)
{
$conf = $this->getGlobalConfig($input->getOption('config'));
if (!Configuration::envsDir()) {
throw new ConfigurationException("Path for environments configuration is not set.\n".
"Please specify envs path in your `codeception.yml`\n \n".
"envs: tests/_envs"
throw new ConfigurationException(
"Path for environments configuration is not set.\n"
. "Please specify envs path in your `codeception.yml`\n \n"
. "envs: tests/_envs"
);
}
$relativePath = $conf['paths']['envs'];
Expand Down
11 changes: 4 additions & 7 deletions src/Codeception/Command/GenerateGroup.php
Expand Up @@ -21,12 +21,10 @@ class GenerateGroup extends Command

protected function configure()
{
$this->setDefinition(
[
new InputArgument('group', InputArgument::REQUIRED, 'Group class name'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]
);
$this->setDefinition([
new InputArgument('group', InputArgument::REQUIRED, 'Group class name'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]);
}

public function getDescription()
Expand Down Expand Up @@ -55,5 +53,4 @@ public function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Group extension was created in $filename</info>");
$output->writeln('To use this group extension, include it to "extensions" option of global Codeception config.');
}

}
10 changes: 4 additions & 6 deletions src/Codeception/Command/GenerateHelper.php
Expand Up @@ -22,12 +22,10 @@ class GenerateHelper extends Command

protected function configure()
{
$this->setDefinition(
[
new InputArgument('name', InputArgument::REQUIRED, 'suite to be generated'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]
);
$this->setDefinition([
new InputArgument('name', InputArgument::REQUIRED, 'suite to be generated'),
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config'),
]);
}

public function getDescription()
Expand Down

0 comments on commit f11bfe9

Please sign in to comment.