Completed
Push — master ( 02cd0c...4a56be )
by Kevin
26s queued 19s
created

StoryTest::can_get_random_object_from_pool()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Functional;
4
5
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
6
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7
use Zenstruck\Foundry\Test\Factories;
8
use Zenstruck\Foundry\Test\ResetDatabase;
9
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;
10
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;
11
use Zenstruck\Foundry\Tests\Fixtures\Stories\CategoryPoolStory;
12
use Zenstruck\Foundry\Tests\Fixtures\Stories\CategoryStory;
13
use Zenstruck\Foundry\Tests\Fixtures\Stories\PostStory;
14
use Zenstruck\Foundry\Tests\Fixtures\Stories\ServiceStory;
15
16
/**
17
 * @author Kevin Bond <[email protected]>
18
 */
19
final class StoryTest extends KernelTestCase
20
{
21
    use ExpectDeprecationTrait, Factories, ResetDatabase;
22
23
    protected function setUp(): void
24
    {
25
        if (false === \getenv('DATABASE_URL')) {
26
            self::markTestSkipped('doctrine/orm not enabled.');
27
        }
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public function stories_are_only_loaded_once(): void
34
    {
35
        PostFactory::assert()->empty();
36
37
        PostStory::load();
38
        PostStory::load();
39
        PostStory::load();
40
41
        PostFactory::assert()->count(4);
42
    }
43
44
    /**
45
     * @test
46
     */
47
    public function can_access_managed_proxies_via_magic_call(): void
48
    {
49
        $this->assertSame('php', CategoryStory::load()->php()->getName());
0 ignored issues
show
Bug introduced by
The method php() does not exist on Zenstruck\Foundry\Story. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->assertSame('php', CategoryStory::load()->/** @scrutinizer ignore-call */ php()->getName());
Loading history...
Bug introduced by
The method getName() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->assertSame('php', CategoryStory::load()->php()->/** @scrutinizer ignore-call */ getName());
Loading history...
50
    }
51
52
    /**
53
     * @test
54
     */
55
    public function can_access_managed_proxies_via_magic_call_static(): void
56
    {
57
        $this->assertSame('php', CategoryStory::php()->getName());
0 ignored issues
show
Bug introduced by
The method php() does not exist on Zenstruck\Foundry\Tests\...s\Stories\CategoryStory. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        $this->assertSame('php', CategoryStory::/** @scrutinizer ignore-call */ php()->getName());
Loading history...
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function cannot_access_invalid_object(): void
64
    {
65
        $this->expectException(\InvalidArgumentException::class);
66
67
        CategoryStory::load()->get('invalid');
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function stories_can_be_services(): void
74
    {
75
        if (!\getenv('USE_FOUNDRY_BUNDLE')) {
76
            $this->markTestSkipped('Stories cannot be services without the foundry bundle.');
77
        }
78
79
        $this->assertSame('From Service', ServiceStory::post()->getTitle());
0 ignored issues
show
Bug introduced by
The method getTitle() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $this->assertSame('From Service', ServiceStory::post()->/** @scrutinizer ignore-call */ getTitle());
Loading history...
Bug introduced by
The method post() does not exist on Zenstruck\Foundry\Tests\...es\Stories\ServiceStory. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $this->assertSame('From Service', ServiceStory::/** @scrutinizer ignore-call */ post()->getTitle());
Loading history...
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function service_stories_cannot_be_used_without_the_bundle(): void
86
    {
87
        if (\getenv('USE_FOUNDRY_BUNDLE')) {
88
            $this->markTestSkipped('ZenstruckFoundryBundle enabled.');
89
        }
90
91
        $this->expectException(\RuntimeException::class);
92
        $this->expectExceptionMessage('Stories with dependencies (Story services) cannot be used without the foundry bundle.');
93
94
        ServiceStory::load();
95
    }
96
97
    /**
98
     * @test
99
     * @group legacy
100
     */
101
    public function calling_add_is_deprecated(): void
102
    {
103
        $this->expectDeprecation('Since zenstruck\foundry 1.17.0: Using Story::add() is deprecated, use Story::addState().');
104
105
        CategoryFactory::assert()->empty();
106
107
        CategoryStory::load()->add('foo', CategoryFactory::new());
108
109
        CategoryFactory::assert()->count(3);
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function can_get_random_object_from_pool(): void
116
    {
117
        $ids = [];
118
119
        while (5 !== \count(\array_unique($ids))) {
120
            $ids[] = CategoryPoolStory::getRandom('pool-name')->getId();
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Zenstruck\Foundry\Proxy. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
            $ids[] = CategoryPoolStory::getRandom('pool-name')->/** @scrutinizer ignore-call */ getId();
Loading history...
121
        }
122
123
        $this->assertCount(5, \array_unique($ids));
124
    }
125
126
    /**
127
     * @test
128
     */
129
    public function can_get_random_object_set_from_pool(): void
130
    {
131
        $objects = CategoryPoolStory::getRandomSet('pool-name', 3);
132
133
        $this->assertCount(3, $objects);
134
        $this->assertCount(3, \array_unique(\array_map(static function($category) { return $category->getId(); }, $objects)));
135
    }
136
137
    /**
138
     * @test
139
     */
140
    public function can_get_random_object_range_from_pool(): void
141
    {
142
        $counts = [];
143
144
        while (4 !== \count(\array_unique($counts))) {
145
            $counts[] = \count(CategoryPoolStory::getRandomRange('pool-name', 0, 3));
146
        }
147
148
        $this->assertCount(4, \array_unique($counts));
149
        $this->assertContains(0, $counts);
150
        $this->assertContains(1, $counts);
151
        $this->assertContains(2, $counts);
152
        $this->assertContains(3, $counts);
153
        $this->assertNotContains(4, $counts);
154
        $this->assertNotContains(5, $counts);
155
    }
156
157
    /**
158
     * @test
159
     */
160
    public function random_set_number_must_be_positive(): void
161
    {
162
        $this->expectException(\InvalidArgumentException::class);
163
164
        CategoryPoolStory::getRandomSet('pool-name', 0);
165
    }
166
167
    /**
168
     * @test
169
     */
170
    public function random_range_min_must_be_zero_or_greater(): void
171
    {
172
        $this->expectException(\InvalidArgumentException::class);
173
174
        CategoryPoolStory::getRandomRange('pool-name', -1, 25);
175
    }
176
177
    /**
178
     * @test
179
     */
180
    public function random_range_min_must_be_less_than_max(): void
181
    {
182
        $this->expectException(\InvalidArgumentException::class);
183
184
        CategoryPoolStory::getRandomRange('pool-name', 50, 25);
185
    }
186
187
    /**
188
     * @test
189
     */
190
    public function random_range_more_than_available(): void
191
    {
192
        $this->expectException(\RuntimeException::class);
193
194
        CategoryPoolStory::getRandomRange('pool-name', 0, 100);
195
    }
196
}
197