仅使用 Behat 时发生语义错误

Rav*_*one 5 php selenium symfony behat mink

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

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

http://localhost:8000/vendor/add-product
Run Code Online (Sandbox Code Playgroud)

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

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

[语义错误] 属性 AppBundle\Entity\Product::$productPrice 中的注释“@Symfony\Component\Validator\Constraints\GreaterThanOrEqual”不存在,或无法自动加载。

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

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;
    ...
Run Code Online (Sandbox Code Playgroud)

这是我的特点:

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"
Run Code Online (Sandbox Code Playgroud)

请帮忙,我真的认为一切都到位了。只是在使用Behat进行测试时发生了错误。可能出了什么问题?


编辑:

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

php bin/console cache:clear --env=test
Run Code Online (Sandbox Code Playgroud)

之前通过的测试现在失败并出现此错误:

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in 
class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
Run Code Online (Sandbox Code Playgroud)

请帮我。

Rav*_*one 1

天啊...经过 xx 周的痛苦地在互联网上寻找解决方案,这个错误的问题根源在于配置错误:

供应商/autoload.php

当我运行命令时:

php composer.phar update
Run Code Online (Sandbox Code Playgroud)

它将我的vendor/autoload.php重写为:

<?php
autoload.php @generated by Composer

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

return ComposerAutoloaderInit130fb5116714b6c2da66a4b026258e89::getLoader();
?>
Run Code Online (Sandbox Code Playgroud)

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

<?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;
Run Code Online (Sandbox Code Playgroud)

请注意包含 AnnotationRegistry。


这行:

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

消除此错误:

[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could not be auto-loaded. 
Run Code Online (Sandbox Code Playgroud)

此后测试工作正常。但是,我正在使用验证并出现此错误:

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

通过添加这一行解决了:

AnnotationRegistry::registerLoader([$loader, 'loadClass']);
Run Code Online (Sandbox Code Playgroud)