diff --git a/.github/workflows/run-test.yml b/.github/workflows/run-test.yml index 5edfa8c..5f7efa2 100644 --- a/.github/workflows/run-test.yml +++ b/.github/workflows/run-test.yml @@ -39,5 +39,5 @@ jobs: run: vendor/bin/phpcs --extensions=php --warning-severity=0 --standard=PSR12 -p ./src - name: Run PHPStan run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon - - name: Run unit tests - run: vendor/bin/phpunit -v --whitelist ./src --coverage-clover ./build/logs/clover.xml src/Test + - name: Run atoum unit tests + run: vendor/bin/atoum -f tests/units/* diff --git a/LICENSE.md b/LICENSE.md index 8e18fa6..d4d8a00 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright © 2024 Ambroise Maupate +Copyright © 2023 Ambroise Maupate Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/Makefile b/Makefile index 9b71cd8..c45718c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ test: vendor/bin/phpcbf --report=full --report-file=./report.txt --extensions=php --warning-severity=0 --standard=PSR12 -p ./src vendor/bin/phpstan analyse -c phpstan.neon - vendor/bin/phpunit -v --whitelist ./src --coverage-clover ./build/logs/clover.xml src/Test + vendor/bin/atoum -f tests/units/* diff --git a/composer.json b/composer.json index 693a025..aff7aec 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,10 @@ "php": ">=8.1", "ext-json": "*", "roadiz/nodetype-contracts": "~1.1.2", - "symfony/string": "6.4.*", - "symfony/yaml": "6.4.*", - "symfony/serializer": "6.4.*", - "symfony/options-resolver": "6.4.*" + "symfony/string": "5.4.*", + "symfony/yaml": "5.4.*", + "symfony/serializer": "5.4.*", + "symfony/options-resolver": "5.4.*" }, "suggest": { "api-platform/core": "If you need to create ApiFilter annotation right into your entities" @@ -32,13 +32,13 @@ "require-dev": { "squizlabs/php_codesniffer": "^3.5", "phpstan/phpstan": "^1.5.3", - "phpunit/phpunit": "^9.5", - "api-platform/core": "~3.2.14" + "atoum/atoum": "^4.0.0", + "api-platform/core": "~2.7.0" }, "extra": { "branch-alias": { - "dev-main": "2.3.x-dev", - "dev-develop": "2.4.x-dev" + "dev-main": "2.2.x-dev", + "dev-develop": "2.3.x-dev" } } } diff --git a/phpstan.neon b/phpstan.neon index 6d40c05..c86467e 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: 7 + level: max paths: - src excludePaths: @@ -7,6 +7,5 @@ parameters: - */bower_components/* - */static/* reportUnmatchedIgnoredErrors: false - ignoreErrors: - - identifier: missingType.iterableValue - - identifier: missingType.generics + checkMissingIterableValueType: false + checkGenericClassInNonGenericObjectType: false diff --git a/src/Attribute/AttributeGenerator.php b/src/Attribute/AttributeGenerator.php index 2b6c07d..2b880f0 100644 --- a/src/Attribute/AttributeGenerator.php +++ b/src/Attribute/AttributeGenerator.php @@ -8,13 +8,13 @@ class AttributeGenerator { protected string $className; /** - * @var array + * @var array */ protected array $parameters; /** * @param string $className - * @param array $parameters + * @param int[]|string[] $parameters */ public function __construct(string $className, array $parameters = []) { @@ -24,7 +24,7 @@ public function __construct(string $className, array $parameters = []) public static function wrapString(string $string): string { - return sprintf('"%s"', str_replace('"', '\\"', $string)); + return sprintf('"%s"', $string); } public function generate(int $currentIndentation = 0): string @@ -32,10 +32,20 @@ public function generate(int $currentIndentation = 0): string $formattedParams = []; if (count($this->parameters) > 3) { foreach ($this->parameters as $name => $parameter) { - if (empty($parameter)) { - continue; + if (is_string($name) && !empty($name)) { + $formattedParams[] = sprintf( + '%s%s: %s', + str_repeat(' ', $currentIndentation + 4), + $name, + $parameter + ); + } else { + $formattedParams[] = sprintf( + '%s%s', + str_repeat(' ', $currentIndentation + 4), + $parameter + ); } - $formattedParams[] = $this->formatProperties($name, $parameter, $currentIndentation); } return str_repeat(' ', $currentIndentation) . @@ -43,75 +53,26 @@ public function generate(int $currentIndentation = 0): string sprintf( '(%s%s%s)', PHP_EOL, - implode(',' . PHP_EOL, array_filter($formattedParams)), + implode(',' . PHP_EOL, $formattedParams), PHP_EOL . str_repeat(' ', $currentIndentation), ); } elseif (count($this->parameters) > 0) { foreach ($this->parameters as $name => $parameter) { - if (empty($parameter)) { - continue; + if (is_string($name) && !empty($name)) { + $formattedParams[] = sprintf('%s: %s', $name, $parameter); + } else { + $formattedParams[] = $parameter; } - $formattedParams[] = $this->formatProperties($name, $parameter, -4); } return str_repeat(' ', $currentIndentation) . $this->className . sprintf( '(%s)', - implode(', ', array_filter($formattedParams)) + implode(', ', $formattedParams) ); } else { return str_repeat(' ', $currentIndentation) . $this->className; } } - - /** - * @param string $name - * @param array $parameter - * @param int $currentIndentation - * @return string - * @throws \JsonException - */ - protected function formatArrayObject(string $name, array $parameter, int $currentIndentation = 0): string - { - $encodedParameterContent = []; - foreach ($parameter as $key => $value) { - if (is_string($key)) { - $encodedParameterContent[] = sprintf( - '%s => %s', - self::wrapString($key), - \json_encode($value, \JSON_THROW_ON_ERROR) - ); - } - } - return sprintf( - '%s%s: %s', - str_repeat(' ', $currentIndentation + 4), - $name, - '[' . implode(', ', $encodedParameterContent) . ']' - ); - } - - protected function formatProperties(string|int $name, mixed $parameter, int $currentIndentation = 0): ?string - { - if (empty($parameter)) { - return null; - } - if (is_string($name) && \is_array($parameter)) { - return $this->formatArrayObject($name, $parameter, $currentIndentation); - } - if (is_string($name) && !empty($name)) { - return sprintf( - '%s%s: %s', - str_repeat(' ', $currentIndentation + 4), - $name, - $parameter - ); - } - return sprintf( - '%s%s', - str_repeat(' ', $currentIndentation + 4), - $parameter - ); - } } diff --git a/src/EntityGenerator.php b/src/EntityGenerator.php index fc4a5e4..bfc258b 100644 --- a/src/EntityGenerator.php +++ b/src/EntityGenerator.php @@ -247,15 +247,10 @@ protected function getClassAttributes(): string */ protected function getClassAnnotations(): string { - $annotations = [ - $this->nodeType->getName() . ' node-source entity.', - $this->nodeType->getDescription() - ]; - $annotations = array_filter($annotations); - return ' /** - * ' . implode(PHP_EOL . ' * ', $annotations) . ' + * DO NOT EDIT + * Generated custom node-source type by Roadiz. */' . PHP_EOL; } diff --git a/src/Field/AbstractFieldGenerator.php b/src/Field/AbstractFieldGenerator.php index e0497db..e0a689e 100644 --- a/src/Field/AbstractFieldGenerator.php +++ b/src/Field/AbstractFieldGenerator.php @@ -150,27 +150,6 @@ protected function getFieldAttributes(bool $exclude = false): array $attributes[] = new AttributeGenerator('SymfonySerializer\Groups', [ $this->getSerializationGroups() ]); - - $description = $this->field->getLabel(); - if (!empty($this->field->getDescription())) { - $description .= ': ' . $this->field->getDescription(); - } - if ($this->field->isEnum() && null !== $defaultValues = $this->field->getDefaultValues()) { - $enumValues = explode(',', $defaultValues); - $enumValues = array_filter(array_map('trim', $enumValues)); - $openapiContext = [ - 'type' => 'string', - 'enum' => $enumValues, - 'example' => $enumValues[0] ?? null, - ]; - } - $attributes[] = new AttributeGenerator('\ApiPlatform\Metadata\ApiProperty', [ - 'description' => AttributeGenerator::wrapString($description), - 'schema' => $openapiContext ?? null, - 'example' => $this->field->getPlaceholder() ? - AttributeGenerator::wrapString($this->field->getPlaceholder()) : - null, - ]); if ($this->getSerializationMaxDepth() > 0) { $attributes[] = new AttributeGenerator('SymfonySerializer\MaxDepth', [ $this->getSerializationMaxDepth() diff --git a/src/Field/CustomFormsFieldGenerator.php b/src/Field/CustomFormsFieldGenerator.php index 6adc91d..f0fb9df 100644 --- a/src/Field/CustomFormsFieldGenerator.php +++ b/src/Field/CustomFormsFieldGenerator.php @@ -50,12 +50,16 @@ public function getFieldGetter(): string public function ' . $this->field->getGetterName() . '(): array { if (null === $this->' . $this->field->getVarName() . ') { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->' . $this->field->getVarName() . ' = $this->objectManager ->getRepository(' . $this->options['custom_form_class'] . '::class) - ->findByNodeAndFieldName( + ->findByNodeAndField( $this->getNode(), - \'' . $this->field->getName() . '\' + $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '") ); } else { $this->' . $this->field->getVarName() . ' = []; @@ -80,15 +84,22 @@ protected function getFieldSetter(): string */ public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['custom_form_class'] . ' $customForm): static { - if (null !== $this->objectManager) { - $nodeCustomForm = new ' . $this->options['custom_form_proxy_class'] . '( - $this->getNode(), - $customForm - ); - $nodeCustomForm->setFieldName(\'' . $this->field->getName() . '\'); - $this->objectManager->persist($nodeCustomForm); - $this->getNode()->addCustomForm($nodeCustomForm); - $this->' . $this->field->getVarName() . ' = null; + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { + $field = $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '"); + if (null !== $field) { + $nodeCustomForm = new ' . $this->options['custom_form_proxy_class'] . '( + $this->getNode(), + $customForm, + $field + ); + $this->objectManager->persist($nodeCustomForm); + $this->getNode()->addCustomForm($nodeCustomForm); + $this->' . $this->field->getVarName() . ' = null; + } } return $this; }' . PHP_EOL; diff --git a/src/Field/DocumentsFieldGenerator.php b/src/Field/DocumentsFieldGenerator.php index be8732e..6e85f13 100644 --- a/src/Field/DocumentsFieldGenerator.php +++ b/src/Field/DocumentsFieldGenerator.php @@ -58,12 +58,16 @@ public function getFieldGetter(): string public function ' . $this->field->getGetterName() . '(): array { if (null === $this->' . $this->field->getVarName() . ') { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->' . $this->field->getVarName() . ' = $this->objectManager ->getRepository(' . $this->options['document_class'] . '::class) - ->findByNodeSourceAndFieldName( + ->findByNodeSourceAndField( $this, - \'' . $this->field->getName() . '\' + $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '") ); } else { $this->' . $this->field->getVarName() . ' = []; @@ -88,16 +92,23 @@ protected function getFieldSetter(): string */ public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['document_class'] . ' $document): static { - if (null !== $this->objectManager) { - $nodeSourceDocument = new ' . $this->options['document_proxy_class'] . '( - $this, - $document - ); - $nodeSourceDocument->setFieldName(\'' . $this->field->getName() . '\'); - if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) { - $this->objectManager->persist($nodeSourceDocument); - $this->addDocumentsByFields($nodeSourceDocument); - $this->' . $this->field->getVarName() . ' = null; + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { + $field = $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '"); + if (null !== $field) { + $nodeSourceDocument = new ' . $this->options['document_proxy_class'] . '( + $this, + $document, + $field + ); + if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) { + $this->objectManager->persist($nodeSourceDocument); + $this->addDocumentsByFields($nodeSourceDocument); + $this->' . $this->field->getVarName() . ' = null; + } } } return $this; diff --git a/src/Field/NodesFieldGenerator.php b/src/Field/NodesFieldGenerator.php index 9f11d15..54ed301 100644 --- a/src/Field/NodesFieldGenerator.php +++ b/src/Field/NodesFieldGenerator.php @@ -130,12 +130,16 @@ public function getFieldGetter(): string public function ' . $this->field->getGetterName() . 'Sources(): array { if (null === $this->' . $this->getFieldSourcesName() . ') { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->' . $this->getFieldSourcesName() . ' = $this->objectManager ->getRepository(' . $this->getRepositoryClass() . '::class) - ->findByNodesSourcesAndFieldNameAndTranslation( + ->findByNodesSourcesAndFieldAndTranslation( $this, - \'' . $this->field->getName() . '\' + $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '") ); } else { $this->' . $this->getFieldSourcesName() . ' = []; diff --git a/src/RepositoryGenerator.php b/src/RepositoryGenerator.php index ffa1a54..8c27975 100644 --- a/src/RepositoryGenerator.php +++ b/src/RepositoryGenerator.php @@ -84,7 +84,7 @@ protected function getClassHeader(): string use Doctrine\Persistence\ManagerRegistry; use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface; use RZ\Roadiz\CoreBundle\SearchEngine\NodeSourceSearchHandlerInterface; -use Symfony\Bundle\SecurityBundle\Security; +use Symfony\Component\Security\Core\Security; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** diff --git a/src/Test/EntityGeneratorFactoryTest.php b/src/Test/EntityGeneratorFactoryTest.php deleted file mode 100644 index 51fdb99..0000000 --- a/src/Test/EntityGeneratorFactoryTest.php +++ /dev/null @@ -1,104 +0,0 @@ -getMockNodeTypeResolver(), - $this->getMockDefaultValuesResolver(), - $options ?? [ - 'parent_class' => '\mock\Entity\NodesSources', - 'node_class' => '\mock\Entity\Node', - 'translation_class' => '\mock\Entity\Translation', - 'document_class' => '\mock\Entity\Document', - 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', - 'custom_form_class' => '\mock\Entity\CustomForm', - 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', - 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', - 'namespace' => '\tests\mocks\GeneratedNodesSources', - 'use_native_json' => true, - 'use_api_platform_filters' => true, - ] - ); - } - - public function testCreate(): void - { - $generator = $this->getEntityGeneratorFactory(); - - $this->assertEquals( - file_get_contents(dirname(__DIR__) . '/../tests/mocks/GeneratedNodesSources/NSMock.php'), - $generator->create($this->getMockNodeType())->getClassContent() - ); - } - - public function testCreateWithCustomRepository(): void - { - $generator = $this->getEntityGeneratorFactory([ - 'parent_class' => '\mock\Entity\NodesSources', - 'node_class' => '\mock\Entity\Node', - 'translation_class' => '\mock\Entity\Translation', - 'document_class' => '\mock\Entity\Document', - 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', - 'custom_form_class' => '\mock\Entity\CustomForm', - 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', - 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', - 'namespace' => '\tests\mocks\GeneratedNodesSourcesWithRepository', - 'use_native_json' => true, - 'use_api_platform_filters' => true, - ]); - - /* - * Uncomment for generating a mock file from tests - */ -// file_put_contents( -// dirname(__DIR__) . '/../test/mocks/GeneratedNodesSourcesWithRepository/NSMock.php', -// $generator->createWithCustomRepository($this->getMockNodeType())->getClassContent() -// ); - - $this->assertEquals( - file_get_contents(dirname(__DIR__) . '/../tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php'), - $generator->createWithCustomRepository($this->getMockNodeType())->getClassContent() - ); - } - - public function testCreateCustomRepository(): void - { - $generator = $this->getEntityGeneratorFactory([ - 'parent_class' => '\mock\Entity\NodesSources', - 'node_class' => '\mock\Entity\Node', - 'translation_class' => '\mock\Entity\Translation', - 'document_class' => '\mock\Entity\Document', - 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', - 'custom_form_class' => '\mock\Entity\CustomForm', - 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', - 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', - 'namespace' => '\tests\mocks\GeneratedNodesSourcesWithRepository', - 'use_native_json' => true, - 'use_api_platform_filters' => true, - ]); - - /* - * Uncomment for generating a mock file from tests - */ -// file_put_contents( -// dirname(__DIR__) . '/../test/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php', -// $generator->createCustomRepository($this->getMockNodeType())->getClassContent() -// ); - - $this->assertEquals( - file_get_contents(dirname(__DIR__) . '/../tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php'), - $generator->createCustomRepository($this->getMockNodeType())->getClassContent() - ); - } -} diff --git a/src/Test/EntityGeneratorTest.php b/src/Test/EntityGeneratorTest.php deleted file mode 100644 index 799fdcc..0000000 --- a/src/Test/EntityGeneratorTest.php +++ /dev/null @@ -1,76 +0,0 @@ -getMockNodeTypeResolver(), - $this->getMockDefaultValuesResolver(), - $options ?? [ - 'parent_class' => '\mock\Entity\NodesSources', - 'node_class' => '\mock\Entity\Node', - 'translation_class' => '\mock\Entity\Translation', - 'document_class' => '\mock\Entity\Document', - 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', - 'custom_form_class' => '\mock\Entity\CustomForm', - 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', - 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', - 'namespace' => '\tests\mocks\GeneratedNodesSources', - 'use_native_json' => true, - 'use_api_platform_filters' => true, - ] - ); - } - - public function testGetClassContent(): void - { - $mockNodeType = $this->getMockNodeType(); - $generator = $this->getEntityGenerator($mockNodeType); - - /* - * Uncomment for generating a mock file from tests - */ -// file_put_contents( -// dirname(__DIR__) . '/../test/mocks/GeneratedNodesSources/NSMock.php', -// $dumpInstance->getClassContent() -// ); - - $this->assertEquals( - file_get_contents(dirname(__DIR__) . '/../tests/mocks/GeneratedNodesSources/NSMock.php'), - $generator->getClassContent() - ); - - /** - * TEST without leading slashs - */ - $generatorWithoutLeadingSlashes = $this->getEntityGenerator($mockNodeType, [ - 'parent_class' => 'mock\Entity\NodesSources', - 'node_class' => 'mock\Entity\Node', - 'translation_class' => 'mock\Entity\Translation', - 'document_class' => 'mock\Entity\Document', - 'document_proxy_class' => 'mock\Entity\NodesSourcesDocument', - 'custom_form_class' => 'mock\Entity\CustomForm', - 'custom_form_proxy_class' => 'mock\Entity\NodesSourcesCustomForm', - 'repository_class' => 'mock\Entity\Repository\NodesSourcesRepository', - 'namespace' => 'tests\mocks\GeneratedNodesSources', - 'use_native_json' => true, - 'use_api_platform_filters' => true, - ]); - $this->assertEquals( - file_get_contents(dirname(__DIR__) . '/../tests/mocks/GeneratedNodesSources/NSMock.php'), - $generatorWithoutLeadingSlashes->getClassContent() - ); - } -} diff --git a/tests/mocks/GeneratedNodesSources/NSMock.php b/tests/mocks/GeneratedNodesSources/NSMock.php index 45a8a75..0b16aed 100644 --- a/tests/mocks/GeneratedNodesSources/NSMock.php +++ b/tests/mocks/GeneratedNodesSources/NSMock.php @@ -18,7 +18,8 @@ use ApiPlatform\Serializer\Filter\PropertyFilter; /** - * Mock node-source entity. + * DO NOT EDIT + * Generated custom node-source type by Roadiz. */ #[ Gedmo\Loggable(logEntryClass: \RZ\Roadiz\CoreBundle\Entity\UserLogEntry::class), @@ -39,7 +40,6 @@ class NSMock extends \mock\Entity\NodesSources #[ SymfonySerializer\SerializedName(serializedName: "fooDatetime"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "foo_datetime"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo DateTime field"), SymfonySerializer\MaxDepth(2), ApiFilter(OrmFilter\OrderFilter::class), ApiFilter(OrmFilter\DateFilter::class), @@ -79,7 +79,6 @@ public function setFooDatetime(?\DateTime $fooDatetime): static #[ SymfonySerializer\SerializedName(serializedName: "foo"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), Gedmo\Versioned, ORM\Column( @@ -124,7 +123,6 @@ public function setFoo(?string $foo): static #[ SymfonySerializer\SerializedName(serializedName: "fooIndexed"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo indexed field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), ApiFilter(OrmFilter\SearchFilter::class, strategy: "partial"), ApiFilter(\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter::class), @@ -171,7 +169,6 @@ public function setFooIndexed(?string $fooIndexed): static #[ SymfonySerializer\SerializedName(serializedName: "boolIndexed"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Bool indexed field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), ApiFilter(OrmFilter\OrderFilter::class), ApiFilter(OrmFilter\BooleanFilter::class), @@ -232,7 +229,6 @@ public function setBoolIndexed(bool $boolIndexed): static #[ SymfonySerializer\SerializedName(serializedName: "fooMarkdown"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo markdown field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), Gedmo\Versioned, ORM\Column(name: "foo_markdown", type: "text", nullable: true), @@ -323,7 +319,6 @@ public function setFooMarkdownExcluded(?string $fooMarkdownExcluded): static #[ SymfonySerializer\SerializedName(serializedName: "fooDecimalExcluded"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo expression excluded decimal: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2), ApiFilter(OrmFilter\OrderFilter::class), ApiFilter(OrmFilter\NumericFilter::class), @@ -388,7 +383,6 @@ public function setFooDecimalExcluded(int|float|null $fooDecimalExcluded): stati #[ SymfonySerializer\SerializedName(serializedName: "singleEventReference"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Référence à l'événement: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2), ORM\ManyToOne(targetEntity: \App\Entity\Base\Event::class), ORM\JoinColumn(name: "single_event_reference_id", referencedColumnName: "id", onDelete: "SET NULL"), @@ -442,7 +436,6 @@ public function setSingleEventReference(?\App\Entity\Base\Event $singleEventRefe #[ SymfonySerializer\SerializedName(serializedName: "eventReferences"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Remontée d'événements manuelle: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2), ORM\ManyToMany(targetEntity: \App\Entity\Base\Event::class), ORM\JoinTable(name: "node_type_event_references"), @@ -630,7 +623,6 @@ public function setEventReferencesExcluded(Collection|array $eventReferencesExcl Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "bar"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_documents"]), - \ApiPlatform\Metadata\ApiProperty(description: "Bar documents field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1) ] private ?array $bar = null; @@ -648,12 +640,16 @@ public function setEventReferencesExcluded(Collection|array $eventReferencesExcl public function getBar(): array { if (null === $this->bar) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->bar = $this->objectManager ->getRepository(\mock\Entity\Document::class) - ->findByNodeSourceAndFieldName( + ->findByNodeSourceAndField( $this, - 'bar' + $this->getNode()->getNodeType()->getFieldByName("bar") ); } else { $this->bar = []; @@ -669,16 +665,23 @@ public function getBar(): array */ public function addBar(\mock\Entity\Document $document): static { - if (null !== $this->objectManager) { - $nodeSourceDocument = new \mock\Entity\NodesSourcesDocument( - $this, - $document - ); - $nodeSourceDocument->setFieldName('bar'); - if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) { - $this->objectManager->persist($nodeSourceDocument); - $this->addDocumentsByFields($nodeSourceDocument); - $this->bar = null; + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { + $field = $this->getNode()->getNodeType()->getFieldByName("bar"); + if (null !== $field) { + $nodeSourceDocument = new \mock\Entity\NodesSourcesDocument( + $this, + $document, + $field + ); + if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) { + $this->objectManager->persist($nodeSourceDocument); + $this->addDocumentsByFields($nodeSourceDocument); + $this->bar = null; + } } } return $this; @@ -694,7 +697,6 @@ public function addBar(\mock\Entity\Document $document): static Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "theForms"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_custom_forms"]), - \ApiPlatform\Metadata\ApiProperty(description: "Custom forms field"), SymfonySerializer\MaxDepth(2) ] private ?array $theForms = null; @@ -711,12 +713,16 @@ public function addBar(\mock\Entity\Document $document): static public function getTheForms(): array { if (null === $this->theForms) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->theForms = $this->objectManager ->getRepository(\mock\Entity\CustomForm::class) - ->findByNodeAndFieldName( + ->findByNodeAndField( $this->getNode(), - 'the_forms' + $this->getNode()->getNodeType()->getFieldByName("the_forms") ); } else { $this->theForms = []; @@ -732,15 +738,22 @@ public function getTheForms(): array */ public function addTheForms(\mock\Entity\CustomForm $customForm): static { - if (null !== $this->objectManager) { - $nodeCustomForm = new \mock\Entity\NodesSourcesCustomForm( - $this->getNode(), - $customForm - ); - $nodeCustomForm->setFieldName('the_forms'); - $this->objectManager->persist($nodeCustomForm); - $this->getNode()->addCustomForm($nodeCustomForm); - $this->theForms = null; + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { + $field = $this->getNode()->getNodeType()->getFieldByName("the_forms"); + if (null !== $field) { + $nodeCustomForm = new \mock\Entity\NodesSourcesCustomForm( + $this->getNode(), + $customForm, + $field + ); + $this->objectManager->persist($nodeCustomForm); + $this->getNode()->addCustomForm($nodeCustomForm); + $this->theForms = null; + } } return $this; } @@ -758,7 +771,6 @@ public function addTheForms(\mock\Entity\CustomForm $customForm): static Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "fooBar"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_nodes"]), - \ApiPlatform\Metadata\ApiProperty(description: "ForBar nodes field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2) ] private ?array $fooBarSources = null; @@ -776,12 +788,16 @@ public function addTheForms(\mock\Entity\CustomForm $customForm): static public function getFooBarSources(): array { if (null === $this->fooBarSources) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->fooBarSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldNameAndTranslation( + ->findByNodesSourcesAndFieldAndTranslation( $this, - 'foo_bar' + $this->getNode()->getNodeType()->getFieldByName("foo_bar") ); } else { $this->fooBarSources = []; @@ -827,12 +843,16 @@ public function setFooBarSources(?array $fooBarSources): static public function getFooBarHiddenSources(): array { if (null === $this->fooBarHiddenSources) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->fooBarHiddenSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldNameAndTranslation( + ->findByNodesSourcesAndFieldAndTranslation( $this, - 'foo_bar_hidden' + $this->getNode()->getNodeType()->getFieldByName("foo_bar_hidden") ); } else { $this->fooBarHiddenSources = []; @@ -866,7 +886,6 @@ public function setFooBarHiddenSources(?array $fooBarHiddenSources): static Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "fooBarTyped"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_nodes"]), - \ApiPlatform\Metadata\ApiProperty(description: "ForBar nodes typed field"), SymfonySerializer\MaxDepth(2) ] private ?array $fooBarTypedSources = null; @@ -884,12 +903,16 @@ public function setFooBarHiddenSources(?array $fooBarHiddenSources): static public function getFooBarTypedSources(): array { if (null === $this->fooBarTypedSources) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->fooBarTypedSources = $this->objectManager ->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class) - ->findByNodesSourcesAndFieldNameAndTranslation( + ->findByNodesSourcesAndFieldAndTranslation( $this, - 'foo_bar_typed' + $this->getNode()->getNodeType()->getFieldByName("foo_bar_typed") ); } else { $this->fooBarTypedSources = []; @@ -918,7 +941,6 @@ public function setFooBarTypedSources(?array $fooBarTypedSources): static #[ SymfonySerializer\SerializedName(serializedName: "layout"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "ForBar layout enum", schema: ["type" => "string", "enum" => ["light","dark","transparent"], "example" => "light"]), SymfonySerializer\MaxDepth(2), ApiFilter(OrmFilter\SearchFilter::class, strategy: "exact"), ApiFilter(\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter::class), @@ -967,7 +989,6 @@ public function setLayout(?string $layout): static #[ SymfonySerializer\SerializedName(serializedName: "fooManyToOne"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "For many_to_one field"), SymfonySerializer\MaxDepth(2), ORM\ManyToOne(targetEntity: \MyCustomEntity::class), ORM\JoinColumn(name: "foo_many_to_one_id", referencedColumnName: "id", onDelete: "SET NULL"), @@ -1009,7 +1030,6 @@ public function setFooManyToOne(?\MyCustomEntity $fooManyToOne = null): static #[ SymfonySerializer\SerializedName(serializedName: "fooManyToMany"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "For many_to_many field"), SymfonySerializer\MaxDepth(2), ORM\ManyToMany(targetEntity: \MyCustomEntity::class), ORM\JoinTable(name: "node_type_foo_many_to_many"), diff --git a/tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php b/tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php index 50a691f..1f25db5 100644 --- a/tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php +++ b/tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php @@ -18,7 +18,8 @@ use ApiPlatform\Serializer\Filter\PropertyFilter; /** - * Mock node-source entity. + * DO NOT EDIT + * Generated custom node-source type by Roadiz. */ #[ Gedmo\Loggable(logEntryClass: \RZ\Roadiz\CoreBundle\Entity\UserLogEntry::class), @@ -39,7 +40,6 @@ class NSMock extends \mock\Entity\NodesSources #[ SymfonySerializer\SerializedName(serializedName: "fooDatetime"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "foo_datetime"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo DateTime field"), SymfonySerializer\MaxDepth(2), ApiFilter(OrmFilter\OrderFilter::class), ApiFilter(OrmFilter\DateFilter::class), @@ -79,7 +79,6 @@ public function setFooDatetime(?\DateTime $fooDatetime): static #[ SymfonySerializer\SerializedName(serializedName: "foo"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), Gedmo\Versioned, ORM\Column( @@ -124,7 +123,6 @@ public function setFoo(?string $foo): static #[ SymfonySerializer\SerializedName(serializedName: "fooIndexed"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo indexed field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), ApiFilter(OrmFilter\SearchFilter::class, strategy: "partial"), ApiFilter(\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter::class), @@ -171,7 +169,6 @@ public function setFooIndexed(?string $fooIndexed): static #[ SymfonySerializer\SerializedName(serializedName: "boolIndexed"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Bool indexed field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), ApiFilter(OrmFilter\OrderFilter::class), ApiFilter(OrmFilter\BooleanFilter::class), @@ -232,7 +229,6 @@ public function setBoolIndexed(bool $boolIndexed): static #[ SymfonySerializer\SerializedName(serializedName: "fooMarkdown"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo markdown field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1), Gedmo\Versioned, ORM\Column(name: "foo_markdown", type: "text", nullable: true), @@ -323,7 +319,6 @@ public function setFooMarkdownExcluded(?string $fooMarkdownExcluded): static #[ SymfonySerializer\SerializedName(serializedName: "fooDecimalExcluded"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Foo expression excluded decimal: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2), ApiFilter(OrmFilter\OrderFilter::class), ApiFilter(OrmFilter\NumericFilter::class), @@ -388,7 +383,6 @@ public function setFooDecimalExcluded(int|float|null $fooDecimalExcluded): stati #[ SymfonySerializer\SerializedName(serializedName: "singleEventReference"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Référence à l'événement: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2), ORM\ManyToOne(targetEntity: \App\Entity\Base\Event::class), ORM\JoinColumn(name: "single_event_reference_id", referencedColumnName: "id", onDelete: "SET NULL"), @@ -442,7 +436,6 @@ public function setSingleEventReference(?\App\Entity\Base\Event $singleEventRefe #[ SymfonySerializer\SerializedName(serializedName: "eventReferences"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "Remontée d'événements manuelle: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2), ORM\ManyToMany(targetEntity: \App\Entity\Base\Event::class), ORM\JoinTable(name: "node_type_event_references"), @@ -630,7 +623,6 @@ public function setEventReferencesExcluded(Collection|array $eventReferencesExcl Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "bar"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_documents"]), - \ApiPlatform\Metadata\ApiProperty(description: "Bar documents field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(1) ] private ?array $bar = null; @@ -648,12 +640,16 @@ public function setEventReferencesExcluded(Collection|array $eventReferencesExcl public function getBar(): array { if (null === $this->bar) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->bar = $this->objectManager ->getRepository(\mock\Entity\Document::class) - ->findByNodeSourceAndFieldName( + ->findByNodeSourceAndField( $this, - 'bar' + $this->getNode()->getNodeType()->getFieldByName("bar") ); } else { $this->bar = []; @@ -669,16 +665,23 @@ public function getBar(): array */ public function addBar(\mock\Entity\Document $document): static { - if (null !== $this->objectManager) { - $nodeSourceDocument = new \mock\Entity\NodesSourcesDocument( - $this, - $document - ); - $nodeSourceDocument->setFieldName('bar'); - if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) { - $this->objectManager->persist($nodeSourceDocument); - $this->addDocumentsByFields($nodeSourceDocument); - $this->bar = null; + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { + $field = $this->getNode()->getNodeType()->getFieldByName("bar"); + if (null !== $field) { + $nodeSourceDocument = new \mock\Entity\NodesSourcesDocument( + $this, + $document, + $field + ); + if (!$this->hasNodesSourcesDocuments($nodeSourceDocument)) { + $this->objectManager->persist($nodeSourceDocument); + $this->addDocumentsByFields($nodeSourceDocument); + $this->bar = null; + } } } return $this; @@ -694,7 +697,6 @@ public function addBar(\mock\Entity\Document $document): static Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "theForms"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_custom_forms"]), - \ApiPlatform\Metadata\ApiProperty(description: "Custom forms field"), SymfonySerializer\MaxDepth(2) ] private ?array $theForms = null; @@ -711,12 +713,16 @@ public function addBar(\mock\Entity\Document $document): static public function getTheForms(): array { if (null === $this->theForms) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->theForms = $this->objectManager ->getRepository(\mock\Entity\CustomForm::class) - ->findByNodeAndFieldName( + ->findByNodeAndField( $this->getNode(), - 'the_forms' + $this->getNode()->getNodeType()->getFieldByName("the_forms") ); } else { $this->theForms = []; @@ -732,15 +738,22 @@ public function getTheForms(): array */ public function addTheForms(\mock\Entity\CustomForm $customForm): static { - if (null !== $this->objectManager) { - $nodeCustomForm = new \mock\Entity\NodesSourcesCustomForm( - $this->getNode(), - $customForm - ); - $nodeCustomForm->setFieldName('the_forms'); - $this->objectManager->persist($nodeCustomForm); - $this->getNode()->addCustomForm($nodeCustomForm); - $this->theForms = null; + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { + $field = $this->getNode()->getNodeType()->getFieldByName("the_forms"); + if (null !== $field) { + $nodeCustomForm = new \mock\Entity\NodesSourcesCustomForm( + $this->getNode(), + $customForm, + $field + ); + $this->objectManager->persist($nodeCustomForm); + $this->getNode()->addCustomForm($nodeCustomForm); + $this->theForms = null; + } } return $this; } @@ -758,7 +771,6 @@ public function addTheForms(\mock\Entity\CustomForm $customForm): static Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "fooBar"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_nodes"]), - \ApiPlatform\Metadata\ApiProperty(description: "ForBar nodes field: Maecenas sed diam eget risus varius blandit sit amet non magna"), SymfonySerializer\MaxDepth(2) ] private ?array $fooBarSources = null; @@ -776,12 +788,16 @@ public function addTheForms(\mock\Entity\CustomForm $customForm): static public function getFooBarSources(): array { if (null === $this->fooBarSources) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->fooBarSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldNameAndTranslation( + ->findByNodesSourcesAndFieldAndTranslation( $this, - 'foo_bar' + $this->getNode()->getNodeType()->getFieldByName("foo_bar") ); } else { $this->fooBarSources = []; @@ -827,12 +843,16 @@ public function setFooBarSources(?array $fooBarSources): static public function getFooBarHiddenSources(): array { if (null === $this->fooBarHiddenSources) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->fooBarHiddenSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldNameAndTranslation( + ->findByNodesSourcesAndFieldAndTranslation( $this, - 'foo_bar_hidden' + $this->getNode()->getNodeType()->getFieldByName("foo_bar_hidden") ); } else { $this->fooBarHiddenSources = []; @@ -866,7 +886,6 @@ public function setFooBarHiddenSources(?array $fooBarHiddenSources): static Serializer\Exclude, SymfonySerializer\SerializedName(serializedName: "fooBarTyped"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default", "nodes_sources_nodes"]), - \ApiPlatform\Metadata\ApiProperty(description: "ForBar nodes typed field"), SymfonySerializer\MaxDepth(2) ] private ?array $fooBarTypedSources = null; @@ -884,12 +903,16 @@ public function setFooBarHiddenSources(?array $fooBarHiddenSources): static public function getFooBarTypedSources(): array { if (null === $this->fooBarTypedSources) { - if (null !== $this->objectManager) { + if ( + null !== $this->objectManager && + null !== $this->getNode() && + null !== $this->getNode()->getNodeType() + ) { $this->fooBarTypedSources = $this->objectManager ->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class) - ->findByNodesSourcesAndFieldNameAndTranslation( + ->findByNodesSourcesAndFieldAndTranslation( $this, - 'foo_bar_typed' + $this->getNode()->getNodeType()->getFieldByName("foo_bar_typed") ); } else { $this->fooBarTypedSources = []; @@ -918,7 +941,6 @@ public function setFooBarTypedSources(?array $fooBarTypedSources): static #[ SymfonySerializer\SerializedName(serializedName: "layout"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "ForBar layout enum", schema: ["type" => "string", "enum" => ["light","dark","transparent"], "example" => "light"]), SymfonySerializer\MaxDepth(2), ApiFilter(OrmFilter\SearchFilter::class, strategy: "exact"), ApiFilter(\RZ\Roadiz\CoreBundle\Api\Filter\NotFilter::class), @@ -967,7 +989,6 @@ public function setLayout(?string $layout): static #[ SymfonySerializer\SerializedName(serializedName: "fooManyToOne"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "For many_to_one field"), SymfonySerializer\MaxDepth(2), ORM\ManyToOne(targetEntity: \MyCustomEntity::class), ORM\JoinColumn(name: "foo_many_to_one_id", referencedColumnName: "id", onDelete: "SET NULL"), @@ -1009,7 +1030,6 @@ public function setFooManyToOne(?\MyCustomEntity $fooManyToOne = null): static #[ SymfonySerializer\SerializedName(serializedName: "fooManyToMany"), SymfonySerializer\Groups(["nodes_sources", "nodes_sources_default"]), - \ApiPlatform\Metadata\ApiProperty(description: "For many_to_many field"), SymfonySerializer\MaxDepth(2), ORM\ManyToMany(targetEntity: \MyCustomEntity::class), ORM\JoinTable(name: "node_type_foo_many_to_many"), diff --git a/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php b/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php index 6b1c913..703a978 100644 --- a/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php +++ b/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php @@ -11,7 +11,7 @@ use Doctrine\Persistence\ManagerRegistry; use RZ\Roadiz\CoreBundle\Preview\PreviewResolverInterface; use RZ\Roadiz\CoreBundle\SearchEngine\NodeSourceSearchHandlerInterface; -use Symfony\Bundle\SecurityBundle\Security; +use Symfony\Component\Security\Core\Security; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** diff --git a/src/Test/JoinedTableDefaultValuesResolver.php b/tests/mocks/JoinedTableDefaultValuesResolver.php similarity index 94% rename from src/Test/JoinedTableDefaultValuesResolver.php rename to tests/mocks/JoinedTableDefaultValuesResolver.php index 4327bc6..c70f733 100644 --- a/src/Test/JoinedTableDefaultValuesResolver.php +++ b/tests/mocks/JoinedTableDefaultValuesResolver.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace RZ\Roadiz\EntityGenerator\Test; +namespace tests\mocks; use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface; use RZ\Roadiz\EntityGenerator\Field\DefaultValuesResolverInterface; diff --git a/src/Test/NodeTypeAwareTestTrait.php b/tests/mocks/NodeTypeAwareTrait.php similarity index 81% rename from src/Test/NodeTypeAwareTestTrait.php rename to tests/mocks/NodeTypeAwareTrait.php index d43bf6c..eea532b 100644 --- a/src/Test/NodeTypeAwareTestTrait.php +++ b/tests/mocks/NodeTypeAwareTrait.php @@ -1,23 +1,20 @@ createStub(NodeTypeInterface::class); - $mockNodeType - ->method('getFields') - ->willReturn( - new ArrayCollection([ - (new SimpleNodeTypeField()) + $mockNodeType = $this->newMockInstance(NodeTypeInterface::class); + $mockNodeType->getMockController()->getFields = function() { + return new ArrayCollection([ + (new NodeTypeField()) ->setName('foo_datetime') ->setTypeName('datetime') ->setDoctrineType('datetime') @@ -29,7 +26,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setVirtual(false) ->setLabel('Foo DateTime field') ->setIndexed(true), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo') ->setTypeName('string') ->setVirtual(false) @@ -37,7 +34,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setLabel('Foo field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('fooIndexed') ->setTypeName('string') ->setVirtual(false) @@ -45,7 +42,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setLabel('Foo indexed field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(true), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('boolIndexed') ->setTypeName('bool') ->setDoctrineType('boolean') @@ -54,7 +51,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setLabel('Bool indexed field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(true), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_markdown') ->setTypeName('markdown') ->setDoctrineType('text') @@ -64,7 +61,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setDefaultValues("allow_h2: false\r\nallow_h3: false\r\nallow_h4: false\r\nallow_h5: false\r\nallow_h6: false\r\nallow_bold: true\r\nallow_italic: true\r\nallow_blockquote: false\r\nallow_image: false\r\nallow_list: false\r\nallow_nbsp: true\r\nallow_nb_hyphen: true\r\nallow_return: true\r\nallow_link: false\r\nallow_hr: false\r\nallow_preview: true") ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_markdown_excluded') ->setTypeName('markdown') ->setDoctrineType('text') @@ -74,7 +71,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setDefaultValues("allow_h2: false\r\nallow_h3: false\r\nallow_h4: false\r\nallow_h5: false\r\nallow_h6: false\r\nallow_bold: true\r\nallow_italic: true\r\nallow_blockquote: false\r\nallow_image: false\r\nallow_list: false\r\nallow_nbsp: true\r\nallow_nb_hyphen: true\r\nallow_return: true\r\nallow_link: false\r\nallow_hr: false\r\nallow_preview: true") ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_decimal_excluded') ->setTypeName('decimal') ->setDoctrineType('decimal') @@ -83,7 +80,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setLabel('Foo expression excluded decimal') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(true), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('single_event_reference') ->setTypeName('many_to_one') ->setVirtual(false) @@ -91,7 +88,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setDefaultValues("# Entity class name\r\nclassname: \\App\\Entity\\Base\\Event\r\n# Displayable is the method used to display entity name\r\ndisplayable: getName\r\n# Same as Displayable but for a secondary information\r\nalt_displayable: getSortingFirstDateTime\r\n# Same as Displayable but for a secondary information\r\nthumbnail: getMainDocument\r\n# Searchable entity fields\r\nsearchable:\r\n - name\r\n - slug\r\n# This order will only be used for explorer\r\norderBy:\r\n - field: sortingLastDateTime\r\n direction: DESC") ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('event_references') ->setTypeName('many_to_many') ->setVirtual(false) @@ -99,7 +96,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setDefaultValues("# Entity class name\r\nclassname: \\App\\Entity\\Base\\Event\r\n# Displayable is the method used to display entity name\r\ndisplayable: getName\r\n# Same as Displayable but for a secondary information\r\nalt_displayable: getSortingFirstDateTime\r\n# Same as Displayable but for a secondary information\r\nthumbnail: getMainDocument\r\n# Searchable entity fields\r\nsearchable:\r\n - name\r\n - slug\r\n# This order will only be used for explorer\r\norderBy:\r\n - field: sortingLastDateTime\r\n direction: DESC") ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('event_references_proxied') ->setTypeName('many_to_many') ->setVirtual(false) @@ -107,7 +104,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setDefaultValues("# Entity class name\r\nclassname: \\App\\Entity\\Base\\Event\r\n# Displayable is the method used to display entity name\r\ndisplayable: getName\r\n# Same as Displayable but for a secondary information\r\nalt_displayable: getSortingFirstDateTime\r\n# Same as Displayable but for a secondary information\r\nthumbnail: getMainDocument\r\n# Searchable entity fields\r\nsearchable:\r\n - name\r\n - slug\r\n# This order will only be used for explorer\r\norderBy:\r\n - field: sortingLastDateTime\r\n direction: DESC\r\n# Use a proxy entity\r\nproxy:\r\n classname: \\App\\Entity\\PositionedCity\r\n self: nodeSource\r\n relation: city\r\n # This order will preserve position\r\n orderBy:\r\n - field: position\r\n direction: ASC") ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('event_references_excluded') ->setTypeName('many_to_many') ->setVirtual(false) @@ -116,7 +113,7 @@ protected function getMockNodeType(): NodeTypeInterface ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setDefaultValues("# Entity class name\r\nclassname: \\App\\Entity\\Base\\Event\r\n# Displayable is the method used to display entity name\r\ndisplayable: getName\r\n# Same as Displayable but for a secondary information\r\nalt_displayable: getSortingFirstDateTime\r\n# Same as Displayable but for a secondary information\r\nthumbnail: getMainDocument\r\n# Searchable entity fields\r\nsearchable:\r\n - name\r\n - slug\r\n# This order will only be used for explorer\r\norderBy:\r\n - field: sortingLastDateTime\r\n direction: DESC") ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('bar') ->setTypeName('documents') ->setSerializationMaxDepth(1) @@ -124,20 +121,20 @@ protected function getMockNodeType(): NodeTypeInterface ->setLabel('Bar documents field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('the_forms') ->setTypeName('custom_forms') ->setVirtual(true) ->setLabel('Custom forms field') ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_bar') ->setTypeName('nodes') ->setVirtual(true) ->setLabel('ForBar nodes field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_bar_hidden') ->setTypeName('nodes') ->setVirtual(true) @@ -145,20 +142,20 @@ protected function getMockNodeType(): NodeTypeInterface ->setLabel('ForBar hidden nodes field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_bar_typed') ->setTypeName('nodes') ->setVirtual(true) ->setLabel('ForBar nodes typed field') ->setIndexed(false) ->setDefaultValues('MockTwo'), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('layout') ->setTypeName('enum') ->setLabel('ForBar layout enum') ->setIndexed(true) ->setDefaultValues('light, dark, transparent'), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_many_to_one') ->setTypeName('many_to_one') ->setVirtual(false) @@ -168,7 +165,7 @@ classname: \MyCustomEntity displayable: getName EOT) ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_many_to_many') ->setTypeName('many_to_many') ->setVirtual(false) @@ -181,7 +178,7 @@ classname: \MyCustomEntity direction: asc EOT) ->setIndexed(false), - (new SimpleNodeTypeField()) + (new NodeTypeField()) ->setName('foo_many_to_many_proxied') ->setTypeName('many_to_many') ->setVirtual(false) @@ -204,45 +201,42 @@ classname: Themes\MyTheme\Entities\PositionedCity direction: ASC EOT) ->setIndexed(false), - ]) - ); - - $mockNodeType - ->method('getSourceEntityTableName') - ->willReturn('ns_mock'); - $mockNodeType - ->method('getSourceEntityClassName') - ->willReturn('NSMock'); - $mockNodeType - ->method('getName') - ->willReturn('Mock'); - $mockNodeType - ->method('isReachable') - ->willReturn(true); - $mockNodeType - ->method('isPublishable') - ->willReturn(true); + ]); + }; + $mockNodeType->getMockController()->getSourceEntityTableName = function() { + return 'ns_mock'; + }; + $mockNodeType->getMockController()->getSourceEntityClassName = function() { + return 'NSMock'; + }; + $mockNodeType->getMockController()->getName = function() { + return 'Mock'; + }; + $mockNodeType->getMockController()->isReachable = function() { + return true; + }; + $mockNodeType->getMockController()->isPublishable = function() { + return true; + }; return $mockNodeType; } - protected function getMockNodeTypeResolver(): NodeTypeResolverInterface + protected function getMockNodeTypeResolver() { - $mockNodeTypeResolver = $this->createStub(NodeTypeResolverInterface::class); - $mockNodeTypeResolver->method('get')->willReturnCallback( - function (string $nodeTypeName): NodeTypeInterface { - $mockNodeType = $this->createStub(NodeTypeInterface::class); - $mockNodeType - ->method('getSourceEntityFullQualifiedClassName') - ->willReturn('tests\mocks\GeneratedNodesSources\NS' . $nodeTypeName) - ; - return $mockNodeType; - } - ); + $mockNodeTypeResolver = $this->newMockInstance(NodeTypeResolverInterface::class); + $test = $this; + $mockNodeTypeResolver->getMockController()->get = function(string $nodeTypeName) use ($test) { + $mockNodeType = $test->newMockInstance(NodeTypeInterface::class); + $mockNodeType->getMockController()->getSourceEntityFullQualifiedClassName = function() use ($nodeTypeName) { + return 'tests\mocks\GeneratedNodesSources\NS' . $nodeTypeName; + }; + return $mockNodeType; + }; return $mockNodeTypeResolver; } - protected function getMockDefaultValuesResolver(): JoinedTableDefaultValuesResolver + protected function getMockDefaultValuesResolver() { - return new JoinedTableDefaultValuesResolver(); + return $this->newMockInstance(JoinedTableDefaultValuesResolver::class); } } diff --git a/src/Test/SimpleNodeTypeField.php b/tests/mocks/NodeTypeField.php similarity index 82% rename from src/Test/SimpleNodeTypeField.php rename to tests/mocks/NodeTypeField.php index 261d72e..22e9dca 100644 --- a/src/Test/SimpleNodeTypeField.php +++ b/tests/mocks/NodeTypeField.php @@ -1,14 +1,13 @@ description = $description; return $this; @@ -60,9 +59,9 @@ public function getLabel(): string /** * @param string|null $label - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setLabel(?string $label): SimpleNodeTypeField + public function setLabel(?string $label): NodeTypeField { $this->label = $label; return $this; @@ -78,9 +77,9 @@ public function getName(): string /** * @param string|null $name - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setName(?string $name): SimpleNodeTypeField + public function setName(?string $name): NodeTypeField { $this->name = $name; return $this; @@ -96,9 +95,9 @@ public function getPlaceholder(): ?string /** * @param string|null $placeholder - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setPlaceholder(?string $placeholder): SimpleNodeTypeField + public function setPlaceholder(?string $placeholder): NodeTypeField { $this->placeholder = $placeholder; return $this; @@ -114,9 +113,9 @@ public function getDefaultValues(): ?string /** * @param string|null $defaultValues - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setDefaultValues(?string $defaultValues): SimpleNodeTypeField + public function setDefaultValues(?string $defaultValues): NodeTypeField { $this->defaultValues = $defaultValues; return $this; @@ -132,9 +131,9 @@ public function getGroupName(): ?string /** * @param string|null $groupName - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setGroupName(?string $groupName): SimpleNodeTypeField + public function setGroupName(?string $groupName): NodeTypeField { $this->groupName = $groupName; return $this; @@ -150,9 +149,9 @@ public function getMinLength(): ?int /** * @param int|null $minLength - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setMinLength(?int $minLength): SimpleNodeTypeField + public function setMinLength(?int $minLength): NodeTypeField { $this->minLength = $minLength; return $this; @@ -168,9 +167,9 @@ public function getMaxLength(): ?int /** * @param int|null $maxLength - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setMaxLength(?int $maxLength): SimpleNodeTypeField + public function setMaxLength(?int $maxLength): NodeTypeField { $this->maxLength = $maxLength; return $this; @@ -186,9 +185,9 @@ public function isVisible(): bool /** * @param bool $visible - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setVisible(bool $visible): SimpleNodeTypeField + public function setVisible(bool $visible): NodeTypeField { $this->visible = $visible; return $this; @@ -204,9 +203,9 @@ public function isUniversal(): bool /** * @param bool $universal - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setUniversal(bool $universal): SimpleNodeTypeField + public function setUniversal(bool $universal): NodeTypeField { $this->universal = $universal; return $this; @@ -222,9 +221,9 @@ public function isSearchable(): bool /** * @param bool $searchable - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setSearchable(bool $searchable): SimpleNodeTypeField + public function setSearchable(bool $searchable): NodeTypeField { $this->searchable = $searchable; return $this; @@ -240,9 +239,9 @@ public function isVirtual(): bool /** * @param bool $virtual - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setVirtual(bool $virtual): SimpleNodeTypeField + public function setVirtual(bool $virtual): NodeTypeField { $this->virtual = $virtual; return $this; @@ -258,9 +257,9 @@ public function isIndexed(): bool /** * @param bool $indexed - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setIndexed(bool $indexed): SimpleNodeTypeField + public function setIndexed(bool $indexed): NodeTypeField { $this->indexed = $indexed; return $this; @@ -276,9 +275,9 @@ public function isExpanded(): bool /** * @param bool $expanded - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setExpanded(bool $expanded): SimpleNodeTypeField + public function setExpanded(bool $expanded): NodeTypeField { $this->expanded = $expanded; return $this; @@ -294,9 +293,9 @@ public function getTypeName(): string /** * @param string $typeName - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setTypeName(string $typeName): SimpleNodeTypeField + public function setTypeName(string $typeName): NodeTypeField { $this->typeName = $typeName; return $this; @@ -312,9 +311,9 @@ public function getNodeTypeName(): string /** * @param string $nodeTypeName - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setNodeTypeName(string $nodeTypeName): SimpleNodeTypeField + public function setNodeTypeName(string $nodeTypeName): NodeTypeField { $this->nodeTypeName = $nodeTypeName; return $this; @@ -330,9 +329,9 @@ public function getDoctrineType(): string /** * @param string $doctrineType - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setDoctrineType(string $doctrineType): SimpleNodeTypeField + public function setDoctrineType(string $doctrineType): NodeTypeField { $this->doctrineType = $doctrineType; return $this; @@ -523,9 +522,9 @@ public function getSerializationGroups(): array /** * @param array $serializationGroups - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setSerializationGroups(array $serializationGroups): SimpleNodeTypeField + public function setSerializationGroups(array $serializationGroups): NodeTypeField { $this->serializationGroups = $serializationGroups; return $this; @@ -541,9 +540,9 @@ public function isExcludedFromSerialization(): bool /** * @param bool $excludedFromSerialization - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setExcludedFromSerialization(bool $excludedFromSerialization): SimpleNodeTypeField + public function setExcludedFromSerialization(bool $excludedFromSerialization): NodeTypeField { $this->excludedFromSerialization = $excludedFromSerialization; return $this; @@ -559,9 +558,9 @@ public function getSerializationExclusionExpression(): ?string /** * @param string|null $serializationExclusionExpression - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setSerializationExclusionExpression(?string $serializationExclusionExpression): SimpleNodeTypeField + public function setSerializationExclusionExpression(?string $serializationExclusionExpression): NodeTypeField { $this->serializationExclusionExpression = $serializationExclusionExpression; return $this; @@ -577,9 +576,9 @@ public function getSerializationMaxDepth(): ?int /** * @param int|null $serializationMaxDepth - * @return SimpleNodeTypeField + * @return NodeTypeField */ - public function setSerializationMaxDepth(?int $serializationMaxDepth): SimpleNodeTypeField + public function setSerializationMaxDepth(?int $serializationMaxDepth): NodeTypeField { $this->serializationMaxDepth = $serializationMaxDepth; return $this; diff --git a/tests/units/EntityGenerator.php b/tests/units/EntityGenerator.php new file mode 100644 index 0000000..3f225b5 --- /dev/null +++ b/tests/units/EntityGenerator.php @@ -0,0 +1,83 @@ +getMockNodeType(); + $mockNodeTypeResolver = $this->getMockNodeTypeResolver(); + $mockDefaultValuesResolver = $this->getMockDefaultValuesResolver(); + + /* + * Uncomment for generating a mock file from tests + */ +// $dumpInstance = $this->newTestedInstance($mockNodeType, $mockNodeTypeResolver, [ +// 'parent_class' => '\mock\Entity\NodesSources', +// 'node_class' => '\mock\Entity\Node', +// 'translation_class' => '\mock\Entity\Translation', +// 'document_class' => '\mock\Entity\Document', +// 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', +// 'custom_form_class' => '\mock\Entity\CustomForm', +// 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', +// 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', +// 'namespace' => '\tests\mocks\GeneratedNodesSources', +// 'use_native_json' => true, +// 'use_api_platform_filters' => true, +// ]); +// file_put_contents( +// dirname(__DIR__) . '/mocks/GeneratedNodesSources/NSMock.php', +// $dumpInstance->getClassContent() +// ); + + $this + // creation of a new instance of the tested class + ->given($this->newTestedInstance($mockNodeType, $mockNodeTypeResolver, $mockDefaultValuesResolver, [ + 'parent_class' => '\mock\Entity\NodesSources', + 'node_class' => '\mock\Entity\Node', + 'translation_class' => '\mock\Entity\Translation', + 'document_class' => '\mock\Entity\Document', + 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', + 'custom_form_class' => '\mock\Entity\CustomForm', + 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', + 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', + 'namespace' => '\tests\mocks\GeneratedNodesSources', + 'use_native_json' => true, + 'use_api_platform_filters' => true, + ])) + ->then + ->string($this->testedInstance->getClassContent()) + ->isEqualTo(file_get_contents(dirname(__DIR__) . '/mocks/GeneratedNodesSources/NSMock.php')) + ; + + /** + * TEST without leading slashs + */ + $this + // creation of a new instance of the tested class + ->given($this->newTestedInstance($mockNodeType, $mockNodeTypeResolver, $mockDefaultValuesResolver, [ + 'parent_class' => 'mock\Entity\NodesSources', + 'node_class' => 'mock\Entity\Node', + 'translation_class' => 'mock\Entity\Translation', + 'document_class' => 'mock\Entity\Document', + 'document_proxy_class' => 'mock\Entity\NodesSourcesDocument', + 'custom_form_class' => 'mock\Entity\CustomForm', + 'custom_form_proxy_class' => 'mock\Entity\NodesSourcesCustomForm', + 'repository_class' => 'mock\Entity\Repository\NodesSourcesRepository', + 'namespace' => 'tests\mocks\GeneratedNodesSources', + 'use_native_json' => true, + 'use_api_platform_filters' => true, + ])) + ->then + ->string($this->testedInstance->getClassContent()) + ->isEqualTo(file_get_contents(dirname(__DIR__) . '/mocks/GeneratedNodesSources/NSMock.php')) + ; + } +} diff --git a/tests/units/EntityGeneratorFactory.php b/tests/units/EntityGeneratorFactory.php new file mode 100644 index 0000000..ba75bd8 --- /dev/null +++ b/tests/units/EntityGeneratorFactory.php @@ -0,0 +1,135 @@ +getMockNodeType(); + $mockNodeTypeResolver = $this->getMockNodeTypeResolver(); + $mockDefaultValuesResolver = $this->getMockDefaultValuesResolver(); + + $this + // creation of a new instance of the tested class + ->given($this->newTestedInstance($mockNodeTypeResolver, $mockDefaultValuesResolver, [ + 'parent_class' => '\mock\Entity\NodesSources', + 'node_class' => '\mock\Entity\Node', + 'translation_class' => '\mock\Entity\Translation', + 'document_class' => '\mock\Entity\Document', + 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', + 'custom_form_class' => '\mock\Entity\CustomForm', + 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', + 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', + 'namespace' => '\tests\mocks\GeneratedNodesSources', + 'use_native_json' => true, + 'use_api_platform_filters' => true, + ])) + ->then + ->string($this->testedInstance->create($mockNodeType)->getClassContent()) + ->isEqualTo(file_get_contents(dirname(__DIR__) . '/mocks/GeneratedNodesSources/NSMock.php')) + ; + } + + public function testCreateWithCustomRepository() + { + $mockNodeType = $this->getMockNodeType(); + $mockNodeTypeResolver = $this->getMockNodeTypeResolver(); + $mockDefaultValuesResolver = $this->getMockDefaultValuesResolver(); + + /* + * Uncomment for generating a mock file from tests + */ +// $dumpInstance = $this->newTestedInstance($mockNodeTypeResolver, [ +// 'parent_class' => '\mock\Entity\NodesSources', +// 'node_class' => '\mock\Entity\Node', +// 'translation_class' => '\mock\Entity\Translation', +// 'document_class' => '\mock\Entity\Document', +// 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', +// 'custom_form_class' => '\mock\Entity\CustomForm', +// 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', +// 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', +// 'namespace' => '\tests\mocks\GeneratedNodesSourcesWithRepository', +// 'use_native_json' => true, +// 'use_api_platform_filters' => true, +// ]); +// file_put_contents( +// dirname(__DIR__) . '/mocks/GeneratedNodesSourcesWithRepository/NSMock.php', +// $dumpInstance->createWithCustomRepository($mockNodeType)->getClassContent() +// ); + + $this + // creation of a new instance of the tested class + ->given($this->newTestedInstance($mockNodeTypeResolver, $mockDefaultValuesResolver, [ + 'parent_class' => '\mock\Entity\NodesSources', + 'node_class' => '\mock\Entity\Node', + 'translation_class' => '\mock\Entity\Translation', + 'document_class' => '\mock\Entity\Document', + 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', + 'custom_form_class' => '\mock\Entity\CustomForm', + 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', + 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', + 'namespace' => '\tests\mocks\GeneratedNodesSourcesWithRepository', + 'use_native_json' => true, + 'use_api_platform_filters' => true, + ])) + ->then + ->string($this->testedInstance->createWithCustomRepository($mockNodeType)->getClassContent()) + ->isEqualTo(file_get_contents(dirname(__DIR__) . '/mocks/GeneratedNodesSourcesWithRepository/NSMock.php')) + ; + } + + public function testCreateCustomRepository() + { + $mockNodeType = $this->getMockNodeType(); + $mockNodeTypeResolver = $this->getMockNodeTypeResolver(); + $mockDefaultValuesResolver = $this->getMockDefaultValuesResolver(); + + /* + * Uncomment for generating a mock file from tests + */ +// $dumpInstance = $this->newTestedInstance($mockNodeTypeResolver, [ +// 'parent_class' => '\mock\Entity\NodesSources', +// 'node_class' => '\mock\Entity\Node', +// 'translation_class' => '\mock\Entity\Translation', +// 'document_class' => '\mock\Entity\Document', +// 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', +// 'custom_form_class' => '\mock\Entity\CustomForm', +// 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', +// 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', +// 'namespace' => '\tests\mocks\GeneratedNodesSourcesWithRepository', +// 'use_native_json' => true, +// 'use_api_platform_filters' => true, +// ]); +// file_put_contents( +// dirname(__DIR__) . '/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php', +// $dumpInstance->createCustomRepository($mockNodeType)->getClassContent() +// ); + + $this + // creation of a new instance of the tested class + ->given($this->newTestedInstance($mockNodeTypeResolver, $mockDefaultValuesResolver, [ + 'parent_class' => '\mock\Entity\NodesSources', + 'node_class' => '\mock\Entity\Node', + 'translation_class' => '\mock\Entity\Translation', + 'document_class' => '\mock\Entity\Document', + 'document_proxy_class' => '\mock\Entity\NodesSourcesDocument', + 'custom_form_class' => '\mock\Entity\CustomForm', + 'custom_form_proxy_class' => '\mock\Entity\NodesSourcesCustomForm', + 'repository_class' => '\mock\Entity\Repository\NodesSourcesRepository', + 'namespace' => '\tests\mocks\GeneratedNodesSourcesWithRepository', + 'use_native_json' => true, + 'use_api_platform_filters' => true, + ])) + ->then + ->string($this->testedInstance->createCustomRepository($mockNodeType)->getClassContent()) + ->isEqualTo(file_get_contents(dirname(__DIR__) . '/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php')) + ; + } +}