Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
CONFIGURATION="--configuration phpunit-dama-doctrine.xml.dist"
fi

vendor/bin/simple-phpunit -v ${CONFIGURATION}
vendor/bin/simple-phpunit ${CONFIGURATION}
env:
USE_ORM: ${{ matrix.use-orm }}
USE_ODM: ${{ matrix.use-odm }}
Expand Down
6 changes: 5 additions & 1 deletion bin/doctrine
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ use Doctrine\ORM\ORMSetup;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;

$ORMconfig = ORMSetup::createAnnotationMetadataConfiguration(['/app/tests/Fixtures/Entity'], true);
$entities = ['/app/tests/Fixtures/Entity'];
if (PHP_VERSION_ID >= 80100) {
$entities[] = '/app/tests/Fixtures/PHP81';
}
$ORMconfig = ORMSetup::createAnnotationMetadataConfiguration($entities, true);
$entityManager = EntityManager::create(['memory' => true, 'url' => getenv('DATABASE_URL')], $ORMconfig);

ConsoleRunner::run(new SingleManagerProvider($entityManager));
6 changes: 5 additions & 1 deletion config/cli-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMSetup;

$ORMconfig = ORMSetup::createAnnotationMetadataConfiguration(['/app/tests/Fixtures/Entity'], true);
$entities = ['/app/tests/Fixtures/Entity'];
if (PHP_VERSION_ID >= 80100) {
$entities[] = '/app/tests/Fixtures/PHP81';
}
$ORMconfig = ORMSetup::createAnnotationMetadataConfiguration($entities, true);
$entityManager = EntityManager::create(['memory' => true, 'url' => getenv('DATABASE_URL')], $ORMconfig);

return DependencyFactory::fromEntityManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
Expand All @@ -39,7 +40,7 @@ protected function addDefaultValueUsingFactory(SymfonyStyle $io, MakeFactoryData

$makeFactoryData->addUse($factoryClass);

$factoryShortName = \mb_substr($factoryClass, \mb_strrpos($factoryClass, '\\') + 1);
$factoryShortName = Str::getShortClassName($factoryClass);
$makeFactoryData->addDefaultProperty(\lcfirst($fieldName), "{$factoryShortName}::new(),");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public function __invoke(SymfonyStyle $io, MakeFactoryData $makeFactoryData, Mak
}

$type = \mb_strtoupper($property['type']);
if (isset($property['enumType'])) {
$makeFactoryData->addEnumDefaultProperty($fieldName, $property['enumType']);

continue;
}

$value = "null, // TODO add {$type} type manually";
$length = $property['length'] ?? '';

Expand Down
19 changes: 19 additions & 0 deletions src/Bundle/Maker/Factory/MakeFactoryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,23 @@ public function getMethodsPHPDoc(): array

return $methodsInPHPDoc;
}

public function addEnumDefaultProperty(string $propertyName, string $enumClass): void
{
if (PHP_VERSION_ID < 80100) {
throw new \LogicException('Cannot add enum for php version inferior than 8.1');
}

if (!enum_exists($enumClass)) {
throw new \InvalidArgumentException("Enum of class \"$enumClass\" does not exist.");
}

$this->addUse($enumClass);

$enumShortClassName = Str::getShortClassName($enumClass);
$this->addDefaultProperty(
$propertyName,
"self::faker()->randomElement({$enumShortClassName}::cases()),"
);
}
}
6 changes: 6 additions & 0 deletions src/Bundle/Maker/Factory/ObjectDefaultPropertiesGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public function __invoke(SymfonyStyle $io, MakeFactoryData $makeFactoryData, Mak

$value = \sprintf('null, // TODO add %svalue manually', $type ? "{$type} " : '');

if (PHP_VERSION_ID >= 80100 && enum_exists($type ?? '')) {
$makeFactoryData->addEnumDefaultProperty($property->getName(), $type);

continue;
}

if (\array_key_exists($type ?? '', self::DEFAULTS_FOR_NOT_PERSISTED)) {
$value = self::DEFAULTS_FOR_NOT_PERSISTED[$type];
}
Expand Down
3 changes: 2 additions & 1 deletion src/ChainManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\Mapping\MappingException;
use Doctrine\ORM\Mapping\MappingException as ORMMappingException;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Persistence\ObjectRepository;

Expand All @@ -31,7 +32,7 @@ public function getRepository($persistentObject, $persistentManagerName = null):
foreach ($this->managerRegistries as $managerRegistry) {
try {
return $managerRegistry->getRepository($persistentObject, $persistentManagerName);
} catch (MappingException) {
} catch (MappingException|ORMMappingException) {
// the class is not managed by the current manager
}
}
Expand Down
60 changes: 42 additions & 18 deletions tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,34 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
]);

if ($this->enableDoctrine && \getenv('USE_ORM')) {
$mappings = [
'Test' => [
'is_bundle' => false,
'type' => 'annotation',
'dir' => '%kernel.project_dir%/tests/Fixtures/Entity',
'prefix' => 'Zenstruck\Foundry\Tests\Fixtures\Entity',
'alias' => 'Test',
],
];

if (PHP_VERSION_ID >= 80100) {
$mappings['Test8.1'] = [
'is_bundle' => false,
'type' => 'annotation',
'dir' => '%kernel.project_dir%/tests/Fixtures/PHP81',
'prefix' => 'Zenstruck\Foundry\Tests\Fixtures\PHP81',
'alias' => 'TestPHP81',
];
}

$c->loadFromExtension(
'doctrine',
[
'dbal' => ['url' => '%env(resolve:DATABASE_URL)%'],
'orm' => [
'auto_generate_proxy_classes' => true,
'auto_mapping' => true,
'mappings' => [
'Test' => [
'is_bundle' => false,
'type' => 'annotation',
'dir' => '%kernel.project_dir%/tests/Fixtures/Entity',
'prefix' => 'Zenstruck\Foundry\Tests\Fixtures\Entity',
'alias' => 'Test',
],
],
'mappings' => $mappings
],
]
);
Expand Down Expand Up @@ -162,6 +174,26 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
}

