Passed
Pull Request — master (#38)
by Kevin
19:12
created

FactoryCollectionTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
eloc 20
c 1
b 0
f 1
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A can_create_with_random_range() 0 16 2
A min_must_be_less_than_or_equal_to_max() 0 6 1
A can_create_with_static_size() 0 9 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Unit;
4
5
use Zenstruck\Foundry\FactoryCollection;
6
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
7
use Zenstruck\Foundry\Tests\UnitTestCase;
8
use function Zenstruck\Foundry\factory;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
final class FactoryCollectionTest extends UnitTestCase
14
{
15
    /**
16
     * @test
17
     */
18
    public function can_create_with_static_size(): void
19
    {
20
        $collection = new FactoryCollection(factory(Category::class)->withoutPersisting(), 2);
21
22
        $this->assertCount(2, $collection->create());
23
        $this->assertCount(2, $collection->create());
24
        $this->assertCount(2, $collection->create());
25
        $this->assertCount(2, $collection->create());
26
        $this->assertCount(2, $collection->create());
27
    }
28
29
    /**
30
     * @test
31
     */
32
    public function can_create_with_random_range(): void
33
    {
34
        $collection = new FactoryCollection(factory(Category::class)->withoutPersisting(), 0, 3);
35
        $counts = [];
36
37
        while (4 !== \count(\array_unique($counts))) {
38
            $counts[] = \count($collection->create());
39
        }
40
41
        $this->assertCount(4, \array_unique($counts));
42
        $this->assertContains(0, $counts);
43
        $this->assertContains(1, $counts);
44
        $this->assertContains(2, $counts);
45
        $this->assertContains(3, $counts);
46
        $this->assertNotContains(4, $counts);
47
        $this->assertNotContains(5, $counts);
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function min_must_be_less_than_or_equal_to_max(): void
54
    {
55
        $this->expectException(\InvalidArgumentException::class);
56
        $this->expectDeprecationMessage('Min must be less than max.');
57
58
        new FactoryCollection(factory(Category::class)->withoutPersisting(), 4, 3);
59
    }
60
}
61