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 FieldDefinition\Value #160

Merged
merged 1 commit into from
Apr 2, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ For a full diff see [`fa9c564...master`][fa9c564...master].
* Removed the fluent interface from `FixtureFactory::defineEntity()` ([#131]), by [@localheinz]
* Extracted `FieldDefinition\Reference` ([#157]), by [@localheinz]
* Extracted `FieldDefinition\References` ([#159]), by [@localheinz]
* Extracted `FieldDefinition\Value` ([#160]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -86,5 +87,6 @@ For a full diff see [`fa9c564...master`][fa9c564...master].
[#155]: https://github.com/ergebnis/factory-bot/pull/155
[#157]: https://github.com/ergebnis/factory-bot/pull/157
[#159]: https://github.com/ergebnis/factory-bot/pull/159
[#160]: https://github.com/ergebnis/factory-bot/pull/160

[@localheinz]: https://github.com/localheinz
15 changes: 15 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ parameters:
count: 1
path: src/FieldDefinition/Resolvable.php

-
message: "#^Property Ergebnis\\\\FactoryBot\\\\FieldDefinition\\\\Value\\:\\:\\$value has no typehint specified\\.$#"
count: 1
path: src/FieldDefinition/Value.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FieldDefinition\\\\Value\\:\\:__construct\\(\\) has parameter \\$value with no typehint specified\\.$#"
count: 1
path: src/FieldDefinition/Value.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FieldDefinition\\\\Value\\:\\:resolve\\(\\) has no return typehint specified\\.$#"
count: 1
path: src/FieldDefinition/Value.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:defineEntity\\(\\) has parameter \\$afterCreate with a nullable type declaration\\.$#"
count: 1
Expand Down
30 changes: 25 additions & 5 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
</MixedReturnStatement>
</file>
<file src="src/FieldDefinition.php">
<MissingClosureReturnType occurrences="2">
<MissingClosureReturnType occurrences="1">
<code>static function () use (&amp;$n, $funcOrString) {</code>
<code>static function () use ($value) {</code>
</MissingClosureReturnType>
<MissingParamType occurrences="1">
<code>$value</code>
Expand Down Expand Up @@ -56,6 +55,17 @@
<code>resolve</code>
</MissingReturnType>
</file>
<file src="src/FieldDefinition/Value.php">
<MissingParamType occurrences="1">
<code>$value</code>
</MissingParamType>
<MissingPropertyType occurrences="1">
<code>$value</code>
</MissingPropertyType>
<MissingReturnType occurrences="1">
<code>resolve</code>
</MissingReturnType>
</file>
<file src="src/FixtureFactory.php">
<MissingClosureReturnType occurrences="1">
<code>static function () use ($fieldDefinition) {</code>
Expand All @@ -82,6 +92,14 @@
<code>$collection</code>
</MixedAssignment>
</file>
<file src="test/DataProvider/ValueProvider.php">
<InvalidReturnType occurrences="1">
<code>\Generator&lt;string, array&lt;string[], bool, float, int, object, \stdClass, string&gt;&gt;</code>
</InvalidReturnType>
<TooManyTemplateParams occurrences="1">
<code>\Generator&lt;string, array&lt;string[], bool, float, int, object, \stdClass, string&gt;&gt;</code>
</TooManyTemplateParams>
</file>
<file src="test/Fixture/FixtureFactory/Entity/Organization.php">
<PropertyNotSetInConstructor occurrences="1">
<code>$id</code>
Expand Down Expand Up @@ -116,9 +134,11 @@
<MixedArgumentTypeCoercion occurrences="1">
<code>$fieldDefinitions</code>
</MixedArgumentTypeCoercion>
<MixedInferredReturnType occurrences="1">
<code>\Generator</code>
</MixedInferredReturnType>
</file>
<file src="test/Unit/FieldDefinition/ValueTest.php">
<MixedAssignment occurrences="1">
<code>$resolved</code>
</MixedAssignment>
</file>
<file src="test/Unit/FieldDefinitionTest.php">
<MixedAssignment occurrences="1">
Expand Down
6 changes: 2 additions & 4 deletions src/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ public static function references(string $className, int $count = 1): FieldDefin
);
}

public static function value($value): self
public static function value($value): FieldDefinition\Value
{
return new self(static function () use ($value) {
return $value;
});
return new FieldDefinition\Value($value);
}
}
34 changes: 34 additions & 0 deletions src/FieldDefinition/Value.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\FieldDefinition;

use Ergebnis\FactoryBot\FixtureFactory;

/**
* @internal
*/
final class Value implements Resolvable
{
private $value;

public function __construct($value)
{
$this->value = $value;
}

public function resolve(FixtureFactory $fixtureFactory)
{
return $this->value;
}
}
46 changes: 46 additions & 0 deletions test/DataProvider/ValueProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Test\DataProvider;

use Ergebnis\Test\Util\Helper;

final class ValueProvider
{
use Helper;

/**
* @return \Generator<string, array<string[], bool, float, int, object, \stdClass, string>>
*/
public function arbitrary(): \Generator
{
$faker = self::faker();

$values = [
'array' => $faker->words,
'bool-false' => false,
'bool-true' => true,
'float' => $faker->randomFloat(),
'int' => $faker->numberBetween(),
'object' => new \stdClass(),
'resource' => \fopen(__FILE__, 'rb'),
'string' => $faker->sentence,
];

foreach ($values as $key => $value) {
yield $key => [
$value,
];
}
}
}
1 change: 1 addition & 0 deletions test/Integration/FixtureFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* @uses \Ergebnis\FactoryBot\EntityDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition\Reference
* @uses \Ergebnis\FactoryBot\FieldDefinition\Value
*/
final class FixtureFactoryTest extends AbstractTestCase
{
Expand Down
1 change: 1 addition & 0 deletions test/Unit/Definition/DefinitionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @uses \Ergebnis\FactoryBot\Exception\InvalidDefinition
* @uses \Ergebnis\FactoryBot\Exception\InvalidDirectory
* @uses \Ergebnis\FactoryBot\FieldDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition\Value
* @uses \Ergebnis\FactoryBot\FixtureFactory
*/
final class DefinitionsTest extends AbstractTestCase
Expand Down
24 changes: 1 addition & 23 deletions test/Unit/EntityDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class EntityDefinitionTest extends Framework\TestCase
use Helper;

/**
* @dataProvider provideNotFieldDefinition
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\ValueProvider::arbitrary()
*
* @param mixed $fieldDefinition
*/
Expand All @@ -57,28 +57,6 @@ static function ($entity, array $fieldValues): void {
);
}

public function provideNotFieldDefinition(): \Generator
{
$faker = self::faker();

$values = [
'array' => $faker->words,
'bool-false' => false,
'bool-true' => true,
'float' => $faker->randomFloat(),
'int' => $faker->numberBetween(),
'object' => new \stdClass(),
'resource' => \fopen(__FILE__, 'rb'),
'string' => $faker->sentence,
];

foreach ($values as $key => $value) {
yield $key => [
$value,
];
}
}

public function testConstructorSetsValues(): void
{
$classMetadata = $this->prophesize(ORM\Mapping\ClassMetadata::class);
Expand Down
1 change: 1 addition & 0 deletions test/Unit/FieldDefinition/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @uses \Ergebnis\FactoryBot\EntityDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition\Value
* @uses \Ergebnis\FactoryBot\FixtureFactory
*/
final class ReferenceTest 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 @@ -27,6 +27,7 @@
* @uses \Ergebnis\FactoryBot\EntityDefinition
* @uses \Ergebnis\FactoryBot\Exception\InvalidCount
* @uses \Ergebnis\FactoryBot\FieldDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition\Value
* @uses \Ergebnis\FactoryBot\FixtureFactory
*/
final class ReferencesTest extends AbstractTestCase
Expand Down
46 changes: 46 additions & 0 deletions test/Unit/FieldDefinition/ValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Test\Unit\FieldDefinition;

use Ergebnis\FactoryBot\FieldDefinition\Value;
use Ergebnis\FactoryBot\FixtureFactory;
use Ergebnis\FactoryBot\Test\Unit\AbstractTestCase;

/**
* @internal
*
* @covers \Ergebnis\FactoryBot\FieldDefinition\Value
*
* @uses \Ergebnis\FactoryBot\EntityDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition
* @uses \Ergebnis\FactoryBot\FixtureFactory
*/
final class ValueTest extends AbstractTestCase
{
/**
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\ValueProvider::arbitrary()
*
* @param mixed $value
*/
public function testResolveReturnsValue($value): void
{
$fixtureFactory = new FixtureFactory(self::entityManager());

$fieldDefinition = new Value($value);

$resolved = $fieldDefinition->resolve($fixtureFactory);

self::assertSame($value, $resolved);
}
}
1 change: 1 addition & 0 deletions test/Unit/FieldDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* @uses \Ergebnis\FactoryBot\Exception\InvalidCount
* @uses \Ergebnis\FactoryBot\FieldDefinition\Reference
* @uses \Ergebnis\FactoryBot\FieldDefinition\References
* @uses \Ergebnis\FactoryBot\FieldDefinition\Value
* @uses \Ergebnis\FactoryBot\FixtureFactory
*/
final class FieldDefinitionTest extends 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 @@ -35,6 +35,7 @@
* @uses \Ergebnis\FactoryBot\FieldDefinition
* @uses \Ergebnis\FactoryBot\FieldDefinition\Reference
* @uses \Ergebnis\FactoryBot\FieldDefinition\References
* @uses \Ergebnis\FactoryBot\FieldDefinition\Value
*/
final class FixtureFactoryTest extends AbstractTestCase
{
Expand Down