Symfony 4:用于JWT和Behat的测试数据库

编程入门 行业动态 更新时间:2024-10-26 10:28:59
本文介绍了Symfony 4:用于JWT和Behat的测试数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用带有Symfony 4的API Platform 2.1,并且正在使用LexikJWTAuthenticationBundle进行身份验证,并使用Behat进行测试.

I am using API Platform 2.1 with Symfony 4 and I am using the LexikJWTAuthenticationBundle for authentication, and Behat for testing.

我无法正确设置.到目前为止,这是我的配置:

I am unable to set things up properly. Here is my configuration so far:

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 behat/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 symfony/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'); $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 文件(开发环境)并尝试向我的开发数据库添加管理员.

Now when executing vendor/bin/behat, it simply reads the .env file (dev environment) and tries to add an admin to my development database.

1)如何确保创建了测试数据库,并且未将dev数据库用于测试?我尝试用不同的配置创建一个 .env.test ,但是没有用.

1) How do I ensure that a test db is created and the dev db is not used for testing? I have tried creating a .env.test with a different config but that didn't work.

2)即使场景上方没有 @login 批注,它也会从 FeatureContext 到达 login 方法.那应该是这样吗?

2) even without the @login annotation above the scenario it reaches the login method from the FeatureContext. Is that how it is supposed to be?

3)我找不到任何有关如何进行设置的适当文档.API平台文档似乎也不是很有帮助.参考: api-platform/docs/core/jwt/#jwt身份验证他们谈论的是 createDB 和 dropDB ,但是它甚至在任何地方都不存在.因此,我从另一个网站上获取了它.这是正确的方法吗?

3) I can't find proper documentation anywhere of how to set this up. The API Platform docs don't seem to be very helpful either. Reference: api-platform/docs/core/jwt/#jwt-authentication They talk about a createDB and dropDB, but it doesn't even exist anywhere. So I took it from another website. Is this the right way to do it?

推荐答案

对于1)和3),我没有答案,但是对于2)

I have no answer for 1) and 3), but for 2)

/** * @BeforeScenario * @login */

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

should be on a single line, like so:

/** * @BeforeScenario @login */

更多推荐

Symfony 4:用于JWT和Behat的测试数据库

本文发布于:2023-08-05 20:45:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   测试   Symfony   JWT   Behat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!