Skip to content

Commit 98eca98

Browse files
authored
tests: can create object with fields name different from construct (#466)
1 parent 2612d04 commit 98eca98

File tree

7 files changed

+88
-114
lines changed

7 files changed

+88
-114
lines changed

docker/Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ RUN set -eux ; \
3232
pecl clear-cache ; \
3333
apt-get remove -y $PHPIZE_DEPS zlib1g-dev libcurl4-openssl-dev pkg-config libssl-dev ; \
3434
rm -rf /var/lib/apt/lists/* ; \
35-
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false ; \
36-
composer global require --no-progress --no-scripts --no-plugins symfony/flex ; \
37-
composer global config --no-plugins allow-plugins.symfony/flex true
35+
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false
3836

3937
LABEL org.opencontainers.image.source="https://github.com/zenstruck/foundry"
4038

@@ -58,4 +56,7 @@ RUN addgroup --system --gid ${UID} ${USER} ; \
5856

5957
USER ${USER}
6058

59+
RUN composer global require --no-progress --no-scripts --no-plugins symfony/flex ; \
60+
composer global config --no-plugins allow-plugins.symfony/flex true
61+
6162
CMD tail -f /dev/null
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Tests\Fixtures\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObject;
7+
8+
#[ORM\Entity]
9+
#[ORM\Table(name: "entity_with_property_name_different_from_construct")]
10+
class EntityWithPropertyNameDifferentFromConstruct
11+
{
12+
#[ORM\Id]
13+
#[ORM\GeneratedValue]
14+
#[ORM\Column(type: "integer")]
15+
private $id;
16+
17+
#[ORM\ManyToOne(targetEntity: EntityForRelations::class)]
18+
private EntityForRelations $entity;
19+
20+
#[ORM\Column()]
21+
private string $someField;
22+
23+
#[ORM\Embedded(Address::class)]
24+
private Address $address;
25+
26+
private SomeObject $someObject;
27+
28+
public function __construct(string $scalar, EntityForRelations $relationship, Address $embedded, SomeObject $notPersistedObject)
29+
{
30+
$this->someField = $scalar;
31+
$this->entity = $relationship;
32+
$this->address = $embedded;
33+
$this->someObject = $notPersistedObject;
34+
}
35+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Tests\Fixtures\Factories;
4+
5+
use Zenstruck\Foundry\ModelFactory;
6+
use Zenstruck\Foundry\Tests\Fixtures\Entity\EntityWithPropertyNameDifferentFromConstruct;
7+
use Zenstruck\Foundry\Tests\Fixtures\Object\SomeObjectFactory;
8+
9+
final class EntityWithPropertyNameDifferentFromConstructFactory extends ModelFactory
10+
{
11+
protected function getDefaults(): array
12+
{
13+
return [
14+
'scalar' => self::faker()->name(),
15+
'relationship' => EntityForRelationsFactory::new(),
16+
'embedded' => AddressFactory::new(),
17+
'notPersistedObject' => SomeObjectFactory::new(),
18+
];
19+
}
20+
21+
protected static function getClass(): string
22+
{
23+
return EntityWithPropertyNameDifferentFromConstruct::class;
24+
}
25+
}

tests/Fixtures/Migrations/Version20230513160345.php

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -87,53 +87,5 @@ public function up(Schema $schema): void
8787

8888
public function down(Schema $schema): void
8989
{
90-
// this down() migration is auto-generated, please modify it to your needs
91-
$this->addSql('DROP SEQUENCE categories_id_seq CASCADE');
92-
$this->addSql('DROP SEQUENCE comments_id_seq CASCADE');
93-
$this->addSql('DROP SEQUENCE contacts_id_seq CASCADE');
94-
$this->addSql('DROP SEQUENCE entity_for_relations_id_seq CASCADE');
95-
$this->addSql('DROP SEQUENCE entity_with_relations_id_seq CASCADE');
96-
$this->addSql('DROP SEQUENCE posts_id_seq CASCADE');
97-
$this->addSql('DROP SEQUENCE tags_id_seq CASCADE');
98-
$this->addSql('DROP SEQUENCE users_id_seq CASCADE');
99-
$this->addSql('ALTER TABLE productcategory_product DROP CONSTRAINT FK_5BC2A6A2E26A32B1');
100-
$this->addSql('ALTER TABLE productcategory_product DROP CONSTRAINT FK_5BC2A6A24584665A');
101-
$this->addSql('ALTER TABLE comments DROP CONSTRAINT FK_5F9E962AA76ED395');
102-
$this->addSql('ALTER TABLE comments DROP CONSTRAINT FK_5F9E962A4B89032C');
103-
$this->addSql('ALTER TABLE entity_for_relations DROP CONSTRAINT FK_C63B81552E3A088A');
104-
$this->addSql('ALTER TABLE entity_with_relations DROP CONSTRAINT FK_A9C9EC969017888C');
105-
$this->addSql('ALTER TABLE entity_with_relations DROP CONSTRAINT FK_A9C9EC96DA2BFB84');
106-
$this->addSql('ALTER TABLE entity_with_relations DROP CONSTRAINT FK_A9C9EC962E3A088A');
107-
$this->addSql('ALTER TABLE entity_with_relations DROP CONSTRAINT FK_A9C9EC968097B86C');
108-
$this->addSql('ALTER TABLE entity_with_relations DROP CONSTRAINT FK_A9C9EC968572C13C');
109-
$this->addSql('ALTER TABLE entity_with_relations DROP CONSTRAINT FK_A9C9EC96FF92FDCA');
110-
$this->addSql('ALTER TABLE entitywithrelations_category DROP CONSTRAINT FK_CD6EBFAB337AA4F7');
111-
$this->addSql('ALTER TABLE entitywithrelations_category DROP CONSTRAINT FK_CD6EBFAB12469DE2');
112-
$this->addSql('ALTER TABLE posts DROP CONSTRAINT FK_885DBAFA12469DE2');
113-
$this->addSql('ALTER TABLE posts DROP CONSTRAINT FK_885DBAFAEA0D7566');
114-
$this->addSql('ALTER TABLE posts DROP CONSTRAINT FK_885DBAFAD126F51');
115-
$this->addSql('ALTER TABLE posts DROP CONSTRAINT FK_885DBAFA20DBE482');
116-
$this->addSql('ALTER TABLE post_tag DROP CONSTRAINT FK_5ACE3AF04B89032C');
117-
$this->addSql('ALTER TABLE post_tag DROP CONSTRAINT FK_5ACE3AF0BAD26311');
118-
$this->addSql('ALTER TABLE post_tag_secondary DROP CONSTRAINT FK_1515F0214B89032C');
119-
$this->addSql('ALTER TABLE post_tag_secondary DROP CONSTRAINT FK_1515F021BAD26311');
120-
$this->addSql('ALTER TABLE post_post DROP CONSTRAINT FK_93DF0B866FA89B16');
121-
$this->addSql('ALTER TABLE post_post DROP CONSTRAINT FK_93DF0B86764DCB99');
122-
$this->addSql('ALTER TABLE product_tag DROP CONSTRAINT FK_E3A6E39C4584665A');
123-
$this->addSql('ALTER TABLE product_tag DROP CONSTRAINT FK_E3A6E39CBAD26311');
124-
$this->addSql('DROP TABLE categories');
125-
$this->addSql('DROP TABLE productcategory_product');
126-
$this->addSql('DROP TABLE comments');
127-
$this->addSql('DROP TABLE contacts');
128-
$this->addSql('DROP TABLE entity_for_relations');
129-
$this->addSql('DROP TABLE entity_with_relations');
130-
$this->addSql('DROP TABLE entitywithrelations_category');
131-
$this->addSql('DROP TABLE posts');
132-
$this->addSql('DROP TABLE post_tag');
133-
$this->addSql('DROP TABLE post_tag_secondary');
134-
$this->addSql('DROP TABLE post_post');
135-
$this->addSql('DROP TABLE product_tag');
136-
$this->addSql('DROP TABLE tags');
137-
$this->addSql('DROP TABLE users');
13890
}
13991
}

tests/Fixtures/Migrations/Version20230513160346.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,21 @@ public function up(Schema $schema): void
4343
$this->addSql('ALTER TABLE entity_with_relations ADD CONSTRAINT FK_A9C9EC96FF92FDCA FOREIGN KEY (manyToOneWithNotExistingFactory_id) REFERENCES brand_cascade (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
4444
$this->addSql('ALTER TABLE product_tag ADD CONSTRAINT FK_E3A6E39C4584665A FOREIGN KEY (product_id) REFERENCES product_cascade (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
4545
$this->addSql('ALTER TABLE product_tag ADD CONSTRAINT FK_E3A6E39CBAD26311 FOREIGN KEY (tag_id) REFERENCES tag_cascade (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
46+
47+
$this->addSql('CREATE SEQUENCE entity_with_property_name_different_from_construct_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
48+
$this->addSql('CREATE TABLE entity_with_property_name_different_from_construct (id INT NOT NULL, entity_id INT DEFAULT NULL, someField VARCHAR(255) NOT NULL, address_value VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
49+
$this->addSql('CREATE INDEX IDX_AA016C6381257D5D ON entity_with_property_name_different_from_construct (entity_id)');
50+
$this->addSql('ALTER TABLE entity_with_property_name_different_from_construct ADD CONSTRAINT FK_AA016C6381257D5D FOREIGN KEY (entity_id) REFERENCES entity_for_relations (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
51+
52+
if (PHP_VERSION_ID < 80100) {
53+
return;
54+
}
55+
56+
$this->addSql('CREATE SEQUENCE entity_with_enum_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
57+
$this->addSql('CREATE TABLE entity_with_enum (id INT NOT NULL, enum VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
4658
}
4759

4860
public function down(Schema $schema): void
4961
{
50-
// this down() migration is auto-generated, please modify it to your needs
51-
$this->addSql('DROP SEQUENCE brand_cascade_id_seq CASCADE');
52-
$this->addSql('DROP SEQUENCE category_cascade_id_seq CASCADE');
53-
$this->addSql('DROP SEQUENCE image_cascade_id_seq CASCADE');
54-
$this->addSql('DROP SEQUENCE product_cascade_id_seq CASCADE');
55-
$this->addSql('DROP SEQUENCE review_cascade_id_seq CASCADE');
56-
$this->addSql('DROP SEQUENCE tag_cascade_id_seq CASCADE');
57-
$this->addSql('DROP SEQUENCE variant_cascade_id_seq CASCADE');
58-
$this->addSql('ALTER TABLE product_cascade DROP CONSTRAINT FK_D7FE16D844F5D008');
59-
$this->addSql('ALTER TABLE review_cascade DROP CONSTRAINT FK_9DC9B99F4584665A');
60-
$this->addSql('ALTER TABLE variant_cascade DROP CONSTRAINT FK_6982202E4584665A');
61-
$this->addSql('ALTER TABLE variant_cascade DROP CONSTRAINT FK_6982202E3DA5256D');
62-
$this->addSql('ALTER TABLE entity_with_relations DROP CONSTRAINT FK_A9C9EC96FF92FDCA');
63-
$this->addSql('ALTER TABLE productcategory_product DROP CONSTRAINT FK_5BC2A6A2E26A32B1');
64-
$this->addSql('ALTER TABLE productcategory_product DROP CONSTRAINT FK_5BC2A6A24584665A');
65-
$this->addSql('ALTER TABLE product_tag DROP CONSTRAINT FK_E3A6E39C4584665A');
66-
$this->addSql('ALTER TABLE product_tag DROP CONSTRAINT FK_E3A6E39CBAD26311');
67-
$this->addSql('DROP TABLE brand_cascade');
68-
$this->addSql('DROP TABLE category_cascade');
69-
$this->addSql('DROP TABLE image_cascade');
70-
$this->addSql('DROP TABLE product_cascade');
71-
$this->addSql('DROP TABLE review_cascade');
72-
$this->addSql('DROP TABLE tag_cascade');
73-
$this->addSql('DROP TABLE variant_cascade');
7462
}
7563
}

tests/Fixtures/Migrations/Version20230513160347.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/Functional/ORMModelFactoryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;
1919
use Zenstruck\Foundry\Tests\Fixtures\Factories\CommentFactory;
2020
use Zenstruck\Foundry\Tests\Fixtures\Factories\ContactFactory;
21+
use Zenstruck\Foundry\Tests\Fixtures\Factories\EntityForRelationsFactory;
2122
use Zenstruck\Foundry\Tests\Fixtures\Factories\EntityWithEnumFactory;
23+
use Zenstruck\Foundry\Tests\Fixtures\Factories\EntityWithPropertyNameDifferentFromConstructFactory;
2224
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;
2325
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithInvalidInitialize;
2426
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactoryWithNullInitialize;
@@ -676,6 +678,16 @@ public static function addOneToManyWithExtraAttributes(): iterable
676678
yield ['extraPostsAfterInstantiate'];
677679
}
678680

681+
/**
682+
* @test
683+
*/
684+
public function it_can_create_entity_with_property_name_different_from_constructor_name(): void
685+
{
686+
EntityWithPropertyNameDifferentFromConstructFactory::createOne();
687+
EntityWithPropertyNameDifferentFromConstructFactory::assert()->count(1);
688+
EntityForRelationsFactory::assert()->count(1);
689+
}
690+
679691
protected function categoryClass(): string
680692
{
681693
return Category::class;

0 commit comments

Comments
 (0)