Hello StackOverflow社区,我刚加入社区,这是我的第一个问题:)
我正在使用Symfony2。当我使用浏览器(firefox)手动访问页面时:
http://localhost:8000/vendor/add-product
页面呈现正常,我看到一个表单,允许用户将产品添加到他的商店。
但是当我使用Behat进行测试时,我收到了这个错误:
[语义错误]注释" @Symfony \ Component \ Validator \ Constraints \ GreaterThanOrEqual" 在属性AppBundle \ Entity \ Product :: $ productPrice不存在, 或者无法自动加载。
这是(有问题的)源代码的一部分:
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.
请帮帮我。
答案 0 :(得分:0)
当您通过localhost手动访问表单时,很可能使用dev env。但是当Behat走向同一条路线时,它会使用测试环境。
首先尝试使用选项“-e test”清除缓存
另一点是检查你config_test.yml,可能覆盖那里的选项
framework:
validation: { enabled: true, enable_annotations: true }
答案 1 :(得分:0)
Ohmygod ......经过xx周的痛苦遍历互联网寻求解决方案之后,这个错误的问题源于错误配置:
供应商/ 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']);