espend/behat-placeholder-extension

1.2 2021-04-11 09:43 UTC

README

Build Status Total Downloads Latest Stable Version SensioLabsInsight Build Status Scrutinizer Code Quality Code Coverage

要解决的问题

如果您使用外部服务测试应用程序,您会遇到非唯一用户输入的问题。例如,使用相同的电子邮件地址注册用户将失败,因为您的数据库中已经存在具有此电子邮件地址的用户。

Scenario: Register a new user
    Given I fill in "email" with "foo@exmaple.com"
    And I fill in "password" with "my_scret"
    And I press "Register Now"
    Then I should see "foo@exmaple.com" in the ".account-user" element

Scenario: Register a new user
    Given set a random mail in "%email%" placeholder
    And set a random password in "%password%" placeholder
    And I fill in "email" with "%email%"
    And I fill in "password" with "%password%"
    And I press "Register Now"
    Then I should see "%email%" in the ".account-user" element
    Then I should see "Hello %email%" in the ".account-user" element

也可以提取新生成用户的给定值。

Scenario: Register a new user and check id
    [...]
    When set placeholder "%user_id%" of "id" on Doctrine model "AppBundle:User" with "email" equals "%email%"
    Then print placeholder value of "%user_id%"    
    Then I should see "%user_id%" in the ".account-user-id" element

所有占位符都与外部的 Context 参数兼容。

安装

$ composer require espend/behat-placeholder-extension
# behat.yaml

default:
  suites:
    default:
      contexts:
        - espend\Behat\PlaceholderExtension\Context\PlaceholderContext
  
  extensions:
    espend\Behat\PlaceholderExtension\PlaceholderExtension: ~

功能步骤

所有占位符在场景范围内都是有效的。它们在每个场景前后都会被清理。

Given set a placeholder "%foobar%" with value "my_foobar"
Given set a random mail in "%mail%" placeholder
Given set a random password in "%password%" placeholder
Given set a random text with length "15" in "%text%" placeholder
Given set current date as "Y-m-d" format in "%date%" placeholder
Given print placeholder value of "%date%"
Given print all placeholder values

Doctrine

要与底层数据库交互,也有一些Doctrine步骤。它们仅在使用 Behat\Symfony2Extension 的测试套件/扩展上工作。

Given set placeholder "%foobar%" of "id" on Doctrine model "AppBundle:Car" with "name" equals "bmw"
Given set placeholder "%foobar%" of "id" on Doctrine model "AppBundle\Entity\Car" with "name" equals "bmw"
# behat.yaml
default:
  suites:
    default:
      contexts:
        - espend\Behat\PlaceholderExtension\Context\DoctrinePlaceholderContext

  extensions:
    # [...]
    Behat\Symfony2Extension: ~
    Behat\MinkExtension:
      sessions:
        default:
          symfony2: ~
# composer.json
"behat/mink-extension": "*",
"behat/symfony2-extension": "*",

占位符上下文注入

如果您想访问自定义 Context 中的占位符,您需要实现 espend\Behat\PlaceholderExtension\Context\PlaceholderBagAwareContextInterface 接口。参见 PlaceholderContext 以获取完整的工作示例。

class PlaceholderContext implements Context, PlaceholderBagAwareContext {}

待办事项

  • 为控制台命令管道占位符参数: bin/behat --placeholder="%foobar%=foo"
  • 更多与Doctrine相关的步骤
  • 更多特定于占位符的步骤