mardi 21 août 2018

Symfony 4.1 OneToMany in test return empty

Why in my test not working relationships? For example, one Product have two ProductVariant, but $product->getVariants()->count() return 0, but count($variants) return 2.

<?php

namespace App\Tests\Service\Marketplace\Banggood;

use App\Test\TestCase;
use Nelmio\Alice\Loader\NativeLoader;

class ImportOrderTest extends TestCase
{
    public function test_transform()
    {
        $this->loadFixtures();

        $items = $this->getEntityManager()->getRepository('App:OrderProduct')->findBy(['order' => $order]);
        $product = $this->getEntityManager()->getRepository('App:Product')->findOneBy(['foreignId' => 93255]);

        $variants = $this->getEntityManager()->getRepository('App:ProductVariant')->findBy(['product' => $product]);

        dd($product->getVariants()->count(), count($variants));

}

protected function getEntityManager()
{
    return $this->getService('doctrine.orm.entity_manager');
}

protected static function getService($id)
{
    return self::$kernel->getContainer()->get($id);
}

protected function loadFixtures()
{
    $loader = new NativeLoader();
    $objectSet = $loader->loadFile(__DIR__."/../DataFixtures/test/fixtures.yaml");
    foreach ($objectSet->getObjects() as $entity)
    {
        $this->getEntityManager()->persist($entity);
    }

    $this->getEntityManager()->flush();
}

I spent about 4 hours but don't found where is the bug.

Aucun commentaire:

Enregistrer un commentaire