saeven/zf3-circlical-behat-fixtures

Doctrine & Nelmio/Alice fixtures for Zend Framework 3 and Behat

0.9.1 2021-02-06 04:22 UTC

This package is auto-updated.

Last update: 2024-09-17 10:26:20 UTC


README

在 Behat 测试中触发 fixture 加载,如下所示

Feature: I can define fixtures from within my tests
  In order to keep my test database clean
  As a developer writing behat tests
  I want to load fixtures directly from within my test context

  Scenario: Test loading a new fixture
    Given fixture "Application/user" is loaded
    And fixture "Application/user" is loaded
    And fixtures "Application/user, Billing/invoices" are loaded

此包通过将 alice 中的行为添加到反射中,当 fixture 定义的值没有可用的 setter 并且该值非平凡时,自动回退,从而为 alice 添加行为。

亮点

  • 由优秀的 nelmio/alice 提供的 fixtures
  • 包含一个可立即用于 Behat 的上下文
  • 在场景级别定义所需的 fixtures
  • 还添加了一个方便的 CLI 命令

灵感来自 dkorsak/doctrine-data-fixture-module -- 感谢!

安装

  • Composer 安装,然后向 application.config.php 添加 CirclicalBehatFixtures

将其连接到 Behat

  • 编辑 behat.yml,添加 CirclicalBehatFixtures\Behat\DatabaseContext 上下文

例如。

# behat.yml
default:
  autoload: [ '%paths.base%/../contexts' ]
  suites:
    core_features:
      paths: [ '%paths.base%/../features' ]
      contexts:
        - FeatureContext
        - CirclicalBehatFixtures\Behat\DatabaseContext

或者,如果您需要覆盖 Doctrine bin 的位置(符号链接指向的位置)

# behat.yml
default:
  autoload: [ '%paths.base%/../contexts' ]
  suites:
    core_features:
      paths: [ '%paths.base%/../features' ]
      contexts:
        - FeatureContext
        - CirclicalBehatFixtures\Behat\DatabaseContext:
          - vendor/doctrine/doctrine-module/bin/doctrine-module

编写一个 fixture

fixture 识别的语法非常简单,格式为:MODULE/fixturename。示例

  • Application/user
  • Application/roles
  • Member/purchases

在您的各个 ZF 模块中,您将 nelmio/alice fixtures 保存为 module/MODULE/user.yml。以下是基于上述 fixture ID 的示例

  • module/Application/fixtures/user.yml
  • module/Application/fixtures/roles.yml
  • module/Member/fixtures/purchases.yml

编写一个场景

按照您通常的方式执行。此模块提供的上下文为您提供了新的操作

Given Fixture "FIXTUREID" is loaded

例如。

Given Fixture "Application/user" is loaded

您可以根据需要堆叠这些。特征中的第一个会自动清除,后续的会附加。

容器支持

如果您需要将 fixture 导入 Docker 命令中,例如,作为 CI/CD 链的一部分,您可能需要更改 fixture 加载的位置。简而言之,而不是这个命令

vendor/bin/doctrine-module orm:fixtures:load --fixtures=Application/orders

您可能需要运行这样的命令

/usr/local/bin/docker container exec -w /code -i $(docker-compose ps -q php) php vendor/bin/doctrine-module orm:fixtures:load --fixtures=Application/orders

您可以通过在名为 circlical-fixtures-cmd-prefix 的文件中输出前缀来实现这一点,例如,在您的 CI/CD 脚本中

echo "/usr/local/bin/docker container exec -w /code -i $(docker-compose ps -q php) " > ./circlical-fixtures-cmd-prefix

这成为您在 CI 链配置中创建(在启动时)和删除(在拆卸时)此文件的责任。