FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

MappingException: Invalid mapping file User.orm.yml

Open ireizo opened this issue 9 years ago • 11 comments

Hi there, i have a problem with running unittest in symfony project as below:

i get always the error:

1) *******\Tests\Controller\*******ControllerTest::testIndex
Doctrine\Common\Persistence\Mapping\MappingException: Invalid mapping file '*****.UserBundle.Entity.User.orm.yml' for class '*****\UserBundle\Entity\User'.

I can not figure out why this happens by running phpunit but not when doctrine:schem:update or doctrine:generate:entities !

This is the UserClass:

<?php

namespace *****\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 *
 */
class User extends BaseUser
{
    /**
     * @var integer
     */
    protected $confirm;

    /**
     * Set confirm
     *
     * @param integer $confirm
     * @return User
     */
    public function setConfirm($confirm)
    {
        $this->confirm = $confirm;
        return $this;
    }

    /**
     * Get confirm
     *
     * @return integer 
     */
    public function getConfirm()
    {
        return $this->confirm;
    }

}

And this is the User.orm.yml:

********\UserBundle\Entity\User:
    type: entity
    table: fos_user
    repositoryClass: ******\UserBundle\Entity\UserRepository
    id:
        id:
            type: integer
            #id: true
            generator:
                strategy: AUTO

    fields:
        confirm:
            type: smallint  

    lifecycleCallbacks: {  }

ireizo avatar Jan 06 '16 16:01 ireizo

Is there any linked exception which could tell what is wrong in the file ?

stof avatar Jan 07 '16 09:01 stof

btw, if you look at the exception message, it does not talk about a User.orm.yml file, so this may be why things fail

stof avatar Jan 07 '16 09:01 stof

Hi, there are exceptions like below:

/var/www/_/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:86 /var/www/****************/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/FileDriver.php:127 /var/www/****************/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php:56 /var/www/_/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php:102 /var/www/**_/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:116 /var/www/**********/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:332 /var/www/__/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:216 /var/www/*****************/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:265 /var/www/__/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php:67 /var/www/_/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php:50 /var/www/**/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:665 /var/www/***********_/vendor/friendsofsymfony/user-bundle/Doctrine/UserManager.php:40 /var/www/_/app/cache/test/appTestDebugProjectContainer.php:1694 /var/www/**_/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /var/www/*************/app/cache/test/appTestDebugProjectContainer.php:4117 /var/www/*************/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /var/www/*************/app/cache/test/appTestDebugProjectContainer.php:4175 /var/www/__/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /var/www/__/app/cache/test/appTestDebugProjectContainer.php:2660 /var/www/__/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /var/www/__/app/cache/test/appTestDebugProjectContainer.php:3774 /var/www/__/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /var/www/__/app/cache/test/appTestDebugProjectContainer.php:476 /var/www/__/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Container.php:312 /var/www/_/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:523 /var/www/''''''''''''''''''''''''''''''/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php:133 /var/www/**/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php:142 /var/www/'''''''''''''''''''''//vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php:33 /var/www/**/src/***_Bundle/Tests/Controller/_*******ControllerTest.php:11

ireizo avatar Jan 07 '16 12:01 ireizo

Message said about invalid mapping of User.orm.yaml

  1. _\Tests\Controller*__ControllerTest::testIndex Doctrine\Common\Persistence\Mapping\MappingException: Invalid mapping file '_.UserBundle.Entity.User.orm.yml' for class '***\UserBundle\Entity\User'.

ireizo avatar Jan 07 '16 12:01 ireizo

No, the file in the exception message is .UserBundle.Entity.User.orm.yml, not User.orm.yaml. This may be related to your issue (maybe you configured the mapping in a wrong way)

and btw, what you pasted in the previous comment is not the message of a linked exception, but the stack trace of this exception (but I found the code triggering the exception in Doctrine and there is no linked exception).

stof avatar Jan 07 '16 12:01 stof

Ah ok thx but realy i dont understand what you mean.

You mean this have to been only like: User.orm,yml And not XY.UserBundle.Entity.User.orm.yml?

With other words, when User.orm.yml is placed in XY/UserBundle/Resources/config/doctrine and User.php in: XY/UserBundle/Entity

So the message should say about the file like below: XY.UserBundle.Resources.config.doctrine.User.orm.yml

ireizo avatar Jan 07 '16 12:01 ireizo

No. Your orm is ok. PHPUNIT Test framework uses the newest yaml parser of symfony version 3. But when you running symfony version < version 3 you would get with unittesting in problems because the parsing of yaml file would be depricated and would throw exception.

behrus avatar Jan 13 '16 19:01 behrus

Recently I was experiencing similar "MappingException: Invalid mapping file" exceptions using the following code:

//bootstrap.php
$config = Setup::createYAMLMetadataConfiguration(array(__DIR__ . "/../config/dcm"), $isDevMode);

Using Symfony Yaml 2.*, everything worked fine. But with Symfony Yaml ^3.3 I would get the invalid mapping file exception. I traced it to how the different Yaml library parse functions work. When using Doctrine to parse yaml files, it will use the YamlDriver class which loads yaml files using this function:

// vendor/doctrine/orm/lib/Doctrine/Orm/Mapping/Driver/YamlDriver.php
protected function loadMappingFile($file)
{
    return Yaml::parse($file);
}

In Yaml 2.*, passing a filename string to parse works without problem, but with Yaml ^3.3, the parse function expects a yaml string. Some ways to get around this include using xml config files or writing your own Yaml driver and obtaining the config by bypassing the Setup::createYAMLMetadataConfiguration and using the this code:

$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new \MyNamespace\YamlDriver($paths));

return $config;

petejohnson84 avatar Aug 04 '17 13:08 petejohnson84

I got the issue, I fixed it with composer require symfony/yaml 2.x

But I don't want to use an old version, I think it seems to be an incompability with Doctrine...

alcalyn avatar Aug 29 '17 11:08 alcalyn

Make sure to use doctrine/orm 2.5.0 or higher. The YamlDriver class was fixed in doctrine/doctrine2@d3b1bf571bf01bc85d4f6b2732c70d2a44647e10 which is part of 2.5.

xabbuh avatar Aug 30 '17 08:08 xabbuh

In fact it was elsewhere, in Doctrine\Common\Persistence\Mapping\Driver\FileDriver::getElement.

At the lines ~115:

$result = $this->loadMappingFile($this->locator->findMappingFile($className));
if (!isset($result[$className])) {
    throw MappingException::invalidMappingFile($className, str_replace('\\', '.', $className) . $this->locator->getFileExtension());
}

It expected $result to be an array, but I get a string: the yaml file path.

So it enters in the if and I get this exception.

alcalyn avatar Aug 30 '17 09:08 alcalyn