2013-06-14 4 views
3

테스트를 처음 받았고 관리 영역 (방화벽 뒤)에 대한 기능 테스트를 작성하는 데 심각한 문제가 있습니다.기능 테스트에서 인증이 작동하지 않습니다.

첫 번째 반복에서 나는 http://symfony.com/doc/2.2/cookbook/testing/http_authentication.html에 설명 된 방법을 사용했으며 효과가있었습니다. 그러나 나는 데이터베이스가 테스트에서 삽입 된 것들로 어수선하게 흩어지고있는 문제가있었습니다.

다음 반복에서 SQLLite를 사용했습니다 ... 갑자기 인증이 더 이상 작동하지 않아 로그인 페이지로 리디렉션됩니다.

데이터가 SQLLite 데이터베이스에 올바르게로드되는지 어떻게 확인할 수 있습니까?
그 밖의 원인으로이 문제가 발생할 수 있습니까?

내가 지금까지 가지고 :

config_test.yml

imports: 
    - { resource: parameters.yml } 
    - { resource: config_dev.yml } 

framework: 
    test: ~ 
    session: 
     storage_id: session.storage.mock_file 

web_profiler: 
    toolbar: false 
    intercept_redirects: false 

swiftmailer: 
    disable_delivery: true 

doctrine: 
    dbal: 
     driver: pdo_sqlite 
     path: %kernel.root_dir%/../data/%kernel.environment%/database.sqlite 
    orm: 
     auto_generate_proxy_classes: true 
     auto_mapping: true 

security: 
    firewalls: 
     admin: 
      http_basic: 

WebDoctrineTestCase.php

namespace Yanic\EventBundle\Tests\Controller; 

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 
use Symfony\Bundle\FrameworkBundle\Console\Application; 
use Symfony\Component\Console\Input\ArrayInput; 

require_once dirname(__DIR__).'/../../../../app/AppKernel.php'; 

class WebDoctrineTestCase extends WebTestCase 
{ 
    protected static $entityManager; 
    protected static $client; 
    protected static $application; 

    protected static $isFirstTest = true; 

    public function setUp() 
    { 
     parent::setUp(); 

     /*static::$client = static::createClient(
      array(), 
      array(
       'PHP_AUTH_USER' => 'myUsername', 
       'PHP_AUTH_PW' => 'myPassword' 
      ) 
     );*/ 

     static::$kernel = new \AppKernel('test', true); 
     static::$kernel->boot(); 

     if (!$this->useCachedDatabase()) { 
      $this->databaseInit(); 
      $this->loadFixtures(); 
     } 
    } 

    /** 
    * Initialize database 
    */ 
    protected function databaseInit() 
    { 
     static::$entityManager = static::$kernel 
      ->getContainer() 
      ->get('doctrine.orm.entity_manager'); 

     static::$application = new \Symfony\Bundle\FrameworkBundle\Console\Application(static::$kernel); 

     static::$application->setAutoExit(false); 
     $this->runConsole("doctrine:schema:drop", array("--force" => true)); 
     $this->runConsole("doctrine:schema:create"); 
     $this->runConsole("cache:warmup"); 
    } 

    /** 
    * Load tests fixtures 
    */ 
    protected function loadFixtures() 
    { 
     $this->runConsole("doctrine:fixtures:load"); 
    } 

    /** 
    * Use cached database for testing or return false if not 
    */ 
    protected function useCachedDatabase() 
    { 
     $container = static::$kernel->getContainer(); 
     $registry = $container->get('doctrine'); 
     $om = $registry->getEntityManager(); 
     $connection = $om->getConnection(); 

     if ($connection->getDriver() instanceOf SqliteDriver) { 
      $params = $connection->getParams(); 
      $name = isset($params['path']) ? $params['path'] : $params['dbname']; 
      $filename = pathinfo($name, PATHINFO_BASENAME); 
      $backup = $container->getParameter('kernel.cache_dir') . '/'.$filename; 

      // The first time we won't use the cached version 
      if (self::$isFirstTest) { 
       self::$isFirstTest = false; 
       return false; 
      } 

      self::$isFirstTest = false; 

      // Regenerate not-existing database 
      if (!file_exists($name)) { 
       @unlink($backup); 
       return false; 
      } 

      $om->flush(); 
      $om->clear(); 

      // Copy backup to database 
      if (!file_exists($backup)) { 
       copy($name, $backup); 
      } 

      copy($backup, $name); 
      return true; 
     } 

     return false; 
    } 

    /** 
    * Executes a console command 
    * 
    * @param type $command 
    * @param array $options 
    * @return type integer 
    */ 
    protected function runConsole($command, Array $options = array()) 
    { 
     $options["--env"] = "test"; 
     $options["--quiet"] = null; 
     $options["--no-interaction"] = null; 
     $options = array_merge($options, array('command' => $command)); 
     return static::$application->run(new \Symfony\Component\Console\Input\ArrayInput($options)); 
    } 
} 

VenueControllerTest.php

namespace Yanic\EventBundle\Tests\Controller; 

class VenueControllerTest extends WebDoctrineTestCase 
{ 
    public function testNew() 
    { 
     $crawler = static::$client->request('GET', '/admin/de/venues/new'); 

     $this->assertTrue(
      static::$client->getResponse()->isSuccessful(), 
      "Couldn't load form" 
     ); 

     $this->assertFalse(
      static::$client->getResponse()->isRedirect(), 
      'The request should not be redirected' 
     ); 
    } 

    public function testCreate() 
    { 
     static::$client->request('GET', '/admin/de/venues/create'); 

     $this->assertEquals(
      405, 
      static::$client->getResponse()->getStatusCode(), 
      'We should get Method not allowed' 
     ); 

     $crawler = static::$client->request('GET', '/admin/de/venues/new'); 

     $form = $crawler->selectButton('Anlegen')->form(); 

     $country = $crawler->filter('#venue_Country option')->eq(1)->attr('value'); 
     $city = $crawler->filter('#venue_City option')->eq(1)->attr('value'); 

     $form['venue[name]']->setValue('Test venue'); 
     $form['venue[address]']->setValue('any address'); 
     $form['venue[zip]']->setValue('12345'); 
     $form['venue[Country]']->select($country); 
     $form['venue[City]']->select($city); 

     $crawler = static::$client->submit($form); 

     $this->assertTrue(
      static::$client->getResponse()->isSuccessful(), 
      'Posting form return not successful' 
     ); 

     $this->assertTrue(
      static::$client->getResponse()->isRedirect(), 
      'The request should be redirected' 
     ); 
    } 
} 
+0

귀하는 귀하의/관리자 페이지에 액세스하는 데 필요한 실제 역할을 가진 활성화 된/활성화 된 사용자를 생성하는지 확인할 수 있습니까? – nifr

+0

그래 ... 확실해. – Michi

+1

마침내 https://github.com/liip/LiipFunctionalTestBundle을 발견하고 작동 시켰습니다. – Michi

답변

0

을 LiipFunctionalTes로 tBundle :

$this->loadFixtures($this->getBundleFixtures(), null); 

    $client = static::makeClient(false, array(
     'PHP_AUTH_USER' => 'smith', 
     'PHP_AUTH_PW' => 'Smith123456', 
    ));