-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: propagate "schedule for insert" to factory collection
- Loading branch information
Showing
8 changed files
with
227 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ | |
|
||
namespace Zenstruck\Foundry; | ||
|
||
use Zenstruck\Foundry\Persistence\PersistentObjectFactory; | ||
use Zenstruck\Foundry\Persistence\PersistMode; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* | ||
|
@@ -22,12 +25,28 @@ | |
*/ | ||
final class FactoryCollection implements \IteratorAggregate | ||
{ | ||
private PersistMode $persistMode; | ||
|
||
/** | ||
* @param TFactory $factory | ||
* @phpstan-param \Closure():iterable<Attributes>|\Closure():iterable<TFactory> $items | ||
*/ | ||
private function __construct(public readonly Factory $factory, private \Closure $items) | ||
{ | ||
$this->persistMode = $this->factory instanceof PersistentObjectFactory | ||
? $this->factory->persistMode() | ||
: PersistMode::WITHOUT_PERSISTING; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
public function withPersistMode(PersistMode $persistMode): static | ||
{ | ||
$clone = clone $this; | ||
$clone->persistMode = $persistMode; | ||
|
||
return $clone; | ||
} | ||
|
||
/** | ||
|
@@ -133,7 +152,16 @@ public function all(): array | |
$factories[] = $this->factory->with($attributesOrFactory)->with(['__index' => $i++]); | ||
} | ||
|
||
return $factories; // @phpstan-ignore return.type (PHPStan does not understand we have an array of factories) | ||
return array_map( // @phpstan-ignore return.type (PHPStan does not understand we have an array of factories) | ||
function (Factory $f) { | ||
if ($f instanceof PersistentObjectFactory) { | ||
return $f->withPersistMode($this->persistMode); | ||
} | ||
|
||
return $f; | ||
}, | ||
$factories | ||
); | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
|
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
39 changes: 39 additions & 0 deletions
39
tests/Fixture/Entity/EdgeCases/InversedOneToOneWithOneToMany/InverseSide.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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithOneToMany; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Zenstruck\Foundry\Tests\Fixture\Model\Base; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
#[ORM\Entity] | ||
#[ORM\Table('inversed_one_to_one_with_one_to_many_inverse_side')] | ||
class InverseSide extends Base | ||
{ | ||
#[ORM\OneToOne(mappedBy: 'inverseSide')] | ||
private ?OwningSide $owningSide = null; | ||
|
||
public function getOwningSide(): ?OwningSide | ||
{ | ||
return $this->owningSide; | ||
} | ||
|
||
public function setOwningSide(OwningSide $owningSide): void | ||
{ | ||
$this->owningSide = $owningSide; | ||
$owningSide->inverseSide = $this; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/Fixture/Entity/EdgeCases/InversedOneToOneWithOneToMany/Item.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithOneToMany; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Zenstruck\Foundry\Tests\Fixture\Model\Base; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
#[ORM\Entity] | ||
#[ORM\Table('inversed_one_to_one_with_one_to_many_item_if_collection')] | ||
class Item extends Base | ||
{ | ||
#[ORM\ManyToOne(inversedBy: 'items')] | ||
public ?OwningSide $owningSide = null; | ||
} |
63 changes: 63 additions & 0 deletions
63
tests/Fixture/Entity/EdgeCases/InversedOneToOneWithOneToMany/OwningSide.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,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithOneToMany; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Zenstruck\Foundry\Tests\Fixture\Model\Base; | ||
|
||
/** | ||
* @author Nicolas PHILIPPE <[email protected]> | ||
*/ | ||
#[ORM\Entity] | ||
#[ORM\Table('inversed_one_to_one_with_one_to_many_owning_side')] | ||
class OwningSide extends Base | ||
{ | ||
#[ORM\OneToOne(inversedBy: 'owningSide')] | ||
public ?InverseSide $inverseSide = null; | ||
|
||
/** @var Collection<int, Item> */ | ||
#[ORM\OneToMany(targetEntity: Item::class, mappedBy: 'owningSide')] | ||
private Collection $items; | ||
|
||
public function __construct() | ||
{ | ||
$this->items = new ArrayCollection(); | ||
} | ||
|
||
/** | ||
* @return Collection<int, Item> | ||
*/ | ||
public function getItems(): Collection | ||
{ | ||
return $this->items; | ||
} | ||
|
||
public function addItem(Item $item): void | ||
{ | ||
if (!$this->items->contains($item)) { | ||
$this->items->add($item); | ||
$item->owningSide = $this; | ||
} | ||
} | ||
|
||
public function removeItem(Item $item): void | ||
{ | ||
if ($this->items->contains($item)) { | ||
$this->items->removeElement($item); | ||
$item->owningSide = null; | ||
} | ||
} | ||
} |
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