我正在使用API平台2.1和Symfony 4,我使用LexikJWTAuthenticationBundle进行身份验证,使用Behat进行测试。
我无法正确设置。这是我到目前为止的配置:
<form class="form-for">
<div>
<div class="form-group">
<label class="form-for-label">Name</label>
<input type="text" name="Name" value="" placeholder="Name">
</div>
</div>
<div>
<div class="form-group">
<label class="form-for-label">Description</label>
<input type="text" name="Description" value="" placeholder="Description">
</div>
</div>
<div>
<div class="form-group">
<label class="form-for-label">Creation Date</label>
<input type="text" name="Creation Date" value="" placeholder="Creation Date">
</div>
</div>
<input type="submit" />
</form>
以下是我的功能背景:
Feature: Books feature
@createSchema @dropSchema
Scenario: Adding a new book
When I add "Content-Type" header equal to "application/json"
And I add "Accept" header equal to "application/json"
And I send a "POST" request to "/api/books" with body:
"""
{
"title": "King",
"author": "T. M. Frazier",
"enabled": true
}
"""
Then the response status code should be 201
And the response should be in JSON
And the header "Content-Type" should be equal to "application/json"
And the JSON nodes should contain:
| title | King |
| author | T. M. Frazier |
And the JSON node "enabled" should be true
现在执行<?php
use Behat\Behat\Context\Context;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use App\Entity\User;
use Behatch\Context\RestContext;
use Behat\Behat\Context\SnippetAcceptingContext;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Tools\SchemaTool;
/**
* This context class contains the definitions of the steps used by the demo
* feature file. Learn how to get started with Behat and BDD on Behat's website.
*
* @see http://behat.org/en/latest/quick_start.html
*/
class FeatureContext implements Context, SnippetAcceptingContext
{
/**
* @var KernelInterface
*/
private $kernel;
private $manager;
private $jwtManager;
private $schemaTool;
private $response;
private $classes;
private $restContext;
public function __construct(KernelInterface $kernel,$manager,$jwtManager)
{
$this->kernel = $kernel;
$this->manager = $manager->getManager();
$this->jwtManager = $jwtManager;
$this->schemaTool = new SchemaTool($this->manager);
$this->classes = $this->manager->getMetadataFactory()->getAllMetadata();
}
/**
* @When a demo scenario sends a request to :path
*/
public function aDemoScenarioSendsARequestTo(string $path)
{
$this->response = $this->kernel->handle(Request::create($path, 'GET'));
}
/**
* @Then the response should be received
*/
public function theResponseShouldBeReceived()
{
if ($this->response === null) {
throw new \RuntimeException('No response received');
}
}
/**
* @BeforeScenario @createSchema
*/
public function createDatabase()
{
$this->schemaTool->createSchema($this->classes);
}
/**
* @AfterScenario @dropSchema
*/
public function dropDatabase()
{
$this->schemaTool->dropSchema($this->classes);
}
/**
* @BeforeScenario
* @login
*
* @see https://symfony.com/doc/current/security/entity_provider.html#creating-your-first-user
*/
public function login(BeforeScenarioScope $scope)
{
$user = new User();
$user->setUsername('admin');
$user->setPassword('ATestPassword');
$user->setEmail('test@test.com');
$this->manager->persist($user);
$this->manager->flush();
$token = $this->jwtManager->create($user);
$this->restContext = $scope->getEnvironment()->getContext(RestContext::class);
$this->restContext->iAddHeaderEqualTo('Authorization', "Bearer $token");
}
/**
* @AfterScenario
* @logout
*/
public function logout() {
$this->restContext->iAddHeaderEqualTo('Authorization', '');
}
}
时,它只是读取vendor/bin/behat
文件(开发环境)并尝试将管理员添加到我的开发数据库中。
1)如何确保创建测试数据库并且不使用dev db进行测试?我尝试使用不同的配置创建.env
,但这不起作用。
2)即使没有方案上方的.env.test
注释,它也会从@login
到达login
方法。这是怎么回事?
3)我无法在任何地方找到适当的文档如何进行设置。 API平台文档似乎也不是很有帮助。参考:https://api-platform.com/docs/core/jwt/#jwt-authentication
他们谈论FeatureContext
和createDB
,但它甚至不存在于任何地方。所以我从另一个网站上拿走了它。这是正确的方法吗?
答案 0 :(得分:1)
对于1)和3),我没有答案,但是对于2)
/**
* @BeforeScenario
* @login
*/
应该在一行上,就像这样:
/**
* @BeforeScenario @login
*/