2014-05-13 24 views
5

SituazioneBehat 3 entro symfony2.4 (accesso dottrina)

voglio usare BDD e Behat nei miei progetti symfony da ora in poi. Il progetto attuale è sf2.4 e mi sforzo di far funzionare Behat 3. Io uso the latest doc concerning behat3, come raccomandato da jakub in this post.

problema

Behat 3 sembra funzionare bene. Tuttavia, per essere in grado di iniziare veramente, ho bisogno di avere accesso al Kernel (container, doctrine, ecc ...). Ho provato con my_project, testando il progetto riproducendo ls esempio per Behat. Tuttavia, utilizzando $ this-> contenitore(), $ this-> kernel-> getContainer() solleva invariabilmente un 'eccezione in attesa' (codice si ferma a iShouldGet passo):

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 
     ); 
    } 
} 

ho cercato di creare la stessa Behat test 'ls' entro AcmeDemoBundle:

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

Tuttavia, si genera un errore:

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

Soluzione

Potrebbe essere dovuto all'utilizzo di Behat3, ma non sono sicuro. Qualche suggerimento sul perché questo problema si verifica/come risolverlo? In generale, un buon consiglio su come integrare behat al progetto symfony2 (2.4) sarebbe molto apprezzato.

Grazie mille in anticipo.

saluti,


NB: Qui ci sono i miei file:

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; 

risposta

0

Con Behat 3.0. 14 e symfony 2.3, avevo bisogno di aggiungere il SE istruzione nella parte superiore della classe FeatureContext:

use Behat\Symfony2Extension\Context\KernelAwareContext; 
use Behat\Symfony2Extension\Context\KernelDictionary; 

La classe caratteristica anche necessaria per implementare l'interfaccia KernelAwareContext in questo modo:

class FeatureContext extends MinkContext implements KernelAwareContext 

Dopo di che, sono stato in grado di accedere al contenitore symfony nel FeatureContext di classe, come nel step argument transfomation nell'esempio qui sotto:

/** 
* @Transform :location 
*/ 
public function castAddressToLocation($location) 
{ 
    return $this->getContainer()->get('geocoder')->getLocation($location); 
} 
2

Finora questo sempre lavorato per me. Ho aggiunto esempi di utilizzo per il tuo Gherkin, quindi è abbastanza semplice.

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 
    } 
} 
Problemi correlati