Permalink
Browse files

Merge pull request #26 from jakzal/Behat24

Updated contexts for Behat 2.4
  • Loading branch information...
2 parents ac7499e + dbbb325 commit fdd34724890828bd8442a443fd59f8af81daed17 @everzet everzet committed Jun 17, 2012
@@ -26,7 +26,7 @@ public function pageShouldExist($page)
{
return array(
new Step\When('I go to "'.$page.'"'),
- new Step\Then('The response status code should not be 404'),
+ new Step\Then('the response status code should not be 404'),
);
}
@@ -41,7 +41,7 @@ public function pageShouldNotExist($page)
{
return array(
new Step\When('I go to "'.$page.'"'),
- new Step\Then('The response status code should be 404'),
+ new Step\Then('the response status code should be 404'),
);
}
@@ -56,7 +56,7 @@ public function iShouldNotBeAllowedToGoTo($page)
{
return array(
new Step\When('I go to "'.$page.'"'),
- new Step\Then('The response status code should be 403'),
+ new Step\Then('the response status code should be 403'),
);
}
@@ -71,7 +71,7 @@ public function iShouldBeAllowedToGoTo($page)
{
return array(
new Step\When('I go to "'.$page.'"'),
- new Step\Then('The response status code should be 200'),
+ new Step\Then('the response status code should be 200'),
);
}
}
@@ -5,18 +5,18 @@
require_once 'PHPUnit/Autoload.php';
require_once 'PHPUnit/Framework/Assert/Functions.php';
-use Behat\Behat\Context\BehatContext;
+use Behat\MinkExtension\Context\RawMinkContext;
use Behat\Mink\Exception\UnsupportedDriverActionException,
- Behat\Mink\Driver\GoutteDriver;
+ Behat\Mink\Driver\BrowserKitDriver;
/**
* Context class for managing redirects within an application.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
* @author Marijn Huizendveld <marijn.huizendveld@gmail.com>
*/
-class MinkRedirectContext extends BehatContext
+class MinkRedirectContext extends RawMinkContext
{
/**
* Prevent following redirects.
@@ -39,7 +39,7 @@ public function iDoNotFollowRedirects()
*/
public function afterScenario($event)
{
- if ($this->getSession()->getDriver() instanceof GoutteDriver) {
+ if ($this->getSession()->getDriver() instanceof BrowserKitDriver) {
$this->getClient()->followRedirects(true);
}
}
@@ -55,8 +55,7 @@ public function afterScenario($event)
*/
public function iAmRedirected($location = null)
{
- $session = $this->getSession();
- $headers = $session->getResponseHeaders();
+ $headers = $this->getSession()->getResponseHeaders();
assertArrayHasKey('Location', $headers, 'The response contains a "Location" header');
@@ -84,25 +83,12 @@ protected function getClient()
{
$driver = $this->getSession()->getDriver();
- if (!$driver instanceof GoutteDriver) {
- $message = "This step is only supported by the @mink:symfony and @mink:goutte drivers";
+ if (!$driver instanceof BrowserKitDriver) {
+ $message = 'This step is only supported by the browserkit drivers';
throw new UnsupportedDriverActionException($message, $driver);
}
return $driver->getClient();
}
-
- /**
- * Returns current active mink session.
- *
- * If you are using MinkContext as a subcontext instead of using it as
- * the main one, overwrite this method
- *
- * @return Behat\Mink\Session
- */
- protected function getSession()
- {
- return $this->getMainContext()->getSession();
- }
}
@@ -2,9 +2,11 @@
namespace Behat\CommonContexts;
-use Behat\BehatBundle\Context\BehatContext;
+use Behat\Behat\Context\BehatContext;
use Behat\Behat\Event\ScenarioEvent;
+use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Doctrine\ORM\Tools\SchemaTool;
+use Symfony\Component\HttpKernel\KernelInterface;
/**
* Provides hooks for building and cleaning up a database schema with Doctrine.
@@ -13,9 +15,14 @@
*
* @author Jakub Zalas <jakub@zalas.pl>
*/
-class SymfonyDoctrineContext extends BehatContext
+class SymfonyDoctrineContext extends BehatContext implements KernelAwareInterface
{
/**
+ * @var \Symfony\Component\HttpKernel\KernelInterface $kernel
+ */
+ private $kernel = null;
+
+ /**
* @param \Behat\Behat\Event\ScenarioEvent|\Behat\Behat\Event\OutlineExampleEvent $event
*
* @BeforeScenario
@@ -50,6 +57,16 @@ public function closeDBALConnections($event)
}
/**
+ * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
+ *
+ * @return null
+ */
+ public function setKernel(KernelInterface $kernel)
+ {
+ $this->kernel = $kernel;
+ }
+
+ /**
* @return array
*/
protected function getMetadata()
@@ -62,35 +79,14 @@ protected function getMetadata()
*/
protected function getEntityManager()
{
- return $this->getContainer()->get('doctrine.orm.entity_manager');
+ return $this->kernel->getContainer()->get('doctrine.orm.entity_manager');
}
/**
* @return array
*/
protected function getConnections()
{
- if ($this->getContainer()->has('behat.mink')) {
- $driver = $this->getMinkContext()->getSession()->getDriver();
-
- if ($driver instanceof \Behat\MinkBundle\Driver\SymfonyDriver) {
- return $driver->getClient()->getContainer()->get('doctrine')->getConnections();
- }
- }
-
- return $this->getContainer()->get('doctrine')->getConnections();
- }
-
- /**
- * Gets the Mink context.
- *
- * If you are using MinkContext as a subcontext instead of using it as
- * the main one, overwrite this method
- *
- * @return \Behat\Mink\Behat\Context\BaseMinkContext
- */
- protected function getMinkContext()
- {
- return $this->getMainContext();
+ return $this->kernel->getContainer()->get('doctrine')->getConnections();
}
}
@@ -2,19 +2,29 @@
namespace Behat\CommonContexts;
-use Symfony\Component\HttpKernel\KernelInterface;
use Behat\Behat\Context\BehatContext;
+use Behat\Symfony2Extension\Context\KernelAwareInterface;
+use Behat\MinkExtension\Context\RawMinkContext;
+use Symfony\Component\HttpKernel\KernelInterface;
/**
* Provides some steps/methods which are useful for testing a Symfony2 application.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
-class SymfonyMailerContext extends BehatContext
+class SymfonyMailerContext extends RawMinkContext implements KernelAwareInterface
{
- private $kernel;
+ /**
+ * @var \Symfony\Component\HttpKernel\KernelInterface $kernel
+ */
+ private $kernel = null;
- public function __construct(KernelInterface $kernel)
+ /**
+ * @param \Symfony\Component\HttpKernel\KernelInterface $kernel
+ *
+ * @return null
+ */
+ public function setKernel(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
@@ -91,7 +101,7 @@ public function emailWithSubjectShouldHaveBeenSent($subject, $to = null)
public function loadProfile($token = null)
{
if (null === $token) {
- $headers = $this->getMinkContext()->getSession()->getResponseHeaders();
+ $headers = $this->getSession()->getResponseHeaders();
if (!isset($headers['X-Debug-Token']) && !isset($headers['x-debug-token'])) {
throw new \RuntimeException('Debug-Token not found in response headers. Have you turned on the debug flag?');
@@ -101,17 +111,4 @@ public function loadProfile($token = null)
return $this->kernel->getContainer()->get('profiler')->loadProfile($token);
}
-
- /**
- * Gets the Mink context.
- *
- * If you are using MinkContext as a subcontext instead of using it as
- * the main one, overwrite this method
- *
- * @return \Behat\Mink\Behat\Context\BaseMinkContext
- */
- protected function getMinkContext()
- {
- return $this->getMainContext();
- }
}
View
@@ -16,26 +16,17 @@ that:
namespace Acme\DemoBundle\Features\Context;
-use Behat\BehatBundle\Context\BehatContext,
- Behat\BehatBundle\Context\MinkContext;
-use Behat\Behat\Context\ClosuredContextInterface,
- Behat\Behat\Context\TranslatedContextInterface,
- Behat\Behat\Exception\PendingException;
-use Behat\Gherkin\Node\PyStringNode,
- Behat\Gherkin\Node\TableNode;
+use Behat\Behat\Context\BehatContext;
+use Behat\CommonContexts\SymfonyMailerContext;
/**
* Feature context.
*/
-class FeatureContext extends MinkContext
+class FeatureContext extends BehatContext
{
- public function __construct($kernel)
+ public function __construct()
{
- $this->useContext('symfony_extra',
- new \Behat\CommonContexts\SymfonyMailerContext($kernel)
- );
-
- parent::__construct($kernel);
+ $this->useContext('symfony_extra', new SymfonyMailerContext());
}
}
View
@@ -34,10 +34,10 @@
},
"suggest": {
- "behat/behat-bundle": "*",
- "behat/mink-bundle": "*",
- "doctrine/doctrine-bundle": "*",
- "kriswallsmith/buzz": ">=0.5.0"
+ "behat/symfony2-extension": "*",
+ "behat/mink-extension": "*",
+ "doctrine/doctrine-bundle": "*",
+ "kriswallsmith/buzz": ">=0.5.0"
},
"autoload": {

5 comments on commit fdd3472

@everzet I'm trying to figure out how you'd use these context's in your own FeatureContext for v2.4. Because they now require a $kernel (via setKernel($kernel)), but we have no way to get this during the FeatureContext constructor:

<?php

    /**
     * Initializes context.
     */
    public function __construct()
    {
        // $kernel does not exist here, so cannot set it on the SymfonyDoctrineContext ... ?
        $this->useContext('symfony_doctrine', new SymfonyDoctrineContext());
    }

Unless this project is in an intermin state and you are converting it to extensions???

Owner

stof replied Jun 19, 2012

@jonathaningram you don't need to pass it. The subcontexts are initialized by the initializers too

@stof I just debugged all uses of:

<?php

foreach ($context->getSubcontexts() as $subcontext) {
    // ...
}

(there were 3 uses).

And at no point does my main FeatureContext equal $context - thus it's sub-contexts are not initialised...any ideas why not?

Owner

everzet replied Jun 19, 2012

@jonathaningram gist your suite configuration and context class. We can't guess without info (well, we can, but it woun't be efficient) :)

@everzet https://gist.github.com/2952919 - haha I didn't expect you to guess :)

So I've just debugged again at Behat\Symfony2Extension\Context\Initializer:

<?php

    /**
     * Initializes provided context.
     *
     * @param ContextInterface $context
     */
    public function initialize(ContextInterface $context)
    {
        $context->setKernel($this->kernel);
    }

And that line is never called (hence I assume that the SymfonyDoctrineContext never gets the kernel injected.

Note: I'm also trying to do this with my own domain context (for nice separation of feature steps), but this minimal working example should demonstrate the problem, no?

Please sign in to comment.