Symfony 4:JWT和Behat的测试DB



我正在使用Symfony 4的API平台2.1,我正在使用Lexikjwtauthentication -bundle进行身份验证,并进行测试。

我无法正确设置东西。这是我到目前为止的配置:

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 BehatBehatContextContext;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpKernelKernelInterface;
use BehatBehatHookScopeBeforeScenarioScope;
use AppEntityUser;
use BehatchContextRestContext;
use BehatBehatContextSnippetAcceptingContext;
use DoctrineCommonPersistenceObjectManager;
use DoctrineORMToolsSchemaTool;

/**
 * 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时,它只会读取.env文件(DEV环境(,然后尝试将管理员添加到我的开发数据库中。

1(如何确保创建测试数据库并且不使用Dev DB进行测试?我尝试创建具有不同配置的.env.test,但这不起作用。

2(即使没有@login的注释,即可从FeatureContext到达login方法。那应该是什么吗?

3(我找不到正确的文档。API平台文档似乎也不是很有帮助。参考:https://api-platform.com/docs/core/jwt/#jwt-authentication他们谈论createDBdropDB,但在任何地方甚至都不存在。所以我从另一个网站上拿走了它。这是正确的方法吗?

我没有答案1(和3(,但是2(

    /**
     * @BeforeScenario
     * @login
     */

应该在一行上,就像这样:

    /**
     * @BeforeScenario @login
     */

最新更新