Skip to content

Commit

Permalink
chore: upgrade foundry 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored and smnandre committed Aug 24, 2024
1 parent 8299ae3 commit 3efde01
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,22 @@

use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\CompositeIdEntity;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;
use Zenstruck\Foundry\Persistence\Proxy;

/**
* @extends PersistentProxyObjectFactory<CompositeIdEntity>
*
* @method static CompositeIdEntity|Proxy createOne(array $attributes = [])
* @method static CompositeIdEntity[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static CompositeIdEntity|Proxy find(object|array|mixed $criteria)
* @method static CompositeIdEntity|Proxy findOrCreate(array $attributes)
* @method static CompositeIdEntity|Proxy first(string $sortedField = 'id')
* @method static CompositeIdEntity|Proxy last(string $sortedField = 'id')
* @method static CompositeIdEntity|Proxy random(array $attributes = [])
* @method static CompositeIdEntity|Proxy randomOrCreate(array $attributes = []))
* @method static CompositeIdEntity[]|Proxy[] all()
* @method static CompositeIdEntity[]|Proxy[] findBy(array $attributes)
* @method static CompositeIdEntity[]|Proxy[] randomSet(int $number, array $attributes = []))
* @method static CompositeIdEntity[]|Proxy[] randomRange(int $min, int $max, array $attributes = []))
* @method CompositeIdEntity|Proxy create(array|callable $attributes = [])
*/
class CompositeIdEntityFactory extends PersistentProxyObjectFactory
{
protected function defaults(): array|callable
public static function class(): string
{
return CompositeIdEntity::class;
}

protected function defaults(): array
{
return [
'firstIdPart' => rand(1, \PHP_INT_MAX),
'secondIdPart' => rand(1, \PHP_INT_MAX),
];
}

public static function class(): string
{
return CompositeIdEntity::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,20 @@
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\ForeignKeyIdEntity;
use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory;

use Zenstruck\Foundry\Persistence\Proxy;
use function Zenstruck\Foundry\lazy;

/**
* @extends PersistentProxyObjectFactory<ForeignKeyIdEntity>
*
* @method static ForeignKeyIdEntity|Proxy createOne(array $attributes = [])
* @method static ForeignKeyIdEntity[]|Proxy[] createMany(int $number, array|callable $attributes = [])
* @method static ForeignKeyIdEntity|Proxy find(object|array|mixed $criteria)
* @method static ForeignKeyIdEntity|Proxy findOrCreate(array $attributes)
* @method static ForeignKeyIdEntity|Proxy first(string $sortedField = 'id')
* @method static ForeignKeyIdEntity|Proxy last(string $sortedField = 'id')
* @method static ForeignKeyIdEntity|Proxy random(array $attributes = [])
* @method static ForeignKeyIdEntity|Proxy randomOrCreate(array $attributes = []))
* @method static ForeignKeyIdEntity[]|Proxy[] all()
* @method static ForeignKeyIdEntity[]|Proxy[] findBy(array $attributes)
* @method static ForeignKeyIdEntity[]|Proxy[] randomSet(int $number, array $attributes = []))
* @method static ForeignKeyIdEntity[]|Proxy[] randomRange(int $min, int $max, array $attributes = []))
*/
class ForeignKeyIdEntityFactory extends PersistentProxyObjectFactory
{
protected function defaults(): array|callable
public static function class(): string
{
return ['id' => lazy(static fn () => new Entity1())];
return ForeignKeyIdEntity::class;
}

public static function class(): string
protected function defaults(): array
{
return ForeignKeyIdEntity::class;
return ['id' => lazy(static fn () => new Entity1())];
}
}
64 changes: 32 additions & 32 deletions src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

use function Zenstruck\Foundry\factory;
use function Zenstruck\Foundry\object;
use function Zenstruck\Foundry\Persistence\persist;
use function Zenstruck\Foundry\Persistence\proxy;

/**
* @author Kevin Bond <[email protected]>
Expand Down Expand Up @@ -78,7 +79,6 @@ private function executeHydrationTestCase(callable $testFactory, ?int $minPhpVer
$hydratedComponent2 = clone $testCase->component;

$liveMetadata = $testCase->liveMetadata;

$this->factory()->mountFromObject(
$originalComponentWithData,
$testCase->inputProps,
Expand Down Expand Up @@ -401,14 +401,14 @@ public function onEntireEntityUpdated($oldValue)
}];

yield 'Persisted entity: deleting entity between dehydration and hydration sets it to null' => [function () {
$product = persist(ProductFixtureEntity::class);
$product = proxy(persist(ProductFixtureEntity::class));

return HydrationTest::create(new class {
// test that event the writable path doesn't cause problems
#[LiveProp(writable: ['name'])]
public ?ProductFixtureEntity $product;
})
->mountWith(['product' => $product])
->mountWith(['product' => $product->_real()])
->beforeHydration(function () use ($product) {
$product->_delete();
})
Expand Down Expand Up @@ -749,9 +749,9 @@ public function onEntireEntityUpdated($oldValue)
}];

yield 'Array with objects: (de)hydrates correctly' => [function () {
$prod1 = factory(ProductFixtureEntity::class, ['name' => 'item1']);
$prod1 = persist(ProductFixtureEntity::class, ['name' => 'item1']);
$prod2 = new ProductFixtureEntity();
$prod3 = factory(ProductFixtureEntity::class, ['name' => 'item3']);
$prod3 = persist(ProductFixtureEntity::class, ['name' => 'item3']);

return HydrationTest::create(new DummyObjectWithObjects())
->mountWith(['products' => [$prod1, $prod2, $prod3]])
Expand Down Expand Up @@ -971,10 +971,10 @@ public function mount()
}];

yield 'Array with DTOs: fully writable allows anything to change' => [function () {
$address1 = factory(Address::class, ['address' => '17 Arcadia Road', 'city' => 'London']);
$address2 = factory(Address::class, ['address' => '4 Privet Drive', 'city' => 'Little Whinging']);
$address3 = factory(Address::class, ['address' => '124 Conch St.', 'city' => 'Bikini Bottom']);
$address4 = factory(Address::class, ['address' => '32 Windsor Gardens', 'city' => 'London']);
$address1 = object(Address::class, ['address' => '17 Arcadia Road', 'city' => 'London']);
$address2 = object(Address::class, ['address' => '4 Privet Drive', 'city' => 'Little Whinging']);
$address3 = object(Address::class, ['address' => '124 Conch St.', 'city' => 'Bikini Bottom']);
$address4 = object(Address::class, ['address' => '32 Windsor Gardens', 'city' => 'London']);

return HydrationTest::create(new class {
/**
Expand All @@ -997,19 +997,19 @@ public function mount()
->userUpdatesProps(['addresses' => [$address3, $address4]])
->assertObjectAfterHydration(function (object $object) {
self::assertEquals([
factory(Address::class, ['address' => '124 Conch St.', 'city' => 'Bikini Bottom']),
factory(Address::class, ['address' => '32 Windsor Gardens', 'city' => 'London']),
object(Address::class, ['address' => '124 Conch St.', 'city' => 'Bikini Bottom']),
object(Address::class, ['address' => '32 Windsor Gardens', 'city' => 'London']),
], $object->addresses);
});
}];

yield 'Array with DTOs: fully writable allows partial changes' => [function () {
$address1 = factory(Address::class, ['address' => '1600 Pennsylvania Avenue', 'city' => 'Washington DC']);
$address2 = factory(Address::class, ['address' => '221 B Baker St', 'city' => 'Birmingham']);
$address1 = object(Address::class, ['address' => '1600 Pennsylvania Avenue', 'city' => 'Washington DC']);
$address2 = object(Address::class, ['address' => '221 B Baker St', 'city' => 'Birmingham']);

return HydrationTest::create(new class {
/**
* @var Address[]
* @var \Symfony\UX\LiveComponent\Tests\Fixtures\Dto\Address[]
*/
#[LiveProp(writable: true, useSerializerForHydration: true)]
public array $addresses = [];
Expand All @@ -1028,8 +1028,8 @@ public function mount()
->userUpdatesProps(['addresses.1.city' => 'London'])
->assertObjectAfterHydration(function (object $object) {
self::assertEquals([
persist(Address::class, ['address' => '1600 Pennsylvania Avenue', 'city' => 'Washington DC']),
persist(Address::class, ['address' => '221 B Baker St', 'city' => 'London']),
object(Address::class, ['address' => '1600 Pennsylvania Avenue', 'city' => 'Washington DC']),
object(Address::class, ['address' => '221 B Baker St', 'city' => 'London']),
], $object->addresses);
});
}];
Expand All @@ -1043,14 +1043,14 @@ public function mount()
public array $dtos = [];
})
->mountWith(['dtos' => [
factory(HoldsArrayOfDtos::class, ['addresses' => [
factory(Address::class, ['address' => '742 Evergreen Terrace', 'city' => 'Boston']),
factory(Address::class, ['address' => 'Apartment 5A, 129 West 81st Street', 'city' => 'New York']),
factory(Address::class, ['address' => '52 Festive Road', 'city' => 'London']),
object(HoldsArrayOfDtos::class, ['addresses' => [
object(Address::class, ['address' => '742 Evergreen Terrace', 'city' => 'Boston']),
object(Address::class, ['address' => 'Apartment 5A, 129 West 81st Street', 'city' => 'New York']),
object(Address::class, ['address' => '52 Festive Road', 'city' => 'London']),
]]),
factory(HoldsArrayOfDtos::class, ['addresses' => [
factory(Address::class, ['address' => '698 Sycamore Road', 'city' => 'San Pueblo']),
factory(Address::class, ['address' => 'Madison Square Garden', 'city' => 'Chicago']),
object(HoldsArrayOfDtos::class, ['addresses' => [
object(Address::class, ['address' => '698 Sycamore Road', 'city' => 'San Pueblo']),
object(Address::class, ['address' => 'Madison Square Garden', 'city' => 'Chicago']),
]]),
]])
->assertDehydratesTo(['dtos' => [
Expand All @@ -1071,19 +1071,19 @@ public function mount()
->userUpdatesProps([
'dtos.0.addresses.0.city' => 'Springfield',
'dtos.1.addresses.1.address' => '1060 West Addison Street',
'dtos.1.addresses.1' => factory(Address::class, ['address' => '10 Downing Street', 'city' => 'London']),
'dtos.1.addresses.1' => object(Address::class, ['address' => '10 Downing Street', 'city' => 'London']),
])
->assertObjectAfterHydration(function (object $object) {
self::assertEquals(
[
persist(HoldsArrayOfDtos::class, ['addresses' => [
factory(Address::class, ['address' => '742 Evergreen Terrace', 'city' => 'Springfield']),
factory(Address::class, ['address' => 'Apartment 5A, 129 West 81st Street', 'city' => 'New York']),
factory(Address::class, ['address' => '52 Festive Road', 'city' => 'London']),
object(HoldsArrayOfDtos::class, ['addresses' => [
object(Address::class, ['address' => '742 Evergreen Terrace', 'city' => 'Springfield']),
object(Address::class, ['address' => 'Apartment 5A, 129 West 81st Street', 'city' => 'New York']),
object(Address::class, ['address' => '52 Festive Road', 'city' => 'London']),
]]),
persist(HoldsArrayOfDtos::class, ['addresses' => [
factory(Address::class, ['address' => '698 Sycamore Road', 'city' => 'San Pueblo']),
factory(Address::class, ['address' => '10 Downing Street', 'city' => 'London']),
object(HoldsArrayOfDtos::class, ['addresses' => [
object(Address::class, ['address' => '698 Sycamore Road', 'city' => 'San Pueblo']),
object(Address::class, ['address' => '10 Downing Street', 'city' => 'London']),
]]),
],
$object->dtos
Expand Down

0 comments on commit 3efde01

Please sign in to comment.