Merge pull request #161 from open-sausages/pulls/fix-suite-loader

BUG Prevent behat from always running root behat.yml fixtures
This commit is contained in:
Chris Joe 2017-06-15 19:58:40 +12:00 committed by GitHub
commit 3d2e742709
4 changed files with 60 additions and 26 deletions

View File

@ -4,9 +4,11 @@ namespace SilverStripe\BehatExtension\Context;
use Behat\Behat\Context\Context; use Behat\Behat\Context\Context;
use Behat\Mink\Element\NodeElement; use Behat\Mink\Element\NodeElement;
use SilverStripe\Security\Authenticator;
use SilverStripe\Security\Group; use SilverStripe\Security\Group;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
/** /**
* LoginContext * LoginContext
@ -127,7 +129,10 @@ class LoginContext implements Context
/** @var Member $member */ /** @var Member $member */
$member = Member::get()->filter('Email', $id)->First(); $member = Member::get()->filter('Email', $id)->First();
assertNotNull($member); assertNotNull($member);
assertTrue($member->checkPassword($password)->isValid()); $authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::CHECK_PASSWORD);
foreach ($authenticators as $authenticator) {
assertTrue($authenticator->checkPassword($member, $password)->isValid());
}
} }
/** /**

View File

@ -12,15 +12,16 @@ trait ModuleCommandTrait
* Find target module being tested * Find target module being tested
* *
* @param string $name * @param string $name
* @param bool $error Throw error if not found
* @return Module * @return Module
*/ */
protected function getModule($name) protected function getModule($name, $error = true)
{ {
if (strpos($name, '@') === 0) { if (strpos($name, '@') === 0) {
$name = substr($name, 1); $name = substr($name, 1);
} }
$module = ModuleLoader::inst()->getManifest()->getModule($name); $module = ModuleLoader::inst()->getManifest()->getModule($name);
if (!$module) { if (!$module && $error) {
throw new InvalidArgumentException("No module $name installed"); throw new InvalidArgumentException("No module $name installed");
} }
return $module; return $module;

View File

@ -7,18 +7,22 @@ use Behat\Testwork\Suite\Cli\SuiteController;
use Behat\Testwork\Suite\ServiceContainer\SuiteExtension; use Behat\Testwork\Suite\ServiceContainer\SuiteExtension;
use Behat\Testwork\Suite\SuiteRegistry; use Behat\Testwork\Suite\SuiteRegistry;
use Exception; use Exception;
use InvalidArgumentException;
use RuntimeException;
use SilverStripe\Core\Manifest\Module; use SilverStripe\Core\Manifest\Module;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Parser;
/** /**
* Locates test suite configuration based on module name. * Locates test suite configuration based on module name.
* *
* Replaces:
* @see SuiteController for similar core behat controller * @see SuiteController for similar core behat controller
*/ */
class ModuleSuiteLocator implements Controller class ModuleSuiteLocator implements Controller
@ -72,6 +76,12 @@ class ModuleSuiteLocator implements Controller
. "Must be in @modulename format. Supports @vendor/name syntax for vendor installed modules. " . "Must be in @modulename format. Supports @vendor/name syntax for vendor installed modules. "
. "Ensure that a modulename/behat.yml exists containing a behat suite of the same name." . "Ensure that a modulename/behat.yml exists containing a behat suite of the same name."
); );
$command->addOption(
'--suite',
'-s',
InputOption::VALUE_REQUIRED,
'Only execute a specific suite.'
);
} }
/** /**
@ -79,13 +89,20 @@ class ModuleSuiteLocator implements Controller
* *
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
* * @throws RuntimeException
* @throws \RuntimeException
* @return null * @return null
*/ */
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
if (!$input->hasArgument('module')) { // Register all suites if no arguments given
if (!$input->getArgument('module') && !$input->getOption('suite')) {
foreach ($this->suiteConfigurations as $name => $config) {
$this->registry->registerSuiteConfiguration(
$name,
$config['type'],
$config['settings']
);
}
return null; return null;
} }
@ -94,23 +111,36 @@ class ModuleSuiteLocator implements Controller
return; return;
} }
// Get module // Get module either via @module or --suite module
$moduleName = $input->getArgument('module'); if ($input->getArgument('module')) {
$module = $this->getModule($moduleName); // Get suite from module
$moduleName = $input->getArgument('module');
$module = $this->getModule($moduleName);
// Suite name always omits vendor // Suite name always omits vendor
$suiteName = $module->getShortName(); $suiteName = $module->getShortName();
} else {
// Suite doesn't exist, so load dynamically from nested `behat.yml` // Get module from suite
if (!isset($this->suiteConfigurations[$suiteName])) { $suiteName = $input->getOption('suite');
$config = $this->loadSuiteConfiguration($suiteName, $module); $module = $this->getModule($suiteName, false);
$this->registry->registerSuiteConfiguration(
$suiteName,
$config['type'],
$config['settings']
);
} }
// Load registered suite
if (isset($this->suiteConfigurations[$suiteName])) {
$config = $this->suiteConfigurations[$suiteName];
} elseif ($module) {
// Suite doesn't exist, so load dynamically from nested `behat.yml`
$config = $this->loadSuiteConfiguration($suiteName, $module);
} else {
throw new InvalidArgumentException("Could not find suite config {$suiteName}");
}
// Register config
$this->registry->registerSuiteConfiguration(
$suiteName,
$config['type'],
$config['settings']
);
return null; return null;
} }
@ -133,7 +163,7 @@ class ModuleSuiteLocator implements Controller
} }
} }
} }
throw new \InvalidArgumentException("No behat.yml found for module " . $module->getName()); throw new InvalidArgumentException("No behat.yml found for module " . $module->getName());
} }
/** /**

View File

@ -149,11 +149,9 @@ class Extension implements ExtensionInterface
new Reference(SuiteExtension::REGISTRY_ID) new Reference(SuiteExtension::REGISTRY_ID)
]); ]);
$definition->addTag(CliExtension::CONTROLLER_TAG, ['priority' => 9999]); $definition->addTag(CliExtension::CONTROLLER_TAG, ['priority' => 9999]);
$container->setDefinition(CliExtension::CONTROLLER_TAG . '.sslocator', $definition); $container->setDefinition(CliExtension::CONTROLLER_TAG . '.suite', $definition);
} }
/** /**
* Loads suite bootstrap controller. * Loads suite bootstrap controller.
* This is responsible for invoking --init commands for modules. * This is responsible for invoking --init commands for modules.