Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Extract FieldValueResolutionStrategy interface #1374

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/FieldResolution/DefaultStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* @internal
*/
final class DefaultStrategy implements ResolutionStrategy
final class DefaultStrategy implements FieldValueResolutionStrategy, ResolutionStrategy
{
public function resolveFieldValue(
Generator $faker,
Expand Down
33 changes: 33 additions & 0 deletions src/FieldResolution/FieldValueResolutionStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020-2024 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 Ergebnis\FactoryBot\FieldResolution;

use Ergebnis\FactoryBot\FieldDefinition;
use Ergebnis\FactoryBot\FixtureFactory;
use Faker\Generator;

/**
* @internal
*/
interface FieldValueResolutionStrategy
{
/**
* @return mixed
*/
public function resolveFieldValue(
Generator $faker,
FixtureFactory $fixtureFactory,
FieldDefinition\Resolvable $fieldDefinition,
);
}
11 changes: 0 additions & 11 deletions src/FieldResolution/ResolutionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,13 @@
namespace Ergebnis\FactoryBot\FieldResolution;

use Ergebnis\FactoryBot\Count;
use Ergebnis\FactoryBot\FieldDefinition;
use Ergebnis\FactoryBot\FixtureFactory;
use Faker\Generator;

/**
* @internal
*/
interface ResolutionStrategy
{
/**
* @return mixed
*/
public function resolveFieldValue(
Generator $faker,
FixtureFactory $fixtureFactory,
FieldDefinition\Resolvable $fieldDefinition,
);

public function resolveCount(
Generator $faker,
Count $count,
Expand Down
2 changes: 1 addition & 1 deletion src/FieldResolution/WithOptionalStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* @internal
*/
final class WithOptionalStrategy implements ResolutionStrategy
final class WithOptionalStrategy implements FieldValueResolutionStrategy, ResolutionStrategy
{
public function resolveFieldValue(
Generator $faker,
Expand Down
2 changes: 1 addition & 1 deletion src/FieldResolution/WithoutOptionalStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* @internal
*/
final class WithoutOptionalStrategy implements ResolutionStrategy
final class WithoutOptionalStrategy implements FieldValueResolutionStrategy, ResolutionStrategy
{
public function resolveFieldValue(
Generator $faker,
Expand Down
6 changes: 5 additions & 1 deletion src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

final class FixtureFactory
{
private FieldResolution\FieldValueResolutionStrategy $fieldValueResolutionStrategy;
private FieldResolution\ResolutionStrategy $resolutionStrategy;
private bool $persistAfterCreate = false;

Expand All @@ -32,6 +33,7 @@
private ORM\EntityManagerInterface $entityManager,
private Generator $faker,
) {
$this->fieldValueResolutionStrategy = new FieldResolution\DefaultStrategy();
$this->resolutionStrategy = new FieldResolution\DefaultStrategy();
}

Expand Down Expand Up @@ -239,7 +241,7 @@
);

$fieldValues = \array_map(function (FieldDefinition\Resolvable $fieldDefinition) {
return $this->resolutionStrategy->resolveFieldValue(
return $this->fieldValueResolutionStrategy->resolveFieldValue(
$this->faker,
$this,
$fieldDefinition,
Expand Down Expand Up @@ -325,6 +327,7 @@
{
$instance = clone $this;

$instance->fieldValueResolutionStrategy = new FieldResolution\WithOptionalStrategy();
$instance->resolutionStrategy = new FieldResolution\WithOptionalStrategy();

return $instance;
Expand All @@ -342,6 +345,7 @@
{
$instance = clone $this;

$instance->fieldValueResolutionStrategy = new FieldResolution\WithoutOptionalStrategy();
$instance->resolutionStrategy = new FieldResolution\WithoutOptionalStrategy();

return $instance;
Expand Down Expand Up @@ -450,7 +454,7 @@
$inversedBy,
);

if (!$collection instanceof Common\Collections\Collection) {

Check warning on line 457 in src/FixtureFactory.php

View workflow job for this annotation

GitHub Actions / Mutation Tests (8.1, locked)

Escaped Mutant for Mutator "InstanceOf_": --- Original +++ New @@ @@ } $classMetadataOfFieldValue = $this->entityManager->getClassMetadata($fieldValue::class); $collection = $classMetadataOfFieldValue->getFieldValue($fieldValue, $inversedBy); - if (!$collection instanceof Common\Collections\Collection) { + if (!true) { return; } $collection->add($entity);
return;
}

Expand Down