如何使用php&修复'无原型'贝哈特

时间:2018-06-12 08:27:27

标签: php bdd behat

美好的一天。我正在学习Behat的一些PHP BDD测试。我一直在密切关注这个Behat 3教程:http://docs.behat.org/en/latest/quick_start.html

我在Windows 10上,使用的是php 5.6.33。我已将composer.pharcomposer.json添加到我的项目文件夹中,后一个文件如下所示:

{
    "require-dev": {
        "behat/behat": "~3.0.4",
        "phpunit/phpunit": "~4.1.0"
    }
}

然后,正如教程所示,我按以下顺序运行命令:

  1. php composer.phar install
  2. vendor/bin/behat --init
  3. 我已将sample.feature个文件放入features文件夹。
  4. 功能文件如下所示:

    Feature: Product basket
      In order to buy products
      As a customer
      I need to be able to put interesting products into a basket
    
      Rules:
      - VAT is 20%
      - Delivery for basket under £10 is £3
      - Delivery for basket over £10 is £2
    
      Scenario: Buying a single product under £10
        Given there is a "Sith Lord Lightsaber", which costs £5
        When I add the "Sith Lord Lightsaber" to the basket
        Then I should have 1 product in the basket
        And the overall basket price should be £9
    
      Scenario: Buying a single product over £10
        Given there is a "Sith Lord Lightsaber", which costs £15
        When I add the "Sith Lord Lightsaber" to the basket
        Then I should have 1 product in the basket
        And the overall basket price should be £20
    
      Scenario: Buying two products over £10
        Given there is a "Sith Lord Lightsaber", which costs £10
        And there is a "Jedi Lightsaber", which costs £5
        When I add the "Sith Lord Lightsaber" to the basket
        And I add the "Jedi Lightsaber" to the basket
        Then I should have 2 products in the basket
        And the overall basket price should be £20
    

    但是当我运行vendor/bin/behat --init时,我得到了这个异常,实际上并没有阻止生成代码片段:

        ReflectionException: Method FeatureContext::__construct does not have a prototype in D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\AnnotatedContextReader.php on line 86
    
        Call Stack:
            0.0054     237328   1. {main}() D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\bin\behat:0
            0.5684    2249712   2. Symfony\Component\Console\Application->run(???, ???) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\bin\behat:31
            0.7328    2785200   3. Behat\Testwork\Cli\Application->doRun(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Application.php:117
            5.8172   12067888   4. Symfony\Component\Console\Application->doRun(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Cli\Application.php:102
            5.8180   12068848   5. Symfony\Component\Console\Application->doRunCommand(class Behat\Testwork\Cli\Command, class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Application.php:193
            5.8181   12069424   6. Symfony\Component\Console\Command\Command->run(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Application.php:843
            5.8252   12079976   7. Behat\Testwork\Cli\Command->execute(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\symfony\console\Command\Command.php:241
            5.8558   12157688   8. Behat\Testwork\Tester\Cli\ExerciseController->execute(class Symfony\Component\Console\Input\ArgvInput, class Symfony\Component\Console\Output\ConsoleOutput) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Cli\Command.php:63
            5.9129   12222752   9. Behat\Testwork\Tester\Cli\ExerciseController->testSpecifications(class Symfony\Component\Console\Input\ArgvInput, array(2)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Tester\Cli\ExerciseController.php:108
            5.9802   12264200  10. Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise->test(array(2), bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Tester\Cli\ExerciseController.php:146
            5.9802   12264432  11. Behat\Testwork\Tester\Runtime\RuntimeExercise->test(array(2), bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\EventDispatcher\Tester\EventDispatchingExercise.php:70
            6.0493   12324584  12. Behat\Testwork\EventDispatcher\Tester\EventDispatchingSuiteTester->setUp(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, class Behat\Testwork\Specification\GroupedSpecificationIterator, bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Tester\Runtime\RuntimeExercise.php:69
            6.0691   12342664  13. Behat\Testwork\Hook\Tester\HookableSuiteTester->setUp(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, class Behat\Testwork\Specification\GroupedSpecificationIterator, bool) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\EventDispatcher\Tester\EventDispatchingSuiteTester.php:59
            6.0976   12359008  14. Behat\Testwork\Hook\HookDispatcher->dispatchScopeHooks(class Behat\Testwork\Hook\Scope\BeforeSuiteScope) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\Tester\HookableSuiteTester.php:63
            6.0976   12359192  15. Behat\Testwork\Hook\HookRepository->getScopeHooks(class Behat\Testwork\Hook\Scope\BeforeSuiteScope) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\HookDispatcher.php:57
            6.0976   12359280  16. Behat\Testwork\Hook\HookRepository->getEnvironmentHooks(class Behat\Behat\Context\Environment\UninitializedContextEnvironment) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\HookRepository.php:67
            6.0977   12359352  17. Behat\Testwork\Environment\EnvironmentManager->readEnvironmentCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Hook\HookRepository.php:50
            6.0977   12359600  18. Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader->readEnvironmentCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Testwork\Environment\EnvironmentManager.php:115
            6.0978   12360208  19. Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader->readContextCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, string(14)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader.php:66
            6.1051   12363528  20. Behat\Behat\Context\Reader\ContextReaderCachedPerContext->readContextCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, string(14)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Environment\Reader\ContextEnvironmentReader.php:87
            6.1052   12363616  21. Behat\Behat\Context\Reader\AnnotatedContextReader->readContextCallees(class Behat\Behat\Context\Environment\UninitializedContextEnvironment, string(14)) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\ContextReaderCachedPerContext.php:52
            6.1052   12365152  22. Behat\Behat\Context\Reader\AnnotatedContextReader->readMethodCallees(string(14), class ReflectionMethod) D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\AnnotatedContextReader.php:64
            6.1053   12365328  23. ReflectionMethod->getPrototype() D:\Projects\php-projects\local-behat-phpunit-2\vendor\behat\behat\src\Behat\Behat\Context\Reader\AnnotatedContextReader.php:86
    

    我刚开始学习PHP,但据我所知,接口AnnotatedContextReader确实没有__construct方法的原型,但提到接口扩展Context接口和这个有这样的方法原型。所以,我决定暂时跳过该异常并完成提供的教程。我可以运行我的behat测试,但是控制台充满了ReflectionExceptions抱怨我的测试步骤没有原型。这大大增加了执行时间,绝对不应该发生。我有什么不对的想法吗?有关代码段,请参阅我提供的教程链接。

1 个答案:

答案 0 :(得分:0)

好的,所以这有点令人尴尬。经过一番挖掘后,我发现这种行为与我使用的php版本无关。显示异常跟踪是因为我启用了xdebug扩展并配置为显示所有可能的输出。所以我相信这就是为什么我的控制台充满了ReflectionException并且测试执行非常慢。设置此行解决了我的问题:

xdebug.show_exception_trace=0

另一方面,它并没有向我解释为什么ReflectionException首先发生,它只是屏蔽异常跟踪。我知道这是因为基本接口中缺少方法原型,但这是故意这样做的吗?