Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Behat 3 within symfony2.4 (doctrine access)

Situation

I want to use BDD and Behat in my symfony projects from now on. Current project is sf2.4, and I strive to make Behat 3 work. I use the latest doc concerning behat3, as recommanded by jakub in this post.

issue

Behat 3 seems to work well. However, in order to be able to really start, i need to have access to the Kernel (container, doctrine, etc...). I tried with my_project, testing project reproducing ls example for Behat. However, using $this->container(), $this->kernel->getContainer() invariably raises a 'pending exception' (code stops at iShouldGet step):

public function iShouldGet(PyStringNode $string)
{
    //$container = $this->kernel->getContainer();
    //$container = $this->getContainer();
    //$doctrine = $this->getContainer()->get('doctrine');
    if ((string) $string !== $this->output) {
        throw new Exception(
            "Actual output is:\n" . $this->output
        );
    }
}

I tried to create the same behat 'ls' test within AcmeDemoBundle:

|Acme 
    |Demo
        |Features
            |Context/FeatureContext.php
            ls.feature
                

However, it raises an error:

[Behat\Testwork\Tester\Exception\WrongPathsException]    
No specifications found at path(s) `@AcmeDemoBundle`. 

Solution

It might be due to use of Behat3, but I am not sure. Any hint why this issue occurs / how to solve it? In general, good advice on how to integrate behat to symfony2 (2.4) project would be much appreciated.

Thanks a lot in advance.

Regards,


NB: Here are my files:

behat.yml

# behat.yml
default:
  suites:
      my_suite:
          type: symfony_bundle
          bundle: AcmeDemoBundle
          mink_session: default
          mink_javascript_session: selenium2
  extensions:
      #Behat\MinkExtension\Extension:
      #Behat\MinkExtension\ServiceContainer\MinkExtension:
      Behat\MinkExtension:
          base_url: 'http://demo.com' 
          # this will be the url of our application
          #base_url: 'http://wikipedia.org'
          sessions:
              default:
                  goutte: ~
              selenium2:
                  selenium2: ~
      Behat\Symfony2Extension: ~

app/autoload.php

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/**
 * @var ClassLoader $loader
 */
$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));


$loader->add('Behat\Gherkin',realpath(__DIR__.'/../vendor/behat/gherkin/src'));
$loader->add( 'Behat\Behat' ,realpath(__DIR__.'/../vendor/behat/behat/src'));
$loader->add('Behat\BehatBundle' ,realpath(__DIR__.'/../vendor/bundles'));

return $loader;
like image 707
Wisebes Avatar asked Nov 01 '22 23:11

Wisebes


1 Answers

So far this always worked for me. I added example usages for your Gherkin so it is fairly straight forward.

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Symfony\Component\HttpKernel\KernelInterface;

class FeatureContext extends MinkContext implements KernelAwareContext
{
    protected $kernel;

    public function setKernel(KernelInterface $kernelInterface)
    {
        $this->kernel = $kernelInterface;
    }

    /**
     * @Given /^I can access service container$/
     */
    public function iCanAccessServiceContainer()
    {
        $container = $this->kernel->getContainer();
        return $container->getParameter('whatever');
    }

    /**
     * @Given /^I can access entity manager$/
     */
    public function iCanAccessEntityManager()
    {
        $em = $this->kernel->getContainer()->get('doctrine')->getManager();
        // So on
    }

    /**
     * @Given /^I can access repository$/
     */
    public function iCanAccessRepository()
    {
        $em = $this->kernel->getContainer()->get('doctrine')->getManager();
        $repo = $em->getRepository('WhateverBundle:WhateverEntity');
        // So on
    }
}
like image 167
BentCoder Avatar answered Nov 11 '22 17:11

BentCoder