diff --git a/.github/workflows/run-test.yml b/.github/workflows/run-test.yml index 96cc206..1a24c5b 100644 --- a/.github/workflows/run-test.yml +++ b/.github/workflows/run-test.yml @@ -19,7 +19,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php-version: ['8.1', '8.2', '8.3'] + php-version: ['8.0', '8.1'] steps: - uses: shivammathur/setup-php@v2 with: @@ -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 tests + - name: Run atoum unit tests + run: vendor/bin/atoum -f tests/units/* diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..87d3a8b --- /dev/null +++ b/.travis.yml @@ -0,0 +1,18 @@ +language: php +sudo: required +php: + - 8.0 + - 8.1 + - nightly +env: + - XDEBUG_MODE=coverage +install: + - curl -s http://getcomposer.org/installer | php + - php composer.phar install --dev --no-interaction +script: + - vendor/bin/phpcs --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/* +jobs: + allow_failures: + - php: nightly diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9e67c84 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Roadiz + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/LICENSE.md b/LICENSE.md deleted file mode 100644 index 8e18fa6..0000000 --- a/LICENSE.md +++ /dev/null @@ -1,9 +0,0 @@ -The MIT License (MIT) - -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: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c45718c --- /dev/null +++ b/Makefile @@ -0,0 +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/atoum -f tests/units/* diff --git a/composer.json b/composer.json index afb14f9..bd4e42d 100644 --- a/composer.json +++ b/composer.json @@ -12,37 +12,33 @@ } ], "require": { - "php": ">=8.1", + "php": ">=8.0", "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" }, "autoload": { "psr-4": { - "RZ\\Roadiz\\EntityGenerator\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "RZ\\Roadiz\\EntityGenerator\\Tests\\": "tests/" + "RZ\\Roadiz\\EntityGenerator\\": "src/", + "tests\\mocks\\": "tests/mocks/" } }, "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.1.x-dev", + "dev-develop": "2.2.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..808de9b 100644 --- a/src/EntityGenerator.php +++ b/src/EntityGenerator.php @@ -191,9 +191,9 @@ protected function getClassHeader(): string ]; if ($this->options['use_api_platform_filters'] === true) { - $useStatements[] = 'use ApiPlatform\Metadata\ApiFilter;'; - $useStatements[] = 'use ApiPlatform\Doctrine\Orm\Filter as OrmFilter;'; - $useStatements[] = 'use ApiPlatform\Serializer\Filter\PropertyFilter;'; + $useStatements[] = 'use ApiPlatform\Core\Annotation\ApiFilter;'; + $useStatements[] = 'use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter as OrmFilter;'; + $useStatements[] = 'use ApiPlatform\Core\Serializer\Filter\PropertyFilter;'; } /* * BE CAREFUL, USE statements are required for field generators which @@ -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/EntityGeneratorFactory.php b/src/EntityGeneratorFactory.php index 201b906..f9846c1 100644 --- a/src/EntityGeneratorFactory.php +++ b/src/EntityGeneratorFactory.php @@ -10,11 +10,18 @@ final class EntityGeneratorFactory { + private NodeTypeResolverInterface $nodeTypeResolverBag; + private DefaultValuesResolverInterface $defaultValuesResolver; + private array $options; + public function __construct( - private readonly NodeTypeResolverInterface $nodeTypeResolverBag, - private readonly DefaultValuesResolverInterface $defaultValuesResolver, - private readonly array $options + NodeTypeResolverInterface $nodeTypeResolverBag, + DefaultValuesResolverInterface $defaultValuesResolver, + 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 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/Field/NonVirtualFieldGenerator.php b/src/Field/NonVirtualFieldGenerator.php index 9a9df5e..4c51820 100644 --- a/src/Field/NonVirtualFieldGenerator.php +++ b/src/Field/NonVirtualFieldGenerator.php @@ -52,6 +52,18 @@ protected function getFieldLength(): ?int }; } + protected function getMaxDefaultValuesLength(): int + { + // get max length of exploded default values + $max = 0; + foreach (explode(',', $this->field->getDefaultValues()) as $value) { + $value = trim($value); + $max = max($max, \mb_strlen($value)); + } + + return $max > 0 ? $max : 250; + } + protected function isExcludingFieldFromJmsSerialization(): bool { return false; 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/tests/EntityGeneratorFactoryTest.php b/tests/EntityGeneratorFactoryTest.php deleted file mode 100644 index 82185bc..0000000 --- a/tests/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' => '\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 deleted file mode 100644 index df91155..0000000 --- a/tests/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' => '\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/GeneratedNodesSourcesWithRepository/Repository/NSMockRepository.php b/tests/Mocks/GeneratedNodesSourcesWithRepository/Repository/NSMockRepository.php deleted file mode 100644 index 0f8cab8..0000000 --- a/tests/Mocks/GeneratedNodesSourcesWithRepository/Repository/NSMockRepository.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * @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/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 5b5e167..7349831 100644 --- a/tests/Mocks/GeneratedNodesSources/NSMock.php +++ b/tests/mocks/GeneratedNodesSources/NSMock.php @@ -6,19 +6,20 @@ * THIS IS A GENERATED FILE, DO NOT EDIT IT * IT WILL BE RECREATED AT EACH NODE-TYPE UPDATE */ -namespace RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSources; +namespace tests\mocks\GeneratedNodesSources; use Doctrine\Common\Collections\Collection; use JMS\Serializer\Annotation as Serializer; use Symfony\Component\Serializer\Annotation as SymfonySerializer; use Gedmo\Mapping\Annotation as Gedmo; use Doctrine\ORM\Mapping as ORM; -use ApiPlatform\Metadata\ApiFilter; -use ApiPlatform\Doctrine\Orm\Filter as OrmFilter; -use ApiPlatform\Serializer\Filter\PropertyFilter; +use ApiPlatform\Core\Annotation\ApiFilter; +use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter as OrmFilter; +use ApiPlatform\Core\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 similarity index 90% rename from tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php rename to tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php index e91fb63..f9013e8 100644 --- a/tests/Mocks/GeneratedNodesSourcesWithRepository/NSMock.php +++ b/tests/mocks/GeneratedNodesSourcesWithRepository/NSMock.php @@ -6,23 +6,24 @@ * THIS IS A GENERATED FILE, DO NOT EDIT IT * IT WILL BE RECREATED AT EACH NODE-TYPE UPDATE */ -namespace RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository; +namespace tests\mocks\GeneratedNodesSourcesWithRepository; use Doctrine\Common\Collections\Collection; use JMS\Serializer\Annotation as Serializer; use Symfony\Component\Serializer\Annotation as SymfonySerializer; use Gedmo\Mapping\Annotation as Gedmo; use Doctrine\ORM\Mapping as ORM; -use ApiPlatform\Metadata\ApiFilter; -use ApiPlatform\Doctrine\Orm\Filter as OrmFilter; -use ApiPlatform\Serializer\Filter\PropertyFilter; +use ApiPlatform\Core\Annotation\ApiFilter; +use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter as OrmFilter; +use ApiPlatform\Core\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), - ORM\Entity(repositoryClass: \RZ\Roadiz\EntityGenerator\Tests\Mocks\GeneratedNodesSourcesWithRepository\Repository\NSMockRepository::class), + ORM\Entity(repositoryClass: \tests\mocks\GeneratedNodesSourcesWithRepository\Repository\NSMockRepository::class), ORM\Table(name: "ns_mock"), ORM\Index(columns: ["foo_datetime"]), ORM\Index(columns: ["fooIndexed"]), @@ -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 new file mode 100644 index 0000000..703a978 --- /dev/null +++ b/tests/mocks/GeneratedNodesSourcesWithRepository/NSMockRepository.php @@ -0,0 +1,38 @@ + + * + * @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/JoinedTableDefaultValuesResolver.php b/tests/mocks/JoinedTableDefaultValuesResolver.php similarity index 94% rename from tests/JoinedTableDefaultValuesResolver.php rename to tests/mocks/JoinedTableDefaultValuesResolver.php index a8183e8..c70f733 100644 --- a/tests/JoinedTableDefaultValuesResolver.php +++ b/tests/mocks/JoinedTableDefaultValuesResolver.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace RZ\Roadiz\EntityGenerator\Tests; +namespace tests\mocks; use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface; use RZ\Roadiz\EntityGenerator\Field\DefaultValuesResolverInterface; diff --git a/tests/NodeTypeAwareTestTrait.php b/tests/mocks/NodeTypeAwareTrait.php similarity index 81% rename from tests/NodeTypeAwareTestTrait.php rename to tests/mocks/NodeTypeAwareTrait.php index 6a64ef8..eea532b 100644 --- a/tests/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/tests/SimpleNodeTypeField.php b/tests/mocks/NodeTypeField.php similarity index 82% rename from tests/SimpleNodeTypeField.php rename to tests/mocks/NodeTypeField.php index befe9ca..22e9dca 100644 --- a/tests/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')) + ; + } +}