From 2e1cfcc65dd1b24062eec1f867e40a453a50d885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Ochman?= Date: Wed, 15 Aug 2012 16:50:19 +0200 Subject: [PATCH] initial commit --- LICENSE | 22 +++++ build.php | 61 ++++++++++++ composer.json | 27 +++++ init.php | 20 ++++ .../Compiler/MinkExtensionBaseUrlPass.php | 44 +++++++++ .../SilverStripeAwareInitializer.php | 98 +++++++++++++++++++ .../SilverStripeAwareContextInterface.php | 27 +++++ src/Behat/SilverStripeExtension/Extension.php | 69 +++++++++++++ .../services/silverstripe.yml | 13 +++ 9 files changed, 381 insertions(+) create mode 100644 LICENSE create mode 100644 build.php create mode 100644 composer.json create mode 100644 init.php create mode 100644 src/Behat/SilverStripeExtension/Compiler/MinkExtensionBaseUrlPass.php create mode 100644 src/Behat/SilverStripeExtension/Context/Initializer/SilverStripeAwareInitializer.php create mode 100644 src/Behat/SilverStripeExtension/Context/SilverStripeAwareContextInterface.php create mode 100644 src/Behat/SilverStripeExtension/Extension.php create mode 100644 src/Behat/SilverStripeExtension/services/silverstripe.yml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9fceec8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2012 Michał Ochman + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/build.php b/build.php new file mode 100644 index 0000000..692c589 --- /dev/null +++ b/build.php @@ -0,0 +1,61 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +$filename = 'silverstripe_extension.phar'; + +if (file_exists($filename)) { + unlink($filename); +} + +$phar = new \Phar($filename, 0, 'extension.phar'); +$phar->setSignatureAlgorithm(\Phar::SHA1); +$phar->startBuffering(); + +foreach (findFiles('src') as $path) { + $phar->addFromString($path, file_get_contents(__DIR__ . '/' . $path)); +} + +$phar->addFromString('init.php', file_get_contents(__DIR__ . '/init.php')); + +$phar->setStub(<< + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +Phar::mapPhar('extension.phar'); + +return require 'phar://extension.phar/init.php'; + +__HALT_COMPILER(); +STUB +); +$phar->stopBuffering(); + +function findFiles($dir) +{ + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST); + + $files = array(); + foreach ($iterator as $path) { + if ($path->isFile()) { + $files[] = $path->getPath() . DIRECTORY_SEPARATOR . $path->getFilename(); + } + } + + return $files; +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a397b7c --- /dev/null +++ b/composer.json @@ -0,0 +1,27 @@ +{ + "name": "behat/silverstripe-extension", + "type": "behat-extension", + "description": "SilverStripe framework extension for Behat", + "keywords": ["framework", "web", "bdd", "silverstripe"], + "homepage": "http://silverstripe.org", + "license": "MIT", + "authors": [ + { + "name": "Michał Ochman", + "email": "ochman.d.michal@gmail.com" + } + ], + + "require": { + "php": ">=5.3.2", + "behat/behat": "2.4@stable", + "behat/mink": "1.4@stable", + "behat/mink-extension": "1.0@stable" + }, + + "autoload": { + "psr-0": { + "Behat\\SilverStripeExtension": "src/" + } + } +} \ No newline at end of file diff --git a/init.php b/init.php new file mode 100644 index 0000000..bf9f14d --- /dev/null +++ b/init.php @@ -0,0 +1,20 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +spl_autoload_register(function($class) +{ + if (false !== strpos($class, 'Behat\\SilverStripeExtension')) { + require_once(__DIR__ . '/src/' . str_replace('\\', '/', $class) . '.php'); + return true; + } +}, true, false); + +return new Behat\SilverStripeExtension\Extension; \ No newline at end of file diff --git a/src/Behat/SilverStripeExtension/Compiler/MinkExtensionBaseUrlPass.php b/src/Behat/SilverStripeExtension/Compiler/MinkExtensionBaseUrlPass.php new file mode 100644 index 0000000..a66ee28 --- /dev/null +++ b/src/Behat/SilverStripeExtension/Compiler/MinkExtensionBaseUrlPass.php @@ -0,0 +1,44 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * Behat\SilverStripe container compilation pass. + * Passes Base URL available in MinkExtension config. + * + * @author Michał Ochman + */ +class MinkExtensionBaseUrlPass implements CompilerPassInterface +{ + /** + * Passes MinkExtension's base_url parameter + * + * @param ContainerBuilder $container + */ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('behat.mink')) { + throw new \Exception('MinkExtension not defined'); + } + if (!$container->hasParameter('behat.mink.base_url')) { + throw new \Exception('MinkExtension improperly configured. Missing base_url parameter.'); + } + $base_url = $container->getParameter('behat.mink.base_url'); + if (empty($base_url)) { + throw new \Exception('MinkExtension improperly configured. Missing or empty base_url parameter.'); + } + $container->setParameter('behat.silverstripe_extension.framework_host', $base_url); + } +} diff --git a/src/Behat/SilverStripeExtension/Context/Initializer/SilverStripeAwareInitializer.php b/src/Behat/SilverStripeExtension/Context/Initializer/SilverStripeAwareInitializer.php new file mode 100644 index 0000000..df22643 --- /dev/null +++ b/src/Behat/SilverStripeExtension/Context/Initializer/SilverStripeAwareInitializer.php @@ -0,0 +1,98 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +/** + * SilverStripe aware contexts initializer. + * Sets SilverStripe instance to the SilverStripeAware contexts. + * + * @author Michał Ochman + */ +class SilverStripeAwareInitializer implements InitializerInterface +{ + private $database_name; + private $ajax_steps; + + /** + * Initializes initializer. + */ + public function __construct($framework_path, $framework_host, $ajax_steps) + { + $this->bootstrap($framework_path, $framework_host); + $this->database_name = $this->initializeTempDb(); + $this->ajax_steps = $ajax_steps; + } + + public function __destruct() + { + $this->deleteTempDb(); + } + + /** + * Checks if initializer supports provided context. + * + * @param ContextInterface $context + * + * @return Boolean + */ + public function supports(ContextInterface $context) + { + return $context instanceof SilverStripeAwareContextInterface; + } + + /** + * Initializes provided context. + * + * @param ContextInterface $context + */ + public function initialize(ContextInterface $context) + { + $context->setDatabase($this->database_name); + $context->setAjaxEnabledSteps($this->ajax_steps); + } + + protected function bootstrap($framework_path, $framework_host) + { + file_put_contents('php://stderr', 'Bootstrapping' . PHP_EOL); + + // Set file to URL mappings + global $_FILE_TO_URL_MAPPING; + $_FILE_TO_URL_MAPPING[dirname($framework_path)] = $framework_host; + + // Connect to database + require_once $framework_path . '/core/Core.php'; + + // Remove the error handler so that PHPUnit can add its own + restore_error_handler(); + } + + protected function initializeTempDb() + { + file_put_contents('php://stderr', 'Creating temp DB' . PHP_EOL); + $dbname = \SapphireTest::create_temp_db(); + \DB::set_alternative_database_name($dbname); + + return $dbname; + } + + protected function deleteTempDb() + { + file_put_contents('php://stderr', 'Killing temp DB' . PHP_EOL); + \SapphireTest::kill_temp_db(); + \DB::set_alternative_database_name(null); + } +} \ No newline at end of file diff --git a/src/Behat/SilverStripeExtension/Context/SilverStripeAwareContextInterface.php b/src/Behat/SilverStripeExtension/Context/SilverStripeAwareContextInterface.php new file mode 100644 index 0000000..fb0c041 --- /dev/null +++ b/src/Behat/SilverStripeExtension/Context/SilverStripeAwareContextInterface.php @@ -0,0 +1,27 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * SilverStripe aware interface for contexts. + * + * @author Michał Ochman + */ +interface SilverStripeAwareContextInterface +{ + /** + * Sets SilverStripe instance. + * + * @param String $database_name Temp database name + */ + public function setDatabase($database_name); +} \ No newline at end of file diff --git a/src/Behat/SilverStripeExtension/Extension.php b/src/Behat/SilverStripeExtension/Extension.php new file mode 100644 index 0000000..d15b12f --- /dev/null +++ b/src/Behat/SilverStripeExtension/Extension.php @@ -0,0 +1,69 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * SilverStripe extension for Behat class. + * + * @author Michał Ochman + */ +class Extension extends BaseExtension +{ + /** + * Loads a specific configuration. + * + * @param array $config Extension configuration hash (from behat.yml) + * @param ContainerBuilder $container ContainerBuilder instance + */ + public function load(array $config, ContainerBuilder $container) + { + if (!isset($config['framework_path'])) { + throw new \InvalidArgumentException('Specify `framework_path` parameter for silverstripe_extension'); + } + + $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/services')); + $loader->load('silverstripe.yml'); + + $behat_base_path = $container->getParameter('behat.paths.base'); + $config['framework_path'] = realpath(sprintf('%s%s%s', + rtrim($behat_base_path, DIRECTORY_SEPARATOR), + DIRECTORY_SEPARATOR, + ltrim($config['framework_path'], DIRECTORY_SEPARATOR) + )); + if (!file_exists($config['framework_path']) || !is_dir($config['framework_path'])) { + throw new \InvalidArgumentException('Path specified as `framework_path` either doesn\'t exist or is not a directory'); + } + + $container->setParameter('behat.silverstripe_extension.framework_path', $config['framework_path']); + if (isset($config['ajax_steps'])) { + $container->setParameter('behat.silverstripe_extension.ajax_steps', $config['ajax_steps']); + } + } + + /** + * Returns compiler passes used by SilverStripe extension. + * + * @return array + */ + public function getCompilerPasses() + { + return array( + new Compiler\MinkExtensionBaseUrlPass(), + ); + } +} diff --git a/src/Behat/SilverStripeExtension/services/silverstripe.yml b/src/Behat/SilverStripeExtension/services/silverstripe.yml new file mode 100644 index 0000000..c69de33 --- /dev/null +++ b/src/Behat/SilverStripeExtension/services/silverstripe.yml @@ -0,0 +1,13 @@ +parameters: + behat.silverstripe_extension.context.initializer.class: Behat\SilverStripeExtension\Context\Initializer\SilverStripeAwareInitializer + behat.silverstripe_extension.framework_path: ~ + behat.silverstripe_extension.ajax_steps: ~ +services: + behat.silverstripe_extension.context.initializer: + class: %behat.silverstripe_extension.context.initializer.class% + arguments: + - %behat.silverstripe_extension.framework_path% + - %behat.silverstripe_extension.framework_host% + - %behat.silverstripe_extension.ajax_steps% + tags: + - { name: behat.context.initializer }