Merge pull request #125 from open-sausages/pulls/4.0/namespace-everything

Update for framework namespacing
This commit is contained in:
Ingo Schommer 2016-09-08 16:12:56 +12:00 committed by GitHub
commit 1271a4e48d
13 changed files with 92 additions and 79 deletions

View File

@ -2,6 +2,7 @@
namespace SilverStripe\BehatExtension\Compiler;
use SilverStripe\Dev\SapphireTest;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@ -21,13 +22,8 @@ class CoreInitializationPass implements CompilerPassInterface
$frameworkPath = $container->getParameter('behat.silverstripe_extension.framework_path');
$_GET['flush'] = 1;
require_once $frameworkPath . '/core/Core.php';
if (class_exists('TestRunner')) {
// 3.x compat
\TestRunner::use_test_manifest();
} else {
\SapphireTest::use_test_manifest();
}
SapphireTest::use_test_manifest();
unset($_GET['flush']);

View File

@ -58,8 +58,8 @@ class MinkExtensionBaseUrlPass implements CompilerPassInterface
* Try to auto-detect host for webroot based on _ss_environment.php data (unless explicitly set in behat.yml)
* Copied logic from Core.php, because it needs to be executed prior to {@link SilverStripeAwareInitializer}.
*
* @param String Absolute start path to search upwards from
* @return Boolean Absolute path to environment file
* @param string $path Absolute start path to search upwards from
* @return string Absolute path to environment file
*/
protected function findEnvironmentConfigFile($path)
{
@ -85,8 +85,8 @@ class MinkExtensionBaseUrlPass implements CompilerPassInterface
/**
* Copied logic from Core.php, because it needs to be executed prior to {@link SilverStripeAwareInitializer}.
*
* @param String Absolute start path to search upwards from
* @param Array Map of paths to host names
* @param string $path Absolute start path to search upwards from
* @param array $mapping Map of paths to host names
* @return String URL
*/
protected function findBaseUrlFromMapping($path, $mapping)

View File

@ -2,13 +2,12 @@
namespace SilverStripe\BehatExtension\Console\Processor;
use SilverStripe\Core\Manifest\SS_ClassLoader;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Behat\Behat\Console\Processor\InitProcessor as BaseProcessor;
/**
@ -32,7 +31,7 @@ class InitProcessor extends BaseProcessor
public function configure(Command $command)
{
parent::configure($command);
$command->addOption(
'--namespace',
null,
@ -78,7 +77,7 @@ class InitProcessor extends BaseProcessor
// Can't use 'behat.paths.base' since that's locked at this point to base folder (not module)
$pathSuffix = $this->container->getParameter('behat.silverstripe_extension.context.path_suffix');
$currentModuleName = null;
$modules = \SS_ClassLoader::instance()->getManifest()->getModules();
$modules = SS_ClassLoader::instance()->getManifest()->getModules();
$currentModuleName = $this->container->getParameter('behat.silverstripe_extension.module');
// get module from short notation if path starts from @

View File

@ -2,12 +2,12 @@
namespace SilverStripe\BehatExtension\Console\Processor;
use SilverStripe\Core\Manifest\SS_ClassLoader;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Behat\Behat\Console\Processor\LocatorProcessor as BaseProcessor;
/**
@ -56,12 +56,12 @@ class LocatorProcessor extends BaseProcessor
public function process(InputInterface $input, OutputInterface $output)
{
$featuresPath = $input->getArgument('features');
// Can't use 'behat.paths.base' since that's locked at this point to base folder (not module)
$pathSuffix = $this->container->getParameter('behat.silverstripe_extension.context.path_suffix');
$currentModuleName = null;
$modules = \SS_ClassLoader::instance()->getManifest()->getModules();
$modules = SS_ClassLoader::instance()->getManifest()->getModules();
// get module specified in behat.yml
$currentModuleName = $this->container->getParameter('behat.silverstripe_extension.module');

View File

@ -6,11 +6,10 @@ use Behat\Behat\Context\BehatContext;
use Behat\Behat\Context\Step;
use Behat\Behat\Event\StepEvent;
use Behat\Behat\Event\ScenarioEvent;
use Behat\Mink\Driver\Selenium2Driver;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Filesystem;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
// PHPUnit
require_once BASE_PATH . '/vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';
@ -60,6 +59,7 @@ class BasicContext extends BehatContext
/**
* Get Mink session from MinkContext
*
* @param string $name
* @return \Behat\Mink\Session
*/
public function getSession($name = null)
@ -274,10 +274,10 @@ JS;
*/
public function cleanAssetsAfterScenario(ScenarioEvent $event)
{
foreach (\File::get() as $file) {
foreach (File::get() as $file) {
$file->delete();
}
\Filesystem::removeFolder(ASSETS_PATH, true);
Filesystem::removeFolder(ASSETS_PATH, true);
}
public function takeScreenshot(StepEvent $event)
@ -298,7 +298,7 @@ JS;
return;
} // quit silently when path is not set
\Filesystem::makeFolder($path);
Filesystem::makeFolder($path);
$path = realpath($path);
if (!file_exists($path)) {

View File

@ -18,6 +18,7 @@ class ModuleContextClassGuesser implements ClassGuesserInterface
* Initializes guesser.
*
* @param string $namespaceSuffix
* @param string $contextClass
*/
public function __construct($namespaceSuffix, $contextClass)
{
@ -28,7 +29,8 @@ class ModuleContextClassGuesser implements ClassGuesserInterface
/**
* Sets bundle namespace to use for guessing.
*
* @param string $namespace
* @param string $namespaceBase
* @return $this
*/
public function setNamespaceBase($namespaceBase)
{

View File

@ -2,15 +2,15 @@
namespace SilverStripe\BehatExtension\Context;
use Behat\Behat\Context\ClosuredContextInterface;
use Behat\Behat\Context\TranslatedContextInterface;
use Behat\Behat\Context\BehatContext;
use Behat\Behat\Context\Step;
use Behat\Behat\Event\FeatureEvent;
use Behat\Behat\Event\ScenarioEvent;
use Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Session;
use SilverStripe\BehatExtension\Utility\TestMailer;
use SilverStripe\Control\Email\Email;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use Symfony\Component\DomCrawler\Crawler;
// PHPUnit
@ -23,6 +23,9 @@ class EmailContext extends BehatContext
{
protected $context;
/**
* @var TestMailer
*/
protected $mailer;
/**
@ -44,6 +47,9 @@ class EmailContext extends BehatContext
/**
* Get Mink session from MinkContext
*
* @param string $name
* @return Session
*/
public function getSession($name = null)
{
@ -57,9 +63,9 @@ class EmailContext extends BehatContext
{
// Also set through the 'supportbehat' extension
// to ensure its available both in CLI execution and the tested browser session
$this->mailer = new \SilverStripe\BehatExtension\Utility\TestMailer();
\Email::set_mailer($this->mailer);
\Config::inst()->update("Email", "send_all_emails_to", null);
$this->mailer = new TestMailer();
Injector::inst()->registerService($this->mailer, 'SilverStripe\\Control\\Email\\Mailer');
Email::config()->update("send_all_emails_to", null);
}
/**
@ -172,7 +178,7 @@ class EmailContext extends BehatContext
assertNotNull($linkEl);
$link = $linkEl->attr('href');
assertNotNull($link);
return new Step\When(sprintf('I go to "%s"', $link));
}
@ -193,7 +199,7 @@ class EmailContext extends BehatContext
assertNotNull($link);
return new Step\When(sprintf('I go to "%s"', $link));
}
/**
* Assumes an email has been identified by a previous step,
* e.g. through 'Given there should be an email to "test@test.com"'.
@ -249,7 +255,7 @@ class EmailContext extends BehatContext
$emailContent = strip_tags($emailContent);
$emailContent = preg_replace("/\h+/", " ", $emailContent);
$rows = $table->getRows();
// For "should not contain"
if (trim($negate)) {
foreach ($rows as $row) {

View File

@ -6,7 +6,13 @@ use Behat\Behat\Context\BehatContext;
use Behat\Behat\Event\ScenarioEvent;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use SilverStripe\Filesystem\Storage\AssetStore;
use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Storage\AssetStore;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\FixtureFactory;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\YamlFixture;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\Versioning\Versioned;
@ -23,7 +29,7 @@ class FixtureContext extends BehatContext
protected $context;
/**
* @var \FixtureFactory
* @var FixtureFactory
*/
protected $fixtureFactory;
@ -55,20 +61,23 @@ class FixtureContext extends BehatContext
}
/**
* @return \FixtureFactory
* @return FixtureFactory
*/
public function getFixtureFactory()
{
if (!$this->fixtureFactory) {
$this->fixtureFactory = \Injector::inst()->create('FixtureFactory', 'FixtureContextFactory');
$this->fixtureFactory = Injector::inst()->create(
'SilverStripe\\Dev\\FixtureFactory',
'FixtureContextFactory'
);
}
return $this->fixtureFactory;
}
/**
* @param \FixtureFactory $factory
* @param FixtureFactory $factory
*/
public function setFixtureFactory(\FixtureFactory $factory)
public function setFixtureFactory(FixtureFactory $factory)
{
$this->fixtureFactory = $factory;
}
@ -94,9 +103,9 @@ class FixtureContext extends BehatContext
*/
public function beforeDatabaseDefaults(ScenarioEvent $event)
{
\SapphireTest::empty_temp_db();
SapphireTest::empty_temp_db();
DB::get_conn()->quiet();
$dataClasses = \ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject');
$dataClasses = ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject');
array_shift($dataClasses);
foreach ($dataClasses as $dataClass) {
\singleton($dataClass)->requireDefaultRecords();
@ -108,7 +117,7 @@ class FixtureContext extends BehatContext
*/
public function afterResetDatabase(ScenarioEvent $event)
{
\SapphireTest::empty_temp_db();
SapphireTest::empty_temp_db();
}
/**
@ -406,7 +415,7 @@ class FixtureContext extends BehatContext
// Save fixtures into database
// TODO Run prepareAsset() for each File and Folder record
$yamlFixture = new \YamlFixture($yaml);
$yamlFixture = new YamlFixture($yaml);
$yamlFixture->writeInto($this->getFixtureFactory());
}
@ -595,7 +604,7 @@ class FixtureContext extends BehatContext
*/
protected function prepareFixture($class, $identifier, $data = array())
{
if ($class == 'File' || is_subclass_of($class, 'File')) {
if ($class == 'SilverStripe\\Assets\\File' || is_subclass_of($class, 'SilverStripe\\Assets\\File')) {
$data = $this->prepareAsset($class, $identifier, $data);
}
return $data;
@ -611,11 +620,11 @@ class FixtureContext extends BehatContext
$sourcePath = $this->joinPaths($this->getFilesPath(), basename($relativeTargetPath));
// Create file or folder on filesystem
if ($class == 'Folder' || is_subclass_of($class, 'Folder')) {
$parent = \Folder::find_or_make($relativeTargetPath);
if ($class == 'SilverStripe\\Assets\\Folder' || is_subclass_of($class, 'SilverStripe\\Assets\\Folder')) {
$parent = Folder::find_or_make($relativeTargetPath);
$data['ID'] = $parent->ID;
} else {
$parent = \Folder::find_or_make(dirname($relativeTargetPath));
$parent = Folder::find_or_make(dirname($relativeTargetPath));
if (!file_exists($sourcePath)) {
throw new \InvalidArgumentException(sprintf(
'Source file for "%s" cannot be found in "%s"',
@ -676,11 +685,11 @@ class FixtureContext extends BehatContext
// Try direct mapping
$class = str_replace(' ', '', ucwords($type));
if (class_exists($class) && is_subclass_of($class, 'SilverStripe\\ORM\\DataObject')) {
return \ClassInfo::class_name($class);
return ClassInfo::class_name($class);
}
// Fall back to singular names
foreach (array_values(\ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject')) as $candidate) {
foreach (array_values(ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject')) as $candidate) {
if (strcasecmp(singleton($candidate)->singular_name(), $type) === 0) {
return $candidate;
}

View File

@ -4,8 +4,8 @@ namespace SilverStripe\BehatExtension\Context\Initializer;
use Behat\Behat\Context\Initializer\InitializerInterface;
use Behat\Behat\Context\ContextInterface;
use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
use SilverStripe\Core\Injector\Injector;
/*
* This file is part of the Behat/SilverStripeExtension
@ -24,11 +24,11 @@ use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
*/
class SilverStripeAwareInitializer implements InitializerInterface
{
private $databaseName;
/**
* @var Array
* @var array
*/
protected $ajaxSteps;
@ -59,6 +59,8 @@ class SilverStripeAwareInitializer implements InitializerInterface
/**
* Initializes initializer.
*
* @param string $frameworkPath
*/
public function __construct($frameworkPath)
{
@ -66,7 +68,7 @@ class SilverStripeAwareInitializer implements InitializerInterface
file_put_contents('php://stdout', "Creating test session environment" . PHP_EOL);
$testEnv = \Injector::inst()->get('TestSessionEnvironment');
$testEnv = Injector::inst()->get('TestSessionEnvironment');
$testEnv->startTestSession(array(
'createDatabase' => true
));
@ -183,7 +185,7 @@ class SilverStripeAwareInitializer implements InitializerInterface
}
/**
* @param String Absolute path to 'framework' module
* @param string $frameworkPath Absolute path to 'framework' module
*/
protected function bootstrap($frameworkPath)
{

View File

@ -4,6 +4,7 @@ namespace SilverStripe\BehatExtension\Context;
use Behat\Behat\Context\BehatContext;
use Behat\Behat\Context\Step;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;
@ -73,20 +74,20 @@ class LoginContext extends BehatContext
if (!isset($this->cache_generatedMembers[$permCode])) {
$group = Group::get()->filter('Title', "$permCode group")->first();
if (!$group) {
$group = \Injector::inst()->create('SilverStripe\\Security\\Group');
$group = Injector::inst()->create('SilverStripe\\Security\\Group');
}
$group->Title = "$permCode group";
$group->write();
$permission = \Injector::inst()->create('SilverStripe\\Security\\Permission');
$permission = Injector::inst()->create('SilverStripe\\Security\\Permission');
$permission->Code = $permCode;
$permission->write();
$group->Permissions()->add($permission);
$member = DataObject::get_one('SilverStripe\\Security\\Member', sprintf('"Email" = \'%s\'', "$permCode@example.org"));
if (!$member) {
$member = \Injector::inst()->create('SilverStripe\\Security\\Member');
$member = Injector::inst()->create('SilverStripe\\Security\\Member');
}
// make sure any validation for password is skipped, since we're not testing complexity here
@ -179,7 +180,7 @@ class LoginContext extends BehatContext
*/
public function stepPasswordForEmailShouldBe($id, $password)
{
$member = Member::get()->filter('Email', $id)->First();
$member = Member::get()->filter('SilverStripe\\Control\\Email\\Email', $id)->First();
assertNotNull($member);
assertTrue($member->checkPassword($password)->valid());
}

View File

@ -21,14 +21,14 @@ interface SilverStripeAwareContextInterface
/**
* Sets SilverStripe instance.
*
* @param String $database_name Temp database name
* @param string $databaseName Temp database name
*/
public function setDatabase($databaseName);
/**
* Marks steps as AJAX steps for special treatment
*
* @param array $ajax_steps Array of step name parts to match
* @param array $ajaxSteps Array of step name parts to match
*/
public function setAjaxSteps($ajaxSteps);
}

View File

@ -3,19 +3,13 @@
namespace SilverStripe\BehatExtension\Context;
use Behat\Behat\Context\Step;
use Behat\Behat\Event\FeatureEvent;
use Behat\Behat\Event\ScenarioEvent;
use Behat\Behat\Event\SuiteEvent;
use Behat\Gherkin\Node\PyStringNode;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Driver\GoutteDriver;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Mink\Exception\ElementNotFoundException;
use SilverStripe\BehatExtension\Context\SilverStripeAwareContextInterface;
use Symfony\Component\Yaml\Yaml;
use InvalidArgumentException;
// Mink etc.
require_once 'vendor/autoload.php';
@ -32,7 +26,7 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
protected $databaseName;
/**
* @var Array Partial string match for step names
* @var array Partial string match for step names
* that are considered to trigger Ajax request in the CMS,
* and hence need special timeout handling.
* @see \SilverStripe\BehatExtension\Context\BasicContext->handleAjaxBeforeStep().
@ -151,8 +145,8 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex
* Also supports direct CSS selectors and regions identified by a "data-title" attribute.
* When using the "data-title" attribute, ensure not to include double quotes.
*
* @param String $region Region name or CSS selector
* @return MinkElement|null
* @param string $region Region name or CSS selector
* @return MinkElement
*/
public function getRegionObj($region)
{

View File

@ -2,12 +2,16 @@
namespace SilverStripe\BehatExtension\Utility;
use SilverStripe\Control\Email\Mailer;
use SilverStripe\Core\Injector\Injector;
use TestSessionEnvironment;
/**
* Same principle as core TestMailer class,
* but saves emails in {@link TestSessionEnvironment}
* to share the state between PHP calls (CLI vs. browser).
*/
class TestMailer extends \Mailer
class TestMailer extends Mailer
{
/**
@ -17,7 +21,7 @@ class TestMailer extends \Mailer
public function __construct()
{
$this->testSessionEnvironment = \Injector::inst()->get('TestSessionEnvironment');
$this->testSessionEnvironment = Injector::inst()->get('TestSessionEnvironment');
}
/**
@ -39,7 +43,7 @@ class TestMailer extends \Mailer
return true;
}
/**
* Send a multi-part HTML email
* TestMailer will merely record that the email was asked to be sent, without sending anything.
@ -54,7 +58,7 @@ class TestMailer extends \Mailer
$plainContent = false,
$inlineImages = false
) {
$this->saveEmail(array(
'Type' => 'html',
'To' => $to,
@ -68,7 +72,7 @@ class TestMailer extends \Mailer
return true;
}
/**
* Clear the log of emails sent
*/