CheckMethods   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 93.94%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 29
c 1
b 0
f 0
dl 0
loc 99
ccs 31
cts 33
cp 0.9394
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkAddMulti() 0 14 1
A checkSetMethod() 0 12 1
A checkAddSingle() 0 27 5
1
<?php
2
3
namespace VGirol\JsonApiFaker\Testing;
4
5
use PHPUnit\Framework\Assert as PHPUnit;
6
use VGirol\JsonApiFaker\Contract\FactoryContract;
7
use VGirol\JsonApiFaker\Exception\JsonApiFakerException;
8
9
/**
10
 * Add the ability to unit test some usual methods like getter, setter, ...
11
 */
12
trait CheckMethods
13
{
14
    /**
15
     * Undocumented function
16
     *
17
     * @param FactoryContract $factory
18
     * @param string          $setter
19
     * @param string          $getter
20
     * @param array           $data1
21
     * @param array           $data2
22
     *
23
     * @return void
24
     * @throws JsonApiFakerException
25
     * @throws \PHPUnit\Framework\AssertionFailedError
26
     */
27 12
    protected function checkAddSingle($factory, string $setter, string $getter, array $data1, array $data2)
28
    {
29 12
        PHPUnit::assertEmpty($factory->{$getter}());
30
31 12
        if (empty($data1)) {
32
            throw new JsonApiFakerException('You must provide data for first run.');
33
        }
34
35 12
        foreach ($data1 as $key => $value) {
36 12
            $obj = $factory->{$setter}($key, $value);
37
38 12
            PHPUnit::assertSame($obj, $factory);
39
        }
40
41 12
        PHPUnit::assertEquals($data1, $factory->{$getter}());
42
43 12
        if (empty($data2)) {
44
            throw new JsonApiFakerException('You must provide data for second run.');
45
        }
46
47 12
        foreach ($data2 as $key => $value) {
48 12
            $factory->{$setter}($key, $value);
49
        }
50
51 12
        PHPUnit::assertEquals(
52 12
            array_merge($data1, $data2),
53 12
            $factory->{$getter}()
54
        );
55 12
    }
56
57
    /**
58
     * Undocumented function
59
     *
60
     * @param FactoryContract $factory
61
     * @param string          $setter
62
     * @param string          $getter
63
     * @param array           $data1
64
     * @param array           $data2
65
     *
66
     * @return void
67
     * @throws JsonApiFakerException
68
     * @throws \PHPUnit\Framework\AssertionFailedError
69
     */
70 6
    protected function checkAddMulti($factory, string $setter, string $getter, array $data1, array $data2)
71
    {
72 6
        PHPUnit::assertEmpty($factory->{$getter}());
73
74 6
        $obj = $factory->{$setter}($data1);
75
76 6
        PHPUnit::assertEquals($data1, $factory->{$getter}());
77 6
        PHPUnit::assertSame($obj, $factory);
78
79 6
        $factory->{$setter}($data2);
80
81 6
        PHPUnit::assertEquals(
82 6
            array_merge($data1, $data2),
83 6
            $factory->{$getter}()
84
        );
85 6
    }
86
87
    /**
88
     * Undocumented function
89
     *
90
     * @param FactoryContract $factory
91
     * @param string $setter
92
     * @param string $getter
93
     * @param mixed $data1
94
     * @param mixed $data2
95
     *
96
     * @return void
97
     * @throws \PHPUnit\Framework\AssertionFailedError
98
     */
99 33
    protected function checkSetMethod($factory, string $setter, string $getter, $data1, $data2)
100
    {
101 33
        PHPUnit::assertEmpty($factory->{$getter}());
102
103 33
        $obj = $factory->{$setter}($data1);
104
105 33
        PHPUnit::assertEquals($data1, $factory->{$getter}());
106 33
        PHPUnit::assertSame($obj, $factory);
107
108 33
        $factory->{$setter}($data2);
109
110 33
        PHPUnit::assertEquals($data2, $factory->{$getter}());
111 33
    }
112
}
113