initial commit

This commit is contained in:
Michał Ochman 2012-08-15 16:50:19 +02:00
parent c8f0197a55
commit 2e1cfcc65d
9 changed files with 381 additions and 0 deletions

22
LICENSE Normal file
View File

@ -0,0 +1,22 @@
Copyright (c) 2012 Michał Ochman <ochman.d.michal@gmail.com>
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.

61
build.php Normal file
View File

@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Behat/SilverStripeExtension
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* 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(<<<STUB
<?php
/*
* This file is part of the SilverStripe-Behaviour-Testing-Framework
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* 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;
}

27
composer.json Normal file
View File

@ -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/"
}
}
}

20
init.php Normal file
View File

@ -0,0 +1,20 @@
<?php
/*
* This file is part of the Behat/SilverStripeExtension
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* 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;

View File

@ -0,0 +1,44 @@
<?php
namespace Behat\SilverStripeExtension\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder,
Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
/*
* This file is part of the Behat\SilverStripeExtension
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* 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 <ochman.d.michal@gmail.com>
*/
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);
}
}

View File

@ -0,0 +1,98 @@
<?php
namespace Behat\SilverStripeExtension\Context\Initializer;
use Behat\Behat\Context\Initializer\InitializerInterface,
Behat\Behat\Context\ContextInterface;
use Behat\SilverStripeExtension\Context\SilverStripeAwareContextInterface;
/*
* This file is part of the Behat/SilverStripeExtension
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* 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 <ochman.d.michal@gmail.com>
*/
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);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Behat\SilverStripeExtension\Context;
/*
* This file is part of the Behat/SilverStripeExtension
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* 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 <ochman.d.michal@gmail.com>
*/
interface SilverStripeAwareContextInterface
{
/**
* Sets SilverStripe instance.
*
* @param String $database_name Temp database name
*/
public function setDatabase($database_name);
}

View File

@ -0,0 +1,69 @@
<?php
namespace Behat\SilverStripeExtension;
use Symfony\Component\Config\FileLocator,
Symfony\Component\DependencyInjection\ContainerBuilder,
Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Behat\Behat\Extension\Extension as BaseExtension;
/*
* This file is part of the Behat\SilverStripeExtension
*
* (c) Michał Ochman <ochman.d.michal@gmail.com>
*
* 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 <ochman.d.michal@gmail.com>
*/
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(),
);
}
}

View File

@ -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 }