Added some more tests.

This commit is contained in:
Jan Böhmer 2019-11-09 16:14:57 +01:00
parent 24e4a554f8
commit 3438f15274
6 changed files with 219 additions and 30 deletions

View file

@ -199,16 +199,21 @@ abstract class BaseAdminController extends AbstractController
if ($mass_creation_form->isSubmitted() && $mass_creation_form->isValid()) {
$data = $mass_creation_form->getData();
dump($data);
//Create entries based on input
$errors = $importer->massCreation($data['lines'], $this->entity_class, $data['parent']);
$errors = [];
$results = $importer->massCreation($data['lines'], $this->entity_class, $data['parent'], $errors);
//Show errors to user:
foreach ($errors as $name => $error) {
/* @var $error ConstraintViolationList */
$this->addFlash('error', $name.':'.$error);
foreach ($errors as $error) {
dump($error);
$this->addFlash('error', $error['entity']->getFullPath().':'.$error['violations']);
}
//Persist valid entities to DB
foreach ($results as $result) {
$em->persist($result);
}
$em->flush();
}
return $this->render($this->twig_template, [

View file

@ -111,14 +111,15 @@ class ElementTypeNameGenerator
* @param bool $use_html If set to true, a html string is returned, where the type is set italic
*
* @return string The localized string
* @throws EntityNotSupportedException When the passed entity is not supported.
*/
public function getTypeNameCombination(NamedDBElement $entity, bool $use_html = false): string
{
$type = $this->getLocalizedTypeLabel($entity);
if ($use_html) {
return '<i>'.$type.':</i> '.$entity->getName();
return '<i>'.$type.':</i> '.htmlspecialchars($entity->getName());
}
return $type.': '.htmlspecialchars($entity->getName());
return $type.': '.$entity->getName();
}
}

View file

@ -21,6 +21,7 @@
namespace App\Services;
use App\Entity\Base\DBElement;
use App\Entity\Base\StructuralDBElement;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\File\File;
@ -44,7 +45,7 @@ class EntityImporter
protected function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csv_separator' => ';',
'csv_separator' => ';',
'format' => 'json',
'preserve_children' => true,
'parent' => null,
@ -53,24 +54,37 @@ class EntityImporter
}
/**
* Creates many entries at once, based on a (text) list of names.
* Creates many entries at once, based on a (text) list of name.
* The created enties are not persisted to database yet, so you have to do it yourself.
*
* @param string $lines The list of names seperated by \n
* @param string $class_name The name of the class for which the entities should be created
* @param StructuralDBElement|null $parent The element which will be used as parent element for new elements.
* @param array $errors An associative array containing all validation errors.
*
* @return array An associative array containing an ConstraintViolationList and the entity name as key are returned,
* if an error happened during validation. When everything was successfull, the array should be empty.
* @return StructuralDBElement[] An array containing all valid imported entities (with the type $class_name)
*/
public function massCreation(string $lines, string $class_name, ?StructuralDBElement $parent): array
public function massCreation(string $lines, string $class_name, ?StructuralDBElement $parent = null, array &$errors = []): array
{
//Expand every line to a single entry:
$names = explode("\n", $lines);
if (!is_a($class_name, StructuralDBElement::class, true)) {
throw new \InvalidArgumentException('$class_name must be a StructuralDBElement type!');
}
if ($parent !== null && !is_a($parent, $class_name)) {
throw new \InvalidArgumentException('$parent must have the same type as specified in $class_name!');
}
$errors = [];
$valid_entities = [];
foreach ($names as $name) {
$name = trim($name);
if ($name === '') {
//Skip empty lines (StrucuralDBElements must have a name)
continue;
}
/** @var $entity StructuralDBElement */
//Create new element with given name
$entity = new $class_name();
@ -81,17 +95,13 @@ class EntityImporter
$tmp = $this->validator->validate($entity);
//If no error occured, write entry to DB:
if (0 === \count($tmp)) {
$this->em->persist($entity);
$valid_entities[] = $entity;
} else { //Otherwise log error
dump($tmp);
$errors[$entity->getFullPath()] = $tmp;
$errors[] = ['entity' => $entity, 'violations' => $tmp];
}
}
//Save changes to database
$this->em->flush();
return $errors;
return $valid_entities;
}
/**