try to fix loading of reports fixtures

This commit is contained in:
Julien Fastré 2021-10-14 13:57:42 +02:00
parent 9a02665053
commit eaeb6c18b0

View File

@ -3,17 +3,17 @@
/* /*
* Chill is a suite of a modules, Chill is a software for social workers * Chill is a suite of a modules, Chill is a software for social workers
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop> * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as * it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the * published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version. * License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
@ -38,43 +38,43 @@ use Chill\MainBundle\DataFixtures\ORM\LoadScopes;
class LoadReports extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface class LoadReports extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{ {
use \Symfony\Component\DependencyInjection\ContainerAwareTrait; use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
/** /**
* *
* @var \Faker\Generator * @var \Faker\Generator
*/ */
private $faker; private $faker;
public function __construct() public function __construct()
{ {
$this->faker = FakerFactory::create('fr_FR'); $this->faker = FakerFactory::create('fr_FR');
} }
public function getOrder() public function getOrder()
{ {
return 15002; return 15002;
} }
public function load(ObjectManager $manager) public function load(ObjectManager $manager)
{ {
$this->createExpected($manager); $this->createExpected($manager);
//create random 2 times, to allow multiple report on some people //create random 2 times, to allow multiple report on some people
$this->createRandom($manager, 90); $this->createRandom($manager, 90);
$this->createRandom($manager, 30); $this->createRandom($manager, 30);
$manager->flush(); $manager->flush();
} }
private function createRandom(ObjectManager $manager, $percentage) private function createRandom(ObjectManager $manager, $percentage)
{ {
$people = $this->getPeopleRandom($percentage); $people = $this->getPeopleRandom($percentage);
foreach ($people as $person) { foreach ($people as $person) {
//create a report, set logement or education report //create a report, set logement or education report
$report = (new Report()) $report = (new Report())
->setPerson($person) ->setPerson($person)
->setCFGroup(rand(0,10) > 5 ? ->setCFGroup(rand(0,10) > 5 ?
$this->getReference('cf_group_report_logement') : $this->getReference('cf_group_report_logement') :
$this->getReference('cf_group_report_education') $this->getReference('cf_group_report_education')
) )
@ -84,14 +84,14 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
$manager->persist($report); $manager->persist($report);
} }
} }
private function createExpected(ObjectManager $manager) private function createExpected(ObjectManager $manager)
{ {
$charline = $this->container->get('doctrine.orm.entity_manager') $charline = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ChillPersonBundle:Person') ->getRepository('ChillPersonBundle:Person')
->findOneBy(array('firstName' => 'Charline', 'lastName' => 'Depardieu')) ->findOneBy(array('firstName' => 'Charline', 'lastName' => 'DEPARDIEU'))
; ;
$report = (new Report()) $report = (new Report())
->setPerson($charline) ->setPerson($charline)
->setCFGroup($this->getReference('cf_group_report_logement')) ->setCFGroup($this->getReference('cf_group_report_logement'))
@ -99,12 +99,12 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
->setScope($this->getReference('scope_social')) ->setScope($this->getReference('scope_social'))
; ;
$this->fillReport($report); $this->fillReport($report);
$manager->persist($report); $manager->persist($report);
} }
/** /**
* *
* @return \Chill\MainBundle\Entity\Scope * @return \Chill\MainBundle\Entity\Scope
*/ */
private function getScopeRandom() private function getScopeRandom()
@ -112,14 +112,14 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
$ref = LoadScopes::$references[array_rand(LoadScopes::$references)]; $ref = LoadScopes::$references[array_rand(LoadScopes::$references)];
return $this->getReference($ref); return $this->getReference($ref);
} }
private function getPeopleRandom($percentage) private function getPeopleRandom($percentage)
{ {
$people = $this->container->get('doctrine.orm.entity_manager') $people = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ChillPersonBundle:Person') ->getRepository('ChillPersonBundle:Person')
->findAll() ->findAll()
; ;
//keep only a part ($percentage) of the people //keep only a part ($percentage) of the people
$selectedPeople = array(); $selectedPeople = array();
foreach($people as $person) { foreach($people as $person) {
@ -127,10 +127,10 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
$selectedPeople[] = $person; $selectedPeople[] = $person;
} }
} }
return $selectedPeople; return $selectedPeople;
} }
private function fillReport(Report $report) private function fillReport(Report $report)
{ {
//setUser //setUser
@ -138,7 +138,7 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
$report->setUser( $report->setUser(
$this->getReference($usernameRef) $this->getReference($usernameRef)
); );
//set date if null //set date if null
if ($report->getDate() === NULL) { if ($report->getDate() === NULL) {
//set date. 30% of the dates are 2015-05-01 //set date. 30% of the dates are 2015-05-01
@ -148,9 +148,9 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
} else { } else {
$report->setDate($this->faker->dateTimeBetween('-1 year', 'now') $report->setDate($this->faker->dateTimeBetween('-1 year', 'now')
->setTime(0, 0, 0)); ->setTime(0, 0, 0));
} }
} }
//fill data //fill data
$datas = array(); $datas = array();
foreach ($report->getCFGroup()->getCustomFields() as $field) { foreach ($report->getCFGroup()->getCustomFields() as $field) {
@ -167,66 +167,66 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
} }
} }
$report->setCFData($datas); $report->setCFData($datas);
return $report; return $report;
} }
/** /**
* pick a random choice * pick a random choice
* *
* @param CustomField $field * @param CustomField $field
* @return string[]|string the array of slug if multiple, a single slug otherwise * @return string[]|string the array of slug if multiple, a single slug otherwise
*/ */
private function getRandomChoice(CustomField $field) private function getRandomChoice(CustomField $field)
{ {
$choices = $field->getOptions()['choices']; $choices = $field->getOptions()['choices'];
$multiple = $field->getOptions()['multiple']; $multiple = $field->getOptions()['multiple'];
$other = $field->getOptions()['other']; $other = $field->getOptions()['other'];
//add other if allowed //add other if allowed
if($other) { if($other) {
$choices[] = array('slug' => '_other'); $choices[] = array('slug' => '_other');
} }
//initialize results //initialize results
$picked = array(); $picked = array();
if ($multiple) { if ($multiple) {
$numberSelected = rand(1, count($choices) -1); $numberSelected = rand(1, count($choices) -1);
for ($i = 0; $i < $numberSelected; $i++) { for ($i = 0; $i < $numberSelected; $i++) {
$picked[] = $this->pickChoice($choices); $picked[] = $this->pickChoice($choices);
} }
if ($other) { if ($other) {
$result = array("_other" => NULL, "_choices" => $picked); $result = array("_other" => NULL, "_choices" => $picked);
if (in_array('_other', $picked)) { if (in_array('_other', $picked)) {
$result['_other'] = $this->faker->realText(70); $result['_other'] = $this->faker->realText(70);
} }
return $result; return $result;
} }
} else { } else {
$picked = $this->pickChoice($choices); $picked = $this->pickChoice($choices);
if ($other) { if ($other) {
$result = array('_other' => NULL, '_choices' => $picked); $result = array('_other' => NULL, '_choices' => $picked);
if ($picked === '_other') { if ($picked === '_other') {
$result['_other'] = $this->faker->realText(70); $result['_other'] = $this->faker->realText(70);
} }
return $result; return $result;
} }
} }
} }
/** /**
* pick a choice within a 'choices' options (for choice type) * pick a choice within a 'choices' options (for choice type)
* *
* @param array $choices * @param array $choices
* @return the slug of the selected choice * @return the slug of the selected choice
*/ */
@ -234,7 +234,7 @@ class LoadReports extends AbstractFixture implements OrderedFixtureInterface, Co
{ {
return $choices[array_rand($choices)]['slug']; return $choices[array_rand($choices)]['slug'];
} }
} }