-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(make:factory): use factories to default non-nullable relationships
Co-authored-by: Benjamin Knecht <benblub@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
249 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Zenstruck\Foundry\Tests\Fixtures\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Brand; | ||
|
||
/** | ||
* @ORM\Entity | ||
* @ORM\Table(name="entity_with_relations") | ||
*/ | ||
class EntityWithRelations | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @ORM\OneToOne(targetEntity=Category::class, cascade={"persist", "remove"}) | ||
* @ORM\JoinColumn(nullable=false) | ||
*/ | ||
private $oneToOne; | ||
|
||
/** | ||
* @ORM\OneToOne(targetEntity=Category::class, cascade={"persist", "remove"}) | ||
* @ORM\JoinColumn(nullable=true) | ||
*/ | ||
private $oneToOneNullable; | ||
|
||
/** | ||
* @ORM\ManyToOne(targetEntity=Category::class) | ||
* @ORM\JoinColumn(nullable=false) | ||
*/ | ||
private $manyToOne; | ||
|
||
/** | ||
* @ORM\ManyToOne(targetEntity=Category::class) | ||
* @ORM\JoinColumn(nullable=true) | ||
*/ | ||
private $manyToOneNullable; | ||
|
||
/** | ||
* @ORM\ManyToOne(targetEntity=Category::class) | ||
*/ | ||
private $manyToOneNullableDefault; | ||
|
||
/** | ||
* @ORM\ManyToMany(targetEntity=Category::class) | ||
*/ | ||
private $manyToMany; | ||
|
||
/** | ||
* @ORM\ManyToOne(targetEntity=Brand::class) | ||
* @ORM\JoinColumn(nullable=false) | ||
*/ | ||
private $manyToOneWithNotExistingFactory; | ||
} |
67 changes: 67 additions & 0 deletions
67
tests/Fixtures/Maker/expected/can_create_factory_with_relation_defaults.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace App\Factory; | ||
|
||
use Zenstruck\Foundry\ModelFactory; | ||
use Zenstruck\Foundry\Proxy; | ||
use Zenstruck\Foundry\Tests\Fixtures\Entity\EntityWithRelations; | ||
|
||
/** | ||
* @extends ModelFactory<EntityWithRelations> | ||
* | ||
* @method EntityWithRelations|Proxy create(array|callable $attributes = []) | ||
* @method static EntityWithRelations|Proxy createOne(array $attributes = []) | ||
* @method static EntityWithRelations|Proxy find(object|array|mixed $criteria) | ||
* @method static EntityWithRelations|Proxy findOrCreate(array $attributes) | ||
* @method static EntityWithRelations|Proxy first(string $sortedField = 'id') | ||
* @method static EntityWithRelations|Proxy last(string $sortedField = 'id') | ||
* @method static EntityWithRelations|Proxy random(array $attributes = []) | ||
* @method static EntityWithRelations|Proxy randomOrCreate(array $attributes = []) | ||
* @method static EntityWithRelations[]|Proxy[] all() | ||
* @method static EntityWithRelations[]|Proxy[] createMany(int $number, array|callable $attributes = []) | ||
* @method static EntityWithRelations[]|Proxy[] createSequence(array|callable $sequence) | ||
* @method static EntityWithRelations[]|Proxy[] findBy(array $attributes) | ||
* @method static EntityWithRelations[]|Proxy[] randomRange(int $min, int $max, array $attributes = []) | ||
* @method static EntityWithRelations[]|Proxy[] randomSet(int $number, array $attributes = []) | ||
*/ | ||
final class EntityWithRelationsFactory 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 [ | ||
'manyToOne' => \Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory::new(), | ||
'manyToOneWithNotExistingFactory' => null, // TODO add Zenstruck\Foundry\Tests\Fixtures\Entity\Cascade\Brand ORM type manually | ||
'oneToOne' => \Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory::new(), | ||
]; | ||
} | ||
|
||
/** | ||
* @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization | ||
*/ | ||
protected function initialize(): self | ||
{ | ||
return $this | ||
// ->afterInstantiate(function(EntityWithRelations $entityWithRelations): void {}) | ||
; | ||
} | ||
|
||
protected static function getClass(): string | ||
{ | ||
return EntityWithRelations::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Fixtures\Migrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version20221117081744 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Seventh migration.'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
// this up() migration is auto-generated, please modify it to your needs | ||
$this->addSql('CREATE TABLE entity_with_relations (id INT AUTO_INCREMENT NOT NULL, oneToOne_id INT NOT NULL, oneToOneNullable_id INT DEFAULT NULL, manyToOne_id INT NOT NULL, manyToOneNullable_id INT DEFAULT NULL, manyToOneNullableDefault_id INT DEFAULT NULL, manyToOneWithNotExistingFactory_id INT NOT NULL, UNIQUE INDEX UNIQ_A9C9EC969017888C (oneToOne_id), UNIQUE INDEX UNIQ_A9C9EC96DA2BFB84 (oneToOneNullable_id), INDEX IDX_A9C9EC962E3A088A (manyToOne_id), INDEX IDX_A9C9EC968097B86C (manyToOneNullable_id), INDEX IDX_A9C9EC968572C13C (manyToOneNullableDefault_id), INDEX IDX_A9C9EC96FF92FDCA (manyToOneWithNotExistingFactory_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'); | ||
$this->addSql('CREATE TABLE entitywithrelations_category (entitywithrelations_id INT NOT NULL, category_id INT NOT NULL, INDEX IDX_CD6EBFAB337AA4F7 (entitywithrelations_id), INDEX IDX_CD6EBFAB12469DE2 (category_id), PRIMARY KEY(entitywithrelations_id, category_id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'); | ||
$this->addSql('ALTER TABLE entity_with_relations ADD CONSTRAINT FK_A9C9EC969017888C FOREIGN KEY (oneToOne_id) REFERENCES categories (id)'); | ||
$this->addSql('ALTER TABLE entity_with_relations ADD CONSTRAINT FK_A9C9EC96DA2BFB84 FOREIGN KEY (oneToOneNullable_id) REFERENCES categories (id)'); | ||
$this->addSql('ALTER TABLE entity_with_relations ADD CONSTRAINT FK_A9C9EC962E3A088A FOREIGN KEY (manyToOne_id) REFERENCES categories (id)'); | ||
$this->addSql('ALTER TABLE entity_with_relations ADD CONSTRAINT FK_A9C9EC968097B86C FOREIGN KEY (manyToOneNullable_id) REFERENCES categories (id)'); | ||
$this->addSql('ALTER TABLE entity_with_relations ADD CONSTRAINT FK_A9C9EC968572C13C FOREIGN KEY (manyToOneNullableDefault_id) REFERENCES categories (id)'); | ||
$this->addSql('ALTER TABLE entity_with_relations ADD CONSTRAINT FK_A9C9EC96FF92FDCA FOREIGN KEY (manyToOneWithNotExistingFactory_id) REFERENCES brand_cascade (id)'); | ||
$this->addSql('ALTER TABLE entitywithrelations_category ADD CONSTRAINT FK_CD6EBFAB337AA4F7 FOREIGN KEY (entitywithrelations_id) REFERENCES entity_with_relations (id) ON DELETE CASCADE'); | ||
$this->addSql('ALTER TABLE entitywithrelations_category ADD CONSTRAINT FK_CD6EBFAB12469DE2 FOREIGN KEY (category_id) REFERENCES categories (id) ON DELETE CASCADE'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
// this down() migration is auto-generated, please modify it to your needs | ||
$this->addSql('ALTER TABLE entity_with_relations DROP FOREIGN KEY FK_A9C9EC969017888C'); | ||
$this->addSql('ALTER TABLE entity_with_relations DROP FOREIGN KEY FK_A9C9EC96DA2BFB84'); | ||
$this->addSql('ALTER TABLE entity_with_relations DROP FOREIGN KEY FK_A9C9EC962E3A088A'); | ||
$this->addSql('ALTER TABLE entity_with_relations DROP FOREIGN KEY FK_A9C9EC968097B86C'); | ||
$this->addSql('ALTER TABLE entity_with_relations DROP FOREIGN KEY FK_A9C9EC968572C13C'); | ||
$this->addSql('ALTER TABLE entity_with_relations DROP FOREIGN KEY FK_A9C9EC96FF92FDCA'); | ||
$this->addSql('ALTER TABLE entitywithrelations_category DROP FOREIGN KEY FK_CD6EBFAB337AA4F7'); | ||
$this->addSql('ALTER TABLE entitywithrelations_category DROP FOREIGN KEY FK_CD6EBFAB12469DE2'); | ||
$this->addSql('DROP TABLE entity_with_relations'); | ||
$this->addSql('DROP TABLE entitywithrelations_category'); | ||
} | ||
|
||
public function isTransactional(): bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters