mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
load third party fixtures
This commit is contained in:
parent
8002725c87
commit
038d7ad2e1
@ -73,7 +73,8 @@
|
|||||||
"symfony/web-profiler-bundle": "^5.0",
|
"symfony/web-profiler-bundle": "^5.0",
|
||||||
"symfony/var-dumper": "4.*",
|
"symfony/var-dumper": "4.*",
|
||||||
"symfony/debug-bundle": "^5.1",
|
"symfony/debug-bundle": "^5.1",
|
||||||
"symfony/phpunit-bridge": "^5.2"
|
"symfony/phpunit-bridge": "^5.2",
|
||||||
|
"nelmio/alice": "^3.8"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"auto-scripts": {
|
"auto-scripts": {
|
||||||
|
@ -55,11 +55,11 @@ class LoadCenters extends AbstractFixture implements OrderedFixtureInterface
|
|||||||
public function load(ObjectManager $manager)
|
public function load(ObjectManager $manager)
|
||||||
{
|
{
|
||||||
foreach (static::$centers as $new) {
|
foreach (static::$centers as $new) {
|
||||||
$centerA = new Center();
|
$center = new Center();
|
||||||
$centerA->setName($new['name']);
|
$center->setName($new['name']);
|
||||||
|
|
||||||
$manager->persist($centerA);
|
$manager->persist($center);
|
||||||
$this->addReference($new['ref'], $centerA);
|
$this->addReference($new['ref'], $center);
|
||||||
static::$refs[] = $new['ref'];
|
static::$refs[] = $new['ref'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,122 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Chill\ThirdPartyBundle\DataFixtures\ORM;
|
||||||
|
|
||||||
|
use Chill\MainBundle\DataFixtures\ORM\LoadCenters;
|
||||||
|
use Chill\MainBundle\DataFixtures\ORM\LoadPostalCodes;
|
||||||
|
use Chill\MainBundle\Entity\PostalCode;
|
||||||
|
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||||
|
use Doctrine\Persistence\ObjectManager;
|
||||||
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
||||||
|
use Nelmio\Alice\Loader\NativeLoader;
|
||||||
|
use Nelmio\Alice\ObjectSet;
|
||||||
|
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
|
class LoadThirdParty extends Fixture Implements DependentFixtureInterface
|
||||||
|
{
|
||||||
|
public function load(ObjectManager $manager)
|
||||||
|
{
|
||||||
|
$thirdParties = $this->getThirdParties()->getObjects();
|
||||||
|
|
||||||
|
foreach ($thirdParties as $name => $thirdParty) {
|
||||||
|
if ('a' === $name[0]) {
|
||||||
|
// this is an address
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($this->getCenters() as $center) {
|
||||||
|
$thirdParty->addCenter($center);
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager->persist($thirdParty);
|
||||||
|
}
|
||||||
|
|
||||||
|
$manager->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCenters(): \Iterator
|
||||||
|
{
|
||||||
|
$references = \array_map(function($a) { return $a['ref']; },
|
||||||
|
LoadCenters::$centers);
|
||||||
|
$number = random_int(1, count($references));
|
||||||
|
|
||||||
|
if ($number === 1) {
|
||||||
|
yield $this->getReference($references[\array_rand($references)]);
|
||||||
|
} else {
|
||||||
|
foreach (array_rand($references, $number) as $index) {
|
||||||
|
yield $this->getReference($references[$index]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDependencies()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
LoadCenters::class,
|
||||||
|
LoadPostalCodes::class
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getThirdParties(): ObjectSet
|
||||||
|
{
|
||||||
|
$loader = new NativeLoader();
|
||||||
|
$objectSet = $loader->loadData([
|
||||||
|
Address::class => [
|
||||||
|
'address{1..75}' => [
|
||||||
|
'street' => '<fr_FR:streetName()>',
|
||||||
|
'streetNumber' => '<fr_FR:buildingNumber()>',
|
||||||
|
'validFrom' => '<dateTimeBetween(\'-1 year\', \'now\')>',
|
||||||
|
'postCode' => $this->getPostalCode()
|
||||||
|
],
|
||||||
|
],
|
||||||
|
ThirdParty::class => [
|
||||||
|
'thirdparty{1..75}' => [
|
||||||
|
'name' => '<fr_FR:company()>',
|
||||||
|
'telephone' => '<fr_FR:phonenumber()>',
|
||||||
|
'email' => '<email()>',
|
||||||
|
'comment' => '<fr_FR:realTextBetween(10, 500)>',
|
||||||
|
'address' => '@address<current()>'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $objectSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getPostalCode(): PostalCode
|
||||||
|
{
|
||||||
|
$ref = LoadPostalCodes::$refs[\array_rand(LoadPostalCodes::$refs)];
|
||||||
|
return $this->getReference($ref);
|
||||||
|
if (count($this->postalCodesIds) === 0) {
|
||||||
|
// fill the postal codes
|
||||||
|
$this->em->createQuery('SELECT p.id FROM '.PostalCode::class)
|
||||||
|
->getScalarResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
$id = $this->postalCodesIds[\array_rand($this->postalCodesIds)];
|
||||||
|
|
||||||
|
return $this->em->getRepository(PostalCode::class)
|
||||||
|
->find($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createAddress(): ObjectSet
|
||||||
|
{
|
||||||
|
$loader = new NativeLoader();
|
||||||
|
$objectSet = $loader->loadData([
|
||||||
|
Address::class => [
|
||||||
|
'address1' => [
|
||||||
|
'name' => '<fr_FR:company()>',
|
||||||
|
'telephone' => '<fr_FR:phonenumber()>',
|
||||||
|
'email' => '<email()>',
|
||||||
|
'comment' => '<fr_FR:realTextBetween(10, 500)>'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
|
||||||
|
return $objectSet;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -33,6 +33,7 @@ class ChillThirdPartyExtension extends Extension implements PrependExtensionInte
|
|||||||
$loader->load('services/search.yaml');
|
$loader->load('services/search.yaml');
|
||||||
$loader->load('services/templating.yaml');
|
$loader->load('services/templating.yaml');
|
||||||
$loader->load('services/menu.yaml');
|
$loader->load('services/menu.yaml');
|
||||||
|
$loader->load('services/fixtures.yaml');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function prepend(ContainerBuilder $container)
|
public function prepend(ContainerBuilder $container)
|
||||||
|
@ -1 +1,5 @@
|
|||||||
|
---
|
||||||
services:
|
services:
|
||||||
|
Chill\ThirdPartyBundle\DataFixtures\ORM\LoadThirdParty:
|
||||||
|
tags:
|
||||||
|
- { 'name': doctrine.fixture.orm }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user