【问题标题】:Behat 3 + Symfony 3 Context with namespace doesn't work, when without worksBehat 3 + Symfony 3 Context with namespace 不起作用,当没有工作时
【发布时间】:2017-03-29 08:06:10
【问题描述】:

我有以下目录结构

composer.json
behat.yml
src
|--AppBundle
   |--Features
      |--example.feature
      |--Context
         |--FeatureContext.php

还有下面的behat.yml

default:
  autoload:
    '': 'src/AppBundle/Features/Context'
  suites:
    default:
      paths: ['src/AppBundle/Features']
      contexts:
        - FeatureContext:
          session: '@session'
    # and extensions standard for Symphony

FeatureContext.php包含

<?php

//namespace AppBundle\Features\Context;

use Behat\Behat\Context\Context;
use Behat\MinkExtension\Context\MinkContext;

/**
 * Defines application features from the specific context.
 */
class FeatureContext extends MinkContext implements Context
{   ...   }

有注释命名空间。当我运行 behat 时,现在可以正确找到上下文。当我取消注释 namespace 时出现错误:

[Behat\Behat\Context\Exception\ContextNotFoundException]
FeatureContext 找不到上下文类,无法使用。

FeatureContext.php 中的 namespace 未注释时,如何使其工作?我对 PSR-0 和 PSR-4 不太了解,但如果问题与此有关,我会附加 composer.json 的片段。

"autoload": {
    "psr-4": {
        "": "src/"
    },
    "classmap": [
        "app/AppKernel.php",
        "app/AppCache.php"
    ]
},
"autoload-dev": {
    "psr-4": {
        "Tests\\": "tests/"
    }
},

我正在寻找编码的最佳实践,所以如果我做的不好,我会投票支持适当的建议。

  • 我似乎应该在 FeatureContext.php 中有 namespace,不是吗?
  • 我似乎不应该在 composer.json 中使用 PSR-0,是吗?

【问题讨论】:

    标签: symfony autoload behat psr-4 psr-0


    【解决方案1】:

    看看下面的例子。完整示例在这里:http://www.inanzzz.com/index.php/post/l41o/testing-a-basic-auth-symfony-api-with-behat3 您还可以在此处找到更多行为示例:http://www.inanzzz.com/index.php/posts/behat

    注意 1:您可以直接在上下文文件中访问 session,因此无需注入它。您可能需要使用implements KernelAwareContextimplements KernelAwareInterfaceimplements ContainerAwareInterface。只需查看上面的博文即可。

    注意 2:您根本不需要 composer.sjon 中的autoload-dev。摆脱它。

    composer.json

    注意:使用新版本!

    {
        "require-dev": {
            "behat/behat": "3.0.15",
            "behat/symfony2-extension": "2.1.0",
            "behat/mink": "1.7.0",
            "behat/mink-extension": "2.1.0",
            "behat/mink-browserkit-driver": "1.3.0"
        },
    }
    

    behat.yml

    default:
        extensions:
            Behat\Symfony2Extension: ~
            Behat\MinkExtension:
                base_url: http://your_local_app_domain.com/app_test.php/
                sessions:
                    symfony2:
                        symfony2: ~
        suites:
            api:
                type: symfony_bundle
                bundle: ApplicationApiBundle
                mink_session: symfony2
                contexts:
                    - Application\ApiBundle\Features\Context\FeatureContext:
                        param: 'whatever'
    

    FeatureContext.php

    namespace Application\ApiBundle\Features\Context;
    
    use Behat\MinkExtension\Context\MinkContext;
    
    class FeatureContext extends MinkContext
    {
        private $param;
    
        public function __construct($param)
        {
            $this->param = $param;
        }
    
        ......
    }
    

    测试

    $ bin/behat --suite=api @ApplicationApiBundle/YourFeature.feature
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多