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 Count #262

Merged
merged 1 commit into from
Jul 7, 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: 1 addition & 1 deletion .github/workflows/integrate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on: # yamllint disable-line rule:truthy

env:
MIN_COVERED_MSI: 97
MIN_MSI: 94
MIN_MSI: 96
PHP_EXTENSIONS: "mbstring"

jobs:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ For a full diff see [`fa9c564...main`][fa9c564...main].
* Renamed `FixtureFactory::getList()` to `FixtureFactory::createMultiple()` ([#190]), by [@localheinz]
* Renamed `FixtureFactory::defineEntity()` to `FixtureFactory::define()` ([#197]), by [@localheinz]
* Extracted `FieldDefinition\Optional` ([#260]), by [@localheinz]
* Extracted `Count` ([#262]), by [@localheinz]

### Fixed

Expand Down Expand Up @@ -109,5 +110,6 @@ For a full diff see [`fa9c564...main`][fa9c564...main].
[#197]: https://github.com/ergebnis/factory-bot/pull/197
[#259]: https://github.com/ergebnis/factory-bot/pull/259
[#260]: https://github.com/ergebnis/factory-bot/pull/260
[#262]: https://github.com/ergebnis/factory-bot/pull/262

[@localheinz]: https://github.com/localheinz
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MIN_COVERED_MSI:=97
MIN_MSI:=94
MIN_MSI:=96

.PHONY: it
it: coding-standards static-code-analysis tests ## Runs the coding-standards, static-code-analysis, and tests targets
Expand Down
20 changes: 20 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ parameters:
count: 1
path: src/EntityDefinition.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FieldDefinition\\:\\:references\\(\\) has parameter \\$count with a nullable type declaration\\.$#"
count: 1
path: src/FieldDefinition.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FieldDefinition\\:\\:references\\(\\) has parameter \\$count with null as default value\\.$#"
count: 1
path: src/FieldDefinition.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:define\\(\\) has parameter \\$afterCreate with a nullable type declaration\\.$#"
count: 1
Expand All @@ -15,6 +25,16 @@ parameters:
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:createMultiple\\(\\) has parameter \\$count with a nullable type declaration\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:createMultiple\\(\\) has parameter \\$count with null as default value\\.$#"
count: 1
path: src/FixtureFactory.php

-
message: "#^Method Ergebnis\\\\FactoryBot\\\\FixtureFactory\\:\\:createCollectionFrom\\(\\) return type with generic class Doctrine\\\\Common\\\\Collections\\\\ArrayCollection does not specify its types\\: TKey, T$#"
count: 1
Expand Down
44 changes: 44 additions & 0 deletions src/Count.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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;

final class Count
{
/**
* @var int
*/
private $value;

/**
* @param int $value
*
* @throws Exception\InvalidCount
*/
public function __construct(int $value)
{
if (1 > $value) {
throw Exception\InvalidCount::notGreaterThanOrEqualTo(
1,
$value
);
}

$this->value = $value;
}

public function value(): int
{
return $this->value;
}
}
12 changes: 7 additions & 5 deletions src/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ public static function optionalReference(string $className): FieldDefinition\Opt
* @psalm-return FieldDefinition\References<T>
* @psalm-template T
*
* @param string $className
* @param int $count
*
* @throws Exception\InvalidCount
* @param string $className
* @param null|Count $count
*
* @return FieldDefinition\References
*/
public static function references(string $className, int $count = 1): FieldDefinition\References
public static function references(string $className, ?Count $count = null): FieldDefinition\References
{
if (null === $count) {
$count = new Count(1);
}

return new FieldDefinition\References(
$className,
$count
Expand Down
17 changes: 4 additions & 13 deletions src/FieldDefinition/References.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Ergebnis\FactoryBot\FieldDefinition;

use Ergebnis\FactoryBot\Exception;
use Ergebnis\FactoryBot\Count;
use Ergebnis\FactoryBot\FixtureFactory;

/**
Expand All @@ -35,7 +35,7 @@ final class References implements Resolvable
private $className;

/**
* @var int
* @var Count
*/
private $count;

Expand All @@ -45,19 +45,10 @@ final class References implements Resolvable
* @psalm-param class-string<T> $className
*
* @param string $className
* @param int $count
*
* @throws Exception\InvalidCount
* @param Count $count
*/
public function __construct(string $className, int $count)
public function __construct(string $className, Count $count)
{
if (1 > $count) {
throw Exception\InvalidCount::notGreaterThanOrEqualTo(
1,
$count
);
}

$this->className = $className;
$this->count = $count;
}
Expand Down
18 changes: 5 additions & 13 deletions src/FixtureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,26 +246,18 @@ public function create(string $className, array $fieldOverrides = [])
*
* @param string $className
* @param array<string, mixed> $fieldOverrides
* @param int $count
*
* @throws Exception\InvalidCount
* @param null|Count $count
*
* @return array<int, object>
*/
public function createMultiple(string $className, array $fieldOverrides = [], int $count = 1): array
public function createMultiple(string $className, array $fieldOverrides = [], ?Count $count = null): array
{
$minimumCount = 1;

if ($minimumCount > $count) {
throw Exception\InvalidCount::notGreaterThanOrEqualTo(
$minimumCount,
$count
);
if (null === $count) {
$count = new Count(1);
}

$instances = [];

for ($i = 0; $i < $count; ++$i) {
for ($i = 0; $count->value() > $i; ++$i) {
$instances[] = $this->create(
$className,
$fieldOverrides
Expand Down
57 changes: 57 additions & 0 deletions test/Unit/CountTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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;

use Ergebnis\FactoryBot\Count;
use Ergebnis\FactoryBot\Exception;
use PHPUnit\Framework;

/**
* @internal
*
* @covers \Ergebnis\FactoryBot\Count
*
* @uses \Ergebnis\FactoryBot\Exception\InvalidCount
*/
final class CountTest extends Framework\TestCase
{
/**
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\NumberProvider::intLessThanOne()
*
* @param int $value
*/
public function testConstructorRejectsInvalidValue(int $value): void
{
$this->expectException(Exception\InvalidCount::class);
$this->expectExceptionMessage(\sprintf(
'Count needs to be greater than or equal to %d, but %d is not.',
1,
$value
));

new Count($value);
}

/**
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\NumberProvider::intGreaterThanOne()
*
* @param int $value
*/
public function testConstructorSetsValue(int $value): void
{
$count = new Count($value);

self::assertSame($value, $count->value());
}
}
29 changes: 6 additions & 23 deletions test/Unit/FieldDefinition/ReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Ergebnis\FactoryBot\Test\Unit\FieldDefinition;

use Ergebnis\FactoryBot\Exception;
use Ergebnis\FactoryBot\Count;
use Ergebnis\FactoryBot\FieldDefinition\References;
use Ergebnis\FactoryBot\FixtureFactory;
use Ergebnis\FactoryBot\Test\Fixture;
Expand All @@ -24,6 +24,7 @@
*
* @covers \Ergebnis\FactoryBot\FieldDefinition\References
*
* @uses \Ergebnis\FactoryBot\Count
* @uses \Ergebnis\FactoryBot\EntityDefinition
* @uses \Ergebnis\FactoryBot\Exception\InvalidCount
* @uses \Ergebnis\FactoryBot\FieldDefinition
Expand All @@ -32,33 +33,15 @@
*/
final class ReferencesTest extends AbstractTestCase
{
/**
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\NumberProvider::intLessThanOne()
*
* @param int $count
*/
public function testConstructorRejectsInvalidCount(int $count): void
{
$this->expectException(Exception\InvalidCount::class);
$this->expectExceptionMessage(\sprintf(
'Count needs to be greater than or equal to 1, but %d is not.',
$count
));

new References(
Fixture\FixtureFactory\Entity\User::class,
$count
);
}

/**
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\NumberProvider::intGreaterThanOne()
*
* @param int $count
* @param int $value
*/
public function testResolvesToArrayOfObjectsCreatedByFixtureFactory(int $count): void
public function testResolvesToArrayOfObjectsCreatedByFixtureFactory(int $value): void
{
$className = Fixture\FixtureFactory\Entity\User::class;
$count = new Count($value);

$fixtureFactory = new FixtureFactory(
self::entityManager(),
Expand All @@ -75,7 +58,7 @@ public function testResolvesToArrayOfObjectsCreatedByFixtureFactory(int $count):
$resolved = $fieldDefinition->resolve($fixtureFactory);

self::assertIsArray($resolved);
self::assertCount($count, $resolved);
self::assertCount($count->value(), $resolved);
self::assertContainsOnly($className, $resolved);
}
}
27 changes: 7 additions & 20 deletions test/Unit/FieldDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Ergebnis\FactoryBot\Test\Unit;

use Ergebnis\FactoryBot\Count;
use Ergebnis\FactoryBot\Exception;
use Ergebnis\FactoryBot\FieldDefinition;
use Ergebnis\FactoryBot\FixtureFactory;
Expand All @@ -23,6 +24,7 @@
*
* @covers \Ergebnis\FactoryBot\FieldDefinition
*
* @uses \Ergebnis\FactoryBot\Count
* @uses \Ergebnis\FactoryBot\Exception\InvalidCount
* @uses \Ergebnis\FactoryBot\Exception\InvalidSequence
* @uses \Ergebnis\FactoryBot\FieldDefinition\Closure
Expand Down Expand Up @@ -92,32 +94,16 @@ public function testOptionalReferenceReturnsOptionalReference(): void
self::assertEquals($expected, $fieldDefinition);
}

/**
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\NumberProvider::intLessThanOne()
*
* @param int $count
*/
public function testReferencesThrowsInvalidCountExceptionWhenCountIsLessThanOne(int $count): void
{
$className = self::class;

$this->expectException(Exception\InvalidCount::class);

FieldDefinition::references(
$className,
$count
);
}

public function testReferencesReturnsRequiredReferencesWhenCountIsNotSpecified(): void
{
$className = Fixture\FixtureFactory\Entity\User::class;
$count = new Count(1);

$fieldDefinition = FieldDefinition::references($className);

$expected = new FieldDefinition\References(
$className,
1
$count
);

self::assertEquals($expected, $fieldDefinition);
Expand All @@ -126,11 +112,12 @@ public function testReferencesReturnsRequiredReferencesWhenCountIsNotSpecified()
/**
* @dataProvider \Ergebnis\FactoryBot\Test\DataProvider\NumberProvider::intGreaterThanOne()
*
* @param int $count
* @param int $value
*/
public function testReferencesReturnsRequiredReferencesWhenCountIsSpecified(int $count): void
public function testReferencesReturnsRequiredReferencesWhenCountIsSpecified(int $value): void
{
$className = Fixture\FixtureFactory\Entity\User::class;
$count = new Count($value);

$fieldDefinition = FieldDefinition::references(
$className,
Expand Down
Loading