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 BetweenMinimumAndMaximumCountResolutionStrategy #1377

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.
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,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\Count;
use Faker\Generator;

/**
* @internal
*/
final class BetweenMinimumAndMaximumCountResolutionStrategy implements CountResolutionStrategy
{
public function resolveCount(
Generator $faker,
Count $count,
): int {
return $faker->numberBetween(
$count->minimum(),
$count->maximum(),
);
}
}
13 changes: 1 addition & 12 deletions src/FieldResolution/DefaultStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@

namespace Ergebnis\FactoryBot\FieldResolution;

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

/**
* @internal
*/
final class DefaultStrategy implements CountResolutionStrategy, FieldValueResolutionStrategy
final class DefaultStrategy implements FieldValueResolutionStrategy
{
public function resolveFieldValue(
Generator $faker,
Expand All @@ -37,14 +36,4 @@ public function resolveFieldValue(
$fixtureFactory,
);
}

public function resolveCount(
Generator $faker,
Count $count,
): int {
return $faker->numberBetween(
$count->minimum(),
$count->maximum(),
);
}
}
2 changes: 1 addition & 1 deletion src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
private Generator $faker,
) {
$this->fieldValueResolutionStrategy = new FieldResolution\DefaultStrategy();
$this->countResolutionStrategy = new FieldResolution\DefaultStrategy();
$this->countResolutionStrategy = new FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy();
}

