Passed
Push — master ( 8ba30c...5485cd )
by Oleg
03:02
created

NullValuesRandomizerTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A percentageProvider() 0 6 1
A testThatZeroNeverCalled() 0 10 3
A testPercentage() 0 13 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Generator\Tests;
5
6
use PHPUnit\Framework\TestCase;
7
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
43
    {
44
        return [
45
            [.1],
46
            [.5],
47
            [.0],
48
        ];
49
    }
50
}
51