Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\ManyToOneWithParentAsId;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
* @author Nicolas PHILIPPE <[email protected]>
*/
#[ORM\Entity]
#[ORM\Table('many_to_one_with_parent_as_id_inverse')]
class InverseSide implements \Stringable
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER, options: ['unsigned' => true])]
public ?int $id = null;

#[ORM\Column(nullable: true)]
public ?string $parentName = null;

/** @var Collection<int,OwningSide> */
#[ORM\OneToMany(targetEntity: OwningSide::class, mappedBy: 'inverseSide', cascade: ['persist'])]
public Collection $owningSides;

public function __construct()
{
$this->owningSides = new ArrayCollection();
}

#[\Override]
public function __toString(): string
{
return (string) $this->id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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\ManyToOneWithParentAsId;

use Doctrine\ORM\Mapping as ORM;

/**
* @author Nicolas PHILIPPE <[email protected]>
*/
#[ORM\Entity]
#[ORM\Table('many_to_one_with_parent_as_id')]
class OwningSide
{
#[ORM\Id]
#[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'owningSides')]
#[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id', onDelete: 'cascade')]
public InverseSide $inverseSide;

#[ORM\Column(nullable: true)]
public ?string $childName = null;
}
34 changes: 34 additions & 0 deletions tests/Integration/ORM/EdgeCasesRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Zenstruck\Foundry\Tests\Integration\ORM;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\IgnorePhpunitWarnings;
use PHPUnit\Framework\Attributes\RequiresPhpunit;
Expand All @@ -23,6 +24,7 @@
use Zenstruck\Foundry\Test\ResetDatabase;
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\ChangesEntityRelationshipCascadePersist;
use Zenstruck\Foundry\Tests\Fixture\DoctrineCascadeRelationship\UsingRelationships;
use Zenstruck\Foundry\Tests\Fixture\Entity\Address;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\IndexedOneToMany;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithManyToOne;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithNonNullableOwning;
Expand All @@ -31,12 +33,14 @@
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithoutNullable;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithSetter;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\ManyToOneToSelfReferencing;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\ManyToOneWithParentAsId;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\ManyToOneWithAutoGeneratedUlid;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\OneToManyWithUnionType;
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RichDomainMandatoryRelationship;
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\EdgeCases\MultipleMandatoryRelationshipToSameEntity;
use Zenstruck\Foundry\Tests\Integration\RequiresORM;

use function Zenstruck\Foundry\faker;
use function Zenstruck\Foundry\Persistence\persistent_factory;

/**
Expand All @@ -50,6 +54,13 @@ final class EdgeCasesRelationshipTest extends KernelTestCase
// PHPUnit triggers a warning, which we ignore with #[IgnorePhpunitWarnings]
public const DATA_PROVIDER_WARNING_REGEX = 'Data set "(.)*" provided by Zenstruck(.)*::provideCascadeRelationshipsCombinations has more arguments \(\d\) than the test method accepts \(0\)';

private EntityManagerInterface $entityManager;

protected function setUp(): void
{
$this->entityManager = self::getContainer()->get(EntityManagerInterface::class); // @phpstan-ignore assign.propertyType
}

/** @test */
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
Expand Down Expand Up @@ -150,6 +161,29 @@ public function many_to_many_to_self_referencing_inverse_side(): void
$inverseSideFactory::assert()->count(1);
}

/** @test */
#[Test]
public function many_to_one_with_parent_as_id(): void
{
$owningSideFactory = persistent_factory(ManyToOneWithParentAsId\OwningSide::class);
$inverseSideFactory = persistent_factory(ManyToOneWithParentAsId\InverseSide::class);

$child = $owningSideFactory->create([
'inverseSide' => $inverseSideFactory,
'childName' => faker()->name(),
]);

// child has same ID as parent
$childId = $child->inverseSide->id;

$childEntity = $this->entityManager->getRepository(ManyToOneWithParentAsId\OwningSide::class)->find($childId);
self::assertInstanceOf(ManyToOneWithParentAsId\OwningSide::class, $childEntity);
self::assertSame($childEntity->childName, $child->childName);

$owningSideFactory::assert()->count(1);
$inverseSideFactory::assert()->count(1);
}

/** @test */
#[Test]
#[DataProvider('provideCascadeRelationshipsCombinations')]
Expand Down