- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是测试新手,在尝试为管理区域(防火墙后)编写功能测试时遇到了一个主要问题。
在我的第一次迭代中,我使用了 http://symfony.com/doc/2.2/cookbook/testing/http_authentication.html 中描述的方式它奏效了。但是我遇到了数据库被测试插入的东西弄得乱七八糟的问题。
在下一次迭代中,我开始使用 SQLLite... 突然身份验证不再起作用,我被重定向到登录页面。
如何确保数据正确加载到 SQLLite 数据库中?
还有什么可能导致此问题?
我目前拥有的:
配置测试.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'
);
}
}
最佳答案
使用 LiipFunctionalTestBundle :
$this->loadFixtures($this->getBundleFixtures(), null);
$client = static::makeClient(false, array(
'PHP_AUTH_USER' => 'smith',
'PHP_AUTH_PW' => 'Smith123456',
));
关于symfony - 身份验证在功能测试中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17110787/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!