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

2024-04-18

我将 API Platform 2.1 与 Symfony 4 结合使用,并使用 LexikJWTAuthenticationBundle 进行身份验证,并使用 Behat 进行测试。

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

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('[email protected] /cdn-cgi/l/email-protection');

        $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文件(开发环境)并尝试将管理员添加到我的开发数据库中。

1)如何确保创建测试数据库并且不使用开发数据库进行测试?我尝试过创建一个.env.test使用不同的配置,但这不起作用。

2)即使没有@login其到达场景上方的注释login方法从FeatureContext。事情应该是这样的吗?

3)我在任何地方都找不到如何设置的正确文档。 API 平台文档似乎也不是很有帮助。参考:https://api-platform.com/docs/core/jwt/#jwt-authentication https://api-platform.com/docs/core/jwt/#jwt-authentication他们谈论一个createDB and dropDB,但它根本不存在于任何地方。所以我从另一个网站上拿来的。这是正确的做法吗?


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

    /**
     * @BeforeScenario
     * @login
     */

应该在一行上,如下所示:

    /**
     * @BeforeScenario @login
     */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Symfony 4:JWT 和 Behat 的测试数据库 的相关文章

随机推荐

  • Spark SQL 未正确转换时区[重复]

    这个问题在这里已经有答案了 使用 Scala 2 10 4 和 Spark 1 5 1 和 Spark 1 6 sqlContext sql select id to date from utc timestamp from unixtim
  • 如何在Android中的seekbar下方显示分隔符值?

    我添加了一个seekbar我的一项活动 它的最大值是 5 现在 我想在搜索栏下方显示分隔符值 增量为 1 如 0 1 2 3 4 和 5 我怎样才能做到这一点 有没有什么系统方法可以实现这一目标 但我无法亲自动手 欢迎任何意见 注意 我想以
  • PHP Ajax上传进度条

  • 什么是消息边界?

    什么是 消息边界 在以下情况下 TCP 和 UDP 之间的区别之一是 UDP 保留消息 边界 我理解之间的区别TCP and UDP 但我不确定的定义 消息边界 由于 UDP 在每个单独的数据包中包含目的地和端口信息 因此是否可以为消息提供
  • 使用 terraform 将公共 GKE 更改为私有 GKE 集群

    如何将现有的GKE集群更改为GKE私有集群 我是否能够根据防火墙规则从互联网连接到 Kubectl API 还是应该拥有堡垒主机 我不想实施Cloud Nat or nat gateway 我有一个鱿鱼代理虚拟机 可以处理 Pod 的互联网
  • 如何使用 javascript/jquery 设置 asp 面板元素可见/隐藏

    我有一个asp Panel我的页面上的元素 我可以在后面的代码中设置其可见性 但我还需要通过 javascipt 隐藏它 My panel定义如下
  • 使用 docker-client api 将镜像推送到 docker 注册表

    在探索 docker client api java 时 设置与虚拟机上运行的 docker 守护进程的连接到底需要哪些证书 我在网上找到的代码 Create a client based on DOCKER HOST and DOCKER
  • Hibernate 单向一对多关联 - 为什么连接表更好?

    在本文档中 向下滚动到单向部分 http docs jboss org hibernate stable annotations reference en html single entity mapping association col
  • 自动替换在 emacs 中输入的某些文本字符串

    有一个小细节一直困扰着我一段时间 即我经常打字 inclued代替 include 如果不是很明显的话 我编写了大量 C 和 C 程序 这个拼写错误破坏了无数的构建并消耗了本来可以用来喝咖啡或冲浪的时间 当然 emacs 可以提供帮助并在我
  • 当 ExceptionMapper 创建响应时,未遍历 RestEasy 后处理拦截器链

    我正在使用 RestEasy 构建我的 Restful Web 服务 我已经实现了 ExceptionMappers 来准备特定的异常响应 我还实现了 MessageBodyWriterInterceptors 和几个 PostProces
  • EasyMock的使用方法

    期望似乎对我不起作用 package com jjs caf library client drafting import static org junit Assert import org easymock EasyMock impor
  • 用于从段落中删除所有属性的正则表达式

    我知道正则表达式通常不应该用于解析 html 内容 在我的特殊情况下 我需要它们 原因是 我使用 rte 编辑器 并且在粘贴到编辑器中时需要对段落属性进行一些替换 我有类似的东西 p text blah blah p 我需要删除所有属性 以
  • 数组中的clear方法

    我正在尝试创建一个清除方法来清除我拥有的数组 我已经看到使用清除方法是我所需要的 但我似乎无法使用它 list clear 我认为我必须做的 public void clear return doctors clear 顺便说一下 医生是一
  • 参数和属性之间的区别[重复]

    这个问题在这里已经有答案了 可能的重复 getAttribute 和 getParameter 之间的区别 https stackoverflow com questions 5243754 difference between getat
  • 当我的 MySQL 表更新时,如何收到电子邮件?

    您好 我想知道 MySQL 中是否有一种方法可以在 MySQL 表中添加一行时自动向自己发送电子邮件 实现这一目标的最佳方法是使用触发器和 cron 创建一个 通知队列 表 并在将行插入所需表时使用触发器填充该表 eg CREATE TAB
  • 如何在 R 中创建类似箱线图的分类散点图?

    有谁知道如何创建散点图R创建像这样的情节these http graphpad com support faq graph tip how can i make a barcolumn graph that also shows the i
  • 使用 zip4j 重命名 zip 中的文件

    我在用着zip4j http www lingala net zip4j download php1 3 1 在我的应用程序中压缩文件 现在我尝试重命名 zip 内的文件 而不必重命名文件本身 似乎有一种方法可以做到这一点 但它不起作用 我
  • 将当前日期设置为在 Bootstrap 日期选择器中选择

    我在用引导日期选择器 https github com eternicode bootstrap datepicker在我的代码中 如何在 JavaScript 中选择当前日期并将其显示为已选择 经过研究 这是我正在使用的代码 但当天没有显
  • 本地 JS 文件的 Typescript 声明文件

    当我们正在转换为 Typescript 的过程中 我正在尝试为工作中的 Javascript 文件添加类型 但是 我无法识别声明文件 这是我的文件结构 js Foo js typings Foo 索引 d ts index ts 包 jso
  • Symfony 4:JWT 和 Behat 的测试数据库

    我将 API Platform 2 1 与 Symfony 4 结合使用 并使用 LexikJWTAuthenticationBundle 进行身份验证 并使用 Behat 进行测试 我无法正确设置 这是到目前为止我的配置 Feature