In PHPUnit 10, I have somes fixtures between entities and they share references. I have the following deprecation and i can't manage to remove it :
Argument $class of Doctrine\Common\DataFixtures\AbstractFixture::getReference() will be mandatory in 2.0. (AbstractFixture.php:95 called by ProductFixture.php:26, https://github.com/doctrine/data-fixtures/pull/409, package doctrine/data-fixtures)
here is my code, and the different step to trying to remove the deprecation but this get an exception
class ProductFamilyFixture extends Fixture
{
public const PRODUCT_FAMILY_1_REFERENCE = 'PRODUCTS_FAMILY_1_REFERENCE';
/**
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager): void
{
//*******************************************
//ProductsFamilies
$entity = new ProductsFamilies();
$entity->setDesignation('Famille1');
$manager->persist($entity);
$entity2 = new ProductsFamilies();
$entity2->setDesignation('Famille2');
$manager->persist($entity2);
$this->addReference(self::PRODUCT_FAMILY_1_REFERENCE, $entity);
$manager->flush();
}
}
class ProductFixture extends Fixture implements DependentFixtureInterface
{
public const PRODUCT_1_REFERENCE = 'PRODUCT_1_REFERENCE';
/**
* @param ObjectManager $manager
*/
public function load(ObjectManager $manager): void
{
//*******************************************
$entity = new Products();
$entity->setPn('Pn1')
->setDesignation('Désignation produit1')
->setPrefix('Pr')
// work but send deprecation !!
->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE));
// This doesn't work => error
//OutOfBoundsException: Reference to "PRODUCTS_FAMILY_1_REFERENCE" for class "App\DataFixtures\ProductFamilyFixture" does not exist
// ->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE,ProductFamilyFixture::class));
// ->setFamily($this->getReference('PRODUCT_FAMILY_1_REFERENCE','ProductFamilyFixture'));
// ->setFamily($this->getReference('PRODUCT_FAMILY_1_REFERENCE',ProductFamilyFixture::class));
// ->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE,'ProductFamilyFixture'));
$manager->persist($entity);
$manager->flush();
$this->addReference(self::PRODUCT_1_REFERENCE, $entity);
}
public function getDependencies(): array
{
return [
ProductFamilyFixture::class,
];
}
}
Any idea to remove this deprecation ? Thanks
As mentionned in the deprecation warning you have to set the class when using getReference()
$this->addReference($referenceName, $entity);
Because it's expecting your entity class as a second parameter
public function getReference($name, ?string $class = null)
Throw a deprecation warning if $class
is null.
Just update your code with something like this:
->setFamily($this->getReference(ProductFamilyFixture::PRODUCT_FAMILY_1_REFERENCE, ProductFamily::class));