Php 仅使用Behat时发生语义错误

Php 仅使用Behat时发生语义错误,php,symfony,selenium,behat,mink,Php,Symfony,Selenium,Behat,Mink,你好,StackOverflow社区,我刚加入社区,这是我的第一个问题:) 我用的是Symfony2。当我使用浏览器(firefox)手动访问页面时: 页面呈现良好,我看到一个表单,允许用户向其商店添加产品 但当我在测试中使用Behat时,会出现以下错误: [Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could

你好,StackOverflow社区,我刚加入社区,这是我的第一个问题:)

我用的是Symfony2。当我使用浏览器(firefox)手动访问页面时:

页面呈现良好,我看到一个表单,允许用户向其商店添加产品

但当我在测试中使用Behat时,会出现以下错误:

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in 
class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\GreaterThanOrEqual" in property AppBundle\Entity\Product::$productPrice does not exist, or could not be auto-loaded.
[语义错误]注释“@Symfony\Component\Validator\Constraints\GreaterThanOrEqual” 在属性AppBundle\Entity\Product::$productPrice中不存在, 或无法自动加载

这是相关源代码的一部分:

这是我的特色:

Feature: Buying products
As a customer,
I want to view and select products before buying,
So that I could have more value for my money

Background:
    Given I am on the homepage
    When I fill in "username" with "24thsaint"
    And I fill in "password" with "123123"
    And I press "login-button"

Scenario: Vendor wants to add a new product
    Given I am on the homepage
    When I follow "My Store" ###### The test stops here as I get the Semantical Error
    And I follow "add-new-product"
请帮忙,我真的认为一切都准备好了。只是错误发生在使用Behat进行测试的过程中。可能出了什么问题


编辑:

我按照叶甫盖尼·库兹明爵士的建议清理了缓存。我跑,

php bin/console cache:clear --env=test
以前通过的测试现在失败,并出现以下错误:

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in 
class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\GreaterThanOrEqual" in property AppBundle\Entity\Product::$productPrice does not exist, or could not be auto-loaded.

请帮助我。

当您通过localhost手动访问表单时,您很可能使用dev env。但当Behat走到同一路线时,它使用test env

因此,首先尝试使用选项“-e test”清除缓存

另一点是检查config_test.yml,可能会覆盖其中的选项

framework:
    validation: { enabled: true, enable_annotations: true } 

Ohmygod。。。在痛苦地穿越互联网寻求解决方案xx周后,此错误的根源在于以下方面的错误配置:

供应商/autoload.php

当我运行命令时:

php composer.phar update
它将我的供应商/autoload.php改写为:

<?php
autoload.php @generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit130fb5116714b6c2da66a4b026258e89::getLoader();
?>
消除此错误:

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in 
class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\GreaterThanOrEqual" in property AppBundle\Entity\Product::$productPrice does not exist, or could not be auto-loaded.
在这之后,测试效果良好。 但是,我使用了验证,并且出现了以下错误:

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in 
class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\GreaterThanOrEqual" in property AppBundle\Entity\Product::$productPrice does not exist, or could not be auto-loaded.
通过添加此行解决了此问题:

AnnotationRegistry::registerLoader([$loader, 'loadClass']);

Ohmy先生发生什么事了?我在我的控制台中运行了您的建议:php bin/console cache:clear--env=test现在有一些测试失败了,但是,在我运行命令之前,它们没有失败。我的Symfony缓存损坏了吗?我现在的错误是:“[Semantical error]类AppBundle\Entity\User中的注释“@doctor\ORM\Mapping\Entity”不存在,或者无法自动加载。”我相信你应该检查你的git历史记录,用console clear命令无法损坏缓存,这只是一个迹象,表明您添加了一些更改,但缓存过时,看不到这些更改。清除缓存后,您测试env是否已更新请发布“AppBundle\Entity\User”或检查其是否具有“use Doctrine\ORM\Mapping as ORM”;您好,Evgeniy先生,非常感谢您的帮助。这里提供了AppBundle\Entity\User的部分代码:。。。这个问题非常令人沮丧,因为如果我手动访问这些函数(我手动注册、手动登录等等),我的应用程序就可以工作了。但是当我使用Behat测试它时,事情开始严重失败。您永远不应该修改
供应商/
文件夹中的任何内容--composer会覆盖它。您好,先生。我应该把autoload.php配置放在哪里,这样它就不会被覆盖?试着放在需要自动加载器的地方。