Skip to content

Commit

Permalink
Merge pull request #375 from ergebnis/fix/without-optional
Browse files Browse the repository at this point in the history
Fix: Add WithoutOptionalStrategy
  • Loading branch information
ergebnis-bot committed Aug 5, 2020
2 parents abb051d + b994063 commit 2a906d3
Show file tree
Hide file tree
Showing 8 changed files with 793 additions and 4 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

For a full diff see [`0.3.0...main`][0.3.0...main].
For a full diff see [`0.3.1...main`][0.3.1...main].

## [`0.3.1`][0.3.1]

For a full diff see [`0.3.0...0.3.1`][0.3.0...0.3.1].

### Fixed

* Actually implemented `WithoutOptionalStrategy` ([#375]), by [@localheinz]

## [`0.3.0`][0.3.0]

Expand Down Expand Up @@ -123,12 +131,14 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0].
[0.2.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.2.0
[0.2.1]: https://github.com/ergebnis/factory-bot/releases/tag/0.2.1
[0.3.0]: https://github.com/ergebnis/factory-bot/releases/tag/0.3.0
[0.3.1]: https://github.com/ergebnis/factory-bot/releases/tag/0.3.1

[fa9c564...0.1.0]: https://github.com/ergebnis/factory-bot/compare/fa9c564...0.1.0
[0.1.0...0.2.0]: https://github.com/ergebnis/factory-bot/compare/0.1.0...0.2.0
[0.2.0...0.2.1]: https://github.com/ergebnis/factory-bot/compare/0.2.0...0.2.1
[0.2.1...0.3.0]: https://github.com/ergebnis/factory-bot/compare/0.2.1...0.3.0
[0.3.0...main]: https://github.com/ergebnis/factory-bot/compare/0.3.0...main
[0.3.0...0.3.1]: https://github.com/ergebnis/factory-bot/compare/0.3.0...0.3.1
[0.3.1...main]: https://github.com/ergebnis/factory-bot/compare/0.3.1...main

[#1]: https://github.com/ergebnis/factory-bot/pull/1
[#3]: https://github.com/ergebnis/factory-bot/pull/3
Expand Down Expand Up @@ -198,5 +208,6 @@ For a full diff see [`fa9c564...0.1.0`][fa9c564...0.1.0].
[#365]: https://github.com/ergebnis/factory-bot/pull/365
[#369]: https://github.com/ergebnis/factory-bot/pull/369
[#374]: https://github.com/ergebnis/factory-bot/pull/374
[#375]: https://github.com/ergebnis/factory-bot/pull/375

[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,6 @@ parameters:

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\) with 'Ergebnis\\\\\\\\FactoryBot\\\\\\\\FixtureFactory' and Ergebnis\\\\FactoryBot\\\\FixtureFactory will always evaluate to true\\.$#"
count: 2
count: 3
path: test/Unit/FixtureFactoryTest.php

11 changes: 10 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,14 @@
<code>$name</code>
<code>$fieldValues['name']</code>
</MixedArgument>
<RedundantCondition occurrences="6">
<RedundantCondition occurrences="7">
<code>assertNotNull</code>
<code>assertNotNull</code>
<code>assertInstanceOf</code>
<code>assertInstanceOf</code>
<code>assertInstanceOf</code>
<code>assertInstanceOf</code>
<code>assertInstanceOf</code>
</RedundantCondition>
<RedundantConditionGivenDocblockType occurrences="5">
<code>assertInstanceOf</code>
Expand All @@ -250,4 +251,12 @@
<code>$expected</code>
</MixedAssignment>
</file>
<file src="test/Unit/Strategy/WithoutOptionalStrategyTest.php">
<MixedAssignment occurrences="4">
<code>$resolved</code>
<code>$expected</code>
<code>$resolved</code>
<code>$expected</code>
</MixedAssignment>
</file>
</files>
19 changes: 19 additions & 0 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,25 @@ public function withOptional(): self
return $instance;
}

/**
* Returns a fixture factory that utilizes the WithoutOptionalStrategy.
*
* With this strategy
*
* - an optional reference is never resolved
* - references resolve to an array containing only the minimum number of references
*
* @return self
*/
public function withoutOptional(): self
{
$instance = clone $this;

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

return $instance;
}

/**
* Returns a fixture factory that persists entities after creation.
*
Expand Down
45 changes: 45 additions & 0 deletions src/Strategy/WithoutOptionalStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 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\Strategy;

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

/**
* @internal
*/
final class WithoutOptionalStrategy implements ResolutionStrategy
{
public function resolveFieldValue(
Generator $faker,
FixtureFactory $fixtureFactory,
FieldDefinition\Resolvable $fieldDefinition
) {
if ($fieldDefinition instanceof FieldDefinition\Optional) {
return null;
}

return $fieldDefinition->resolve(
$faker,
$fixtureFactory
);
}

public function resolveCount(Generator $faker, Count $count): int
{
return $count->minimum();
}
}
Loading

0 comments on commit 2a906d3

Please sign in to comment.