diff --git a/composer.json b/composer.json index 5a38e0e21..4673db055 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "require": { "php": ">=7.1.3", "doctrine/inflector": "^1.2|^2.0", - "nikic/php-parser": "^4.0", + "nikic/php-parser": "^4.11", "symfony/config": "^4.0|^5.0", "symfony/console": "^4.0|^5.0", "symfony/dependency-injection": "^4.0|^5.0", diff --git a/src/DependencyInjection/CompilerPass/DoctrineAttributesCheckPass.php b/src/DependencyInjection/CompilerPass/DoctrineAttributesCheckPass.php new file mode 100644 index 000000000..37980b64f --- /dev/null +++ b/src/DependencyInjection/CompilerPass/DoctrineAttributesCheckPass.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; + +class DoctrineAttributesCheckPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + $container->setParameter( + 'maker.compatible_check.doctrine.supports_attributes', + $container->hasParameter('doctrine.orm.metadata.attribute.class') + ); + } +} diff --git a/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php b/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php index 7b9541fe3..5829f4a53 100644 --- a/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php +++ b/src/DependencyInjection/CompilerPass/SetDoctrineAnnotatedPrefixesPass.php @@ -11,8 +11,6 @@ namespace Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass; -use Doctrine\ORM\Mapping\Driver\AnnotationDriver; -use Doctrine\Persistence\Mapping\Driver\AnnotationDriver as AbstractAnnotationDriver; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; @@ -49,20 +47,15 @@ public function process(ContainerBuilder $container): void $class = $arguments[0]->getClass(); $namespace = substr($class, 0, strrpos($class, '\\')); - if ('Doctrine\ORM\Mapping\Driver' === $namespace ? AnnotationDriver::class !== $class : !is_subclass_of($class, AbstractAnnotationDriver::class)) { - continue; - } - - $id = sprintf('.%d_annotation_metadata_driver~%s', $i, ContainerBuilder::hash($arguments)); + $id = sprintf('.%d_doctrine_metadata_driver~%s', $i, ContainerBuilder::hash($arguments)); $container->setDefinition($id, $arguments[0]); $arguments[0] = new Reference($id); $methodCalls[$i] = [$method, $arguments]; } - $isAnnotated = false !== strpos($arguments[0], '_annotation_metadata_driver'); $annotatedPrefixes[$managerName][] = [ $arguments[1], - $isAnnotated ? new Reference($arguments[0]) : null, + new Reference($arguments[0]), ]; } @@ -70,7 +63,7 @@ public function process(ContainerBuilder $container): void } if (null !== $annotatedPrefixes) { - $container->getDefinition('maker.doctrine_helper')->setArgument(2, $annotatedPrefixes); + $container->getDefinition('maker.doctrine_helper')->setArgument(4, $annotatedPrefixes); } } } diff --git a/src/Doctrine/DoctrineHelper.php b/src/Doctrine/DoctrineHelper.php index 022f520b0..12b58d309 100644 --- a/src/Doctrine/DoctrineHelper.php +++ b/src/Doctrine/DoctrineHelper.php @@ -16,6 +16,7 @@ use Doctrine\Common\Persistence\Mapping\MappingException as LegacyPersistenceMappingException; use Doctrine\DBAL\Connection; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\ORM\Mapping\MappingException as ORMMappingException; use Doctrine\ORM\Mapping\NamingStrategy; use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory; @@ -23,9 +24,11 @@ use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory; use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\Driver\AnnotationDriver; +use Doctrine\Persistence\Mapping\Driver\MappingDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; use Doctrine\Persistence\Mapping\MappingException as PersistenceMappingException; use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; +use Symfony\Bundle\MakerBundle\Util\PhpCompatUtil; /** * @author Fabien Potencier @@ -40,6 +43,7 @@ final class DoctrineHelper * @var string */ private $entityNamespace; + private $phpCompatUtil; /** * @var ManagerRegistry @@ -49,16 +53,20 @@ final class DoctrineHelper /** * @var array|null */ - private $annotatedPrefixes; + private $mappingDriversByPrefix; + + private $attributeMappingSupport; /** * @var ManagerRegistry|LegacyManagerRegistry */ - public function __construct(string $entityNamespace, $registry = null, array $annotatedPrefixes = null) + public function __construct(string $entityNamespace, PhpCompatUtil $phpCompatUtil, $registry = null, bool $attributeMappingSupport = false, array $annotatedPrefixes = null) { $this->entityNamespace = trim($entityNamespace, '\\'); + $this->phpCompatUtil = $phpCompatUtil; $this->registry = $registry; - $this->annotatedPrefixes = $annotatedPrefixes; + $this->attributeMappingSupport = $attributeMappingSupport; + $this->mappingDriversByPrefix = $annotatedPrefixes; } /** @@ -85,43 +93,67 @@ public function getEntityNamespace(): string return $this->entityNamespace; } - public function isClassAnnotated(string $className): bool + public function doesClassUseDriver(string $className, string $driverClass): bool { - /** @var EntityManagerInterface $em */ - $em = $this->getRegistry()->getManagerForClass($className); + try { + /** @var EntityManagerInterface $em */ + $em = $this->getRegistry()->getManagerForClass($className); + } catch (\ReflectionException $exception) { + // this exception will be thrown by the registry if the class isn't created yet. + // an example case is the "make:entity" command, which needs to know which driver is used for the class to determine + // if the class should be generated with attributes or annotations. If this exception is thrown, we will check based on the + // namespaces for the given $className and compare it with the doctrine configuration to get the correct MappingDriver. + + return $this->isInstanceOf($this->getMappingDriverForNamespace($className), $driverClass); + } if (null === $em) { throw new \InvalidArgumentException(sprintf('Cannot find the entity manager for class "%s"', $className)); } - if (null === $this->annotatedPrefixes) { + if (null === $this->mappingDriversByPrefix) { // doctrine-bundle <= 2.2 $metadataDriver = $em->getConfiguration()->getMetadataDriverImpl(); if (!$this->isInstanceOf($metadataDriver, MappingDriverChain::class)) { - return $metadataDriver instanceof AnnotationDriver; + return $this->isInstanceOf($metadataDriver, $driverClass); } foreach ($metadataDriver->getDrivers() as $namespace => $driver) { if (0 === strpos($className, $namespace)) { - return $driver instanceof AnnotationDriver; + return $this->isInstanceOf($driver, $driverClass); } } - return $metadataDriver->getDefaultDriver() instanceof AnnotationDriver; + return $this->isInstanceOf($metadataDriver->getDefaultDriver(), $driverClass); } $managerName = array_search($em, $this->getRegistry()->getManagers(), true); - foreach ($this->annotatedPrefixes[$managerName] as [$prefix, $annotationDriver]) { + foreach ($this->mappingDriversByPrefix[$managerName] as [$prefix, $prefixDriver]) { if (0 === strpos($className, $prefix)) { - return null !== $annotationDriver; + return $this->isInstanceOf($prefixDriver, $driverClass); } } return false; } + public function isClassAnnotated(string $className): bool + { + return $this->doesClassUseDriver($className, AnnotationDriver::class); + } + + public function doesClassUsesAttributes(string $className): bool + { + return $this->doesClassUseDriver($className, AttributeDriver::class); + } + + public function isDoctrineSupportingAttributes(): bool + { + return $this->isDoctrineInstalled() && $this->attributeMappingSupport && $this->phpCompatUtil->canUseAttributes(); + } + public function getEntitiesForAutocomplete(): array { $entities = []; @@ -150,7 +182,7 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected $classNames->setAccessible(true); // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes - foreach ($this->annotatedPrefixes ?? [] as $managerName => $prefixes) { + foreach ($this->mappingDriversByPrefix ?? [] as $managerName => $prefixes) { foreach ($prefixes as [$prefix, $annotationDriver]) { if (null !== $annotationDriver) { $classNames->setValue($annotationDriver, null); @@ -182,7 +214,7 @@ public function getMetadata(string $classOrNamespace = null, bool $disconnected $cmf->setMetadataFor($m->getName(), $m); } - if (null === $this->annotatedPrefixes) { + if (null === $this->mappingDriversByPrefix) { // Invalidating the cached AnnotationDriver::$classNames to find new Entity classes $metadataDriver = $em->getConfiguration()->getMetadataDriverImpl(); if ($this->isInstanceOf($metadataDriver, MappingDriverChain::class)) { @@ -265,4 +297,31 @@ public function isKeyword(string $name): bool return $connection->getDatabasePlatform()->getReservedKeywordsList()->isKeyword($name); } + + /** + * this method tries to find the correct MappingDriver for the given namespace/class + * To determine which MappingDriver belongs to the class we check the prefixes configured in Doctrine and use the + * prefix that has the closest match to the given $namespace. + * + * this helper function is needed to create entities with the configuration of doctrine if they are not yet been registered + * in the ManagerRegistry + */ + private function getMappingDriverForNamespace(string $namespace): ?MappingDriver + { + $lowestCharacterDiff = null; + $foundDriver = null; + + foreach ($this->mappingDriversByPrefix as $key => $mappings) { + foreach ($mappings as [$prefix, $driver]) { + $diff = substr_compare($namespace, $prefix, 0); + + if ($diff >= 0 && (null === $lowestCharacterDiff || $diff < $lowestCharacterDiff)) { + $lowestCharacterDiff = $diff; + $foundDriver = $driver; + } + } + } + + return $foundDriver; + } } diff --git a/src/Doctrine/EntityClassGenerator.php b/src/Doctrine/EntityClassGenerator.php index 20644ec3c..ef59c6742 100644 --- a/src/Doctrine/EntityClassGenerator.php +++ b/src/Doctrine/EntityClassGenerator.php @@ -53,6 +53,7 @@ public function generateEntityClass(ClassNameDetails $entityClassDetails, bool $ 'broadcast' => $broadcast, 'should_escape_table_name' => $this->doctrineHelper->isKeyword($tableName), 'table_name' => $tableName, + 'doctrine_use_attributes' => $this->doctrineHelper->isDoctrineSupportingAttributes() && $this->doctrineHelper->doesClassUsesAttributes($entityClassDetails->getFullName()), ] ); diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index 5db667239..d7142c88c 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -160,6 +160,10 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen 'Entity\\' ); + if (!$this->doctrineHelper->isDoctrineSupportingAttributes() && $this->doctrineHelper->doesClassUsesAttributes($entityClassDetails->getFullName())) { + throw new RuntimeCommandException('To use Doctrine entity attributes you\'ll need PHP 8, doctrine/orm 2.9, doctrine/doctrine-bundle 2.4 and symfony/framework-bundle 5.2.'); + } + $classExists = class_exists($entityClassDetails->getFullName()); if (!$classExists) { $broadcast = $input->getOption('broadcast'); @@ -186,8 +190,11 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $generator->writeChanges(); } - if (!$this->doesEntityUseAnnotationMapping($entityClassDetails->getFullName())) { - throw new RuntimeCommandException(sprintf('Only annotation mapping is supported by make:entity, but the %s class uses a different format. If you would like this command to generate the properties & getter/setter methods, add your mapping configuration, and then re-run this command with the --regenerate flag.', $entityClassDetails->getFullName())); + if ( + !$this->doesEntityUseAnnotationMapping($entityClassDetails->getFullName()) + && !$this->doesEntityUseAttributeMapping($entityClassDetails->getFullName()) + ) { + throw new RuntimeCommandException(sprintf('Only annotation or attribute mapping is supported by make:entity, but the %s class uses a different format. If you would like this command to generate the properties & getter/setter methods, add your mapping configuration, and then re-run this command with the --regenerate flag.', $entityClassDetails->getFullName())); } if ($classExists) { @@ -204,7 +211,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen } $currentFields = $this->getPropertyNames($entityClassDetails->getFullName()); - $manipulator = $this->createClassManipulator($entityPath, $io, $overwrite); + $manipulator = $this->createClassManipulator($entityPath, $io, $overwrite, $entityClassDetails->getFullName()); $isFirstField = true; while (true) { @@ -232,7 +239,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $otherManipulator = $manipulator; } else { $otherManipulatorFilename = $this->getPathOfClass($newField->getInverseClass()); - $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite); + $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite, $entityClassDetails->getFullName()); } switch ($newField->getType()) { case EntityRelation::MANY_TO_ONE: @@ -247,7 +254,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen // the new field being added to THIS entity is the inverse $newFieldName = $newField->getInverseProperty(); $otherManipulatorFilename = $this->getPathOfClass($newField->getOwningClass()); - $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite); + $otherManipulator = $this->createClassManipulator($otherManipulatorFilename, $io, $overwrite, $entityClassDetails->getFullName()); // The *other* class will receive the ManyToOne $otherManipulator->addManyToOneRelation($newField->getOwningRelation()); @@ -793,9 +800,13 @@ private function askRelationType(ConsoleStyle $io, string $entityClass, string $ return $io->askQuestion($question); } - private function createClassManipulator(string $path, ConsoleStyle $io, bool $overwrite): ClassSourceManipulator + private function createClassManipulator(string $path, ConsoleStyle $io, bool $overwrite, string $className): ClassSourceManipulator { - $manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite); + $useAttributes = $this->doctrineHelper->doesClassUsesAttributes($className) && $this->doctrineHelper->isDoctrineSupportingAttributes(); + $useAnnotations = $this->doctrineHelper->isClassAnnotated($className) || !$useAttributes; + + $manipulator = new ClassSourceManipulator($this->fileManager->getFileContents($path), $overwrite, $useAnnotations, true, $useAttributes); + $manipulator->setIo($io); return $manipulator; @@ -850,6 +861,26 @@ private function doesEntityUseAnnotationMapping(string $className): bool return $this->doctrineHelper->isClassAnnotated($className); } + private function doesEntityUseAttributeMapping(string $className): bool + { + if (\PHP_VERSION < 80000) { + return false; + } + + if (!class_exists($className)) { + $otherClassMetadatas = $this->doctrineHelper->getMetadata(Str::getNamespace($className).'\\', true); + + // if we have no metadata, we should assume this is the first class being mapped + if (empty($otherClassMetadatas)) { + return false; + } + + $className = reset($otherClassMetadatas)->getName(); + } + + return $this->doctrineHelper->doesClassUsesAttributes($className); + } + private function getEntityNamespace(): string { return $this->doctrineHelper->getEntityNamespace(); diff --git a/src/Maker/MakeResetPassword.php b/src/Maker/MakeResetPassword.php index 79bdde034..8fdfaa3b7 100644 --- a/src/Maker/MakeResetPassword.php +++ b/src/Maker/MakeResetPassword.php @@ -371,8 +371,14 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r $generator->writeChanges(); + $useAttributesForDoctrineMapping = $this->doctrineHelper->isDoctrineSupportingAttributes() && $this->doctrineHelper->doesClassUsesAttributes($requestClassNameDetails->getFullName()); + $manipulator = new ClassSourceManipulator( - $this->fileManager->getFileContents($requestEntityPath) + $this->fileManager->getFileContents($requestEntityPath), + false, + !$useAttributesForDoctrineMapping, + true, + $useAttributesForDoctrineMapping ); $manipulator->addInterface(ResetPasswordRequestInterface::class); diff --git a/src/Maker/MakeUser.php b/src/Maker/MakeUser.php index 870debfd4..c2ea82512 100644 --- a/src/Maker/MakeUser.php +++ b/src/Maker/MakeUser.php @@ -14,6 +14,7 @@ use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Symfony\Bundle\MakerBundle\ConsoleStyle; use Symfony\Bundle\MakerBundle\DependencyBuilder; +use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper; use Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator; use Symfony\Bundle\MakerBundle\Doctrine\ORMDependencyBuilder; use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; @@ -53,12 +54,15 @@ final class MakeUser extends AbstractMaker private $entityClassGenerator; - public function __construct(FileManager $fileManager, UserClassBuilder $userClassBuilder, SecurityConfigUpdater $configUpdater, EntityClassGenerator $entityClassGenerator) + private $doctrineHelper; + + public function __construct(FileManager $fileManager, UserClassBuilder $userClassBuilder, SecurityConfigUpdater $configUpdater, EntityClassGenerator $entityClassGenerator, DoctrineHelper $doctrineHelper) { $this->fileManager = $fileManager; $this->userClassBuilder = $userClassBuilder; $this->configUpdater = $configUpdater; $this->entityClassGenerator = $entityClassGenerator; + $this->doctrineHelper = $doctrineHelper; } public static function getCommandName(): string @@ -79,8 +83,7 @@ public function configureCommand(Command $command, InputConfiguration $inputConf ->addOption('identity-property-name', null, InputOption::VALUE_REQUIRED, 'Enter a property name that will be the unique "display" name for the user (e.g. email, username, uuid)') ->addOption('with-password', null, InputOption::VALUE_NONE, 'Will this app be responsible for checking the password? Choose No if the password is actually checked by some other system (e.g. a single sign-on server)') ->addOption('use-argon2', null, InputOption::VALUE_NONE, 'Use the Argon2i password encoder? (deprecated)') - ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeUser.txt')) - ; + ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeUser.txt')); $inputConf->setArgumentAsNonInteractive('name'); } @@ -160,11 +163,17 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen // need to write changes early so we can modify the contents below $generator->writeChanges(); + $useAttributesForDoctrineMapping = $userClassConfiguration->isEntity() && ($this->doctrineHelper->isDoctrineSupportingAttributes()) && $this->doctrineHelper->doesClassUsesAttributes($userClassNameDetails->getFullName()); + // B) Implement UserInterface $manipulator = new ClassSourceManipulator( $this->fileManager->getFileContents($classPath), - true + true, + !$useAttributesForDoctrineMapping, + true, + $useAttributesForDoctrineMapping ); + $manipulator->setIo($io); $this->userClassBuilder->addUserInterfaceImplementation($manipulator, $userClassConfiguration); diff --git a/src/MakerBundle.php b/src/MakerBundle.php index a76c5e2f9..8c6ae26a4 100644 --- a/src/MakerBundle.php +++ b/src/MakerBundle.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\MakerBundle; +use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\DoctrineAttributesCheckPass; use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\MakeCommandRegistrationPass; use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\RemoveMissingParametersPass; use Symfony\Bundle\MakerBundle\DependencyInjection\CompilerPass\SetDoctrineAnnotatedPrefixesPass; @@ -28,6 +29,7 @@ class MakerBundle extends Bundle public function build(ContainerBuilder $container) { // add a priority so we run before the core command pass + $container->addCompilerPass(new DoctrineAttributesCheckPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 11); $container->addCompilerPass(new MakeCommandRegistrationPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10); $container->addCompilerPass(new RemoveMissingParametersPass()); $container->addCompilerPass(new SetDoctrineManagerRegistryClassPass()); diff --git a/src/Resources/config/makers.xml b/src/Resources/config/makers.xml index 2dd5d1d56..fdb3e9149 100644 --- a/src/Resources/config/makers.xml +++ b/src/Resources/config/makers.xml @@ -120,6 +120,7 @@ + diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 35e167c6d..26af9d257 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -37,7 +37,9 @@ + + %maker.compatible_check.doctrine.supports_attributes% diff --git a/src/Resources/skeleton/doctrine/Entity.tpl.php b/src/Resources/skeleton/doctrine/Entity.tpl.php index 19f3f0d49..8014b9699 100644 --- a/src/Resources/skeleton/doctrine/Entity.tpl.php +++ b/src/Resources/skeleton/doctrine/Entity.tpl.php @@ -10,6 +10,7 @@ use Symfony\UX\Turbo\Attribute\Broadcast; + /** * @ApiResource() @@ -19,6 +20,12 @@ * @ORM\Table(name="``") */ + + +#[ORM\Entity(repositoryClass: ::class)] +#[ORM\Table(name: '``')] + + #[ApiResource] @@ -27,12 +34,15 @@ class { - /** + /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ - private $id; + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] + private $id; public function getId(): ?int { diff --git a/src/Test/MakerTestDetails.php b/src/Test/MakerTestDetails.php index c61330b98..68410310b 100644 --- a/src/Test/MakerTestDetails.php +++ b/src/Test/MakerTestDetails.php @@ -352,4 +352,13 @@ public function getSkipMessage(): ?string { return $this->skipMessage; } + + public function useDoctrineAttributeMapping(): self + { + return $this->addReplacement( + 'config/packages/doctrine.yaml', + 'type: annotation', + 'type: attribute' + ); + } } diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index c0875deef..08bf95dcb 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -42,6 +42,7 @@ final class ClassSourceManipulator private $overwrite; private $useAnnotations; private $fluentMutators; + private $useAttributesForDoctrineMapping; private $parser; private $lexer; private $printer; @@ -55,11 +56,12 @@ final class ClassSourceManipulator private $pendingComments = []; - public function __construct(string $sourceCode, bool $overwrite = false, bool $useAnnotations = true, bool $fluentMutators = true) + public function __construct(string $sourceCode, bool $overwrite = false, bool $useAnnotations = true, bool $fluentMutators = true, bool $useAttributesForDoctrineMapping = false) { $this->overwrite = $overwrite; $this->useAnnotations = $useAnnotations; $this->fluentMutators = $fluentMutators; + $this->useAttributesForDoctrineMapping = $useAttributesForDoctrineMapping; $this->lexer = new Lexer\Emulative([ 'usedAttributes' => [ 'comments', @@ -88,13 +90,20 @@ public function addEntityField(string $propertyName, array $columnOptions, array $typeHint = $this->getEntityTypeHint($columnOptions['type']); $nullable = $columnOptions['nullable'] ?? false; $isId = (bool) ($columnOptions['id'] ?? false); + $attributes = []; + + if ($this->useAttributesForDoctrineMapping) { + $attributes[] = $this->buildAttributeNode('ORM\Column', $columnOptions); + } else { + $comments[] = $this->buildAnnotationLine('@ORM\Column', $columnOptions); + } - $comments[] = $this->buildAnnotationLine('@ORM\Column', $columnOptions); $defaultValue = null; if ('array' === $typeHint) { $defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]); } - $this->addProperty($propertyName, $comments, $defaultValue); + + $this->addProperty($propertyName, $comments, $defaultValue, $attributes); $this->addGetter( $propertyName, @@ -114,16 +123,30 @@ public function addEmbeddedEntity(string $propertyName, string $className): void { $typeHint = $this->addUseStatementIfNecessary($className); - $annotations = [ - $this->buildAnnotationLine( - '@ORM\\Embedded', - [ - 'class' => new ClassNameValue($className, $typeHint), - ] - ), - ]; + $annotations = []; + $attributes = []; + + if (!$this->useAttributesForDoctrineMapping) { + $annotations = [ + $this->buildAnnotationLine( + '@ORM\\Embedded', + [ + 'class' => new ClassNameValue($className, $typeHint), + ] + ), + ]; + } else { + $attributes = [ + $this->buildAttributeNode( + 'ORM\\Embedded', + [ + 'class' => new ClassNameValue($className, $typeHint), + ] + ), + ]; + } - $this->addProperty($propertyName, $annotations); + $this->addProperty($propertyName, $annotations, null, $attributes); // logic to avoid re-adding the same ArrayCollection line $addEmbedded = true; @@ -284,8 +307,7 @@ public function addMethodBody(Builder\Method $methodBuilder, string $methodBody) public function createMethodBuilder(string $methodName, $returnType, bool $isReturnTypeNullable, array $commentLines = []): Builder\Method { $methodNodeBuilder = (new Builder\Method($methodName)) - ->makePublic() - ; + ->makePublic(); if (null !== $returnType) { if (class_exists($returnType) || interface_exists($returnType)) { @@ -311,7 +333,7 @@ public function createMethodLevelBlankLine() return $this->createBlankLineNode(self::CONTEXT_CLASS_METHOD); } - public function addProperty(string $name, array $annotationLines = [], $defaultValue = null): void + public function addProperty(string $name, array $annotationLines = [], $defaultValue = null, array $attributes = []): void { if ($this->propertyExists($name)) { // we never overwrite properties @@ -319,10 +341,15 @@ public function addProperty(string $name, array $annotationLines = [], $defaultV } $newPropertyBuilder = (new Builder\Property($name))->makePrivate(); + if ($annotationLines && $this->useAnnotations) { $newPropertyBuilder->setDocComment($this->createDocBlock($annotationLines)); } + foreach ($attributes as $attribute) { + $newPropertyBuilder->addAttribute($attribute); + } + if (null !== $defaultValue) { $newPropertyBuilder->setDefault($defaultValue); } @@ -388,8 +415,7 @@ private function addCustomGetter(string $propertyName, string $methodName, $retu ->makePublic() ->addStmt( new Node\Stmt\Return_($propertyFetch) - ) - ; + ); if (null !== $returnType) { $getterNodeBuilder->setReturnType($isReturnTypeNullable ? new Node\NullableType($returnType) : $returnType); @@ -493,20 +519,38 @@ private function addSingularRelation(BaseRelation $relation): void $annotationOptions['cascade'] = ['persist', 'remove']; } - $annotations = [ - $this->buildAnnotationLine( - $relation instanceof RelationManyToOne ? '@ORM\\ManyToOne' : '@ORM\\OneToOne', - $annotationOptions - ), - ]; + $annotations = []; + $attributes = []; + + if (!$this->useAttributesForDoctrineMapping) { + $annotations = [ + $this->buildAnnotationLine( + $relation instanceof RelationManyToOne ? '@ORM\\ManyToOne' : '@ORM\\OneToOne', + $annotationOptions + ), + ]; + } else { + $attributes = [ + $this->buildAttributeNode( + $relation instanceof RelationManyToOne ? 'ORM\\ManyToOne' : 'ORM\\OneToOne', + $annotationOptions + ), + ]; + } if (!$relation->isNullable() && $relation->isOwning()) { - $annotations[] = $this->buildAnnotationLine('@ORM\\JoinColumn', [ - 'nullable' => false, - ]); + if (!$this->useAttributesForDoctrineMapping) { + $annotations[] = $this->buildAnnotationLine('@ORM\\JoinColumn', [ + 'nullable' => false, + ]); + } else { + $attributes[] = $this->buildAttributeNode('ORM\\JoinColumn', [ + 'nullable' => false, + ]); + } } - $this->addProperty($relation->getPropertyName(), $annotations); + $this->addProperty($relation->getPropertyName(), $annotations, null, $attributes); $this->addGetter( $relation->getPropertyName(), @@ -571,14 +615,26 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void $annotationOptions['orphanRemoval'] = true; } - $annotations = [ - $this->buildAnnotationLine( - $relation instanceof RelationManyToMany ? '@ORM\\ManyToMany' : '@ORM\\OneToMany', - $annotationOptions - ), - ]; + $annotations = []; + $attributes = []; + + if (!$this->useAttributesForDoctrineMapping) { + $annotations = [ + $this->buildAnnotationLine( + $relation instanceof RelationManyToMany ? '@ORM\\ManyToMany' : '@ORM\\OneToMany', + $annotationOptions + ), + ]; + } else { + $attributes = [ + $this->buildAttributeNode( + $relation instanceof RelationManyToMany ? 'ORM\\ManyToMany' : 'ORM\\OneToMany', + $annotationOptions + ), + ]; + } - $this->addProperty($relation->getPropertyName(), $annotations); + $this->addProperty($relation->getPropertyName(), $annotations, null, $attributes); // logic to avoid re-adding the same ArrayCollection line $addArrayCollection = true; @@ -636,8 +692,7 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $relation->getPropertyName()) ), new Node\Expr\Variable($argName) - )) - ; + )); // set the owning side of the relationship if (!$relation->isOwning()) { @@ -956,13 +1011,11 @@ private function createBlankLineNode(string $context) switch ($context) { case self::CONTEXT_OUTSIDE_CLASS: return (new Builder\Use_('__EXTRA__LINE', Node\Stmt\Use_::TYPE_NORMAL)) - ->getNode() - ; + ->getNode(); case self::CONTEXT_CLASS: return (new Builder\Property('__EXTRA__LINE')) ->makePrivate() - ->getNode() - ; + ->getNode(); case self::CONTEXT_CLASS_METHOD: return new Node\Expr\Variable('__EXTRA__LINE'); default: @@ -1056,8 +1109,7 @@ private function makeMethodFluent(Builder\Method $methodBuilder): void $methodBuilder ->addStmt($this->createBlankLineNode(self::CONTEXT_CLASS_METHOD)) - ->addStmt(new Node\Stmt\Return_(new Node\Expr\Variable('this'))) - ; + ->addStmt(new Node\Stmt\Return_(new Node\Expr\Variable('this'))); $methodBuilder->setReturnType('self'); } @@ -1316,4 +1368,100 @@ private function addMethodParams(Builder\Method $methodBuilder, array $params): $methodBuilder->addParam($param); } } + + /** + * builds a PHPParser Expr Node based on the value given in $value + * throws an Exception when the given $value is not resolvable by this method. + * + * @param mixed $value + * + * @throws \Exception + */ + private function buildNodeExprByValue($value): Node\Expr + { + switch (\gettype($value)) { + case 'string': + $nodeValue = new Node\Scalar\String_($value); + break; + case 'integer': + $nodeValue = new Node\Scalar\LNumber($value); + break; + case 'double': + $nodeValue = new Node\Scalar\DNumber($value); + break; + case 'boolean': + $nodeValue = new Node\Expr\ConstFetch(new Node\Name($value ? 'true' : 'false')); + break; + case 'array': + $context = $this; + $arrayItems = array_map(static function ($key, $value) use ($context) { + return new Node\Expr\ArrayItem( + $context->buildNodeExprByValue($value), + !\is_int($key) ? $context->buildNodeExprByValue($key) : null + ); + }, array_keys($value), array_values($value)); + $nodeValue = new Node\Expr\Array_($arrayItems, ['kind' => Node\Expr\Array_::KIND_SHORT]); + break; + default: + $nodeValue = null; + } + + if (null === $nodeValue) { + if ($value instanceof ClassNameValue) { + $nodeValue = new Node\Expr\ConstFetch(new Node\Name(sprintf('%s::class', $value->getShortName()))); + } else { + throw new \Exception(sprintf('Cannot build a node expr for value of type "%s"', \gettype($value))); + } + } + + return $nodeValue; + } + + /** + * builds an PHPParser attribute node. + * + * @param string $attributeClass the attribute class which should be used for the attribute + * @param array $options the named arguments for the attribute ($key = argument name, $value = argument value) + */ + private function buildAttributeNode(string $attributeClass, array $options): Node\Attribute + { + $options = $this->sortOptionsByClassConstructorParameters($options, $attributeClass); + + $context = $this; + $nodeArguments = array_map(static function ($option, $value) use ($context) { + return new Node\Arg($context->buildNodeExprByValue($value), false, false, [], new Node\Identifier($option)); + }, array_keys($options), array_values($options)); + + return new Node\Attribute( + new Node\Name($attributeClass), + $nodeArguments + ); + } + + /** + * sort the given options based on the constructor parameters for the given $classString + * this prevents code inspections warnings for IDEs like intellij/phpstorm. + * + * option keys that are not found in the constructor will be added at the end of the sorted array + */ + private function sortOptionsByClassConstructorParameters(array $options, string $classString): array + { + if ('ORM\\' === substr($classString, 0, 4)) { + $classString = sprintf('Doctrine\\ORM\\Mapping\\%s', substr($classString, 4)); + } + + $constructorParameterNames = array_map(static function (\ReflectionParameter $reflectionParameter) { + return $reflectionParameter->getName(); + }, (new \ReflectionClass($classString))->getConstructor()->getParameters()); + + $sorted = []; + foreach ($constructorParameterNames as $name) { + if (\array_key_exists($name, $options)) { + $sorted[$name] = $options[$name]; + unset($options[$name]); + } + } + + return array_merge($sorted, $options); + } } diff --git a/tests/Doctrine/EntityRegeneratorTest.php b/tests/Doctrine/EntityRegeneratorTest.php index d75440000..fa9fb1737 100644 --- a/tests/Doctrine/EntityRegeneratorTest.php +++ b/tests/Doctrine/EntityRegeneratorTest.php @@ -113,7 +113,7 @@ private function doTestRegeneration(string $sourceDir, Kernel $kernel, string $n }); $fileManager = new FileManager($fs, $autoloaderUtil, new MakerFileLinkFormatter(null), $tmpDir); - $doctrineHelper = new DoctrineHelper('App\\Entity', $container->get('doctrine')); + $doctrineHelper = new DoctrineHelper('App\\Entity', new PhpCompatUtil($fileManager), $container->get('doctrine')); $phpCompatUtil = new PhpCompatUtil($fileManager); $generator = new Generator($fileManager, 'App\\', $phpCompatUtil); $entityClassGenerator = new EntityClassGenerator($generator, $doctrineHelper); diff --git a/tests/Maker/MakeEntityLegacyTest.php b/tests/Maker/MakeEntityLegacyTest.php new file mode 100644 index 000000000..c753b964d --- /dev/null +++ b/tests/Maker/MakeEntityLegacyTest.php @@ -0,0 +1,558 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MakerBundle\Tests\Maker; + +use Symfony\Bundle\MakerBundle\Maker\MakeEntity; +use Symfony\Bundle\MakerBundle\Test\MakerTestCase; +use Symfony\Bundle\MakerBundle\Test\MakerTestDetails; +use Symfony\Component\Finder\Finder; + +class MakeEntityLegacyTest extends MakerTestCase +{ + public function getTestDetails() + { + yield 'entity_new' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // add not additional fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntity') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_new_api_resource' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // Mark the entity as an API Platform resource + 'y', + // add not additional fields + '', + ]) + ->addExtraDependencies('api') + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntity') + ->configureDatabase() + ->updateSchemaAfterCommand() + ->assert(function (string $output, string $directory) { + $this->assertFileExists($directory.'/src/Entity/User.php'); + + $content = file_get_contents($directory.'/src/Entity/User.php'); + $this->assertStringContainsString('use ApiPlatform\Core\Annotation\ApiResource;', $content); + $this->assertStringContainsString(\PHP_VERSION_ID >= 80000 ? '#[ApiResource]' : '@ApiResource', $content); + }), + ]; + + yield 'entity_with_fields' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // add not additional fields + 'name', + 'string', + '255', // length + // nullable + 'y', + 'createdAt', + // use default datetime + '', + // nullable + 'y', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntity') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_updating_main' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // add additional fields + 'lastName', + 'string', + '', // length (default 255) + // nullable + 'y', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityUpdate') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_many_to_one_simple_with_inverse' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'UserAvatarPhoto', + // field name + 'user', + // add a relationship field + 'relation', + // the target entity + 'User', + // relation type + 'ManyToOne', + // nullable + 'n', + // do you want to generate an inverse relation? (default to yes) + '', + // field name on opposite side - use default 'userAvatarPhotos' + '', + // orphanRemoval (default to no) + '', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityManyToOne') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_many_to_one_simple_no_inverse' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'UserAvatarPhoto', + // field name + 'user', + // add a relationship field + 'relation', + // the target entity + 'User', + // relation type + 'ManyToOne', + // nullable + 'n', + // do you want to generate an inverse relation? (default to yes) + 'n', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityManyToOneNoInverse') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_many_to_one_self_referencing' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // field name + 'guardian', + // add a relationship field + 'relation', + // the target entity + 'User', + // relation type + 'ManyToOne', + // nullable + 'y', + // do you want to generate an inverse relation? (default to yes) + '', + // field name on opposite side + 'dependants', + // orphanRemoval (default to no) + '', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntitySelfReferencing') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_exists_in_root' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'Directory', + // field name + 'parentDirectory', + // add a relationship field + 'relation', + // the target entity + 'Directory', + // relation type + 'ManyToOne', + // nullable + 'y', + // do you want to generate an inverse relation? (default to yes) + '', + // field name on opposite side + 'childDirectories', + // orphanRemoval (default to no) + '', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityExistsInRoot') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_one_to_many_simple' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // field name + 'photos', + // add a relationship field + 'relation', + // the target entity + 'UserAvatarPhoto', + // relation type + 'OneToMany', + // field name on opposite side - use default 'user' + '', + // nullable + 'n', + // orphanRemoval + 'y', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityOneToMany') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_many_to_many_simple' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'Course', + // field name + 'students', + // add a relationship field + 'relation', + // the target entity + 'User', + // relation type + 'ManyToMany', + // inverse side? + 'y', + // field name on opposite side - use default 'courses' + '', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityManyToMany') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_many_to_many_simple_in_custom_root_namespace' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'Course', + // field name + 'students', + // add a relationship field + 'relation', + // the target entity + 'User', + // relation type + 'ManyToMany', + // inverse side? + 'y', + // field name on opposite side - use default 'courses' + '', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace') + ->changeRootNamespace('Custom') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_one_to_one_simple' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'UserProfile', + // field name + 'user', + // add a relationship field + 'relation', + // the target entity + 'User', + // relation type + 'OneToOne', + // nullable + 'n', + // inverse side? + 'y', + // field name on opposite side - use default 'userProfile' + '', + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityOneToOne') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + yield 'entity_many_to_one_vendor_target' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // field name + 'userGroup', + // add a relationship field + 'ManyToOne', + // the target entity + 'Some\\Vendor\\Group', + // nullable + '', + /* + * normally, we ask for the field on the *other* side, but we + * do not here, since the other side won't be mapped. + */ + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget') + ->configureDatabase() + ->addReplacement( + 'composer.json', + '"App\\\Tests\\\": "tests/",', + '"App\\\Tests\\\": "tests/",'."\n".' "Some\\\Vendor\\\": "vendor/some-vendor/src",' + ) + ->assert(function (string $output, string $directory) { + $this->assertStringContainsString('updated: src/Entity/User.php', $output); + $this->assertStringNotContainsString('updated: vendor/', $output); + + // sanity checks on the generated code + $finder = new Finder(); + $finder->in($directory.'/src/Entity')->files()->name('*.php'); + $this->assertCount(1, $finder); + + $this->assertStringNotContainsString('inversedBy', file_get_contents($directory.'/src/Entity/User.php')); + }), + ]; + + yield 'entity_many_to_many_vendor_target' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // field name + 'userGroups', + // add a relationship field + 'ManyToMany', + // the target entity + 'Some\Vendor\Group', + /* + * normally, we ask for the field on the *other* side, but we + * do not here, since the other side won't be mapped. + */ + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget') + ->configureDatabase() + ->addReplacement( + 'composer.json', + '"App\\\Tests\\\": "tests/",', + '"App\\\Tests\\\": "tests/",'."\n".' "Some\\\Vendor\\\": "vendor/some-vendor/src",' + ) + ->assert(function (string $output, string $directory) { + $this->assertStringNotContainsString('updated: vendor/', $output); + + $this->assertStringNotContainsString('inversedBy', file_get_contents($directory.'/src/Entity/User.php')); + }), + ]; + + yield 'entity_one_to_one_vendor_target' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // field name + 'userGroup', + // add a relationship field + 'OneToOne', + // the target entity + 'Some\Vendor\Group', + // nullable, + '', + /* + * normally, we ask for the field on the *other* side, but we + * do not here, since the other side won't be mapped. + */ + // finish adding fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget') + ->configureDatabase() + ->addReplacement( + 'composer.json', + '"App\\\Tests\\\": "tests/",', + '"App\\\Tests\\\": "tests/",'."\n".' "Some\\\Vendor\\\": "vendor/some-vendor/src",' + ) + ->assert(function (string $output, string $directory) { + $this->assertStringNotContainsString('updated: vendor/', $output); + + $this->assertStringNotContainsString('inversedBy', file_get_contents($directory.'/src/Entity/User.php')); + }), + ]; + + yield 'entity_regenerate' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // namespace: use default App\Entity + '', + ]) + ->setArgumentsString('--regenerate') + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityRegenerate') + ->configureDatabase(true), + ]; + + yield 'entity_regenerate_embeddable_object' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // namespace: use default App\Entity + '', + ]) + ->setArgumentsString('--regenerate') + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject') + ->configureDatabase(), + ]; + + yield 'entity_regenerate_embeddable' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // namespace: use default App\Entity + '', + ]) + ->setArgumentsString('--regenerate --overwrite') + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbedable') + ->configureDatabase(), + ]; + + yield 'entity_regenerate_overwrite' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // namespace: use default App\Entity + '', + ]) + ->setArgumentsString('--regenerate --overwrite') + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityRegenerateOverwrite') + ->configureDatabase(false), + ]; + + yield 'entity_updating_overwrite' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // field name + 'firstName', + 'string', + '', // length (default 255) + // nullable + '', + // finish adding fields + '', + ]) + ->setArgumentsString('--overwrite') + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntityOverwrite'), + ]; + + // see #192 + yield 'entity_into_sub_namespace_matching_entity' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'Product\\Category', + // add not additional fields + '', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntitySubNamespaceMatchingEntity') + ->configureDatabase() + ->updateSchemaAfterCommand(), + ]; + + $broadCastTest = MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // Mark the entity as broadcasted + 'y', + // add not additional fields + '', + ]) + ->setRequiredPhpVersion(70200) + ->addExtraDependencies('ux-turbo-mercure') + ->configureDatabase() + ->addReplacement( + '.env', + 'https://127.0.0.1:8000/.well-known/mercure', + 'http://127.0.0.1:1337/.well-known/mercure' + ) + ->updateSchemaAfterCommand() + ->assert(function (string $output, string $directory) { + $this->assertFileExists($directory.'/src/Entity/User.php'); + + $content = file_get_contents($directory.'/src/Entity/User.php'); + $this->assertStringContainsString('use Symfony\UX\Turbo\Attribute\Broadcast;', $content); + $this->assertStringContainsString(\PHP_VERSION_ID >= 80000 ? '#[Broadcast]' : '@Broadcast', $content); + }) + ; + // use the fixtures - which contains a test for Mercure - unless specified to skip those + $skipMercureTest = $_SERVER['MAKER_SKIP_MERCURE_TEST'] ?? false; + if (!$skipMercureTest) { + $broadCastTest->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntity'); + } + yield 'entity_new_broadcast' => [$broadCastTest]; + + yield 'entity_new_with_api_and_broadcast_dependencies' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeEntity::class), + [ + // entity class name + 'User', + // Mark the entity as not an API Platform resource + 'n', + // Mark the entity as not broadcasted + 'n', + // add not additional fields + '', + ]) + ->setRequiredPhpVersion(70200) + ->addExtraDependencies('api') + ->addExtraDependencies('ux-turbo-mercure') + ->setFixtureFilesPath(__DIR__.'/../fixtures/legacy/MakeEntity/MakeEntity') + ->configureDatabase() + ->updateSchemaAfterCommand() + ->assert(function (string $output, string $directory) { + $this->assertFileExists($directory.'/src/Entity/User.php'); + }), + ]; + } +} diff --git a/tests/Maker/MakeEntityTest.php b/tests/Maker/MakeEntityTest.php index bc040a2d7..98b15fd8d 100644 --- a/tests/Maker/MakeEntityTest.php +++ b/tests/Maker/MakeEntityTest.php @@ -28,6 +28,8 @@ public function getTestDetails() // add not additional fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntity') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -43,6 +45,8 @@ public function getTestDetails() // add not additional fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->addExtraDependencies('api') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntity') ->configureDatabase() @@ -75,6 +79,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntity') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -94,6 +100,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityUpdate') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -123,6 +131,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityManyToOne') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -148,6 +158,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityManyToOneNoInverse') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -177,6 +189,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntitySelfReferencing') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -206,6 +220,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityExistsInRoot') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -233,6 +249,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityOneToMany') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -258,6 +276,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityManyToMany') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -283,6 +303,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityManyToManyInCustomNamespace') ->changeRootNamespace('Custom') ->configureDatabase() @@ -311,6 +333,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityOneToOne') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -336,6 +360,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityRelationVendorTarget') ->configureDatabase() ->addReplacement( @@ -374,6 +400,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityRelationVendorTarget') ->configureDatabase() ->addReplacement( @@ -408,6 +436,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityRelationVendorTarget') ->configureDatabase() ->addReplacement( @@ -428,6 +458,8 @@ public function getTestDetails() // namespace: use default App\Entity '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setArgumentsString('--regenerate') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityRegenerate') ->configureDatabase(true), @@ -439,6 +471,8 @@ public function getTestDetails() // namespace: use default App\Entity '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setArgumentsString('--regenerate') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityRegenerateEmbeddableObject') ->configureDatabase(), @@ -450,6 +484,8 @@ public function getTestDetails() // namespace: use default App\Entity '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setArgumentsString('--regenerate --overwrite') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityRegenerateEmbedable') ->configureDatabase(), @@ -461,6 +497,8 @@ public function getTestDetails() // namespace: use default App\Entity '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setArgumentsString('--regenerate --overwrite') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityRegenerateOverwrite') ->configureDatabase(false), @@ -506,7 +544,7 @@ public function getTestDetails() ->configureDatabase(false) ->setCommandAllowedToFail(true) ->assert(function (string $output, string $directory) { - $this->assertStringContainsString('Only annotation mapping is supported', $output); + $this->assertStringContainsString('Only annotation or attribute mapping is supported', $output); }), ]; @@ -529,7 +567,7 @@ public function getTestDetails() ->configureDatabase(false) ->setCommandAllowedToFail(true) ->assert(function (string $output, string $directory) { - $this->assertStringContainsString('Only annotation mapping is supported', $output); + $this->assertStringContainsString('Only annotation or attribute mapping is supported', $output); }), ]; @@ -547,6 +585,8 @@ public function getTestDetails() // finish adding fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setArgumentsString('--overwrite') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityOverwrite'), ]; @@ -560,6 +600,8 @@ public function getTestDetails() // add not additional fields '', ]) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntitySubNamespaceMatchingEntity') ->configureDatabase() ->updateSchemaAfterCommand(), @@ -575,7 +617,8 @@ public function getTestDetails() // add not additional fields '', ]) - ->setRequiredPhpVersion(70200) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->addExtraDependencies('ux-turbo-mercure') ->configureDatabase() ->addReplacement( @@ -589,7 +632,7 @@ public function getTestDetails() $content = file_get_contents($directory.'/src/Entity/User.php'); $this->assertStringContainsString('use Symfony\UX\Turbo\Attribute\Broadcast;', $content); - $this->assertStringContainsString(\PHP_VERSION_ID >= 80000 ? '#[Broadcast]' : '@Broadcast', $content); + $this->assertStringContainsString('#[Broadcast]', $content); }) ; // use the fixtures - which contains a test for Mercure - unless specified to skip those @@ -611,7 +654,8 @@ public function getTestDetails() // add not additional fields '', ]) - ->setRequiredPhpVersion(70200) + ->setRequiredPhpVersion(80000) + ->useDoctrineAttributeMapping() ->addExtraDependencies('api') ->addExtraDependencies('ux-turbo-mercure') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntity') diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index c2588cc80..21ec436e7 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -41,7 +41,7 @@ public function testAddProperty(string $sourceFilename, $propertyName, array $co public function getAddPropertyTests() { yield 'normal_property_add' => [ - 'User_simple.php', + 'legacy/User_simple.php', 'fooProp', [], 'User_simple.php', @@ -65,7 +65,7 @@ public function getAddPropertyTests() ]; yield 'property_empty_class' => [ - 'User_empty.php', + 'legacy/User_empty.php', 'fooProp', [], 'User_empty.php', @@ -91,7 +91,7 @@ public function testAddGetter(string $sourceFilename, string $propertyName, stri public function getAddGetterTests() { yield 'normal_getter_add' => [ - 'User_simple.php', + 'legacy/User_simple.php', 'fooProp', 'string', [], @@ -110,7 +110,7 @@ public function getAddGetterTests() ]; yield 'getter_empty_class' => [ - 'User_empty.php', + 'legacy/User_empty.php', 'fooProp', 'string', [], @@ -137,7 +137,7 @@ public function testAddSetter(string $sourceFilename, string $propertyName, stri public function getAddSetterTests() { yield 'normal_setter_add' => [ - 'User_simple.php', + 'legacy/User_simple.php', 'fooProp', 'string', false, @@ -158,7 +158,7 @@ public function getAddSetterTests() ]; yield 'setter_empty_class' => [ - 'User_empty.php', + 'legacy/User_empty.php', 'fooProp', 'string', false, @@ -202,18 +202,41 @@ public function getAnnotationTests() /** * @dataProvider getAddEntityFieldTests */ - public function testAddEntityField(string $sourceFilename, string $propertyName, array $fieldOptions, $expectedSourceFilename) - { - $source = file_get_contents(__DIR__.'/fixtures/source/'.$sourceFilename); - $expectedSource = file_get_contents(__DIR__.'/fixtures/add_entity_field/'.$expectedSourceFilename); + public function testAddEntityField(string $sourceFilename, string $propertyName, array $fieldOptions, $expectedSourceFilename): void + { + $sourcePath = __DIR__.'/fixtures/source'; + $expectedPath = __DIR__.'/fixtures/add_entity_field'; + + // Legacy Annotation Tests + $this->runAddEntityFieldTests( + file_get_contents(sprintf('%s/legacy/%s', $sourcePath, $sourceFilename)), + $propertyName, + $fieldOptions, + file_get_contents(sprintf('%s/legacy/%s', $expectedPath, $expectedSourceFilename)), + false + ); - $manipulator = new ClassSourceManipulator($source); + // Run Attribute Tests + if ((\PHP_VERSION_ID >= 80000)) { + $this->runAddEntityFieldTests( + file_get_contents(sprintf('%s/%s', $sourcePath, $sourceFilename)), + $propertyName, + $fieldOptions, + file_get_contents(sprintf('%s/%s', $expectedPath, $expectedSourceFilename)), + true + ); + } + } + + private function runAddEntityFieldTests(string $source, string $propertyName, array $fieldOptions, string $expected, bool $php8): void + { + $manipulator = new ClassSourceManipulator($source, false, !$php8, true, $php8); $manipulator->addEntityField($propertyName, $fieldOptions); - $this->assertSame($expectedSource, $manipulator->getSourceCode()); + $this->assertSame($expected, $manipulator->getSourceCode()); } - public function getAddEntityFieldTests() + public function getAddEntityFieldTests(): \Generator { yield 'entity_normal_add' => [ 'User_simple.php', @@ -263,18 +286,39 @@ public function getAddEntityFieldTests() /** * @dataProvider getAddManyToOneRelationTests */ - public function testAddManyToOneRelation(string $sourceFilename, $expectedSourceFilename, RelationManyToOne $manyToOne) + public function testAddManyToOneRelation(string $sourceFilename, $expectedSourceFilename, RelationManyToOne $manyToOne): void { - $source = file_get_contents(__DIR__.'/fixtures/source/'.$sourceFilename); - $expectedSource = file_get_contents(__DIR__.'/fixtures/add_many_to_one_relation/'.$expectedSourceFilename); + $sourcePath = __DIR__.'/fixtures/source'; + $expectedPath = __DIR__.'/fixtures/add_many_to_one_relation'; - $manipulator = new ClassSourceManipulator($source); + // Legacy Annotation Tests + $this->runAddManyToOneRelationTests( + file_get_contents(sprintf('%s/legacy/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/legacy/%s', $expectedPath, $expectedSourceFilename)), + $manyToOne, + false + ); + + // Run Attribute Tests + if ((\PHP_VERSION_ID >= 80000)) { + $this->runAddManyToOneRelationTests( + file_get_contents(sprintf('%s/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/%s', $expectedPath, $expectedSourceFilename)), + $manyToOne, + true + ); + } + } + + public function runAddManyToOneRelationTests(string $source, string $expected, RelationManyToOne $manyToOne, bool $php8): void + { + $manipulator = new ClassSourceManipulator($source, false, !$php8, true, $php8); $manipulator->addManyToOneRelation($manyToOne); - $this->assertSame($expectedSource, $manipulator->getSourceCode()); + $this->assertSame($expected, $manipulator->getSourceCode()); } - public function getAddManyToOneRelationTests() + public function getAddManyToOneRelationTests(): \Generator { yield 'many_to_one_not_nullable' => [ 'User_simple.php', @@ -341,18 +385,39 @@ public function getAddManyToOneRelationTests() /** * @dataProvider getAddOneToManyRelationTests */ - public function testAddOneToManyRelation(string $sourceFilename, $expectedSourceFilename, RelationOneToMany $oneToMany) + public function testAddOneToManyRelation(string $sourceFilename, string $expectedSourceFilename, RelationOneToMany $oneToMany) { - $source = file_get_contents(__DIR__.'/fixtures/source/'.$sourceFilename); - $expectedSource = file_get_contents(__DIR__.'/fixtures/add_one_to_many_relation/'.$expectedSourceFilename); + $sourcePath = __DIR__.'/fixtures/source'; + $expectedPath = __DIR__.'/fixtures/add_one_to_many_relation'; - $manipulator = new ClassSourceManipulator($source); + // Legacy Annotation Tests + $this->runAddOneToManyRelationTests( + file_get_contents(sprintf('%s/legacy/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/legacy/%s', $expectedPath, $expectedSourceFilename)), + $oneToMany, + false + ); + + // Run Attribute Tests + if ((\PHP_VERSION_ID >= 80000)) { + $this->runAddOneToManyRelationTests( + file_get_contents(sprintf('%s/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/%s', $expectedPath, $expectedSourceFilename)), + $oneToMany, + true + ); + } + } + + private function runAddOneToManyRelationTests(string $source, string $expected, RelationOneToMany $oneToMany, bool $php8): void + { + $manipulator = new ClassSourceManipulator($source, false, !$php8, true, $php8); $manipulator->addOneToManyRelation($oneToMany); - $this->assertSame($expectedSource, $manipulator->getSourceCode()); + $this->assertSame($expected, $manipulator->getSourceCode()); } - public function getAddOneToManyRelationTests() + public function getAddOneToManyRelationTests(): \Generator { yield 'one_to_many_simple' => [ 'User_simple.php', @@ -392,18 +457,39 @@ public function getAddOneToManyRelationTests() /** * @dataProvider getAddManyToManyRelationTests */ - public function testAddManyToManyRelation(string $sourceFilename, $expectedSourceFilename, RelationManyToMany $manyToMany) + public function testAddManyToManyRelation(string $sourceFilename, $expectedSourceFilename, RelationManyToMany $manyToMany): void { - $source = file_get_contents(__DIR__.'/fixtures/source/'.$sourceFilename); - $expectedSource = file_get_contents(__DIR__.'/fixtures/add_many_to_many_relation/'.$expectedSourceFilename); + $sourcePath = __DIR__.'/fixtures/source'; + $expectedPath = __DIR__.'/fixtures/add_many_to_many_relation'; - $manipulator = new ClassSourceManipulator($source); + // Legacy Annotation Tests + $this->runAddManyToManyRelationTest( + file_get_contents(sprintf('%s/legacy/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/legacy/%s', $expectedPath, $expectedSourceFilename)), + $manyToMany, + false + ); + + // Run Attribute Tests + if ((\PHP_VERSION_ID >= 80000)) { + $this->runAddManyToManyRelationTest( + file_get_contents(sprintf('%s/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/%s', $expectedPath, $expectedSourceFilename)), + $manyToMany, + true + ); + } + } + + private function runAddManyToManyRelationTest(string $source, string $expected, RelationManyToMany $manyToMany, bool $php8): void + { + $manipulator = new ClassSourceManipulator($source, false, !$php8, true, $php8); $manipulator->addManyToManyRelation($manyToMany); - $this->assertSame($expectedSource, $manipulator->getSourceCode()); + $this->assertSame($expected, $manipulator->getSourceCode()); } - public function getAddManyToManyRelationTests() + public function getAddManyToManyRelationTests(): \Generator { yield 'many_to_many_owning' => [ 'User_simple.php', @@ -440,18 +526,39 @@ public function getAddManyToManyRelationTests() /** * @dataProvider getAddOneToOneRelationTests */ - public function testAddOneToOneRelation(string $sourceFilename, $expectedSourceFilename, RelationOneToOne $oneToOne) + public function testAddOneToOneRelation(string $sourceFilename, $expectedSourceFilename, RelationOneToOne $oneToOne): void { - $source = file_get_contents(__DIR__.'/fixtures/source/'.$sourceFilename); - $expectedSource = file_get_contents(__DIR__.'/fixtures/add_one_to_one_relation/'.$expectedSourceFilename); + $sourcePath = __DIR__.'/fixtures/source'; + $expectedPath = __DIR__.'/fixtures/add_one_to_one_relation'; - $manipulator = new ClassSourceManipulator($source); + // Legacy Annotation Tests + $this->runAddOneToOneRelation( + file_get_contents(sprintf('%s/legacy/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/legacy/%s', $expectedPath, $expectedSourceFilename)), + $oneToOne, + false + ); + + // Run Attribute Tests + if ((\PHP_VERSION_ID >= 80000)) { + $this->runAddOneToOneRelation( + file_get_contents(sprintf('%s/%s', $sourcePath, $sourceFilename)), + file_get_contents(sprintf('%s/%s', $expectedPath, $expectedSourceFilename)), + $oneToOne, + true + ); + } + } + + private function runAddOneToOneRelation(string $source, string $expected, RelationOneToOne $oneToOne, bool $php8): void + { + $manipulator = new ClassSourceManipulator($source, false, !$php8, true, $php8); $manipulator->addOneToOneRelation($oneToOne); - $this->assertSame($expectedSource, $manipulator->getSourceCode()); + $this->assertSame($expected, $manipulator->getSourceCode()); } - public function getAddOneToOneRelationTests() + public function getAddOneToOneRelationTests(): \Generator { yield 'one_to_one_owning' => [ 'User_simple.php', @@ -565,7 +672,7 @@ public function testGenerationWithTabs() public function testAddInterface() { - $source = file_get_contents(__DIR__.'/fixtures/source/User_simple.php'); + $source = file_get_contents(__DIR__.'/fixtures/source/legacy/User_simple.php'); $expectedSource = file_get_contents(__DIR__.'/fixtures/implements_interface/User_simple.php'); $manipulator = new ClassSourceManipulator($source); @@ -587,7 +694,7 @@ public function testAddInterfaceToClassWithOtherInterface() public function testAddMethodBuilder() { - $source = file_get_contents(__DIR__.'/fixtures/source/User_empty.php'); + $source = file_get_contents(__DIR__.'/fixtures/source/legacy/User_empty.php'); $expectedSource = file_get_contents(__DIR__.'/fixtures/add_method/UserEmpty_with_newMethod.php'); $manipulator = new ClassSourceManipulator($source); @@ -765,7 +872,7 @@ class Foo public function testAddTraitInEmptyClass() { - $source = file_get_contents(__DIR__.'/fixtures/source/User_empty.php'); + $source = file_get_contents(__DIR__.'/fixtures/source/legacy/User_empty.php'); $expectedSource = file_get_contents(__DIR__.'/fixtures/add_trait/User_with_only_trait.php'); $manipulator = new ClassSourceManipulator($source); @@ -777,7 +884,7 @@ public function testAddTraitInEmptyClass() public function testAddTraitWithProperty() { - $source = file_get_contents(__DIR__.'/fixtures/source/User_simple.php'); + $source = file_get_contents(__DIR__.'/fixtures/source/legacy/User_simple.php'); $expectedSource = file_get_contents(__DIR__.'/fixtures/add_trait/User_with_prop_trait.php'); $manipulator = new ClassSourceManipulator($source); @@ -825,7 +932,7 @@ public function testAddTraitAlReadyExists() public function testAddConstructor() { - $source = file_get_contents(__DIR__.'/fixtures/source/User_empty.php'); + $source = file_get_contents(__DIR__.'/fixtures/source/legacy/User_empty.php'); $expectedSource = file_get_contents(__DIR__.'/fixtures/add_constructor/UserEmpty_with_constructor.php'); $manipulator = new ClassSourceManipulator($source); @@ -845,7 +952,7 @@ public function testAddConstructor() public function testAddConstructorInClassContainsPropsAndMethods() { - $source = file_get_contents(__DIR__.'/fixtures/source/User_simple.php'); + $source = file_get_contents(__DIR__.'/fixtures/source/legacy/User_simple.php'); $expectedSource = file_get_contents(__DIR__.'/fixtures/add_constructor/UserSimple_with_constructor.php'); $manipulator = new ClassSourceManipulator($source); diff --git a/tests/Util/fixtures/add_entity_field/User_simple.php b/tests/Util/fixtures/add_entity_field/User_simple.php index 6c528dbe0..4cbae0a0d 100644 --- a/tests/Util/fixtures/add_entity_field/User_simple.php +++ b/tests/Util/fixtures/add_entity_field/User_simple.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=false, options={"comment" = "new field"}) - */ + #[ORM\Column(type: 'string', length: 255, nullable: false, options: ['comment' => 'new field'])] private $fooProp; public function getId(): ?int diff --git a/tests/Util/fixtures/add_entity_field/User_simple_datetime.php b/tests/Util/fixtures/add_entity_field/User_simple_datetime.php index 06ca3506c..ee34b2b14 100644 --- a/tests/Util/fixtures/add_entity_field/User_simple_datetime.php +++ b/tests/Util/fixtures/add_entity_field/User_simple_datetime.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; public function getId(): ?int diff --git a/tests/Util/fixtures/add_entity_field/User_simple_prop_already_exists.php b/tests/Util/fixtures/add_entity_field/User_simple_prop_already_exists.php index d4ae050a1..71f0bba0c 100644 --- a/tests/Util/fixtures/add_entity_field/User_simple_prop_already_exists.php +++ b/tests/Util/fixtures/add_entity_field/User_simple_prop_already_exists.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column() - */ + #[ORM\Column] private $firstName; public function getId(): ?int diff --git a/tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php b/tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php index 846c75e3d..461900311 100644 --- a/tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php +++ b/tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="decimal", precision=6, scale=0) - */ + #[ORM\Column(type: 'decimal', precision: 6, scale: 0)] private $decimal; public function getId(): ?int diff --git a/tests/Util/fixtures/add_entity_field/legacy/User_simple.php b/tests/Util/fixtures/add_entity_field/legacy/User_simple.php new file mode 100644 index 000000000..6c528dbe0 --- /dev/null +++ b/tests/Util/fixtures/add_entity_field/legacy/User_simple.php @@ -0,0 +1,40 @@ +id; + } + + public function getFooProp(): ?string + { + return $this->fooProp; + } + + public function setFooProp(string $fooProp): self + { + $this->fooProp = $fooProp; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_entity_field/legacy/User_simple_datetime.php b/tests/Util/fixtures/add_entity_field/legacy/User_simple_datetime.php new file mode 100644 index 000000000..06ca3506c --- /dev/null +++ b/tests/Util/fixtures/add_entity_field/legacy/User_simple_datetime.php @@ -0,0 +1,40 @@ +id; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt): self + { + $this->createdAt = $createdAt; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_entity_field/legacy/User_simple_prop_already_exists.php b/tests/Util/fixtures/add_entity_field/legacy/User_simple_prop_already_exists.php new file mode 100644 index 000000000..d4ae050a1 --- /dev/null +++ b/tests/Util/fixtures/add_entity_field/legacy/User_simple_prop_already_exists.php @@ -0,0 +1,46 @@ +id; + } + + /** + * Some custom comments + * + * @return string + */ + public function getFirstName() + { + // some custom comment + return $this->firstName; + } + + public function setFirstName(string $firstName): self + { + $this->firstName = $firstName; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_entity_field/legacy/User_simple_prop_zero.php b/tests/Util/fixtures/add_entity_field/legacy/User_simple_prop_zero.php new file mode 100644 index 000000000..846c75e3d --- /dev/null +++ b/tests/Util/fixtures/add_entity_field/legacy/User_simple_prop_zero.php @@ -0,0 +1,40 @@ +id; + } + + public function getDecimal(): ?string + { + return $this->decimal; + } + + public function setDecimal(string $decimal): self + { + $this->decimal = $decimal; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php b/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php index fa2bc65d8..f32e27b10 100644 --- a/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php +++ b/tests/Util/fixtures/add_many_to_many_relation/User_simple_inverse.php @@ -6,21 +6,15 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToMany(targetEntity=Recipe::class, mappedBy="foods") - */ + #[ORM\ManyToMany(targetEntity: Recipe::class, mappedBy: 'foods')] private $recipes; public function __construct() diff --git a/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php b/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php index 2d1e7288d..e0eec5367 100644 --- a/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php +++ b/tests/Util/fixtures/add_many_to_many_relation/User_simple_no_inverse.php @@ -6,21 +6,15 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToMany(targetEntity=Recipe::class) - */ + #[ORM\ManyToMany(targetEntity: Recipe::class)] private $recipes; public function __construct() diff --git a/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php b/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php index e1b079156..c8d238e72 100644 --- a/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php +++ b/tests/Util/fixtures/add_many_to_many_relation/User_simple_owning.php @@ -6,21 +6,15 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToMany(targetEntity=Recipe::class, inversedBy="foods") - */ + #[ORM\ManyToMany(targetEntity: Recipe::class, inversedBy: 'foods')] private $recipes; public function __construct() diff --git a/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_inverse.php b/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_inverse.php new file mode 100644 index 000000000..fa2bc65d8 --- /dev/null +++ b/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_inverse.php @@ -0,0 +1,62 @@ +recipes = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + /** + * @return Collection|Recipe[] + */ + public function getRecipes(): Collection + { + return $this->recipes; + } + + public function addRecipe(Recipe $recipe): self + { + if (!$this->recipes->contains($recipe)) { + $this->recipes[] = $recipe; + $recipe->addFood($this); + } + + return $this; + } + + public function removeRecipe(Recipe $recipe): self + { + if ($this->recipes->removeElement($recipe)) { + $recipe->removeFood($this); + } + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_no_inverse.php b/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_no_inverse.php new file mode 100644 index 000000000..2d1e7288d --- /dev/null +++ b/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_no_inverse.php @@ -0,0 +1,59 @@ +recipes = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + /** + * @return Collection|Recipe[] + */ + public function getRecipes(): Collection + { + return $this->recipes; + } + + public function addRecipe(Recipe $recipe): self + { + if (!$this->recipes->contains($recipe)) { + $this->recipes[] = $recipe; + } + + return $this; + } + + public function removeRecipe(Recipe $recipe): self + { + $this->recipes->removeElement($recipe); + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_owning.php b/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_owning.php new file mode 100644 index 000000000..e1b079156 --- /dev/null +++ b/tests/Util/fixtures/add_many_to_many_relation/legacy/User_simple_owning.php @@ -0,0 +1,59 @@ +recipes = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + /** + * @return Collection|Recipe[] + */ + public function getRecipes(): Collection + { + return $this->recipes; + } + + public function addRecipe(Recipe $recipe): self + { + if (!$this->recipes->contains($recipe)) { + $this->recipes[] = $recipe; + } + + return $this; + } + + public function removeRecipe(Recipe $recipe): self + { + $this->recipes->removeElement($recipe); + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_one_relation/User_empty_other_namespace.php b/tests/Util/fixtures/add_many_to_one_relation/User_empty_other_namespace.php index 0a9e5b220..6bebcfa40 100644 --- a/tests/Util/fixtures/add_many_to_one_relation/User_empty_other_namespace.php +++ b/tests/Util/fixtures/add_many_to_one_relation/User_empty_other_namespace.php @@ -6,9 +6,7 @@ class User { - /** - * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="foods") - */ + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'foods')] private $category; public function getCategory(): ?Category diff --git a/tests/Util/fixtures/add_many_to_one_relation/User_simple_no_inverse.php b/tests/Util/fixtures/add_many_to_one_relation/User_simple_no_inverse.php index 8ddae8637..af5868e36 100644 --- a/tests/Util/fixtures/add_many_to_one_relation/User_simple_no_inverse.php +++ b/tests/Util/fixtures/add_many_to_one_relation/User_simple_no_inverse.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=Category::class) - */ + #[ORM\ManyToOne(targetEntity: Category::class)] private $category; public function getId(): ?int diff --git a/tests/Util/fixtures/add_many_to_one_relation/User_simple_not_nullable.php b/tests/Util/fixtures/add_many_to_one_relation/User_simple_not_nullable.php index 59de47292..15a8f58fb 100644 --- a/tests/Util/fixtures/add_many_to_one_relation/User_simple_not_nullable.php +++ b/tests/Util/fixtures/add_many_to_one_relation/User_simple_not_nullable.php @@ -4,22 +4,16 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="foods") - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'foods')] + #[ORM\JoinColumn(nullable: false)] private $category; public function getId(): ?int diff --git a/tests/Util/fixtures/add_many_to_one_relation/User_simple_nullable.php b/tests/Util/fixtures/add_many_to_one_relation/User_simple_nullable.php index 311d30afd..3fbd39a2d 100644 --- a/tests/Util/fixtures/add_many_to_one_relation/User_simple_nullable.php +++ b/tests/Util/fixtures/add_many_to_one_relation/User_simple_nullable.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="foods") - */ + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'foods')] private $category; public function getId(): ?int diff --git a/tests/Util/fixtures/add_many_to_one_relation/User_simple_other_namespace.php b/tests/Util/fixtures/add_many_to_one_relation/User_simple_other_namespace.php index 633067951..acc06ab2b 100644 --- a/tests/Util/fixtures/add_many_to_one_relation/User_simple_other_namespace.php +++ b/tests/Util/fixtures/add_many_to_one_relation/User_simple_other_namespace.php @@ -5,21 +5,15 @@ use Doctrine\ORM\Mapping as ORM; use Foo\Entity\Category; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="foods") - */ + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'foods')] private $category; public function getId(): ?int diff --git a/tests/Util/fixtures/add_many_to_one_relation/User_with_relation_same_and_other_namespaces.php b/tests/Util/fixtures/add_many_to_one_relation/User_with_relation_same_and_other_namespaces.php index 264def97f..e99a41190 100644 --- a/tests/Util/fixtures/add_many_to_one_relation/User_with_relation_same_and_other_namespaces.php +++ b/tests/Util/fixtures/add_many_to_one_relation/User_with_relation_same_and_other_namespaces.php @@ -3,17 +3,15 @@ namespace App\Entity; use App\Entity\Category; +use Doctrine\ORM\Mapping as ORM; +#[ORM\Entity] class User { - /** - * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="foods") - */ + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'foods')] private $category; - /** - * @ORM\ManyToOne(targetEntity=\App\Entity\SubDirectory\Category::class, inversedBy="foods") - */ + #[ORM\ManyToOne(targetEntity: \App\Entity\SubDirectory\Category::class, inversedBy: 'foods')] private $subCategory; public function getCategory(): ?Category diff --git a/tests/Util/fixtures/add_many_to_one_relation/legacy/User_empty_other_namespace.php b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_empty_other_namespace.php new file mode 100644 index 000000000..0a9e5b220 --- /dev/null +++ b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_empty_other_namespace.php @@ -0,0 +1,25 @@ +category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_no_inverse.php b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_no_inverse.php new file mode 100644 index 000000000..8ddae8637 --- /dev/null +++ b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_no_inverse.php @@ -0,0 +1,40 @@ +id; + } + + public function getCategory(): ?Category + { + return $this->category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_not_nullable.php b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_not_nullable.php new file mode 100644 index 000000000..59de47292 --- /dev/null +++ b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_not_nullable.php @@ -0,0 +1,41 @@ +id; + } + + public function getCategory(): ?Category + { + return $this->category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_nullable.php b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_nullable.php new file mode 100644 index 000000000..311d30afd --- /dev/null +++ b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_nullable.php @@ -0,0 +1,40 @@ +id; + } + + public function getCategory(): ?Category + { + return $this->category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_other_namespace.php b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_other_namespace.php new file mode 100644 index 000000000..633067951 --- /dev/null +++ b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_simple_other_namespace.php @@ -0,0 +1,41 @@ +id; + } + + public function getCategory(): ?Category + { + return $this->category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_many_to_one_relation/legacy/User_with_relation_same_and_other_namespaces.php b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_with_relation_same_and_other_namespaces.php new file mode 100644 index 000000000..264def97f --- /dev/null +++ b/tests/Util/fixtures/add_many_to_one_relation/legacy/User_with_relation_same_and_other_namespaces.php @@ -0,0 +1,42 @@ +category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } + + public function getSubCategory(): ?\App\Entity\SubDirectory\Category + { + return $this->subCategory; + } + + public function setSubCategory(?\App\Entity\SubDirectory\Category $subCategory): self + { + $this->subCategory = $subCategory; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_many_relation/User_simple.php b/tests/Util/fixtures/add_one_to_many_relation/User_simple.php index 19b79ca62..1896c09bb 100644 --- a/tests/Util/fixtures/add_one_to_many_relation/User_simple.php +++ b/tests/Util/fixtures/add_one_to_many_relation/User_simple.php @@ -6,21 +6,15 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToMany(targetEntity=UserAvatarPhoto::class, mappedBy="user") - */ + #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAvatarPhoto::class)] private $avatarPhotos; public function __construct() diff --git a/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php b/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php index 4f92ff0fd..861e7318c 100644 --- a/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php +++ b/tests/Util/fixtures/add_one_to_many_relation/User_simple_orphan_removal.php @@ -6,21 +6,15 @@ use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToMany(targetEntity=UserAvatarPhoto::class, mappedBy="user", orphanRemoval=true) - */ + #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAvatarPhoto::class, orphanRemoval: true)] private $avatarPhotos; public function __construct() diff --git a/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php b/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php index df6e9ed06..02ea300ec 100644 --- a/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php +++ b/tests/Util/fixtures/add_one_to_many_relation/User_with_use_statements.php @@ -8,21 +8,15 @@ use Some\Other\UserProfile; use Some\Other\FooCategory as Category; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToMany(targetEntity=UserAvatarPhoto::class, mappedBy="user") - */ + #[ORM\OneToMany(mappedBy: 'user', targetEntity: UserAvatarPhoto::class)] private $avatarPhotos; public function __construct() diff --git a/tests/Util/fixtures/add_one_to_many_relation/legacy/User_simple.php b/tests/Util/fixtures/add_one_to_many_relation/legacy/User_simple.php new file mode 100644 index 000000000..19b79ca62 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_many_relation/legacy/User_simple.php @@ -0,0 +1,65 @@ +avatarPhotos = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + /** + * @return Collection|UserAvatarPhoto[] + */ + public function getAvatarPhotos(): Collection + { + return $this->avatarPhotos; + } + + public function addAvatarPhoto(UserAvatarPhoto $avatarPhoto): self + { + if (!$this->avatarPhotos->contains($avatarPhoto)) { + $this->avatarPhotos[] = $avatarPhoto; + $avatarPhoto->setUser($this); + } + + return $this; + } + + public function removeAvatarPhoto(UserAvatarPhoto $avatarPhoto): self + { + if ($this->avatarPhotos->removeElement($avatarPhoto)) { + // set the owning side to null (unless already changed) + if ($avatarPhoto->getUser() === $this) { + $avatarPhoto->setUser(null); + } + } + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_many_relation/legacy/User_simple_orphan_removal.php b/tests/Util/fixtures/add_one_to_many_relation/legacy/User_simple_orphan_removal.php new file mode 100644 index 000000000..4f92ff0fd --- /dev/null +++ b/tests/Util/fixtures/add_one_to_many_relation/legacy/User_simple_orphan_removal.php @@ -0,0 +1,65 @@ +avatarPhotos = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + /** + * @return Collection|UserAvatarPhoto[] + */ + public function getAvatarPhotos(): Collection + { + return $this->avatarPhotos; + } + + public function addAvatarPhoto(UserAvatarPhoto $avatarPhoto): self + { + if (!$this->avatarPhotos->contains($avatarPhoto)) { + $this->avatarPhotos[] = $avatarPhoto; + $avatarPhoto->setUser($this); + } + + return $this; + } + + public function removeAvatarPhoto(UserAvatarPhoto $avatarPhoto): self + { + if ($this->avatarPhotos->removeElement($avatarPhoto)) { + // set the owning side to null (unless already changed) + if ($avatarPhoto->getUser() === $this) { + $avatarPhoto->setUser(null); + } + } + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_many_relation/legacy/User_with_use_statements.php b/tests/Util/fixtures/add_one_to_many_relation/legacy/User_with_use_statements.php new file mode 100644 index 000000000..df6e9ed06 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_many_relation/legacy/User_with_use_statements.php @@ -0,0 +1,67 @@ +avatarPhotos = new ArrayCollection(); + } + + public function getId(): ?int + { + return $this->id; + } + + /** + * @return Collection|UserAvatarPhoto[] + */ + public function getAvatarPhotos(): Collection + { + return $this->avatarPhotos; + } + + public function addAvatarPhoto(UserAvatarPhoto $avatarPhoto): self + { + if (!$this->avatarPhotos->contains($avatarPhoto)) { + $this->avatarPhotos[] = $avatarPhoto; + $avatarPhoto->setUser($this); + } + + return $this; + } + + public function removeAvatarPhoto(UserAvatarPhoto $avatarPhoto): self + { + if ($this->avatarPhotos->removeElement($avatarPhoto)) { + // set the owning side to null (unless already changed) + if ($avatarPhoto->getUser() === $this) { + $avatarPhoto->setUser(null); + } + } + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse.php b/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse.php index 6ca0ec0d6..4c6fcabe3 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse.php +++ b/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserProfile { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=User::class, mappedBy="userProfile", cascade={"persist", "remove"}) - */ + #[ORM\OneToOne(mappedBy: 'userProfile', targetEntity: User::class, cascade: ['persist', 'remove'])] private $user; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse_not_nullable.php b/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse_not_nullable.php index e52100ee3..30d3a8ecc 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse_not_nullable.php +++ b/tests/Util/fixtures/add_one_to_one_relation/UserProfile_simple_inverse_not_nullable.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserProfile { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=User::class, mappedBy="userProfile", cascade={"persist", "remove"}) - */ + #[ORM\OneToOne(mappedBy: 'userProfile', targetEntity: User::class, cascade: ['persist', 'remove'])] private $user; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse.php b/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse.php index 0f00f5bc5..063f53151 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse.php +++ b/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=UserProfile::class, cascade={"persist", "remove"}) - */ + #[ORM\OneToOne(targetEntity: UserProfile::class, cascade: ['persist', 'remove'])] private $userProfile; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse_not_nullable.php b/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse_not_nullable.php index 913d5efd6..aa76c15f6 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse_not_nullable.php +++ b/tests/Util/fixtures/add_one_to_one_relation/User_simple_no_inverse_not_nullable.php @@ -4,22 +4,16 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=UserProfile::class, cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\OneToOne(targetEntity: UserProfile::class, cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $userProfile; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/User_simple_owning.php b/tests/Util/fixtures/add_one_to_one_relation/User_simple_owning.php index f05664cc3..33a99c06e 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/User_simple_owning.php +++ b/tests/Util/fixtures/add_one_to_one_relation/User_simple_owning.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=UserProfile::class, inversedBy="user", cascade={"persist", "remove"}) - */ + #[ORM\OneToOne(inversedBy: 'user', targetEntity: UserProfile::class, cascade: ['persist', 'remove'])] private $userProfile; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/User_simple_self.php b/tests/Util/fixtures/add_one_to_one_relation/User_simple_self.php index fadc4a199..f84ea0ce7 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/User_simple_self.php +++ b/tests/Util/fixtures/add_one_to_one_relation/User_simple_self.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=User::class, inversedBy="user", cascade={"persist", "remove"}) - */ + #[ORM\OneToOne(inversedBy: 'user', targetEntity: User::class, cascade: ['persist', 'remove'])] private $embeddedUser; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use.php b/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use.php index 045ebe968..029e2ce5e 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use.php +++ b/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use.php @@ -7,21 +7,15 @@ use Some\Other\UserProfile; use Some\Other\FooCategory as Category; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=\App\OtherEntity\UserProfile::class, inversedBy="user", cascade={"persist", "remove"}) - */ + #[ORM\OneToOne(inversedBy: 'user', targetEntity: \App\OtherEntity\UserProfile::class, cascade: ['persist', 'remove'])] private $userProfile; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use_alias.php b/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use_alias.php index 4478065d7..13c8ac3fb 100644 --- a/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use_alias.php +++ b/tests/Util/fixtures/add_one_to_one_relation/User_with_use_statements_avoid_duplicate_use_alias.php @@ -7,21 +7,15 @@ use Some\Other\UserProfile; use Some\Other\FooCategory as Category; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\OneToOne(targetEntity=\App\OtherEntity\Category::class, inversedBy="user", cascade={"persist", "remove"}) - */ + #[ORM\OneToOne(inversedBy: 'user', targetEntity: \App\OtherEntity\Category::class, cascade: ['persist', 'remove'])] private $category; public function getId(): ?int diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/UserProfile_simple_inverse.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/UserProfile_simple_inverse.php new file mode 100644 index 000000000..6ca0ec0d6 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/UserProfile_simple_inverse.php @@ -0,0 +1,50 @@ +id; + } + + public function getUser(): ?User + { + return $this->user; + } + + public function setUser(?User $user): self + { + // unset the owning side of the relation if necessary + if ($user === null && $this->user !== null) { + $this->user->setUserProfile(null); + } + + // set the owning side of the relation if necessary + if ($user !== null && $user->getUserProfile() !== $this) { + $user->setUserProfile($this); + } + + $this->user = $user; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/UserProfile_simple_inverse_not_nullable.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/UserProfile_simple_inverse_not_nullable.php new file mode 100644 index 000000000..e52100ee3 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/UserProfile_simple_inverse_not_nullable.php @@ -0,0 +1,45 @@ +id; + } + + public function getUser(): ?User + { + return $this->user; + } + + public function setUser(User $user): self + { + // set the owning side of the relation if necessary + if ($user->getUserProfile() !== $this) { + $user->setUserProfile($this); + } + + $this->user = $user; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_no_inverse.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_no_inverse.php new file mode 100644 index 000000000..0f00f5bc5 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_no_inverse.php @@ -0,0 +1,40 @@ +id; + } + + public function getUserProfile(): ?UserProfile + { + return $this->userProfile; + } + + public function setUserProfile(?UserProfile $userProfile): self + { + $this->userProfile = $userProfile; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_no_inverse_not_nullable.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_no_inverse_not_nullable.php new file mode 100644 index 000000000..913d5efd6 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_no_inverse_not_nullable.php @@ -0,0 +1,41 @@ +id; + } + + public function getUserProfile(): ?UserProfile + { + return $this->userProfile; + } + + public function setUserProfile(UserProfile $userProfile): self + { + $this->userProfile = $userProfile; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_owning.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_owning.php new file mode 100644 index 000000000..f05664cc3 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_owning.php @@ -0,0 +1,40 @@ +id; + } + + public function getUserProfile(): ?UserProfile + { + return $this->userProfile; + } + + public function setUserProfile(?UserProfile $userProfile): self + { + $this->userProfile = $userProfile; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_self.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_self.php new file mode 100644 index 000000000..fadc4a199 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_simple_self.php @@ -0,0 +1,40 @@ +id; + } + + public function getEmbeddedUser(): ?self + { + return $this->embeddedUser; + } + + public function setEmbeddedUser(?self $embeddedUser): self + { + $this->embeddedUser = $embeddedUser; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/User_with_use_statements_avoid_duplicate_use.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_with_use_statements_avoid_duplicate_use.php new file mode 100644 index 000000000..045ebe968 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_with_use_statements_avoid_duplicate_use.php @@ -0,0 +1,43 @@ +id; + } + + public function getUserProfile(): ?\App\OtherEntity\UserProfile + { + return $this->userProfile; + } + + public function setUserProfile(?\App\OtherEntity\UserProfile $userProfile): self + { + $this->userProfile = $userProfile; + + return $this; + } +} diff --git a/tests/Util/fixtures/add_one_to_one_relation/legacy/User_with_use_statements_avoid_duplicate_use_alias.php b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_with_use_statements_avoid_duplicate_use_alias.php new file mode 100644 index 000000000..4478065d7 --- /dev/null +++ b/tests/Util/fixtures/add_one_to_one_relation/legacy/User_with_use_statements_avoid_duplicate_use_alias.php @@ -0,0 +1,43 @@ +id; + } + + public function getCategory(): ?\App\OtherEntity\Category + { + return $this->category; + } + + public function setCategory(?\App\OtherEntity\Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/tests/Util/fixtures/source/UserProfile_simple.php b/tests/Util/fixtures/source/UserProfile_simple.php index ed9730300..11aa39d7a 100644 --- a/tests/Util/fixtures/source/UserProfile_simple.php +++ b/tests/Util/fixtures/source/UserProfile_simple.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserProfile { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId(): ?int diff --git a/tests/Util/fixtures/source/User_simple.php b/tests/Util/fixtures/source/User_simple.php index 5274d16db..c9aaa7f05 100644 --- a/tests/Util/fixtures/source/User_simple.php +++ b/tests/Util/fixtures/source/User_simple.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId(): ?int diff --git a/tests/Util/fixtures/source/User_some_props.php b/tests/Util/fixtures/source/User_some_props.php index 7b95b36c2..2dd9ba4c5 100644 --- a/tests/Util/fixtures/source/User_some_props.php +++ b/tests/Util/fixtures/source/User_some_props.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column() - */ + #[ORM\Column] private $firstName; public function getId(): ?int diff --git a/tests/Util/fixtures/source/User_with_relation.php b/tests/Util/fixtures/source/User_with_relation.php index 23a90e648..9b1ef93b2 100644 --- a/tests/Util/fixtures/source/User_with_relation.php +++ b/tests/Util/fixtures/source/User_with_relation.php @@ -3,12 +3,12 @@ namespace App\Entity; use App\Entity\Category; +use Doctrine\ORM\Mapping as ORM; +#[ORM\Entity] class User { - /** - * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="foods") - */ + #[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'foods')] private $category; public function getCategory(): ?Category diff --git a/tests/Util/fixtures/source/User_with_use_statements.php b/tests/Util/fixtures/source/User_with_use_statements.php index 9db2b7fc9..51a4bc534 100644 --- a/tests/Util/fixtures/source/User_with_use_statements.php +++ b/tests/Util/fixtures/source/User_with_use_statements.php @@ -7,16 +7,12 @@ use Some\Other\UserProfile; use Some\Other\FooCategory as Category; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId(): ?int diff --git a/tests/Util/fixtures/source/legacy/UserProfile_simple.php b/tests/Util/fixtures/source/legacy/UserProfile_simple.php new file mode 100644 index 000000000..ed9730300 --- /dev/null +++ b/tests/Util/fixtures/source/legacy/UserProfile_simple.php @@ -0,0 +1,23 @@ +id; + } +} diff --git a/tests/Util/fixtures/source/legacy/User_empty.php b/tests/Util/fixtures/source/legacy/User_empty.php new file mode 100644 index 000000000..fe362adb5 --- /dev/null +++ b/tests/Util/fixtures/source/legacy/User_empty.php @@ -0,0 +1,5 @@ +id; + } +} diff --git a/tests/Util/fixtures/source/legacy/User_some_props.php b/tests/Util/fixtures/source/legacy/User_some_props.php new file mode 100644 index 000000000..7b95b36c2 --- /dev/null +++ b/tests/Util/fixtures/source/legacy/User_some_props.php @@ -0,0 +1,39 @@ +id; + } + + /** + * Some custom comments + * + * @return string + */ + public function getFirstName() + { + // some custom comment + return $this->firstName; + } +} diff --git a/tests/Util/fixtures/source/legacy/User_with_relation.php b/tests/Util/fixtures/source/legacy/User_with_relation.php new file mode 100644 index 000000000..23a90e648 --- /dev/null +++ b/tests/Util/fixtures/source/legacy/User_with_relation.php @@ -0,0 +1,25 @@ +category; + } + + public function setCategory(?Category $category): self + { + $this->category = $category; + + return $this; + } +} diff --git a/tests/Util/fixtures/source/legacy/User_with_use_statements.php b/tests/Util/fixtures/source/legacy/User_with_use_statements.php new file mode 100644 index 000000000..9db2b7fc9 --- /dev/null +++ b/tests/Util/fixtures/source/legacy/User_with_use_statements.php @@ -0,0 +1,26 @@ +id; + } +} diff --git a/tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php b/tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php index 0ec7ee4f6..1534b3aa1 100644 --- a/tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php +++ b/tests/fixtures/MakeEntityExistsInRoot/src/Entity/Directory.php @@ -4,26 +4,18 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Directory { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $name; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; public function getId() diff --git a/tests/fixtures/MakeEntityManyToMany/src/Entity/User.php b/tests/fixtures/MakeEntityManyToMany/src/Entity/User.php index e64f975d9..a8e96ecc1 100644 --- a/tests/fixtures/MakeEntityManyToMany/src/Entity/User.php +++ b/tests/fixtures/MakeEntityManyToMany/src/Entity/User.php @@ -4,26 +4,18 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; public function getId() diff --git a/tests/fixtures/MakeEntityManyToManyInCustomNamespace/src/Entity/User.php b/tests/fixtures/MakeEntityManyToManyInCustomNamespace/src/Entity/User.php index 2d7d1b375..f652c89c7 100644 --- a/tests/fixtures/MakeEntityManyToManyInCustomNamespace/src/Entity/User.php +++ b/tests/fixtures/MakeEntityManyToManyInCustomNamespace/src/Entity/User.php @@ -4,26 +4,18 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; public function getId() diff --git a/tests/fixtures/MakeEntityManyToOne/src/Entity/User.php b/tests/fixtures/MakeEntityManyToOne/src/Entity/User.php index e64f975d9..a8e96ecc1 100644 --- a/tests/fixtures/MakeEntityManyToOne/src/Entity/User.php +++ b/tests/fixtures/MakeEntityManyToOne/src/Entity/User.php @@ -4,26 +4,18 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; public function getId() diff --git a/tests/fixtures/MakeEntityManyToOneNoInverse/src/Entity/User.php b/tests/fixtures/MakeEntityManyToOneNoInverse/src/Entity/User.php index 773668263..0ca1ef7b2 100644 --- a/tests/fixtures/MakeEntityManyToOneNoInverse/src/Entity/User.php +++ b/tests/fixtures/MakeEntityManyToOneNoInverse/src/Entity/User.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId() diff --git a/tests/fixtures/MakeEntityOneToMany/src/Entity/UserAvatarPhoto.php b/tests/fixtures/MakeEntityOneToMany/src/Entity/UserAvatarPhoto.php index 777b486ec..2ffe37228 100644 --- a/tests/fixtures/MakeEntityOneToMany/src/Entity/UserAvatarPhoto.php +++ b/tests/fixtures/MakeEntityOneToMany/src/Entity/UserAvatarPhoto.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserAvatarPhoto { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId() diff --git a/tests/fixtures/MakeEntityOneToOne/src/Entity/User.php b/tests/fixtures/MakeEntityOneToOne/src/Entity/User.php index e64f975d9..a8e96ecc1 100644 --- a/tests/fixtures/MakeEntityOneToOne/src/Entity/User.php +++ b/tests/fixtures/MakeEntityOneToOne/src/Entity/User.php @@ -4,26 +4,18 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; public function getId() diff --git a/tests/fixtures/MakeEntityOverwrite/src/Entity/User.php b/tests/fixtures/MakeEntityOverwrite/src/Entity/User.php index e3f800e07..88d3ca233 100644 --- a/tests/fixtures/MakeEntityOverwrite/src/Entity/User.php +++ b/tests/fixtures/MakeEntityOverwrite/src/Entity/User.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId() diff --git a/tests/fixtures/MakeEntityRegenerate/src/Entity/User.php b/tests/fixtures/MakeEntityRegenerate/src/Entity/User.php index c2023fb84..3587aa775 100644 --- a/tests/fixtures/MakeEntityRegenerate/src/Entity/User.php +++ b/tests/fixtures/MakeEntityRegenerate/src/Entity/User.php @@ -4,31 +4,21 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; - /** - * @ORM\OneToMany(targetEntity=UserAvatar::class, mappedBy="user") - */ + #[ORM\OneToMany(targetEntity: UserAvatar::class, mappedBy: 'user')] private $avatars; public function getId() diff --git a/tests/fixtures/MakeEntityRegenerate/src/Entity/UserAvatar.php b/tests/fixtures/MakeEntityRegenerate/src/Entity/UserAvatar.php index a203156e9..dfac60fcc 100644 --- a/tests/fixtures/MakeEntityRegenerate/src/Entity/UserAvatar.php +++ b/tests/fixtures/MakeEntityRegenerate/src/Entity/UserAvatar.php @@ -4,22 +4,16 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class UserAvatar { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\ManyToOne(targetEntity=User::class, inversedBy="avatars", cascade={"persist", "remove"}) - * @ORM\JoinColumn(nullable=false) - */ + #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'avatars', cascade: ['persist', 'remove'])] + #[ORM\JoinColumn(nullable: false)] private $user; public function getId() diff --git a/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Food.php b/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Food.php index 92d199341..f47dc9b46 100644 --- a/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Food.php +++ b/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Food.php @@ -7,22 +7,20 @@ /** * @ORM\Entity() */ +#[ORM\Entity] class Food { - /** - * @ORM\Column(name="id", type="integer") - * @ORM\Id - * @ORM\GeneratedValue(strategy="AUTO") - */ + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'AUTO')] + #[ORM\Column(name: 'id', type: 'integer')] private $id; - /** - * @ORM\Column(name="title", type="string", length=255) - */ + #[ORM\Column(name: 'title', type: 'string', length: 255)] private $title; /** * @ORM\Embedded(class=Recipe::class) */ + #[ORM\Embedded(class: Recipe::class)] private $recipe; } diff --git a/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Recipe.php b/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Recipe.php index 7516c9a1d..1ef77adb2 100644 --- a/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Recipe.php +++ b/tests/fixtures/MakeEntityRegenerateEmbedable/src/Entity/Recipe.php @@ -4,18 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Embeddable() - */ +#[ORM\Embeddable] class Recipe { - /** - * @ORM\Column(type="string", length=255) - */ + #[ORM\Column(type: 'string', length: 255)] private $ingredients; - /** - * @ORM\Column(type="string", length=255) - */ + #[ORM\Column(type: 'string', length: 255)] private $steps; } diff --git a/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Currency.php b/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Currency.php index b23d57c86..51c6d350a 100644 --- a/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Currency.php +++ b/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Currency.php @@ -4,16 +4,13 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Embeddable() - */ +#[ORM\Embeddable] class Currency { /** * @var string - * - * @ORM\Column(name="currency", type="string") */ + #[ORM\Column(type: 'string', name: 'currency')] private $currency; public function __construct($currency = null) diff --git a/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Invoice.php b/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Invoice.php index bce27420d..4c4065b81 100644 --- a/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Invoice.php +++ b/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Invoice.php @@ -4,25 +4,17 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class Invoice { - /** - * @ORM\Column(name="id", type="integer") - * @ORM\Id - * @ORM\GeneratedValue(strategy="AUTO") - */ + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'AUTO')] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(name="title", type="string", length=255) - */ + #[ORM\Column(type: 'string', length: 255, name: 'title')] private $title; - /** - * @ORM\Embedded(class=Money::class) - */ + #[ORM\Embedded(class: Money::class)] private $total; } diff --git a/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Money.php b/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Money.php index 484c9dd8d..c9b505ff3 100644 --- a/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Money.php +++ b/tests/fixtures/MakeEntityRegenerateEmbeddableObject/src/Entity/Money.php @@ -4,23 +4,19 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Embeddable - */ +#[ORM\Embeddable] class Money { /** * @var Currency - * - * @ORM\Embedded(class=Currency::class) */ + #[ORM\Embedded(class: Currency::class)] private $currency; /** * @var int - * - * @ORM\Column(name="amount", type="integer") */ + #[ORM\Column(name: 'amount', type: 'integer')] private $amount; public function __construct($amount = null, Currency $currency = null) diff --git a/tests/fixtures/MakeEntityRegenerateOverwrite/src/Entity/User.php b/tests/fixtures/MakeEntityRegenerateOverwrite/src/Entity/User.php index 0e8eb71cb..9c9879314 100644 --- a/tests/fixtures/MakeEntityRegenerateOverwrite/src/Entity/User.php +++ b/tests/fixtures/MakeEntityRegenerateOverwrite/src/Entity/User.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; public function getId() diff --git a/tests/fixtures/MakeEntityRelationVendorTarget/src/Entity/User.php b/tests/fixtures/MakeEntityRelationVendorTarget/src/Entity/User.php index 773668263..0ca1ef7b2 100644 --- a/tests/fixtures/MakeEntityRelationVendorTarget/src/Entity/User.php +++ b/tests/fixtures/MakeEntityRelationVendorTarget/src/Entity/User.php @@ -4,16 +4,12 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; public function getId() diff --git a/tests/fixtures/MakeEntitySelfReferencing/src/Entity/User.php b/tests/fixtures/MakeEntitySelfReferencing/src/Entity/User.php index e64f975d9..a8e96ecc1 100644 --- a/tests/fixtures/MakeEntitySelfReferencing/src/Entity/User.php +++ b/tests/fixtures/MakeEntitySelfReferencing/src/Entity/User.php @@ -4,26 +4,18 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; - /** - * @ORM\Column(type="datetime", nullable=true) - */ + #[ORM\Column(type: 'datetime', nullable: true)] private $createdAt; public function getId() diff --git a/tests/fixtures/MakeEntitySubNamespaceMatchingEntity/src/Entity/Product.php b/tests/fixtures/MakeEntitySubNamespaceMatchingEntity/src/Entity/Product.php index a0a4102d0..caa008892 100644 --- a/tests/fixtures/MakeEntitySubNamespaceMatchingEntity/src/Entity/Product.php +++ b/tests/fixtures/MakeEntitySubNamespaceMatchingEntity/src/Entity/Product.php @@ -4,15 +4,11 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity - */ +#[ORM\Entity] class Product { - /** - * @ORM\Id - * @ORM\GeneratedValue(strategy="AUTO") - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue(strategy: 'AUTO')] + #[ORM\Column(type: 'integer')] private $id; } diff --git a/tests/fixtures/MakeEntityUpdate/src/Entity/User.php b/tests/fixtures/MakeEntityUpdate/src/Entity/User.php index 7c37adc2d..a615b142d 100644 --- a/tests/fixtures/MakeEntityUpdate/src/Entity/User.php +++ b/tests/fixtures/MakeEntityUpdate/src/Entity/User.php @@ -4,21 +4,15 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - */ +#[ORM\Entity] class User { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** - * @ORM\Column(type="string", length=255, nullable=true) - */ + #[ORM\Column(type: 'string', length: 255, nullable: true)] private $firstName; public function getId() diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntity/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntity/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..50922ddaa --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntity/tests/GeneratedEntityTest.php @@ -0,0 +1,35 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u') + ->execute(); + + $user = new User(); + $em->persist($user); + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + + $this->assertcount(1, $actualUser); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityExistsInRoot/src/Entity/Directory.php b/tests/fixtures/legacy/MakeEntity/MakeEntityExistsInRoot/src/Entity/Directory.php new file mode 100644 index 000000000..0ec7ee4f6 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityExistsInRoot/src/Entity/Directory.php @@ -0,0 +1,53 @@ +id; + } + + public function getName() + { + return $this->name; + } + + public function setName(?string $name) + { + $this->name = $name; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityExistsInRoot/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityExistsInRoot/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..9d31ee177 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityExistsInRoot/tests/GeneratedEntityTest.php @@ -0,0 +1,49 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\Directory u')->execute(); + + $directory = new Directory(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $directory->getChildDirectories()); + // set existing field + $directory->setName('root'); + $em->persist($directory); + + $subDir = new Directory(); + $subDir->setName('settings'); + $subDir->setParentDirectory($directory); + $em->persist($subDir); + + // set via the inverse side + $subDir2 = new Directory(); + $subDir2->setName('fixtures'); + $directory->addChildDirectory($subDir2); + $em->persist($subDir2); + + $em->flush(); + $em->refresh($directory); + + $actualDirectory = $em->getRepository(Directory::class) + ->findAll(); + + $this->assertCount(3, $actualDirectory); + $this->assertCount(2, $actualDirectory[0]->getChildDirectories()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToMany/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToMany/src/Entity/User.php new file mode 100644 index 000000000..e64f975d9 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToMany/src/Entity/User.php @@ -0,0 +1,53 @@ +id; + } + + public function getFirstName() + { + return $this->firstName; + } + + public function setFirstName(?string $firstName) + { + $this->firstName = $firstName; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToMany/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToMany/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..0b2b0aa1e --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToMany/tests/GeneratedEntityTest.php @@ -0,0 +1,61 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); + $em->createQuery('DELETE FROM App\\Entity\\Course u')->execute(); + + $user = new User(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $user->getCourses()); + $em->persist($user); + + $course = new Course(); + $course->addStudent($user); + $em->persist($course); + + // set via the inverse side + $course2 = new Course(); + $user->addCourse($course2); + $em->persist($course2); + + $course3 = new Course(); + $user->addCourse($course3); + $em->persist($course3); + + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + + $this->assertcount(1, $actualUser); + $this->assertCount(3, $actualUser[0]->getCourses()); + + // remove some! + $user->removeCourse($course3); + $course2->removeStudent($user); + $em->flush(); + $em->refresh($user); + $em->refresh($course2); + // we removed course3, and course2 removed us! + $this->assertCount(1, $user->getCourses()); + $this->assertEmpty($course2->getStudents()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/config/packages/dev/maker.yaml b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/config/packages/dev/maker.yaml new file mode 100644 index 000000000..2691e7bf5 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/config/packages/dev/maker.yaml @@ -0,0 +1,2 @@ +maker: + root_namespace: 'Custom' diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/src/Entity/User.php new file mode 100644 index 000000000..2d7d1b375 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/src/Entity/User.php @@ -0,0 +1,53 @@ +id; + } + + public function getFirstName() + { + return $this->firstName; + } + + public function setFirstName(?string $firstName) + { + $this->firstName = $firstName; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..fcc0130da --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToManyInCustomNamespace/tests/GeneratedEntityTest.php @@ -0,0 +1,64 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM Custom\\Entity\\User u')->execute(); + $em->createQuery('DELETE FROM Custom\\Entity\\Course u')->execute(); + + $user = new User(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $user->getCourses()); + $em->persist($user); + + $course = new Course(); + $course->addStudent($user); + $em->persist($course); + + // set via the inverse side + $course2 = new Course(); + $user->addCourse($course2); + $em->persist($course2); + + $course3 = new Course(); + $user->addCourse($course3); + $em->persist($course3); + + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + + $this->assertcount(1, $actualUser); + $this->assertCount(3, $actualUser[0]->getCourses()); + + // remove some! + $user->removeCourse($course3); + $course2->removeStudent($user); + $em->flush(); + $em->refresh($user); + $em->refresh($course2); + // we removed course3, and course2 removed us! + $this->assertCount(1, $user->getCourses()); + $this->assertEmpty($course2->getStudents()); + + // check repository namespace + $this->assertStringStartsWith('Custom\\', \get_class($em->getRepository(Course::class))); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOne/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOne/src/Entity/User.php new file mode 100644 index 000000000..e64f975d9 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOne/src/Entity/User.php @@ -0,0 +1,53 @@ +id; + } + + public function getFirstName() + { + return $this->firstName; + } + + public function setFirstName(?string $firstName) + { + $this->firstName = $firstName; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOne/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOne/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..0f4ad20f4 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOne/tests/GeneratedEntityTest.php @@ -0,0 +1,49 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); + $em->createQuery('DELETE FROM App\\Entity\\UserAvatarPhoto u')->execute(); + + $user = new User(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $user->getUserAvatarPhotos()); + // set existing field + $user->setFirstName('Ryan'); + $em->persist($user); + + $photo = new UserAvatarPhoto(); + $photo->setUser($user); + $em->persist($photo); + + // set via the inverse side + $photo2 = new UserAvatarPhoto(); + $user->addUserAvatarPhoto($photo2); + $em->persist($photo2); + + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + + $this->assertcount(1, $actualUser); + $this->assertCount(2, $actualUser[0]->getUserAvatarPhotos()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOneNoInverse/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOneNoInverse/src/Entity/User.php new file mode 100644 index 000000000..773668263 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOneNoInverse/src/Entity/User.php @@ -0,0 +1,23 @@ +id; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOneNoInverse/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOneNoInverse/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..af5e1f900 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityManyToOneNoInverse/tests/GeneratedEntityTest.php @@ -0,0 +1,35 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); + $em->createQuery('DELETE FROM App\\Entity\\UserAvatarPhoto u')->execute(); + + $user = new User(); + $em->persist($user); + + $photo = new UserAvatarPhoto(); + $photo->setUser($user); + $em->persist($photo); + + $em->flush(); + $em->refresh($photo); + + $this->assertSame($photo->getUser(), $user); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityOneToMany/src/Entity/UserAvatarPhoto.php b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToMany/src/Entity/UserAvatarPhoto.php new file mode 100644 index 000000000..777b486ec --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToMany/src/Entity/UserAvatarPhoto.php @@ -0,0 +1,23 @@ +id; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityOneToMany/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToMany/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..80211a65f --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToMany/tests/GeneratedEntityTest.php @@ -0,0 +1,64 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); + $em->createQuery('DELETE FROM App\\Entity\\UserAvatarPhoto u')->execute(); + + $user = new User(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $user->getPhotos()); + $em->persist($user); + + $photo = new UserAvatarPhoto(); + $photo->setUser($user); + $em->persist($photo); + + // set via the inverse side + $photo2 = new UserAvatarPhoto(); + $user->addPhoto($photo2); + $em->persist($photo2); + + $photo3 = new UserAvatarPhoto(); + $user->addPhoto($photo3); + $em->persist($photo3); + + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + + $this->assertcount(1, $actualUser); + $this->assertCount(3, $actualUser[0]->getPhotos()); + + // remove some photos! + $user->removePhoto($photo3); + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + $this->assertCount(2, $actualUser[0]->getPhotos()); + $allUserPhotos = $em->getRepository(UserAvatarPhoto::class) + ->findAll(); + // thanks to orphanRemoval, photo3 should be fully deleted + $this->assertCount(2, $allUserPhotos); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityOneToOne/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToOne/src/Entity/User.php new file mode 100644 index 000000000..e64f975d9 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToOne/src/Entity/User.php @@ -0,0 +1,53 @@ +id; + } + + public function getFirstName() + { + return $this->firstName; + } + + public function setFirstName(?string $firstName) + { + $this->firstName = $firstName; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityOneToOne/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToOne/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..6f544e1bb --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityOneToOne/tests/GeneratedEntityTest.php @@ -0,0 +1,47 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); + $em->createQuery('DELETE FROM App\\Entity\\UserProfile u')->execute(); + + $user = new User(); + // set existing field + $user->setFirstName('Ryan'); + $em->persist($user); + + $profile = new UserProfile(); + // set inverse side - will set owning + $user->setUserProfile($profile); + // purposely don't persist: cascade should be set + // $em->persist($profile); + + $em->flush(); + $em->refresh($user); + $em->refresh($profile); + + $this->assertSame($profile, $user->getUserProfile()); + $this->assertSame($user, $profile->getUser()); + + $em->remove($user); + // don't remove the profile, rely on cascade + $em->flush(); + + $this->assertEmpty($em->getRepository(UserProfile::class)->findAll()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityOverwrite/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityOverwrite/src/Entity/User.php new file mode 100644 index 000000000..e3f800e07 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityOverwrite/src/Entity/User.php @@ -0,0 +1,28 @@ +id; + } + + public function setFirstName() + { + throw new \Exception('This does not work!'); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityOverwrite/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityOverwrite/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..8873b3a8f --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityOverwrite/tests/GeneratedEntityTest.php @@ -0,0 +1,17 @@ +setFirstName('Ryan'); + $this->assertSame('Ryan', $user->getFirstName()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/src/Entity/User.php new file mode 100644 index 000000000..c2023fb84 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/src/Entity/User.php @@ -0,0 +1,48 @@ +id; + } + + public function getFirstName() + { + return $this->firstName; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/src/Entity/UserAvatar.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/src/Entity/UserAvatar.php new file mode 100644 index 000000000..a203156e9 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/src/Entity/UserAvatar.php @@ -0,0 +1,29 @@ +id; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..2d0969990 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerate/tests/GeneratedEntityTest.php @@ -0,0 +1,50 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); + $em->createQuery('DELETE FROM App\\Entity\\UserAvatar u')->execute(); + + $user = new User(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $user->getAvatars()); + // fields should now have setters + $user->setFirstName('Ryan'); + $user->setCreatedAt(new \DateTime()); + $em->persist($user); + + $photo = new UserAvatar(); + $photo->setUser($user); + $em->persist($photo); + + // set via the inverse side + $photo2 = new UserAvatar(); + $user->addAvatar($photo2); + $em->persist($photo2); + + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + + $this->assertcount(1, $actualUser); + $this->assertCount(2, $actualUser[0]->getAvatars()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbedable/src/Entity/Food.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbedable/src/Entity/Food.php new file mode 100644 index 000000000..92d199341 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbedable/src/Entity/Food.php @@ -0,0 +1,28 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\Food f')->execute(); + + $food = new Food(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(Recipe::class, $food->getRecipe()); + // fields should now have setters + $food->setTitle('Borscht'); + + $recipe = new Recipe(); + $recipe->setIngredients('ingridients'); + $recipe->setSteps('steps'); + $food->setRecipe($recipe); + + $em->persist($food); + + $em->flush(); + $em->refresh($food); + + /** @var Food[] $actualFood */ + $actualFood = $em->getRepository(Food::class) + ->findAll(); + + $this->assertcount(1, $actualFood); + + /** @var Recipe $actualRecipe */ + $actualRecipe = $actualFood[0]->getRecipe(); + + $this->assertInstanceOf(Recipe::class, $actualRecipe); + $this->assertEquals('ingridients', $actualRecipe->getIngredients()); + $this->assertEquals('steps', $actualRecipe->getSteps()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/src/Entity/Currency.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/src/Entity/Currency.php new file mode 100644 index 000000000..b23d57c86 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/src/Entity/Currency.php @@ -0,0 +1,43 @@ +currency = $currency; + } + + /** + * @return string + */ + public function getCurrency() + { + return $this->currency; + } + + /** + * @param string $currency + * + * @return Currency + */ + public function setCurrency($currency) + { + $this->currency = $currency; + + return $this; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/src/Entity/Invoice.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/src/Entity/Invoice.php new file mode 100644 index 000000000..bce27420d --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/src/Entity/Invoice.php @@ -0,0 +1,28 @@ +amount = $amount; + $this->currency = $currency ?? new Currency(); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..eb6790735 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateEmbeddableObject/tests/GeneratedEntityTest.php @@ -0,0 +1,48 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\Invoice i')->execute(); + + $invoice = new Invoice(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(Money::class, $invoice->getTotal()); + // fields should now have setters + $invoice->setTitle('Borscht'); + + $total = new Money(100, new Currency('EUR')); + $invoice->setTotal($total); + + $em->persist($invoice); + + $em->flush(); + $em->refresh($invoice); + + /** @var Invoice[] $actualInvoice */ + $actualInvoice = $em->getRepository(Invoice::class) + ->findAll(); + + $this->assertcount(1, $actualInvoice); + + /** @var Money $actualTotal */ + $actualTotal = $actualInvoice[0]->getTotal(); + + $this->assertInstanceOf(Money::class, $actualTotal); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateOverwrite/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateOverwrite/src/Entity/User.php new file mode 100644 index 000000000..0e8eb71cb --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateOverwrite/src/Entity/User.php @@ -0,0 +1,33 @@ +id; + } + + public function setFirstName() + { + throw new \Exception('This does not work!'); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateOverwrite/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateOverwrite/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..8873b3a8f --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRegenerateOverwrite/tests/GeneratedEntityTest.php @@ -0,0 +1,17 @@ +setFirstName('Ryan'); + $this->assertSame('Ryan', $user->getFirstName()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget/src/Entity/User.php new file mode 100644 index 000000000..773668263 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget/src/Entity/User.php @@ -0,0 +1,23 @@ +id; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget/vendor/some-vendor/src/Group.php b/tests/fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget/vendor/some-vendor/src/Group.php new file mode 100644 index 000000000..f38678a23 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityRelationVendorTarget/vendor/some-vendor/src/Group.php @@ -0,0 +1,8 @@ +id; + } + + public function getFirstName() + { + return $this->firstName; + } + + public function setFirstName(?string $firstName) + { + $this->firstName = $firstName; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntitySelfReferencing/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntitySelfReferencing/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..096be0c5f --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntitySelfReferencing/tests/GeneratedEntityTest.php @@ -0,0 +1,49 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); + + $user = new User(); + // check that the constructor was instantiated properly + $this->assertInstanceOf(ArrayCollection::class, $user->getDependants()); + // set existing field + $user->setFirstName('Ryan'); + $em->persist($user); + + $ward = new User(); + $ward->setFirstName('Tim'); + $ward->setGuardian($user); + $em->persist($ward); + + // set via the inverse side + $ward2 = new User(); + $ward2->setFirstName('Fabien'); + $user->addDependant($ward2); + $em->persist($ward2); + + $em->flush(); + $em->refresh($user); + + $actualUser = $em->getRepository(User::class) + ->findAll(); + + $this->assertCount(3, $actualUser); + $this->assertCount(2, $actualUser[0]->getDependants()); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntitySubNamespaceMatchingEntity/src/Entity/Product.php b/tests/fixtures/legacy/MakeEntity/MakeEntitySubNamespaceMatchingEntity/src/Entity/Product.php new file mode 100644 index 000000000..a0a4102d0 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntitySubNamespaceMatchingEntity/src/Entity/Product.php @@ -0,0 +1,18 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\Product\\Category c') + ->execute(); + + $category = new Category(); + $em->persist($category); + $em->flush(); + $em->refresh($category); + + $actualCategories = $em->getRepository(Category::class) + ->findAll(); + + $this->assertcount(1, $actualCategories); + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityUpdate/src/Entity/User.php b/tests/fixtures/legacy/MakeEntity/MakeEntityUpdate/src/Entity/User.php new file mode 100644 index 000000000..7c37adc2d --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityUpdate/src/Entity/User.php @@ -0,0 +1,48 @@ +id; + } + + public function getFirstName(): string + { + return $this->firstName; + } + + public function setFirstName(?string $firstName) + { + $this->firstName = $firstName; + } + + public function getCreatedAt(): ?\DateTimeInterface + { + return $this->createdAt; + } + + public function setCreatedAt(?\DateTimeInterface $createdAt) + { + $this->createdAt = $createdAt; + } +} diff --git a/tests/fixtures/legacy/MakeEntity/MakeEntityUpdate/tests/GeneratedEntityTest.php b/tests/fixtures/legacy/MakeEntity/MakeEntityUpdate/tests/GeneratedEntityTest.php new file mode 100644 index 000000000..a3db17da7 --- /dev/null +++ b/tests/fixtures/legacy/MakeEntity/MakeEntityUpdate/tests/GeneratedEntityTest.php @@ -0,0 +1,38 @@ +getContainer() + ->get('doctrine') + ->getManager(); + + $em->createQuery('DELETE FROM App\\Entity\\User f') + ->execute(); + + $food = new User(); + // set existing field + $food->setFirstName('Mr. Chocolate'); + // set the new, generated field + $food->setLastName('Cake'); + $em->persist($food); + $em->flush(); + + $actualFood = $em->getRepository(User::class) + ->findAll(); + + $this->assertcount(1, $actualFood); + } +}