From 7501ef4fd5ca9bf7783ab3cb58b114cb60f5d00f Mon Sep 17 00:00:00 2001 From: roadiz-ci Date: Tue, 24 Sep 2024 13:10:59 +0000 Subject: [PATCH] Merge branch hotfix/v2.3.28 --- .github/workflows/run-test.yml | 4 +- LICENSE.md | 2 +- Makefile | 4 - composer.json | 24 ++-- phpstan.neon | 7 +- src/Attribute/AttributeGenerator.php | 83 ++++++++--- src/EntityGenerator.php | 9 +- src/EntityGeneratorFactory.php | 13 +- src/Field/AbstractFieldGenerator.php | 21 +++ src/Field/CustomFormsFieldGenerator.php | 35 ++--- src/Field/DocumentsFieldGenerator.php | 37 ++--- src/Field/NodesFieldGenerator.php | 10 +- src/RepositoryGenerator.php | 2 +- tests/EntityGeneratorFactoryTest.php | 104 ++++++++++++++ tests/EntityGeneratorTest.php | 76 ++++++++++ .../JoinedTableDefaultValuesResolver.php | 2 +- .../GeneratedNodesSources/NSMock.php | 122 +++++++--------- .../NSMock.php | 124 +++++++--------- .../Repository/NSMockRepository.php | 38 +++++ ...reTrait.php => NodeTypeAwareTestTrait.php} | 116 ++++++++------- ...eTypeField.php => SimpleNodeTypeField.php} | 89 ++++++------ .../NSMockRepository.php | 38 ----- tests/units/EntityGenerator.php | 83 ----------- tests/units/EntityGeneratorFactory.php | 135 ------------------ 24 files changed, 570 insertions(+), 608 deletions(-) delete mode 100644 Makefile create mode 100644 tests/EntityGeneratorFactoryTest.php create mode 100644 tests/EntityGeneratorTest.php rename tests/{mocks => }/JoinedTableDefaultValuesResolver.php (94%) rename tests/{mocks => Mocks}/GeneratedNodesSources/NSMock.php (90%) rename tests/{mocks => Mocks}/GeneratedNodesSourcesWithRepository/NSMock.php (90%) create mode 100644 tests/Mocks/GeneratedNodesSourcesWithRepository/Repository/NSMockRepository.php rename tests/{mocks/NodeTypeAwareTrait.php => NodeTypeAwareTestTrait.php} (81%) rename tests/{mocks/NodeTypeField.php => SimpleNodeTypeField.php} (82%) delete mode 100644 tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php delete mode 100644 tests/units/EntityGenerator.php delete mode 100644 tests/units/EntityGeneratorFactory.php diff --git a/.github/workflows/run-test.yml b/.github/workflows/run-test.yml index 5f7efa2..96cc206 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 atoum unit tests - run: vendor/bin/atoum -f tests/units/* + - name: Run unit tests + run: vendor/bin/phpunit -v --whitelist ./src tests diff --git a/LICENSE.md b/LICENSE.md index d4d8a00..8e18fa6 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright © 2023 Ambroise Maupate +Copyright © 2024 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 deleted file mode 100644 index c45718c..0000000 --- a/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -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/atoum -f tests/units/* diff --git a/composer.json b/composer.json index aff7aec..afb14f9 100644 --- a/composer.json +++ b/composer.json @@ -15,30 +15,34 @@ "php": ">=8.1", "ext-json": "*", "roadiz/nodetype-contracts": "~1.1.2", - "symfony/string": "5.4.*", - "symfony/yaml": "5.4.*", - "symfony/serializer": "5.4.*", - "symfony/options-resolver": "5.4.*" + "symfony/string": "6.4.*", + "symfony/yaml": "6.4.*", + "symfony/serializer": "6.4.*", + "symfony/options-resolver": "6.4.*" }, "suggest": { "api-platform/core": "If you need to create ApiFilter annotation right into your entities" }, "autoload": { "psr-4": { - "RZ\\Roadiz\\EntityGenerator\\": "src/", - "tests\\mocks\\": "tests/mocks/" + "RZ\\Roadiz\\EntityGenerator\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "RZ\\Roadiz\\EntityGenerator\\Tests\\": "tests/" } }, "require-dev": { "squizlabs/php_codesniffer": "^3.5", "phpstan/phpstan": "^1.5.3", - "atoum/atoum": "^4.0.0", - "api-platform/core": "~2.7.0" + "phpunit/phpunit": "^9.5", + "api-platform/core": "~3.2.14" }, "extra": { "branch-alias": { - "dev-main": "2.2.x-dev", - "dev-develop": "2.3.x-dev" + "dev-main": "2.3.x-dev", + "dev-develop": "2.4.x-dev" } } } diff --git a/phpstan.neon b/phpstan.neon index c86467e..6d40c05 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - level: max + level: 7 paths: - src excludePaths: @@ -7,5 +7,6 @@ parameters: - */bower_components/* - */static/* reportUnmatchedIgnoredErrors: false - checkMissingIterableValueType: false - checkGenericClassInNonGenericObjectType: false + ignoreErrors: + - identifier: missingType.iterableValue + - identifier: missingType.generics diff --git a/src/Attribute/AttributeGenerator.php b/src/Attribute/AttributeGenerator.php index 2b880f0..2b6c07d 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 int[]|string[] $parameters + * @param array $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"', $string); + return sprintf('"%s"', str_replace('"', '\\"', $string)); } public function generate(int $currentIndentation = 0): string @@ -32,20 +32,10 @@ public function generate(int $currentIndentation = 0): string $formattedParams = []; if (count($this->parameters) > 3) { foreach ($this->parameters as $name => $parameter) { - 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 - ); + if (empty($parameter)) { + continue; } + $formattedParams[] = $this->formatProperties($name, $parameter, $currentIndentation); } return str_repeat(' ', $currentIndentation) . @@ -53,26 +43,75 @@ public function generate(int $currentIndentation = 0): string sprintf( '(%s%s%s)', PHP_EOL, - implode(',' . PHP_EOL, $formattedParams), + implode(',' . PHP_EOL, array_filter($formattedParams)), PHP_EOL . str_repeat(' ', $currentIndentation), ); } elseif (count($this->parameters) > 0) { foreach ($this->parameters as $name => $parameter) { - if (is_string($name) && !empty($name)) { - $formattedParams[] = sprintf('%s: %s', $name, $parameter); - } else { - $formattedParams[] = $parameter; + if (empty($parameter)) { + continue; } + $formattedParams[] = $this->formatProperties($name, $parameter, -4); } return str_repeat(' ', $currentIndentation) . $this->className . sprintf( '(%s)', - implode(', ', $formattedParams) + implode(', ', array_filter($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 bfc258b..fc4a5e4 100644 --- a/src/EntityGenerator.php +++ b/src/EntityGenerator.php @@ -247,10 +247,15 @@ protected function getClassAttributes(): string */ protected function getClassAnnotations(): string { + $annotations = [ + $this->nodeType->getName() . ' node-source entity.', + $this->nodeType->getDescription() + ]; + $annotations = array_filter($annotations); + return ' /** - * DO NOT EDIT - * Generated custom node-source type by Roadiz. + * ' . implode(PHP_EOL . ' * ', $annotations) . ' */' . PHP_EOL; } diff --git a/src/EntityGeneratorFactory.php b/src/EntityGeneratorFactory.php index f9846c1..201b906 100644 --- a/src/EntityGeneratorFactory.php +++ b/src/EntityGeneratorFactory.php @@ -10,18 +10,11 @@ final class EntityGeneratorFactory { - private NodeTypeResolverInterface $nodeTypeResolverBag; - private DefaultValuesResolverInterface $defaultValuesResolver; - private array $options; - public function __construct( - NodeTypeResolverInterface $nodeTypeResolverBag, - DefaultValuesResolverInterface $defaultValuesResolver, - array $options + private readonly NodeTypeResolverInterface $nodeTypeResolverBag, + private readonly DefaultValuesResolverInterface $defaultValuesResolver, + private readonly array $options ) { - $this->nodeTypeResolverBag = $nodeTypeResolverBag; - $this->defaultValuesResolver = $defaultValuesResolver; - $this->options = $options; } public function create(NodeTypeInterface $nodeType): EntityGeneratorInterface diff --git a/src/Field/AbstractFieldGenerator.php b/src/Field/AbstractFieldGenerator.php index e0a689e..e0497db 100644 --- a/src/Field/AbstractFieldGenerator.php +++ b/src/Field/AbstractFieldGenerator.php @@ -150,6 +150,27 @@ 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 f0fb9df..6adc91d 100644 --- a/src/Field/CustomFormsFieldGenerator.php +++ b/src/Field/CustomFormsFieldGenerator.php @@ -50,16 +50,12 @@ public function getFieldGetter(): string public function ' . $this->field->getGetterName() . '(): array { if (null === $this->' . $this->field->getVarName() . ') { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->' . $this->field->getVarName() . ' = $this->objectManager ->getRepository(' . $this->options['custom_form_class'] . '::class) - ->findByNodeAndField( + ->findByNodeAndFieldName( $this->getNode(), - $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '") + \'' . $this->field->getName() . '\' ); } else { $this->' . $this->field->getVarName() . ' = []; @@ -84,22 +80,15 @@ protected function getFieldSetter(): string */ public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['custom_form_class'] . ' $customForm): static { - 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; - } + 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; } return $this; }' . PHP_EOL; diff --git a/src/Field/DocumentsFieldGenerator.php b/src/Field/DocumentsFieldGenerator.php index 6e85f13..be8732e 100644 --- a/src/Field/DocumentsFieldGenerator.php +++ b/src/Field/DocumentsFieldGenerator.php @@ -58,16 +58,12 @@ public function getFieldGetter(): string public function ' . $this->field->getGetterName() . '(): array { if (null === $this->' . $this->field->getVarName() . ') { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->' . $this->field->getVarName() . ' = $this->objectManager ->getRepository(' . $this->options['document_class'] . '::class) - ->findByNodeSourceAndField( + ->findByNodeSourceAndFieldName( $this, - $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '") + \'' . $this->field->getName() . '\' ); } else { $this->' . $this->field->getVarName() . ' = []; @@ -92,23 +88,16 @@ protected function getFieldSetter(): string */ public function add' . ucfirst($this->field->getVarName()) . '(' . $this->options['document_class'] . ' $document): static { - 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; - } + 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; } } return $this; diff --git a/src/Field/NodesFieldGenerator.php b/src/Field/NodesFieldGenerator.php index 54ed301..9f11d15 100644 --- a/src/Field/NodesFieldGenerator.php +++ b/src/Field/NodesFieldGenerator.php @@ -130,16 +130,12 @@ public function getFieldGetter(): string public function ' . $this->field->getGetterName() . 'Sources(): array { if (null === $this->' . $this->getFieldSourcesName() . ') { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->' . $this->getFieldSourcesName() . ' = $this->objectManager ->getRepository(' . $this->getRepositoryClass() . '::class) - ->findByNodesSourcesAndFieldAndTranslation( + ->findByNodesSourcesAndFieldNameAndTranslation( $this, - $this->getNode()->getNodeType()->getFieldByName("' . $this->field->getName() . '") + \'' . $this->field->getName() . '\' ); } else { $this->' . $this->getFieldSourcesName() . ' = []; diff --git a/src/RepositoryGenerator.php b/src/RepositoryGenerator.php index 8c27975..ffa1a54 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\Component\Security\Core\Security; +use Symfony\Bundle\SecurityBundle\Security; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; /** diff --git a/tests/EntityGeneratorFactoryTest.php b/tests/EntityGeneratorFactoryTest.php new file mode 100644 index 0000000..82185bc --- /dev/null +++ b/tests/EntityGeneratorFactoryTest.php @@ -0,0 +1,104 @@ +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' => '\RZ\Roadiz\EntityGenerator\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' => '\RZ\Roadiz\EntityGenerator\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' => '\RZ\Roadiz\EntityGenerator\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/Repository/NSMockRepository.php'), + $generator->createCustomRepository($this->getMockNodeType())->getClassContent() + ); + } +} diff --git a/tests/EntityGeneratorTest.php b/tests/EntityGeneratorTest.php new file mode 100644 index 0000000..df91155 --- /dev/null +++ b/tests/EntityGeneratorTest.php @@ -0,0 +1,76 @@ +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' => '\RZ\Roadiz\EntityGenerator\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' => 'RZ\Roadiz\EntityGenerator\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/JoinedTableDefaultValuesResolver.php b/tests/JoinedTableDefaultValuesResolver.php similarity index 94% rename from tests/mocks/JoinedTableDefaultValuesResolver.php rename to tests/JoinedTableDefaultValuesResolver.php index c70f733..a8183e8 100644 --- a/tests/mocks/JoinedTableDefaultValuesResolver.php +++ b/tests/JoinedTableDefaultValuesResolver.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace tests\mocks; +namespace RZ\Roadiz\EntityGenerator\Tests; use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface; use RZ\Roadiz\EntityGenerator\Field\DefaultValuesResolverInterface; diff --git a/tests/mocks/GeneratedNodesSources/NSMock.php b/tests/Mocks/GeneratedNodesSources/NSMock.php similarity index 90% rename from tests/mocks/GeneratedNodesSources/NSMock.php rename to tests/Mocks/GeneratedNodesSources/NSMock.php index 0b16aed..5b5e167 100644 --- a/tests/mocks/GeneratedNodesSources/NSMock.php +++ b/tests/Mocks/GeneratedNodesSources/NSMock.php @@ -6,7 +6,7 @@ * THIS IS A GENERATED FILE, DO NOT EDIT IT * IT WILL BE RECREATED AT EACH NODE-TYPE UPDATE */ -namespace tests\mocks\GeneratedNodesSources; +namespace RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSources; use Doctrine\Common\Collections\Collection; use JMS\Serializer\Annotation as Serializer; @@ -18,8 +18,7 @@ use ApiPlatform\Serializer\Filter\PropertyFilter; /** - * DO NOT EDIT - * Generated custom node-source type by Roadiz. + * Mock node-source entity. */ #[ Gedmo\Loggable(logEntryClass: \RZ\Roadiz\CoreBundle\Entity\UserLogEntry::class), @@ -40,6 +39,7 @@ 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,6 +79,7 @@ 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( @@ -123,6 +124,7 @@ 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), @@ -169,6 +171,7 @@ 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), @@ -229,6 +232,7 @@ 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), @@ -319,6 +323,7 @@ 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), @@ -383,6 +388,7 @@ 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"), @@ -436,6 +442,7 @@ 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"), @@ -623,6 +630,7 @@ 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; @@ -640,16 +648,12 @@ public function setEventReferencesExcluded(Collection|array $eventReferencesExcl public function getBar(): array { if (null === $this->bar) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->bar = $this->objectManager ->getRepository(\mock\Entity\Document::class) - ->findByNodeSourceAndField( + ->findByNodeSourceAndFieldName( $this, - $this->getNode()->getNodeType()->getFieldByName("bar") + 'bar' ); } else { $this->bar = []; @@ -665,23 +669,16 @@ public function getBar(): array */ public function addBar(\mock\Entity\Document $document): static { - 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; - } + 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; } } return $this; @@ -697,6 +694,7 @@ 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; @@ -713,16 +711,12 @@ public function addBar(\mock\Entity\Document $document): static public function getTheForms(): array { if (null === $this->theForms) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->theForms = $this->objectManager ->getRepository(\mock\Entity\CustomForm::class) - ->findByNodeAndField( + ->findByNodeAndFieldName( $this->getNode(), - $this->getNode()->getNodeType()->getFieldByName("the_forms") + 'the_forms' ); } else { $this->theForms = []; @@ -738,22 +732,15 @@ public function getTheForms(): array */ public function addTheForms(\mock\Entity\CustomForm $customForm): static { - 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; - } + 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; } return $this; } @@ -771,6 +758,7 @@ 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; @@ -788,16 +776,12 @@ public function addTheForms(\mock\Entity\CustomForm $customForm): static public function getFooBarSources(): array { if (null === $this->fooBarSources) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->fooBarSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldAndTranslation( + ->findByNodesSourcesAndFieldNameAndTranslation( $this, - $this->getNode()->getNodeType()->getFieldByName("foo_bar") + 'foo_bar' ); } else { $this->fooBarSources = []; @@ -843,16 +827,12 @@ public function setFooBarSources(?array $fooBarSources): static public function getFooBarHiddenSources(): array { if (null === $this->fooBarHiddenSources) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->fooBarHiddenSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldAndTranslation( + ->findByNodesSourcesAndFieldNameAndTranslation( $this, - $this->getNode()->getNodeType()->getFieldByName("foo_bar_hidden") + 'foo_bar_hidden' ); } else { $this->fooBarHiddenSources = []; @@ -886,6 +866,7 @@ 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; @@ -903,16 +884,12 @@ public function setFooBarHiddenSources(?array $fooBarHiddenSources): static public function getFooBarTypedSources(): array { if (null === $this->fooBarTypedSources) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->fooBarTypedSources = $this->objectManager ->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class) - ->findByNodesSourcesAndFieldAndTranslation( + ->findByNodesSourcesAndFieldNameAndTranslation( $this, - $this->getNode()->getNodeType()->getFieldByName("foo_bar_typed") + 'foo_bar_typed' ); } else { $this->fooBarTypedSources = []; @@ -941,6 +918,7 @@ 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), @@ -989,6 +967,7 @@ 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"), @@ -1030,6 +1009,7 @@ 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 similarity index 90% rename from tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php rename to tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php index 1f25db5..e91fb63 100644 --- a/tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php +++ b/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php @@ -6,7 +6,7 @@ * THIS IS A GENERATED FILE, DO NOT EDIT IT * IT WILL BE RECREATED AT EACH NODE-TYPE UPDATE */ -namespace tests\mocks\GeneratedNodesSourcesWithRepository; +namespace RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository; use Doctrine\Common\Collections\Collection; use JMS\Serializer\Annotation as Serializer; @@ -18,12 +18,11 @@ use ApiPlatform\Serializer\Filter\PropertyFilter; /** - * DO NOT EDIT - * Generated custom node-source type by Roadiz. + * Mock node-source entity. */ #[ Gedmo\Loggable(logEntryClass: \RZ\Roadiz\CoreBundle\Entity\UserLogEntry::class), - ORM\Entity(repositoryClass: \tests\mocks\GeneratedNodesSourcesWithRepository\Repository\NSMockRepository::class), + ORM\Entity(repositoryClass: \RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository\Repository\NSMockRepository::class), ORM\Table(name: "ns_mock"), ORM\Index(columns: ["foo_datetime"]), ORM\Index(columns: ["fooIndexed"]), @@ -40,6 +39,7 @@ 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,6 +79,7 @@ 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( @@ -123,6 +124,7 @@ 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), @@ -169,6 +171,7 @@ 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), @@ -229,6 +232,7 @@ 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), @@ -319,6 +323,7 @@ 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), @@ -383,6 +388,7 @@ 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"), @@ -436,6 +442,7 @@ 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"), @@ -623,6 +630,7 @@ 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; @@ -640,16 +648,12 @@ public function setEventReferencesExcluded(Collection|array $eventReferencesExcl public function getBar(): array { if (null === $this->bar) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->bar = $this->objectManager ->getRepository(\mock\Entity\Document::class) - ->findByNodeSourceAndField( + ->findByNodeSourceAndFieldName( $this, - $this->getNode()->getNodeType()->getFieldByName("bar") + 'bar' ); } else { $this->bar = []; @@ -665,23 +669,16 @@ public function getBar(): array */ public function addBar(\mock\Entity\Document $document): static { - 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; - } + 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; } } return $this; @@ -697,6 +694,7 @@ 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; @@ -713,16 +711,12 @@ public function addBar(\mock\Entity\Document $document): static public function getTheForms(): array { if (null === $this->theForms) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->theForms = $this->objectManager ->getRepository(\mock\Entity\CustomForm::class) - ->findByNodeAndField( + ->findByNodeAndFieldName( $this->getNode(), - $this->getNode()->getNodeType()->getFieldByName("the_forms") + 'the_forms' ); } else { $this->theForms = []; @@ -738,22 +732,15 @@ public function getTheForms(): array */ public function addTheForms(\mock\Entity\CustomForm $customForm): static { - 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; - } + 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; } return $this; } @@ -771,6 +758,7 @@ 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; @@ -788,16 +776,12 @@ public function addTheForms(\mock\Entity\CustomForm $customForm): static public function getFooBarSources(): array { if (null === $this->fooBarSources) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->fooBarSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldAndTranslation( + ->findByNodesSourcesAndFieldNameAndTranslation( $this, - $this->getNode()->getNodeType()->getFieldByName("foo_bar") + 'foo_bar' ); } else { $this->fooBarSources = []; @@ -843,16 +827,12 @@ public function setFooBarSources(?array $fooBarSources): static public function getFooBarHiddenSources(): array { if (null === $this->fooBarHiddenSources) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->fooBarHiddenSources = $this->objectManager ->getRepository(\mock\Entity\NodesSources::class) - ->findByNodesSourcesAndFieldAndTranslation( + ->findByNodesSourcesAndFieldNameAndTranslation( $this, - $this->getNode()->getNodeType()->getFieldByName("foo_bar_hidden") + 'foo_bar_hidden' ); } else { $this->fooBarHiddenSources = []; @@ -886,6 +866,7 @@ 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; @@ -903,16 +884,12 @@ public function setFooBarHiddenSources(?array $fooBarHiddenSources): static public function getFooBarTypedSources(): array { if (null === $this->fooBarTypedSources) { - if ( - null !== $this->objectManager && - null !== $this->getNode() && - null !== $this->getNode()->getNodeType() - ) { + if (null !== $this->objectManager) { $this->fooBarTypedSources = $this->objectManager ->getRepository(\tests\mocks\GeneratedNodesSources\NSMockTwo::class) - ->findByNodesSourcesAndFieldAndTranslation( + ->findByNodesSourcesAndFieldNameAndTranslation( $this, - $this->getNode()->getNodeType()->getFieldByName("foo_bar_typed") + 'foo_bar_typed' ); } else { $this->fooBarTypedSources = []; @@ -941,6 +918,7 @@ 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), @@ -989,6 +967,7 @@ 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"), @@ -1030,6 +1009,7 @@ 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/Repository/NSMockRepository.php b/tests/Mocks/GeneratedNodesSourcesWithRepository/Repository/NSMockRepository.php new file mode 100644 index 0000000..0f8cab8 --- /dev/null +++ b/tests/Mocks/GeneratedNodesSourcesWithRepository/Repository/NSMockRepository.php @@ -0,0 +1,38 @@ + + * + * @method \RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository\NSMock|null find($id, $lockMode = null, $lockVersion = null) + * @method \RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository\NSMock|null findOneBy(array $criteria, array $orderBy = null) + * @method \RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository\NSMock[] findAll() + * @method \RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository\NSMock[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + */ +class NSMockRepository extends \RZ\Roadiz\CoreBundle\Repository\NodesSourcesRepository +{ + public function __construct( + ManagerRegistry $registry, + PreviewResolverInterface $previewResolver, + EventDispatcherInterface $dispatcher, + Security $security, + ?NodeSourceSearchHandlerInterface $nodeSourceSearchHandler + ) { + parent::__construct($registry, $previewResolver, $dispatcher, $security, $nodeSourceSearchHandler); + + $this->_entityName = \RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository\NSMock::class; + } +} diff --git a/tests/mocks/NodeTypeAwareTrait.php b/tests/NodeTypeAwareTestTrait.php similarity index 81% rename from tests/mocks/NodeTypeAwareTrait.php rename to tests/NodeTypeAwareTestTrait.php index eea532b..6a64ef8 100644 --- a/tests/mocks/NodeTypeAwareTrait.php +++ b/tests/NodeTypeAwareTestTrait.php @@ -1,20 +1,23 @@ newMockInstance(NodeTypeInterface::class); - $mockNodeType->getMockController()->getFields = function() { - return new ArrayCollection([ - (new NodeTypeField()) + $mockNodeType = $this->createStub(NodeTypeInterface::class); + $mockNodeType + ->method('getFields') + ->willReturn( + new ArrayCollection([ + (new SimpleNodeTypeField()) ->setName('foo_datetime') ->setTypeName('datetime') ->setDoctrineType('datetime') @@ -26,7 +29,7 @@ protected function getMockNodeType() ->setVirtual(false) ->setLabel('Foo DateTime field') ->setIndexed(true), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo') ->setTypeName('string') ->setVirtual(false) @@ -34,7 +37,7 @@ protected function getMockNodeType() ->setLabel('Foo field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(false), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('fooIndexed') ->setTypeName('string') ->setVirtual(false) @@ -42,7 +45,7 @@ protected function getMockNodeType() ->setLabel('Foo indexed field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(true), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('boolIndexed') ->setTypeName('bool') ->setDoctrineType('boolean') @@ -51,7 +54,7 @@ protected function getMockNodeType() ->setLabel('Bool indexed field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(true), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_markdown') ->setTypeName('markdown') ->setDoctrineType('text') @@ -61,7 +64,7 @@ protected function getMockNodeType() ->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 NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_markdown_excluded') ->setTypeName('markdown') ->setDoctrineType('text') @@ -71,7 +74,7 @@ protected function getMockNodeType() ->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 NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_decimal_excluded') ->setTypeName('decimal') ->setDoctrineType('decimal') @@ -80,7 +83,7 @@ protected function getMockNodeType() ->setLabel('Foo expression excluded decimal') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(true), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('single_event_reference') ->setTypeName('many_to_one') ->setVirtual(false) @@ -88,7 +91,7 @@ protected function getMockNodeType() ->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 NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('event_references') ->setTypeName('many_to_many') ->setVirtual(false) @@ -96,7 +99,7 @@ protected function getMockNodeType() ->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 NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('event_references_proxied') ->setTypeName('many_to_many') ->setVirtual(false) @@ -104,7 +107,7 @@ protected function getMockNodeType() ->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 NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('event_references_excluded') ->setTypeName('many_to_many') ->setVirtual(false) @@ -113,7 +116,7 @@ protected function getMockNodeType() ->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 NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('bar') ->setTypeName('documents') ->setSerializationMaxDepth(1) @@ -121,20 +124,20 @@ protected function getMockNodeType() ->setLabel('Bar documents field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(false), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('the_forms') ->setTypeName('custom_forms') ->setVirtual(true) ->setLabel('Custom forms field') ->setIndexed(false), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->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 NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_bar_hidden') ->setTypeName('nodes') ->setVirtual(true) @@ -142,20 +145,20 @@ protected function getMockNodeType() ->setLabel('ForBar hidden nodes field') ->setDescription('Maecenas sed diam eget risus varius blandit sit amet non magna') ->setIndexed(false), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_bar_typed') ->setTypeName('nodes') ->setVirtual(true) ->setLabel('ForBar nodes typed field') ->setIndexed(false) ->setDefaultValues('MockTwo'), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('layout') ->setTypeName('enum') ->setLabel('ForBar layout enum') ->setIndexed(true) ->setDefaultValues('light, dark, transparent'), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_many_to_one') ->setTypeName('many_to_one') ->setVirtual(false) @@ -165,7 +168,7 @@ classname: \MyCustomEntity displayable: getName EOT) ->setIndexed(false), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_many_to_many') ->setTypeName('many_to_many') ->setVirtual(false) @@ -178,7 +181,7 @@ classname: \MyCustomEntity direction: asc EOT) ->setIndexed(false), - (new NodeTypeField()) + (new SimpleNodeTypeField()) ->setName('foo_many_to_many_proxied') ->setTypeName('many_to_many') ->setVirtual(false) @@ -201,42 +204,45 @@ classname: Themes\MyTheme\Entities\PositionedCity direction: ASC EOT) ->setIndexed(false), - ]); - }; - $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; - }; + ]) + ); + + $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); return $mockNodeType; } - protected function getMockNodeTypeResolver() + protected function getMockNodeTypeResolver(): NodeTypeResolverInterface { - $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; - }; + $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; + } + ); return $mockNodeTypeResolver; } - protected function getMockDefaultValuesResolver() + protected function getMockDefaultValuesResolver(): JoinedTableDefaultValuesResolver { - return $this->newMockInstance(JoinedTableDefaultValuesResolver::class); + return new JoinedTableDefaultValuesResolver(); } } diff --git a/tests/mocks/NodeTypeField.php b/tests/SimpleNodeTypeField.php similarity index 82% rename from tests/mocks/NodeTypeField.php rename to tests/SimpleNodeTypeField.php index 22e9dca..befe9ca 100644 --- a/tests/mocks/NodeTypeField.php +++ b/tests/SimpleNodeTypeField.php @@ -1,13 +1,14 @@ description = $description; return $this; @@ -59,9 +60,9 @@ public function getLabel(): string /** * @param string|null $label - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setLabel(?string $label): NodeTypeField + public function setLabel(?string $label): SimpleNodeTypeField { $this->label = $label; return $this; @@ -77,9 +78,9 @@ public function getName(): string /** * @param string|null $name - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setName(?string $name): NodeTypeField + public function setName(?string $name): SimpleNodeTypeField { $this->name = $name; return $this; @@ -95,9 +96,9 @@ public function getPlaceholder(): ?string /** * @param string|null $placeholder - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setPlaceholder(?string $placeholder): NodeTypeField + public function setPlaceholder(?string $placeholder): SimpleNodeTypeField { $this->placeholder = $placeholder; return $this; @@ -113,9 +114,9 @@ public function getDefaultValues(): ?string /** * @param string|null $defaultValues - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setDefaultValues(?string $defaultValues): NodeTypeField + public function setDefaultValues(?string $defaultValues): SimpleNodeTypeField { $this->defaultValues = $defaultValues; return $this; @@ -131,9 +132,9 @@ public function getGroupName(): ?string /** * @param string|null $groupName - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setGroupName(?string $groupName): NodeTypeField + public function setGroupName(?string $groupName): SimpleNodeTypeField { $this->groupName = $groupName; return $this; @@ -149,9 +150,9 @@ public function getMinLength(): ?int /** * @param int|null $minLength - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setMinLength(?int $minLength): NodeTypeField + public function setMinLength(?int $minLength): SimpleNodeTypeField { $this->minLength = $minLength; return $this; @@ -167,9 +168,9 @@ public function getMaxLength(): ?int /** * @param int|null $maxLength - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setMaxLength(?int $maxLength): NodeTypeField + public function setMaxLength(?int $maxLength): SimpleNodeTypeField { $this->maxLength = $maxLength; return $this; @@ -185,9 +186,9 @@ public function isVisible(): bool /** * @param bool $visible - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setVisible(bool $visible): NodeTypeField + public function setVisible(bool $visible): SimpleNodeTypeField { $this->visible = $visible; return $this; @@ -203,9 +204,9 @@ public function isUniversal(): bool /** * @param bool $universal - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setUniversal(bool $universal): NodeTypeField + public function setUniversal(bool $universal): SimpleNodeTypeField { $this->universal = $universal; return $this; @@ -221,9 +222,9 @@ public function isSearchable(): bool /** * @param bool $searchable - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setSearchable(bool $searchable): NodeTypeField + public function setSearchable(bool $searchable): SimpleNodeTypeField { $this->searchable = $searchable; return $this; @@ -239,9 +240,9 @@ public function isVirtual(): bool /** * @param bool $virtual - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setVirtual(bool $virtual): NodeTypeField + public function setVirtual(bool $virtual): SimpleNodeTypeField { $this->virtual = $virtual; return $this; @@ -257,9 +258,9 @@ public function isIndexed(): bool /** * @param bool $indexed - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setIndexed(bool $indexed): NodeTypeField + public function setIndexed(bool $indexed): SimpleNodeTypeField { $this->indexed = $indexed; return $this; @@ -275,9 +276,9 @@ public function isExpanded(): bool /** * @param bool $expanded - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setExpanded(bool $expanded): NodeTypeField + public function setExpanded(bool $expanded): SimpleNodeTypeField { $this->expanded = $expanded; return $this; @@ -293,9 +294,9 @@ public function getTypeName(): string /** * @param string $typeName - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setTypeName(string $typeName): NodeTypeField + public function setTypeName(string $typeName): SimpleNodeTypeField { $this->typeName = $typeName; return $this; @@ -311,9 +312,9 @@ public function getNodeTypeName(): string /** * @param string $nodeTypeName - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setNodeTypeName(string $nodeTypeName): NodeTypeField + public function setNodeTypeName(string $nodeTypeName): SimpleNodeTypeField { $this->nodeTypeName = $nodeTypeName; return $this; @@ -329,9 +330,9 @@ public function getDoctrineType(): string /** * @param string $doctrineType - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setDoctrineType(string $doctrineType): NodeTypeField + public function setDoctrineType(string $doctrineType): SimpleNodeTypeField { $this->doctrineType = $doctrineType; return $this; @@ -522,9 +523,9 @@ public function getSerializationGroups(): array /** * @param array $serializationGroups - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setSerializationGroups(array $serializationGroups): NodeTypeField + public function setSerializationGroups(array $serializationGroups): SimpleNodeTypeField { $this->serializationGroups = $serializationGroups; return $this; @@ -540,9 +541,9 @@ public function isExcludedFromSerialization(): bool /** * @param bool $excludedFromSerialization - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setExcludedFromSerialization(bool $excludedFromSerialization): NodeTypeField + public function setExcludedFromSerialization(bool $excludedFromSerialization): SimpleNodeTypeField { $this->excludedFromSerialization = $excludedFromSerialization; return $this; @@ -558,9 +559,9 @@ public function getSerializationExclusionExpression(): ?string /** * @param string|null $serializationExclusionExpression - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setSerializationExclusionExpression(?string $serializationExclusionExpression): NodeTypeField + public function setSerializationExclusionExpression(?string $serializationExclusionExpression): SimpleNodeTypeField { $this->serializationExclusionExpression = $serializationExclusionExpression; return $this; @@ -576,9 +577,9 @@ public function getSerializationMaxDepth(): ?int /** * @param int|null $serializationMaxDepth - * @return NodeTypeField + * @return SimpleNodeTypeField */ - public function setSerializationMaxDepth(?int $serializationMaxDepth): NodeTypeField + public function setSerializationMaxDepth(?int $serializationMaxDepth): SimpleNodeTypeField { $this->serializationMaxDepth = $serializationMaxDepth; return $this; diff --git a/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php b/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php deleted file mode 100644 index 703a978..0000000 --- a/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * @method \tests\mocks\GeneratedNodesSourcesWithRepository\NSMock|null find($id, $lockMode = null, $lockVersion = null) - * @method \tests\mocks\GeneratedNodesSourcesWithRepository\NSMock|null findOneBy(array $criteria, array $orderBy = null) - * @method \tests\mocks\GeneratedNodesSourcesWithRepository\NSMock[] findAll() - * @method \tests\mocks\GeneratedNodesSourcesWithRepository\NSMock[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) - */ -class NSMockRepository extends \RZ\Roadiz\CoreBundle\Repository\NodesSourcesRepository -{ - public function __construct( - ManagerRegistry $registry, - PreviewResolverInterface $previewResolver, - EventDispatcherInterface $dispatcher, - Security $security, - ?NodeSourceSearchHandlerInterface $nodeSourceSearchHandler - ) { - parent::__construct($registry, $previewResolver, $dispatcher, $security, $nodeSourceSearchHandler); - - $this->_entityName = \tests\mocks\GeneratedNodesSourcesWithRepository\NSMock::class; - } -} diff --git a/tests/units/EntityGenerator.php b/tests/units/EntityGenerator.php deleted file mode 100644 index 3f225b5..0000000 --- a/tests/units/EntityGenerator.php +++ /dev/null @@ -1,83 +0,0 @@ -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 deleted file mode 100644 index ba75bd8..0000000 --- a/tests/units/EntityGeneratorFactory.php +++ /dev/null @@ -1,135 +0,0 @@ -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')) - ; - } -}