thirdparty: fix denormalization of thirdparty

This commit is contained in:
Mathieu Jaumotte 2021-12-14 15:46:55 +01:00
parent d3127bed6d
commit da7bfa8a4e
4 changed files with 71 additions and 30 deletions

View File

@ -222,7 +222,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
* @var array|null
* @ORM\Column(name="types", type="json", nullable=true)
*/
private $types;
private ?array $thirdPartyTypes = [];
/**
* @ORM\Column(name="updated_at", type="datetime_immutable", nullable=true)
@ -303,18 +303,18 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
return $this;
}
public function addType(?string $type): self
public function addThirdPartyTypes(?string $type): self
{
if (null === $type) {
return $this;
}
if (!in_array($type, $this->types ?? [], true)) {
$this->types[] = $type;
if (!in_array($type, $this->thirdPartyTypes ?? [], true)) {
$this->thirdPartyTypes[] = $type;
}
foreach ($this->children as $child) {
$child->addType($type);
$child->addThirdPartyTypes($type);
}
return $this;
@ -329,7 +329,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
}
if (is_string($typeAndCategory)) {
$this->addType($typeAndCategory);
$this->addThirdPartyTypes($typeAndCategory);
return $this;
}
@ -473,16 +473,16 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
*
* @return array|null
*/
public function getTypes()
public function getThirdPartyTypes()
{
return $this->types;
return $this->thirdPartyTypes ?? [];
}
public function getTypesAndCategories(): array
{
return array_merge(
$this->getCategories()->toArray(),
$this->getTypes() ?? []
$this->getThirdPartyTypes() ?? []
);
}
@ -574,18 +574,18 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
return $this;
}
public function removeType(?string $type): self
public function removeThirdPartyTypes(?string $type): self
{
if (null === $type) {
return $this;
}
if (in_array($type, $this->types ?? [], true)) {
$this->types = array_filter($this->types, fn ($e) => !in_array($e, $this->types, true));
if (in_array($type, $this->thirdPartyTypes ?? [], true)) {
$this->thirdPartyTypes = array_filter($this->thirdPartyTypes, fn ($e) => !in_array($e, $this->thirdPartyTypes, true));
}
foreach ($this->children as $child) {
$child->removeType($type);
$child->removeThirdPartyTypes($type);
}
return $this;
@ -600,7 +600,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
}
if (is_string($typeAndCategory)) {
$this->removeType($typeAndCategory);
$this->removeThirdPartyTypes($typeAndCategory);
return $this;
}
@ -799,13 +799,13 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
*
* @return ThirdParty
*/
public function setTypes(?array $type = null)
public function setThirdPartyTypes(?array $type = [])
{
// remove all keys from the input data
$this->types = array_values($type);
$this->thirdPartyTypes = array_values($type);
foreach ($this->children as $child) {
$child->setTypes($type);
$child->setThirdPartyTypes($type);
}
return $this;
@ -814,7 +814,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface
public function setTypesAndCategories(array $typesAndCategories): self
{
$types = array_filter($typesAndCategories, static fn ($item) => !$item instanceof ThirdPartyCategory);
$this->setTypes($types);
$this->setThirdPartyTypes($types);
// handle categories
foreach ($typesAndCategories as $t) {

View File

@ -35,8 +35,8 @@ final class ThirdPartyTest extends TestCase
$this->assertTrue($tp->getCategories()->contains($cat2));
$this->assertCount(2, $tp->getCategories());
$this->assertCount(1, $tp->getTypes());
$this->assertContains('type', $tp->getTypes());
$this->assertCount(1, $tp->getThirdPartyTypes());
$this->assertContains('type', $tp->getThirdPartyTypes());
$this->assertCount(3, $tp->getTypesAndCategories());
$this->assertContains($cat1, $tp->getTypesAndCategories());
@ -54,8 +54,8 @@ final class ThirdPartyTest extends TestCase
$this->assertFalse($tp->getCategories()->contains($cat2));
$this->assertCount(1, $tp->getCategories());
$this->assertCount(0, $tp->getTypes());
$this->assertNotContains('type', $tp->getTypes());
$this->assertCount(0, $tp->getThirdPartyTypes());
$this->assertNotContains('type', $tp->getThirdPartyTypes());
$this->assertCount(1, $tp->getTypesAndCategories());
$this->assertContains($cat1, $tp->getTypesAndCategories());
@ -77,9 +77,9 @@ final class ThirdPartyTest extends TestCase
$this->assertTrue($tp->getCategories()->contains($cat2));
$this->assertCount(2, $tp->getCategories());
$this->assertCount(2, $tp->getTypes());
$this->assertContains('type1', $tp->getTypes());
$this->assertContains('type2', $tp->getTypes());
$this->assertCount(2, $tp->getThirdPartyTypes());
$this->assertContains('type1', $tp->getThirdPartyTypes());
$this->assertContains('type2', $tp->getThirdPartyTypes());
$this->assertCount(4, $tp->getTypesAndCategories());
$this->assertContains($cat1, $tp->getTypesAndCategories());
@ -93,9 +93,9 @@ final class ThirdPartyTest extends TestCase
$this->assertFalse($tp->getCategories()->contains($cat2));
$this->assertCount(1, $tp->getCategories());
$this->assertCount(1, $tp->getTypes());
$this->assertContains('type1', $tp->getTypes());
$this->assertNotContains('type2', $tp->getTypes());
$this->assertCount(1, $tp->getThirdPartyTypes());
$this->assertContains('type1', $tp->getThirdPartyTypes());
$this->assertNotContains('type2', $tp->getThirdPartyTypes());
$this->assertCount(2, $tp->getTypesAndCategories());
$this->assertContains($cat1, $tp->getTypesAndCategories());

View File

@ -0,0 +1,43 @@
<?php
namespace Chill\ThirdPartyBundle\Test\Serializer\Normalizer;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class ThirdPartyJsonDenormalizerTest extends KernelTestCase
{
private DenormalizerInterface $normalizer;
protected function setUp()
{
self::bootKernel();
$this->normalizer = self::$container->get(DenormalizerInterface::class);
}
public function testDenormalizeContact()
{
$str = <<<JSON
{
"type": "thirdparty",
"name": "badaboum",
"email": "badaboum@email.com",
"telephone": "+32486540660",
"kind": "contact"
}
JSON;
$actual = $this->normalizer->denormalize(json_decode($str, true), ThirdParty::class, 'json', [
'groups' => ['write']
]);
$this->assertInstanceOf(ThirdParty::class, $actual);
$this->assertEquals('badaboum', $actual->getName());
$this->assertEquals('badaboum@email.com', $actual->getEmail());
$this->assertEquals(ThirdParty::KIND_CONTACT, $actual->getKind());
}
}

View File

@ -44,8 +44,6 @@ final class ThirdpartyDocGenNormalizerTest extends KernelTestCase
$actual = $this->normalizer->normalize($thirdparty, 'docgen', ['groups' => ['docgen:read']]);
var_dump($actual);
$this->assertIsArray($actual);
}
}