dqx13503925528 2018-06-12 08:27
浏览 58

如何使用php&behat修复'无原型'

Good day. I'm learning some PHP BDD testing with Behat. I've been following closely this Behat 3 tutorial: http://docs.behat.org/en/latest/quick_start.html

I'm on Windows 10, using php 5.6.33. I've added composer.phar and composer.json to my project folder and the latter file looks like this:

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

Then, as tutorial suggests, I run commands in the following order:

  1. php composer.phar install.
  2. vendor/bin/behat --init.
  3. I've placed sample.feature file into features folder.

Feature file looks like this:

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

But when I run vendor/bin/behat --init I get this exception which actually do not prevent code snippets from being generated:

    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

I've just started to learn PHP, but as far as I understand it interface AnnotatedContextReader indeed does not have a prototype for __construct method, but mentioned interface extends Context interface and this one has such method prototype. So, I decided to skip that exception for the time being and completed provided tutorial. I can run my behat tests, but console is full of ReflectionExceptions complaining about absence of prototypes for my test steps. This drastically increases execution time and definitely should not be happening. Any ideas of what am I doing wrong? For code snippets please refer to tutorial link I've provided.

  • 写回答

1条回答 默认 最新

  • drsc10888 2018-06-14 09:14
    关注

    Okay, so this is a bit embarrasing. After some digging around I've discovered that this behaviour has nothing to do with version of php I'm using. Exception trace was shown because I had xdebug extension enabled and configured to show all possible output. So I believe this is why my console was full of ReflectionExceptions and test execution was extremely slow. setting this line resolved my issue:

    xdebug.show_exception_trace=0
    

    On the other hand it doesn't explain to me why ReflectionException happens in the first place, it just masks exception trace. I understand that this is because missing method prototypes in base interfaces, but is this done intentionally?

    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。