silverstripe-subsites/tests/behat/features/bootstrap/Context/FeatureContext.php

118 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Subsites\Tests\Behaviour;
2017-05-24 15:26:28 +02:00
if (!class_exists('SilverStripe\BehatExtension\Context\SilverStripeContext')) {
return;
}
2017-05-29 13:42:42 +02:00
use SilverStripe\BehatExtension\Context\SilverStripeContext;
2017-05-24 15:26:28 +02:00
use SilverStripe\BehatExtension\Context\BasicContext;
use SilverStripe\BehatExtension\Context\LoginContext;
2017-05-29 13:42:42 +02:00
use SilverStripe\BehatExtension\Context\FixtureContext;
use SilverStripe\Framework\Test\Behaviour\CmsFormsContext;
2017-05-29 13:44:19 +02:00
use SilverStripe\Framework\Test\Behaviour\CmsUiContext;
2017-05-29 13:42:42 +02:00
use SilverStripe\Cms\Test\Behaviour;
use SilverStripe\CMS\Model\SiteTree;
2016-09-22 16:38:29 +02:00
use SilverStripe\Core\ClassInfo;
2017-05-24 15:26:28 +02:00
use SilverStripe\Core\Injector\Injector;
2017-05-29 13:42:42 +02:00
use SilverStripe\Dev\BehatFixtureFactory;
use SilverStripe\Dev\FixtureBlueprint;
use SilverStripe\Dev\FixtureFactory;
use SilverStripe\Security\Member;
// PHPUnit
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
/**
* Features context
*
* Context automatically loaded by Behat.
* Uses subcontexts to extend functionality.
*/
2017-05-24 15:26:28 +02:00
class FeatureContext extends SilverStripeContext
2017-05-29 13:42:42 +02:00
{
/**
* @var FixtureFactory
*/
protected $fixtureFactory;
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* @param array $parameters context parameters (set them up through behat.yml)
*/
2017-05-24 15:26:28 +02:00
public function __construct(array $parameters)
{
parent::__construct($parameters);
$this->useContext('BasicContext', new BasicContext($parameters));
$this->useContext('LoginContext', new LoginContext($parameters));
$this->useContext('CmsFormsContext', new CmsFormsContext($parameters));
$this->useContext('CmsUiContext', new CmsUiContext($parameters));
$fixtureContext = new FixtureContext($parameters);
$fixtureContext->setFixtureFactory($this->getFixtureFactory());
$this->useContext('FixtureContext', $fixtureContext);
// Use blueprints to set user name from identifier
$factory = $fixtureContext->getFixtureFactory();
2017-05-29 13:42:42 +02:00
$blueprint = Injector::inst()->create(FixtureBlueprint::class, Member::class);
2017-05-24 15:26:28 +02:00
$blueprint->addCallback('beforeCreate', function ($identifier, &$data, &$fixtures) {
if (!isset($data['FirstName'])) {
$data['FirstName'] = $identifier;
}
});
2017-05-29 13:42:42 +02:00
$factory->define(Member::class, $blueprint);
// Auto-publish pages
2017-05-29 13:42:42 +02:00
foreach (ClassInfo::subclassesFor(SiteTree::class) as $id => $class) {
$blueprint = Injector::inst()->create(FixtureBlueprint::class, $class);
2017-05-24 15:26:28 +02:00
$blueprint->addCallback('afterCreate', function ($obj, $identifier, &$data, &$fixtures) {
$obj->copyVersionToStage('Stage', 'Live');
});
$factory->define($class, $blueprint);
2017-05-24 15:26:28 +02:00
}
}
2017-05-24 15:26:28 +02:00
public function setMinkParameters(array $parameters)
{
parent::setMinkParameters($parameters);
2017-05-24 15:26:28 +02:00
if (isset($parameters['files_path'])) {
$this->getSubcontext('FixtureContext')->setFilesPath($parameters['files_path']);
}
}
/**
* @return FixtureFactory
*/
2017-05-24 15:26:28 +02:00
public function getFixtureFactory()
{
if (!$this->fixtureFactory) {
2017-05-29 13:42:42 +02:00
$this->fixtureFactory = Injector::inst()->create(BehatFixtureFactory::class);
}
return $this->fixtureFactory;
}
2017-05-24 15:26:28 +02:00
public function setFixtureFactory(FixtureFactory $factory)
{
$this->fixtureFactory = $factory;
}
//
// Place your definition and hook methods here:
//
// /**
// * @Given /^I have done something with "([^"]*)"$/
// */
// public function iHaveDoneSomethingWith($argument) {
// $container = $this->kernel->getContainer();
// $container->get('some_service')->doSomethingWith($argument);
// }
//
}