Skip to content

Commit fb69a3e

Browse files
committed
test(doctrine): runtime resources on an EAV schema
1 parent 5405cb5 commit fb69a3e

31 files changed

Lines changed: 2148 additions & 1 deletion

‎phpstan.neon.dist‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,15 @@ parameters:
199199
identifier: function.alreadyNarrowedType
200200
path: tests/Symfony/Bundle/Command/DebugResourceCommandTest.php
201201
reportUnmatched: false
202+
203+
# Runtime resource example services are registered only by the runtime_resources test kernel configs
204+
-
205+
message: '#Service "ApiPlatform\\Tests\\Fixtures\\TestBundle\\RuntimeResource\\DefinitionProvider" is not registered in the container\.#'
206+
identifier: symfonyContainer.serviceNotFound
207+
path: tests/Functional/RuntimeResourceEavTest.php
208+
count: 4
209+
-
210+
message: '#Service "ApiPlatform\\Tests\\Fixtures\\TestBundle\\RuntimeResource\\RuntimeResources" is not registered in the container\.#'
211+
identifier: symfonyContainer.serviceNotFound
212+
path: tests/Functional/RuntimeResourceEavTest.php
213+
count: 2
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\RuntimeResource;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
#[ORM\Entity]
19+
#[ORM\Table(name: 'runtime_attribute_definition')]
20+
class AttributeDefinition
21+
{
22+
#[ORM\Id]
23+
#[ORM\GeneratedValue]
24+
#[ORM\Column]
25+
private ?int $id = null;
26+
27+
#[ORM\Column]
28+
private int $position;
29+
30+
/**
31+
* @param array<string, mixed> $options
32+
*/
33+
public function __construct(
34+
#[ORM\ManyToOne(inversedBy: 'attributes')]
35+
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
36+
private ResourceDefinition $definition,
37+
#[ORM\Column]
38+
private string $name,
39+
#[ORM\Column]
40+
private string $type,
41+
#[ORM\Column]
42+
private bool $required = false,
43+
#[ORM\Column(name: 'is_collection')]
44+
private bool $collection = false,
45+
#[ORM\Column(type: 'json')]
46+
private array $options = [],
47+
#[ORM\Column]
48+
private bool $filterable = false,
49+
) {
50+
$this->position = $definition->getAttributes()->count();
51+
$definition->getAttributes()->add($this);
52+
}
53+
54+
public function getName(): string
55+
{
56+
return $this->name;
57+
}
58+
59+
public function isCollection(): bool
60+
{
61+
return $this->collection;
62+
}
63+
64+
public function getType(): string
65+
{
66+
return $this->type;
67+
}
68+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\RuntimeResource;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
#[ORM\Entity]
19+
#[ORM\Table(name: 'runtime_attribute_value')]
20+
class AttributeValue
21+
{
22+
public const FIELDS = [
23+
'string' => 'stringValue',
24+
'enum' => 'stringValue',
25+
'text' => 'textValue',
26+
'integer' => 'integerValue',
27+
'float' => 'floatValue',
28+
'boolean' => 'booleanValue',
29+
'datetime' => 'datetimeValue',
30+
'reference' => 'referenceId',
31+
];
32+
33+
#[ORM\Id]
34+
#[ORM\GeneratedValue]
35+
#[ORM\Column]
36+
private ?int $id = null;
37+
38+
#[ORM\Column(name: 'string_value', nullable: true)]
39+
private ?string $stringValue = null;
40+
41+
#[ORM\Column(name: 'text_value', type: 'text', nullable: true)]
42+
private ?string $textValue = null;
43+
44+
#[ORM\Column(name: 'integer_value', nullable: true)]
45+
private ?int $integerValue = null;
46+
47+
#[ORM\Column(name: 'float_value', nullable: true)]
48+
private ?float $floatValue = null;
49+
50+
#[ORM\Column(name: 'boolean_value', nullable: true)]
51+
private ?bool $booleanValue = null;
52+
53+
#[ORM\Column(name: 'datetime_value', nullable: true)]
54+
private ?\DateTimeImmutable $datetimeValue = null;
55+
56+
#[ORM\Column(name: 'reference_id', nullable: true)]
57+
private ?int $referenceId = null;
58+
59+
public function __construct(
60+
#[ORM\ManyToOne(inversedBy: 'values')]
61+
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
62+
private EntityRow $entity,
63+
#[ORM\ManyToOne]
64+
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
65+
private AttributeDefinition $attribute,
66+
#[ORM\Column]
67+
private int $position,
68+
mixed $value,
69+
) {
70+
$this->{self::FIELDS[$attribute->getType()]} = $value;
71+
}
72+
73+
public function getAttribute(): AttributeDefinition
74+
{
75+
return $this->attribute;
76+
}
77+
78+
public function getValue(): mixed
79+
{
80+
return $this->{self::FIELDS[$this->attribute->getType()]};
81+
}
82+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\RuntimeResource;
15+
16+
use Doctrine\Common\Collections\ArrayCollection;
17+
use Doctrine\Common\Collections\Collection;
18+
use Doctrine\ORM\Mapping as ORM;
19+
20+
#[ORM\Entity]
21+
#[ORM\Table(name: 'runtime_entity_row')]
22+
class EntityRow
23+
{
24+
#[ORM\Id]
25+
#[ORM\GeneratedValue]
26+
#[ORM\Column]
27+
private ?int $id = null;
28+
29+
/** @var Collection<int, AttributeValue> */
30+
#[ORM\OneToMany(targetEntity: AttributeValue::class, mappedBy: 'entity', cascade: ['persist'], orphanRemoval: true)]
31+
#[ORM\OrderBy(['position' => 'ASC'])]
32+
private Collection $values;
33+
34+
public function __construct(
35+
#[ORM\ManyToOne]
36+
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
37+
private ResourceDefinition $definition,
38+
) {
39+
$this->values = new ArrayCollection();
40+
}
41+
42+
public function getId(): ?int
43+
{
44+
return $this->id;
45+
}
46+
47+
/**
48+
* @return Collection<int, AttributeValue>
49+
*/
50+
public function getValues(): Collection
51+
{
52+
return $this->values;
53+
}
54+
55+
/**
56+
* @param array<string, mixed> $values
57+
*/
58+
public function replaceValues(array $values): void
59+
{
60+
$this->values->clear();
61+
foreach ($this->definition->getAttributes() as $attribute) {
62+
$value = $values[$attribute->getName()] ?? null;
63+
foreach ($attribute->isCollection() ? (array) $value : [$value] as $position => $item) {
64+
if (null !== $item) {
65+
$this->values->add(new AttributeValue($this, $attribute, $position, $item));
66+
}
67+
}
68+
}
69+
}
70+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\RuntimeResource;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
#[ORM\Entity]
19+
#[ORM\Table(name: 'runtime_generation')]
20+
class Generation
21+
{
22+
#[ORM\Id]
23+
#[ORM\Column]
24+
private int $id = 1;
25+
26+
#[ORM\Column]
27+
private int $value = 0;
28+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\Entity\RuntimeResource;
15+
16+
use Doctrine\Common\Collections\ArrayCollection;
17+
use Doctrine\Common\Collections\Collection;
18+
use Doctrine\ORM\Mapping as ORM;
19+
20+
#[ORM\Entity]
21+
#[ORM\Table(name: 'runtime_resource_definition')]
22+
class ResourceDefinition
23+
{
24+
#[ORM\Id]
25+
#[ORM\GeneratedValue]
26+
#[ORM\Column]
27+
private ?int $id = null;
28+
29+
/** @var Collection<int, AttributeDefinition> */
30+
#[ORM\OneToMany(targetEntity: AttributeDefinition::class, mappedBy: 'definition', cascade: ['persist'])]
31+
#[ORM\OrderBy(['position' => 'ASC'])]
32+
private Collection $attributes;
33+
34+
public function __construct(
35+
#[ORM\Column(unique: true)]
36+
private string $name,
37+
#[ORM\Column]
38+
private string $label = '',
39+
#[ORM\Column(nullable: true)]
40+
private ?string $security = null,
41+
) {
42+
$this->attributes = new ArrayCollection();
43+
}
44+
45+
public function getId(): ?int
46+
{
47+
return $this->id;
48+
}
49+
50+
public function getName(): string
51+
{
52+
return $this->name;
53+
}
54+
55+
/**
56+
* @return Collection<int, AttributeDefinition>
57+
*/
58+
public function getAttributes(): Collection
59+
{
60+
return $this->attributes;
61+
}
62+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\RuntimeResource;
15+
16+
final readonly class Attribute
17+
{
18+
/**
19+
* @param array<string, mixed> $options
20+
*/
21+
public function __construct(
22+
public string $name,
23+
public AttributeType $type,
24+
public bool $required = false,
25+
public bool $collection = false,
26+
public array $options = [],
27+
public bool $filterable = false,
28+
) {
29+
}
30+
31+
public function target(): ?string
32+
{
33+
return AttributeType::Reference === $this->type ? (string) ($this->options['target'] ?? '') : null;
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\RuntimeResource;
15+
16+
enum AttributeType: string
17+
{
18+
case String = 'string';
19+
case Text = 'text';
20+
case Integer = 'integer';
21+
case Float = 'float';
22+
case Boolean = 'boolean';
23+
case Datetime = 'datetime';
24+
case Enum = 'enum';
25+
case Reference = 'reference';
26+
}

0 commit comments

Comments
 (0)