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
68 changes: 68 additions & 0 deletions example/src/Entity/Device.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020-2026 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/factory-bot
*/

namespace Example\Entity;

use Doctrine\ORM;
use Ramsey\Uuid;

#[ORM\Mapping\Entity()]
#[ORM\Mapping\Table(name: 'device')]
class Device
{
#[ORM\Mapping\Column(
name: 'id',
type: 'string',
)]
#[ORM\Mapping\GeneratedValue(strategy: 'NONE')]
#[ORM\Mapping\Id()]
private string $id;

#[ORM\Mapping\Column(
name: 'name',
type: 'string',
)]
private string $name;

#[ORM\Mapping\JoinColumn(
name: 'fingerprint_id',
referencedColumnName: 'id',
)]
#[ORM\Mapping\OneToOne(
targetEntity: Fingerprint::class,
inversedBy: 'device',
)]
private ?Fingerprint $fingerprint;

public function __construct(string $name, ?Fingerprint $fingerprint = null)

Check failure on line 47 in example/src/Entity/Device.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (8.1, locked)

Method Example\Entity\Device::__construct() has parameter $fingerprint with null as default value.

Check failure on line 47 in example/src/Entity/Device.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (8.1, locked)

Method Example\Entity\Device::__construct() has parameter $fingerprint with a nullable type declaration.

Check failure on line 47 in example/src/Entity/Device.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (8.1, locked)

Constructor in Example\Entity\Device has parameter $fingerprint with default value.
{
$this->id = Uuid\Uuid::uuid4()->toString();
$this->name = $name;
$this->fingerprint = $fingerprint;
}

public function id(): string
{
return $this->id;
}

public function name(): string
{
return $this->name;
}

public function fingerprint(): ?Fingerprint

Check failure on line 64 in example/src/Entity/Device.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (8.1, locked)

Method Example\Entity\Device::fingerprint() has a nullable return type declaration.
{
return $this->fingerprint;
}
}
63 changes: 63 additions & 0 deletions example/src/Entity/Fingerprint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020-2026 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/factory-bot
*/

namespace Example\Entity;

use Doctrine\ORM;
use Ramsey\Uuid;

#[ORM\Mapping\Entity()]
#[ORM\Mapping\Table(name: 'fingerprint')]
class Fingerprint
{
#[ORM\Mapping\Column(
name: 'id',
type: 'string',
)]
#[ORM\Mapping\GeneratedValue(strategy: 'NONE')]
#[ORM\Mapping\Id()]
private string $id;

#[ORM\Mapping\Column(
name: 'token',
type: 'string',
)]
private string $token;

#[ORM\Mapping\OneToOne(
targetEntity: Device::class,
mappedBy: 'fingerprint',
)]
private ?Device $device = null;

public function __construct(string $token)
{
$this->id = Uuid\Uuid::uuid4()->toString();
$this->token = $token;
}

public function id(): string
{
return $this->id;
}

public function token(): string
{
return $this->token;
}

public function device(): ?Device

Check failure on line 59 in example/src/Entity/Fingerprint.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (8.1, locked)

Method Example\Entity\Fingerprint::device() has a nullable return type declaration.
{
return $this->device;
}
}
6 changes: 5 additions & 1 deletion src/FixtureFactory.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

declare(strict_types=1);
Expand Down Expand Up @@ -445,10 +445,14 @@
$association = $classMetadata->getAssociationMapping($fieldName);

if (\is_array($association)) {
return $association['inversedBy'];
return $association['inversedBy'] ?? null;

Check failure on line 448 in src/FixtureFactory.php

View workflow job for this annotation

GitHub Actions / Static Code Analysis (8.1, locked)

Offset 'inversedBy' on *NEVER* on left side of ?? always exists and is not nullable.
}

if ($association instanceof ORM\Mapping\AssociationMapping) {
if (!\property_exists($association, 'inversedBy')) {
return null;
}

return $association->inversedBy;
}

Expand Down
24 changes: 24 additions & 0 deletions test/Unit/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,30 @@ public function testCreateOneEstablishesBidirectionalOneToManyAssociations(): vo
self::assertContains($repository, $organization->repositories());
}

public function testCreateOneDoesNotFailForSingleValuedInverseSideAssociationsWithoutInversedBy(): void
{
$faker = self::faker();

$fixtureFactory = new FixtureFactory(
self::entityManager(),
$faker,
);

$fixtureFactory->define(Entity\Device::class, [
'name' => $faker->word(),
]);

$fixtureFactory->define(Entity\Fingerprint::class, [
'token' => $faker->sha1(),
'device' => FieldDefinition::reference(Entity\Device::class),
]);

/** @var Entity\Fingerprint $fingerprint */
$fingerprint = $fixtureFactory->createOne(Entity\Fingerprint::class);

self::assertInstanceOf(Entity\Device::class, $fingerprint->device());
}

public function testOptionalFieldValuesAreSetToNullWhenFakerReturnsFalse(): void
{
$fixtureFactory = new FixtureFactory(
Expand Down
Loading