5

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.

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100

2 Answers2

1

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']);
  • You should never modify anything in the `vendor/` folder -- composer will overwrite it. – mpen Mar 04 '16 at 21:28
  • Hello sir. Where should I put my autoload.php configuration so that it won't be overwritten? – Rave Alroone Mar 05 '16 at 20:13
  • Try putting [this](https://gist.github.com/mnpenner/254ae2777f8ec7b508e8) where-ever you require the autoloader. [More info here](https://getcomposer.org/doc/01-basic-usage.md#autoloading) – mpen Mar 05 '16 at 21:09
0

When you access your form via localhost manually you, most probably, use dev env. But when Behat goes to the same route, it uses test env.

So first try to clear cache with option "-e test"

Another point is to check you config_test.yml, possible you override option there

framework:
    validation: { enabled: true, enable_annotations: true } 
Evgeniy Kuzmin
  • 2,384
  • 1
  • 19
  • 24
  • Ohmy, sir what happened? I ran your suggestions in my console as: php bin/console cache:clear --env=test There are tests that fail now, however, they have not failed before I ran the command. Is my Symfony cache broken? – Rave Alroone Mar 04 '16 at 09:34
  • The error I have now is: "[Semantical Error] The annotation "@Doctrine\ORM\Mapping\Entity" in class AppBundle\Entity\User does not exist, or could not be auto-loaded." – Rave Alroone Mar 04 '16 at 09:39
  • I believe you should to check your git history, it is impossible to broke the cache with the console clear command, it is just a sign that you had some changes that you added, but had outdated cache that didn't see those changes. After you clear the cache, you test env was updated – Evgeniy Kuzmin Mar 04 '16 at 10:27
  • pls post "AppBundle\Entity\User" or check that it has "use Doctrine\ORM\Mapping as ORM;" – Evgeniy Kuzmin Mar 04 '16 at 10:39
  • Hello sir Evgeniy, I really appreciate you for helping me. Provided here is some parts of the code of AppBundle\Entity\User : http://pastebin.com/vtX5LTzV ... This problem is very frustrating because my application works if I access the functions manually (i register manually, login manually, and stuff) - but when I test it using Behat, things begin to fail miserably. – Rave Alroone Mar 04 '16 at 13:47
  • Really hard to guess, may be sources of your config.yml, config_dev.yml and config_test.yml will help – Evgeniy Kuzmin Mar 04 '16 at 14:52