仅使用 Behat 时发生语义错误

Semantical Error happens when using Behat only

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

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

http://localhost:8000/vendor/add-product

页面呈现正常,我看到一个表单,允许用户将产品添加到他的商店。

但是当我使用 Behat 进行测试时,出现此错误:

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

这是有问题的(部分)源代码:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @ORM\Entity
* @ORM\Table(name="app_products")
*/
class Product
{
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $productId;

    /**
    * @ORM\Column(type="integer")
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Store")
    */
    private $storeId;

    /**
    * @ORM\Column(type="string", length=255)
    * @Assert\NotBlank(message="This is important, Product Name should not be blank!")
    * @Assert\Length(max = "255", maxMessage="Your description must not exceed 255 characters.")
    */
    private $productName;

    /**
    * @ORM\Column(type="integer", length=255)
    * @Assert\GreaterThanOrEqual(value=0)
    * @Assert\NotBlank(message="Product Price should not be blank!")
    */
    private $productPrice;

    /**
    * @ORM\Column(type="string", length=255)
    * @Assert\NotBlank(message="Please enter a product description.")
    * @Assert\Length(max = "255", maxMessage="Your description must not exceed 255 characters.")
    */
    private $productDescription;

    /**
    * @ORM\Column(type="integer")
    * @Assert\GreaterThanOrEqual(value=0)
    * @Assert\NotBlank(message="Product Quantity should not be blank!")
    */
    private $productQuantity;

    /**
    * @ORM\Column(type="string", length=255)
    * @Assert\File(mimeTypes={ "image/jpeg" })
    * @Assert\File(maxSize="6000000")
    */
    private $productImage;
    ...

这是我的特色:

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 进行测试时出现错误。可能出了什么问题?


编辑:

我通过清除缓存执行了 Evgeniy Kuzmin 爵士的建议。我运行,

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. 

请帮助我。

当您通过本地主机手动访问您的表单时,您很可能使用了开发环境。但是当 Behat 走同一条路线时,它使用测试环境。

所以首先尝试使用选项“-e test”清除缓存

另一点是检查你 config_test.yml,你是否可以覆盖那里的选项

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

Ohmygod...经过 xx 周痛苦地遍历互联网寻找解决方案后,此错误的问题源于以下配置错误:

vendor/autoload.php

当我运行命令时:

php composer.phar update

它把我的 vendor/autoload.php 改写成了:

<?php
autoload.php @generated by Composer

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

return ComposerAutoloaderInit130fb5116714b6c2da66a4b026258e89::getLoader();
?>

而应该是这样才能使这些注释正常工作并消除该错误:

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;

require_once dirname( __DIR__ ).'/vendor/composer/autoload_real.php';

$loader = ComposerAutoloaderInit130fb5116714b6c2da66a4b026258e89::getLoader();

AnnotationRegistry::registerFile( dirname( __DIR__ ).'/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' );
AnnotationRegistry::registerLoader([$loader, 'loadClass']);

return $loader;

注意包含 AnnotationRegistry。


这一行:

AnnotationRegistry::registerFile( dirname( __DIR__ ).'/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' );

消除此错误:

[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']);