Completed
Pull Request — master (#84)
by
unknown
01:18
created

FilterResponseTest::provideToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TraderInteractiveTest;
4
5
use PHPUnit\Framework\TestCase;
6
use TraderInteractive\FilterResponse;
7
8
/**
9
 * @coversDefaultClass \TraderInteractive\FilterResponse
10
 */
11
class FilterResponseTest extends TestCase
12
{
13
    /**
14
     * @test
15
     * @covers ::__construct
16
     */
17
    public function construct()
18
    {
19
        $value = ['foo' => 'bar'];
20
        $errors = [];
21
        $unknowns = ['other' => 'unknown'];
22
23
        $response = new FilterResponse($value, $errors, $unknowns);
24
25
        $this->assertSame(true, $response->success);
26
        $this->assertSame($value, $response->filteredValue);
27
        $this->assertSame($errors, $response->errors);
28
        $this->assertSame(null, $response->errorMessage);
29
        $this->assertSame($unknowns, $response->unknowns);
30
    }
31
32
    /**
33
     * @test
34
     * @covers ::__construct
35
     */
36
    public function constructWithErrors()
37
    {
38
        $value = ['foo' => 'bar'];
39
        $errors = ['something bad happened', 'and something else too'];
40
        $unknowns = ['other' => 'unknown'];
41
42
        $response = new FilterResponse($value, $errors, $unknowns);
43
44
        $this->assertSame(false, $response->success);
45
        $this->assertSame($value, $response->filteredValue);
46
        $this->assertSame($errors, $response->errors);
47
        $this->assertSame("something bad happened\nand something else too", $response->errorMessage);
48
        $this->assertSame($unknowns, $response->unknowns);
49
    }
50
51
    /**
52
     * @test
53
     * @covers ::__construct
54
     */
55
    public function constructDefault()
56
    {
57
        $input = ['filtered' => 'input'];
58
59
        $response = new FilterResponse($input);
60
61
        $this->assertSame(true, $response->success);
62
        $this->assertSame($input, $response->filteredValue);
63
        $this->assertSame([], $response->errors);
64
        $this->assertSame(null, $response->errorMessage);
65
        $this->assertSame([], $response->unknowns);
66
    }
67
68
    /**
69
     * @test
70
     * @covers ::toArray
71
     * @dataProvider provideToArray
72
     *
73
     * @param array $value    The filtered value to pass to the response.
74
     * @param array $errors   The errors to pass to the response.
75
     * @param array $unknowns The unknowns to pass to the response.
76
     * @param array $expected The expected array value.
77
     */
78
    public function toArray(array $value, array $errors, array $unknowns, array $expected)
79
    {
80
        $response = new FilterResponse($value, $errors, $unknowns);
81
        $arrayResponse = $response->toArray();
82
83
        $this->assertSame($expected, $arrayResponse);
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function provideToArray() : array
90
    {
91
        return [
92
            'success' => [
93
                'input' => ['foo' => 'bar'],
94
                'errors' => [],
95
                'unknowns' => ['other' => 'unknown'],
96
                'expected' => [true, ['foo' => 'bar'], null, ['other' => 'unknown']],
97
            ],
98
            'failure' => [
99
                'input' => ['foo' => 'bar'],
100
                'errors' => ['something bad happened', 'and something else too'],
101
                'unknowns' => ['other' => 'unknown'],
102
                'expected' => [false, null, "something bad happened\nand something else too", ['other' => 'unknown']],
103
            ],
104
        ];
105
    }
106
}
107