Total Complexity | 7 |
Total Lines | 40 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
8 | class NullValuesRandomizerTest extends TestCase |
||
9 | { |
||
10 | /** |
||
11 | * @dataProvider percentageProvider |
||
12 | * |
||
13 | * @param float $percentage |
||
14 | */ |
||
15 | public function testPercentage(float $percentage) |
||
16 | { |
||
17 | $randomizer = new NullValuesRandomizer($percentage); |
||
18 | $hits = 0; |
||
19 | $total = 1000000; |
||
20 | for ($i = 0; $i < $total; ++$i) { |
||
21 | $randomizer->ifShouldWrite() ? ++$hits : null; |
||
22 | } |
||
23 | |||
24 | $expected = (int)($percentage * 100); |
||
25 | $actual = round($hits / $total * 100); |
||
26 | |||
27 | $this->assertThat($actual, new RangeConstraint($expected - 1, $expected + 1)); |
||
28 | } |
||
29 | |||
30 | public function testThatZeroNeverCalled() |
||
31 | { |
||
32 | $randomizer = new NullValuesRandomizer(.0); |
||
33 | |||
34 | $hits = 0; |
||
35 | for ($i = 0; $i < 1000000; ++$i) { |
||
36 | $randomizer->ifShouldWrite() ? ++$hits : null; |
||
37 | } |
||
38 | |||
39 | $this->assertSame(0, $hits); |
||
40 | } |
||
41 | |||
42 | public function percentageProvider(): array |
||
48 | ]; |
||
49 | } |
||
50 | } |
||
51 |