【问题标题】:PHP Integration test using Behat使用 Behat 进行 PHP 集成测试
【发布时间】:2021-04-28 20:04:52
【问题描述】:

我是集成测试的新手,我的任务是创建集成测试场景,用于 cli: bin/console import:products products.xml,它负责从 xml 文件中读取数据并将数据插入到数据库表中。

final class ProductImporter extends AbstractImporter implements ProductImporterInterface
{
private ResourceResolverInterface $productResourceResolver;

private ProductRelationsPurifierInterface $productRelationsPurifier;

private ProductImagesAssignerInterface $productImagesAssigner;

private ProductChannelsAssignerInterface $productChannelsAssigner;

private ProductDesignerAssignerInterface $productDesignerAssigner;

private static array $channels = [];

private static array $designers = [];

public function __construct(
    ResourceResolverInterface $productResourceResolver,
    ProductRelationsPurifierInterface $productRelationsPurifier,
    ProductImagesAssignerInterface $productImagesAssigner,
    ProductChannelsAssignerInterface $productChannelsAssigner,
    ProductDesignerAssignerInterface $productDesignerAssigner
) {
    $this->productResourceResolver = $productResourceResolver;
    $this->productRelationsPurifier = $productRelationsPurifier;
    $this->productImagesAssigner = $productImagesAssigner;
    $this->productChannelsAssigner = $productChannelsAssigner;
    $this->productDesignerAssigner = $productDesignerAssigner;
}

public function import(array $row): ?ResourceInterface
{
    $code = $this->getColumnValue(self::CODE_TAG, $row);
    $designerId = (int) $this->getColumnValue('designerID', $row);
    $attributes = $this->getColumnValue(self::ATTRIBUTES_TAG, $row);
    $productChannels = $this->getColumnValue('channelAttributes', $row)['channel'];
    $images = $this->getColumnValue(self::IMAGES_TAG, $row)['image'];
    $isHidden = (bool) $attributes['isHidden'];

    /** @var ProductInterface $product */
    $product = $this->productResourceResolver->resolveResource($code);

    if (null !== $product->getId()) {
        // only if product is already in db
        $this->productRelationsPurifier->purifyRelations($product);
    }

    $product->setCode($code);
    $product->setEnabled(!$isHidden);
    $this->productImagesAssigner->assignImages($product, $images);
    $this->productDesignerAssigner->assign($product, $designerId);
    $this->productChannelsAssigner->assign($product, $productChannels);

    return $product;
}

public function getResourceCode(): string
{
    return 'product';
}

public static function setChannels(array $channels): void
{
    self::$channels = $channels;
}

public static function getChannels(): array
{
    return self::$channels;
}

public static function addDesigner(DesignerInterface $designer): void
{
    self::$channels[] = $designer;
}

public static function getDesigners(int $designerId): ?DesignerInterface
{
    /** @var DesignerInterface $designer */
    foreach (self::$designers as $designer) {
        if ($designer->getId() === $designerId) {
            return $designer;
        }
    }

    return null;
 }
}

我的问题不是为我编写测试,而是向我展示如何/从哪里开始编写集成测试,以及我需要执行的步骤。

【问题讨论】:

  • 到目前为止你尝试过什么?你被困在哪里了?

标签: php testing integration-testing behat


【解决方案1】:

首先,您需要设置 behat(如果您还没有这样做的话)。大致意思是:

  • 通过作曲家需要它
  • 添加配置
  • 添加第一个上下文
  • 编写 .feature 文件

功能文件以人类可读的语言编写,而上下文将其转换为代码。完成设置后,您可以逐行编写功能文件并使用上下文文件实现其背后的代码。

这是一个 example 功能文件,这是一个 example 上下文。

【讨论】:

    【解决方案2】:

    我假设您已阅读 Behat 的快速入门指南。 您可能错过的重要事项:

    1. 需要composer require --dev
    2. 运行vendor/bin/behat --init会生成骨架文件

    如果您的集成测试是关于运行某种 Web 浏览器并单击按钮 - 您还需要安装 Mink Extension 来为页面导航提供现成的低级语句。

    如果您想在测试期间访问您的实际应用程序(例如,在测试设置期间在 DB 中创建一些 ORM 对象或获得预身份验证会话等),您需要在您的上下文中实例化您的实际应用程序(请参阅项目的 index.phpbootstrap.php 文件以了解它是如何实例化的)。在这种情况下,您可能最好为此目的使用单独的上下文,以便您可以将其用作其他测试上下文的子上下文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-07
      • 2019-12-17
      • 2015-12-28
      • 2020-05-26
      • 2018-01-02
      • 2021-02-03
      • 2015-06-14
      • 2018-12-23
      相关资源
      最近更新 更多