/**
Expand Down Expand Up @@ -454,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
1 change: 1 addition & 0 deletions test/Integration/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#[Framework\Attributes\UsesClass(FieldDefinition\Reference::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Sequence::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Value::class)]
#[Framework\Attributes\UsesClass(FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy::class)]
#[Framework\Attributes\UsesClass(FieldResolution\DefaultStrategy::class)]
final class FixtureFactoryTest extends AbstractTestCase
{
Expand Down
1 change: 1 addition & 0 deletions test/Unit/FieldDefinition/ReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#[Framework\Attributes\UsesClass(FieldDefinition::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Value::class)]
#[Framework\Attributes\UsesClass(FixtureFactory::class)]
#[Framework\Attributes\UsesClass(FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy::class)]
#[Framework\Attributes\UsesClass(FieldResolution\DefaultStrategy::class)]
final class ReferencesTest extends Test\Unit\AbstractTestCase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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\Test\Unit\FieldResolution;

use Ergebnis\FactoryBot\Count;
use Ergebnis\FactoryBot\FieldDefinition;
use Ergebnis\FactoryBot\FieldResolution;
use Ergebnis\FactoryBot\FixtureFactory;
use Ergebnis\FactoryBot\Test;
use PHPUnit\Framework;

#[Framework\Attributes\CoversClass(FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy::class)]
#[Framework\Attributes\UsesClass(Count::class)]
#[Framework\Attributes\UsesClass(FieldDefinition::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Optional::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Value::class)]
#[Framework\Attributes\UsesClass(FixtureFactory::class)]
final class BetweenMinimumAndMaximumCountResolutionStrategyTest extends Test\Unit\AbstractTestCase
{
#[Framework\Attributes\DataProviderExternal(Test\DataProvider\IntProvider::class, 'greaterThanOrEqualToZero')]
public function testResolveCountResolvesCountWithFakerWhenCountIsExact(int $value): void
{
$strategy = new FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy();

$resolved = $strategy->resolveCount(
self::faker(),
Count::exact($value),
);

self::assertSame($value, $resolved);
}

#[Framework\Attributes\DataProviderExternal(Test\DataProvider\IntProvider::class, 'greaterThanOrEqualToZero')]
public function testResolveCountResolvesCountWithFakerWhenCountIsBetweenAndFakerReturnsMinimum(int $minimum): void
{
$maximum = self::faker()->numberBetween($minimum + 1);

$strategy = new FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy();

$resolved = $strategy->resolveCount(
new Test\Double\Faker\MinimumGenerator(),
Count::between(
$minimum,
$maximum,
),
);

self::assertSame($minimum, $resolved);
}

#[Framework\Attributes\DataProviderExternal(Test\DataProvider\IntProvider::class, 'greaterThanZero')]
public function testResolveCountResolvesCountWithFakerWhenCountIsBetweenAndFakerReturnsMaximum(int $maximum): void
{
$minimum = self::faker()->numberBetween(0, $maximum - 1);

$strategy = new FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy();

$resolved = $strategy->resolveCount(
new Test\Double\Faker\MaximumGenerator(),
Count::between(
$minimum,
$maximum,
),
);

self::assertSame($maximum, $resolved);
}

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

$minimum = $faker->numberBetween(1);
$maximum = $faker->numberBetween($minimum + 1);

$strategy = new FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy();

$resolved = $strategy->resolveCount(
$faker,
Count::between(
$minimum,
$maximum,
),
);

self::assertGreaterThanOrEqual($minimum, $resolved);
self::assertLessThanOrEqual($maximum, $resolved);
}
}
70 changes: 0 additions & 70 deletions test/Unit/FieldResolution/DefaultStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,74 +130,4 @@ public function testResolveFieldValueResolvesFieldDefinitionWithFakerAndFixtureF

self::assertSame($expected, $resolved);
}

#[Framework\Attributes\DataProviderExternal(Test\DataProvider\IntProvider::class, 'greaterThanOrEqualToZero')]
public function testResolveCountResolvesCountWithFakerWhenCountIsExact(int $value): void
{
$strategy = new FieldResolution\DefaultStrategy();

$resolved = $strategy->resolveCount(
self::faker(),
Count::exact($value),
);

self::assertSame($value, $resolved);
}

#[Framework\Attributes\DataProviderExternal(Test\DataProvider\IntProvider::class, 'greaterThanOrEqualToZero')]
public function testResolveCountResolvesCountWithFakerWhenCountIsBetweenAndFakerReturnsMinimum(int $minimum): void
{
$maximum = self::faker()->numberBetween($minimum + 1);

$strategy = new FieldResolution\DefaultStrategy();

$resolved = $strategy->resolveCount(
new Test\Double\Faker\MinimumGenerator(),
Count::between(
$minimum,
$maximum,
),
);

self::assertSame($minimum, $resolved);
}

#[Framework\Attributes\DataProviderExternal(Test\DataProvider\IntProvider::class, 'greaterThanZero')]
public function testResolveCountResolvesCountWithFakerWhenCountIsBetweenAndFakerReturnsMaximum(int $maximum): void
{
$minimum = self::faker()->numberBetween(0, $maximum - 1);

$strategy = new FieldResolution\DefaultStrategy();

$resolved = $strategy->resolveCount(
new Test\Double\Faker\MaximumGenerator(),
Count::between(
$minimum,
$maximum,
),
);

self::assertSame($maximum, $resolved);
}

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

$minimum = $faker->numberBetween(1);
$maximum = $faker->numberBetween($minimum + 1);

$strategy = new FieldResolution\DefaultStrategy();

$resolved = $strategy->resolveCount(
$faker,
Count::between(
$minimum,
$maximum,
),
);

self::assertGreaterThanOrEqual($minimum, $resolved);
self::assertLessThanOrEqual($maximum, $resolved);
}
}
1 change: 1 addition & 0 deletions test/Unit/FixtureFactory/UsingDefaultStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#[Framework\Attributes\UsesClass(FieldDefinition\References::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Sequence::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Value::class)]
#[Framework\Attributes\UsesClass(FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy::class)]
#[Framework\Attributes\UsesClass(FieldResolution\DefaultStrategy::class)]
final class UsingDefaultStrategyTest extends Test\Unit\AbstractTestCase
{
Expand Down
1 change: 1 addition & 0 deletions test/Unit/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#[Framework\Attributes\UsesClass(FieldDefinition\References::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Sequence::class)]
#[Framework\Attributes\UsesClass(FieldDefinition\Value::class)]
#[Framework\Attributes\UsesClass(FieldResolution\BetweenMinimumAndMaximumCountResolutionStrategy::class)]
#[Framework\Attributes\UsesClass(FieldResolution\DefaultStrategy::class)]
final class FixtureFactoryTest extends AbstractTestCase
{
Expand Down
Loading