if ($this->enableDoctrine && \getenv('USE_ODM')) {
$mappings = [
'Test' => [
'is_bundle' => false,
'type' => 'annotation',
'dir' => '%kernel.project_dir%/tests/Fixtures/Document',
'prefix' => 'Zenstruck\Foundry\Tests\Fixtures\Document',
'alias' => 'Test',
],
];

if (PHP_VERSION_ID >= 80100) {
$mappings['Test8.1'] = [
'is_bundle' => false,
'type' => 'annotation',
'dir' => '%kernel.project_dir%/tests/Fixtures/PHP81',
'prefix' => 'Zenstruck\Foundry\Tests\Fixtures\PHP81',
'alias' => 'TestPHP81',
];
}

$c->loadFromExtension('doctrine_mongodb', [
'connections' => [
'default' => ['server' => '%env(resolve:MONGO_URL)%'],
Expand All @@ -170,15 +202,7 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
'document_managers' => [
'default' => [
'auto_mapping' => true,
'mappings' => [
'Test' => [
'is_bundle' => false,
'type' => 'annotation',
'dir' => '%kernel.project_dir%/tests/Fixtures/Document',
'prefix' => 'Zenstruck\Foundry\Tests\Fixtures\Document',
'alias' => 'Test',
],
],
'mappings' => $mappings,
],
],
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Factory;

use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\DocumentWithEnum;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\SomeEnum;

/**
* @extends ModelFactory<DocumentWithEnum>
*
* @method DocumentWithEnum|Proxy create(array|callable $attributes = [])
* @method static DocumentWithEnum|Proxy createOne(array $attributes = [])
* @method static DocumentWithEnum|Proxy find(object|array|mixed $criteria)
* @method static DocumentWithEnum|Proxy findOrCreate(array $attributes)
* @method static DocumentWithEnum|Proxy first(string $sortedField = 'id')
* @method static DocumentWithEnum|Proxy last(string $sortedField = 'id')
* @method static DocumentWithEnum|Proxy random(array $attributes = [])
* @method static DocumentWithEnum|Proxy randomOrCreate(array $attributes = [])
* @method static DocumentWithEnum[]|Proxy[] all()
* @method static DocumentWithEnum[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static DocumentWithEnum[]|Proxy[] createSequence(array|callable $sequence)
* @method static DocumentWithEnum[]|Proxy[] findBy(array $attributes)
* @method static DocumentWithEnum[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static DocumentWithEnum[]|Proxy[] randomSet(int $number, array $attributes = [])
*/
final class DocumentWithEnumFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'enum' => self::faker()->randomElement(SomeEnum::cases()),
];
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(DocumentWithEnum $documentWithEnum): void {})
;
}

protected static function getClass(): string
{
return DocumentWithEnum::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Factory;

use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\EntityWithEnum;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\SomeEnum;

/**
* @extends ModelFactory<EntityWithEnum>
*
* @method EntityWithEnum|Proxy create(array|callable $attributes = [])
* @method static EntityWithEnum|Proxy createOne(array $attributes = [])
* @method static EntityWithEnum|Proxy find(object|array|mixed $criteria)
* @method static EntityWithEnum|Proxy findOrCreate(array $attributes)
* @method static EntityWithEnum|Proxy first(string $sortedField = 'id')
* @method static EntityWithEnum|Proxy last(string $sortedField = 'id')
* @method static EntityWithEnum|Proxy random(array $attributes = [])
* @method static EntityWithEnum|Proxy randomOrCreate(array $attributes = [])
* @method static EntityWithEnum[]|Proxy[] all()
* @method static EntityWithEnum[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static EntityWithEnum[]|Proxy[] createSequence(array|callable $sequence)
* @method static EntityWithEnum[]|Proxy[] findBy(array $attributes)
* @method static EntityWithEnum[]|Proxy[] randomRange(int $min, int $max, array $attributes = [])
* @method static EntityWithEnum[]|Proxy[] randomSet(int $number, array $attributes = [])
*/
final class EntityWithEnumFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'enum' => self::faker()->randomElement(SomeEnum::cases()),
];
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
// ->afterInstantiate(function(EntityWithEnum $entityWithEnum): void {})
;
}

protected static function getClass(): string
{
return EntityWithEnum::class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Factory;

use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Proxy;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\EntityWithEnum;
use Zenstruck\Foundry\Tests\Fixtures\PHP81\SomeEnum;

/**
* @extends ModelFactory<EntityWithEnum>
*
* @method EntityWithEnum|Proxy create(array|callable $attributes = [])
* @method static EntityWithEnum|Proxy createOne(array $attributes = [])
* @method static EntityWithEnum[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static EntityWithEnum[]|Proxy[] createSequence(array|callable $sequence)
*/
final class EntityWithEnumFactory extends ModelFactory
{
/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services
*
* @todo inject services if required
*/
public function __construct()
{
parent::__construct();
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories
*
* @todo add your default values here
*/
protected function getDefaults(): array
{
return [
'enum' => self::faker()->randomElement(SomeEnum::cases()),
];
}

/**
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization
*/
protected function initialize(): self
{
return $this
->withoutPersisting()
// ->afterInstantiate(function(EntityWithEnum $entityWithEnum): void {})
;
}

protected static function getClass(): string
{
return EntityWithEnum::class;
}
}
Loading