如何使用silex测试安全端点?

时间:2016-04-04 07:57:09

标签: php symfony phpunit silex

我找到了这个文档:http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html但问题是,silex的WebTestCase版本没有getContainer所以我无法覆盖Session

我尝试了以下方式:

public function createApplication() {
   include __DIR__ . '/../../../../../web/index_dev.php';

   //$app['exception_handler']->disable();
   $app['debug'] = true;
   $app['session.storage'] = $app->share(function() {
            return new MockArraySessionStorage();
   });
   $app['session.test'] = true;

   return $app;
}

public function login() {
   $session = $this->app['session'];

   $firewall = 'admin';
   $token = new UsernamePasswordToken('admin', null, $firewall, array('ROLE_SUPERADMIN'));
   $session->set('_security_' . $firewall, serialize($token));
   $session->save();

   $cookie = new Cookie($session->getName(), $session->getId());
   $this->client->getCookieJar()->set($cookie);
}

但是测试失败了: RuntimeException: Failed to start the session because headers have already been sent by "/Users/user/Documents/Projects/project/vendor/phpunit/phpunit/src/Util/Printer.php"

有什么想法吗?

UPD#1:提供商按以下方式加载:

$app->register(new SessionServiceProvider());
$app['session.storage.options'] = array(
    'name' => $app['sessionOptions']['name'],
    'cookie_lifetime' => $app['sessionOptions']['cookieLifetime'],
    'cookie_domain' => $app['sessionOptions']['cookieDomain']
);
$app['session.storage.handler'] = $app->share(
    function ($app) {
        return new MongoDBSessionHandler(
            $app['mongo.client'],
            array(
                'database' => $app['mongodbOptions']['database'],
                'collection' => $app['mongodbExtraOptions']['sessionCollection'],
            )
        );
    }
);

UPD#2:

public function testListAction() {
    //$this->logIn();

    $this->client->request('GET', $this->baseUri);

    $this->assertTrue($this->client->getResponse()->isOk());
}
public function setUp() {
    parent::setUp();

    $this->client = parent::createClient();
}

0 个答案:

没有答案