Skip to content

Instantly share code, notes, and snippets.

@jrub
Created July 8, 2011 07:18
Show Gist options
  • Save jrub/1071304 to your computer and use it in GitHub Desktop.
Save jrub/1071304 to your computer and use it in GitHub Desktop.
Mink RC2 with MinkBundle and BehatBundle configurations for reusing old steps closured style
<?php
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new Symfony\Bundle\MonologBundle\MonologBundle(),
new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
new MyProject\MyBundle\MyProjectMyBundle(),
new Symfony\Bundle\DoctrineFixturesBundle\DoctrineFixturesBundle(),
);
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}
if ('test' === $this->getEnvironment()) {
$bundles[] = new Behat\MinkBundle\BehatMinkBundle();
$bundles[] = new Behat\BehatBundle\BehatBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
}
<?php
use Symfony\Component\ClassLoader\UniversalClassLoader;
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = new UniversalClassLoader();
$loader->registerNamespaces(array(
'Symfony' => array(__DIR__.'/../vendor/symfony/src', __DIR__.'/../vendor/bundles'),
'Sensio' => __DIR__.'/../vendor/bundles',
'JMS' => __DIR__.'/../vendor/bundles',
'Doctrine\\Common' => __DIR__.'/../vendor/doctrine-common/lib',
'Doctrine\\DBAL' => __DIR__.'/../vendor/doctrine-dbal/lib',
'Doctrine' => __DIR__.'/../vendor/doctrine/lib',
'Monolog' => __DIR__.'/../vendor/monolog/src',
'Assetic' => __DIR__.'/../vendor/assetic/src',
'Metadata' => __DIR__.'/../vendor/metadata/src',
'Doctrine\\Common\\DataFixtures' => __DIR__.'/../vendor/data-fixtures/lib',
'Behat\Mink' => __DIR__.'/../vendor/Behat/Mink/src',
'Behat\MinkBundle' => __DIR__.'/../vendor',
'Behat\BehatBundle' => __DIR__.'/../vendor',
'Behat\Behat' => __DIR__.'/../vendor/Behat/Behat/src',
'Behat\Gherkin' => __DIR__.'/../vendor/Behat/Gherkin/src',
'Goutte' => __DIR__.'/../vendor/Behat/Mink/vendor/Goutte/src',
//'Behat\SahiClient' => __DIR__.'/../vendor/Behat/Mink/vendor/SahiClient/src',
//'Buzz' => __DIR__.'/../vendor/Behat/Mink/vendor/Buzz/lib',
));
$loader->registerPrefixes(array(
'Twig_Extensions_' => __DIR__.'/../vendor/twig-extensions/lib',
'Twig_' => __DIR__.'/../vendor/twig/lib',
));
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->registerPrefixFallbacks(array(__DIR__.'/../vendor/symfony/src/Symfony/Component/Locale/Resources/stubs'));
}
$loader->registerNamespaceFallbacks(array(
__DIR__.'/../src',
));
$loader->register();
AnnotationRegistry::registerLoader(function($class) use ($loader) {
$loader->loadClass($class);
return class_exists($class, false);
});
AnnotationRegistry::registerFile(__DIR__.'/../vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php');
// Swiftmailer needs a special autoloader to allow
// the lazy loading of the init file (which is expensive)
require_once __DIR__.'/../vendor/swiftmailer/lib/classes/Swift.php';
Swift::registerAutoload(__DIR__.'/../vendor/swiftmailer/lib/swift_init.php');
<?php
namespace MyProject\MyBundle\Features\Context;
use Behat\Behat\Context\ClosuredContextInterface as ClosuredContext,
Behat\Behat\Context\TranslatedContextInterface as TranslatedContext,
Behat\BehatBundle\Context\BehatContext;
use Symfony\Component\Finder\Finder;
// load features/support/bootstrap.php file if it exists
if (file_exists(__DIR__ . '/../support/bootstrap.php')) {
require_once __DIR__ . '/../support/bootstrap.php';
}
/**
* LS feature context with closure definitions.
*/
class ClosuredFeatureContext extends BehatContext implements ClosuredContext, TranslatedContext
{
/**
* Environment parameters.
*
* @var array
*/
public $parameters = array();
public function __construct($kernel) {
// don't need loading old env.php, it's empty
parent::__construct($kernel);
}
/**
* Returns list of step definition files.
*
* @see Behat\Behat\Context\ClosuredContextInterface::getStepDefinitionResources()
*
* @return array
*/
public function getStepDefinitionResources() {
// find and return all *.php files under features/steps folder
if (file_exists(__DIR__ . '/../steps')) {
$finder = new Finder();
return $finder->files()->name('*.php')->in(__DIR__ . '/../steps');
}
return array();
}
/**
* Returns list of hook definition files.
*
* @see Behat\Behat\Context\ClosuredContextInterface::getHookDefinitionResources()
*
* @return array
*/
public function getHookDefinitionResources() {
// return array of features/support/hooks.php if it exists
if (file_exists(__DIR__ . '/../support/hooks.php')) {
return array(__DIR__ . '/../support/hooks.php');
}
return array();
}
/**
* Returns list of XLIFF translation files.
*
* @see Behat\Behat\Context\TranslatedContextInterface::getTranslationResources()
*
* @return array
*/
public function getTranslationResources() {
// find and return all *.xliff files under features/steps/i18n folder
if (file_exists(__DIR__ . '/../steps/i18n')) {
$finder = new Finder();
return $finder->files()->name('*.xliff')->in(__DIR__ . '/../steps/i18n');
}
return array();
}
/**
* Calls previously saved on $world closure function.
*
* @param string $name function name
* @param array $args function args
*
* @return mixed
*/
public function __call($name, array $args) {
if (isset($this->$name) && is_callable($this->$name)) {
return call_user_func_array($this->$name, $args);
} else {
$trace = debug_backtrace();
trigger_error(
'Call to undefined method ' . get_class($this) . '::' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_ERROR
);
}
}
}
imports:
- { resource: config_dev.yml }
framework:
test: ~
session:
storage_id: session.storage.filesystem
web_profiler:
toolbar: false
intercept_redirects: false
behat_mink:
base_url: http://localhost/app_test.php/
#default_session: goutte
#goutte: ~
#sahi: ~
behat: ~
swiftmailer:
disable_delivery: true
<?php
namespace MyProject\MyBundle\Features\Context;
use Behat\BehatBundle\Context\BehatContext,
Behat\BehatBundle\Context\MinkContext;
use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\TranslatedContextInterface,
Behat\Behat\Exception\Pending;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
/**
* Feature context.
*/
class FeatureContext extends MinkContext // notice, that it's Behat\BehatBundle\Context\MinkContext actually
{
public function __construct($kernel)
{
parent::__construct($kernel);
$this->useContext(new ClosuredFeatureContext($kernel));
}
//
// /**
// * @Given /^I have done something with "([^"]*)"$/
// */
// public function iHaveDoneSomethingWith($argument)
// {
// $container = $this->getContainer();
// $container->get('some_service')->doSomethingWith($argument);
// }
//
}
jrubio@jrubio-ubuntu:/var/www/reload$ app/console behat:bundle MyProject\\MyBundle --name="customer" -e=test
[Behat\Behat\Exception\Redundant]
Step "/^(?:|I )am on "(?P<page>[^"]+)"$/" is already defined in MyProject\MyBundle\Features\Context\FeatureContext::visit()
MyProject\MyBundle\Features\Context\FeatureContext::visit()
MyProject\MyBundle\Features\Context\ClosuredFeatureContext::visit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment