Hello StackOverflow community, I just joined the community and this is my first question :)
I'm using Symfony2. When I access a page manually using my browser (firefox):
http://localhost:8000/vendor/add-product
The page renders fine and I see a form which will allow a user to add a product to his store.
But when I use Behat for my tests, I get this error:
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\GreaterThanOrEqual" in property AppBundle\Entity\Product::$productPrice does not exist, or could not be auto-loaded.
This is (a part of) the source code in question:
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;
...
And this is my feature:
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"
Please help, I really think everything is in place. It's just that the error occurs during testing with Behat. What could have gone wrong?
Edit:
I did the suggestions of sir Evgeniy Kuzmin by clearing the cache. I ran,
php bin/console cache:clear --env=test
The tests that pass before fail now with this error:
[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in
class AppBundle\Entity\User does not exist, or could not be auto-loaded.
Please help me.
Ohmygod... After xx weeks of painfully traversing the internet for solutions, the problem of this error rooted from a misconfiguration in:
vendor/autoload.php
When I ran the command:
php composer.phar update
It overwrote my vendor/autoload.php to this:
<?php
autoload.php @generated by Composer
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit130fb5116714b6c2da66a4b026258e89::getLoader();
?>
Whereas it should be this to get those annotations working and to get rid of that error:
<?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;
Note the inclusion of the AnnotationRegistry.
This line:
AnnotationRegistry::registerFile( dirname( __DIR__ ).'/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php' );
Eliminates this error:
[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could not be auto-loaded.
The tests worked fine after this. However, I was using validations and this error:
[Semantical Error] The annotation "@Symfony\Component\Validator\Constraints\GreaterThanOrEqual" in property AppBundle\Entity\Product::$productPrice does not exist, or could not be auto-loaded.
Was solved by adding this line:
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With