FilterResponseTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 38
c 2
b 0
f 0
dl 0
loc 100
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 13 1
A settingInvalidPropertyThrowsAnException() 0 7 1
A constructWithErrors() 0 13 1
A settingValidPropertyThrowsAnException() 0 7 1
A constructDefault() 0 11 1
A gettingInvalidPropertyThrowsException() 0 7 1
1
<?php
2
3
namespace TraderInteractiveTest;
4
5
use InvalidArgumentException;
6
use PHPUnit\Framework\TestCase;
7
use TraderInteractive\Exceptions\ReadOnlyViolationException;
8
use TraderInteractive\FilterResponse;
9
10
/**
11
 * @coversDefaultClass \TraderInteractive\FilterResponse
12
 */
13
class FilterResponseTest extends TestCase
14
{
15
    /**
16
     * @test
17
     * @covers ::__construct
18
     * @covers ::__get
19
     */
20
    public function construct()
21
    {
22
        $value = ['foo' => 'bar'];
23
        $errors = [];
24
        $unknowns = ['other' => 'unknown'];
25
26
        $response = new FilterResponse($value, $errors, $unknowns);
27
28
        $this->assertSame(true, $response->success);
29
        $this->assertSame($value, $response->filteredValue);
30
        $this->assertSame($errors, $response->errors);
31
        $this->assertSame(null, $response->errorMessage);
32
        $this->assertSame($unknowns, $response->unknowns);
33
    }
34
35
    /**
36
     * @test
37
     * @covers ::__construct
38
     * @covers ::__get
39
     */
40
    public function constructWithErrors()
41
    {
42
        $value = ['foo' => 'bar'];
43
        $errors = ['something bad happened', 'and something else too'];
44
        $unknowns = ['other' => 'unknown'];
45
46
        $response = new FilterResponse($value, $errors, $unknowns);
47
48
        $this->assertSame(false, $response->success);
49
        $this->assertSame($value, $response->filteredValue);
50
        $this->assertSame($errors, $response->errors);
51
        $this->assertSame("something bad happened\nand something else too", $response->errorMessage);
52
        $this->assertSame($unknowns, $response->unknowns);
53
    }
54
55
    /**
56
     * @test
57
     * @covers ::__construct
58
     * @covers ::__get
59
     */
60
    public function constructDefault()
61
    {
62
        $input = ['filtered' => 'input'];
63
64
        $response = new FilterResponse($input);
65
66
        $this->assertSame(true, $response->success);
67
        $this->assertSame($input, $response->filteredValue);
68
        $this->assertSame([], $response->errors);
69
        $this->assertSame(null, $response->errorMessage);
70
        $this->assertSame([], $response->unknowns);
71
    }
72
73
    /**
74
     * @test
75
     * @covers ::__construct
76
     * @covers ::__get
77
     */
78
    public function gettingInvalidPropertyThrowsException()
79
    {
80
        $this->expectException(InvalidArgumentException::class);
81
        $this->expectExceptionMessage("Property 'foo' does not exist");
82
83
        $response = new FilterResponse([]);
84
        $response->foo;
0 ignored issues
show
Bug Best Practice introduced by
The property foo does not exist on TraderInteractive\FilterResponse. Since you implemented __get, consider adding a @property annotation.
Loading history...
85
    }
86
87
    /**
88
     * @test
89
     * @covers ::__construct
90
     * @covers ::__set
91
     */
92
    public function settingValidPropertyThrowsAnException()
93
    {
94
        $this->expectException(ReadOnlyViolationException::class);
95
        $this->expectExceptionMessage("Property 'success' is read-only");
96
97
        $response = new FilterResponse([]);
98
        $response->success = false;
99
    }
100
101
    /**
102
     * @test
103
     * @covers ::__construct
104
     * @covers ::__set
105
     */
106
    public function settingInvalidPropertyThrowsAnException()
107
    {
108
        $this->expectException(InvalidArgumentException::class);
109
        $this->expectExceptionMessage("Property 'foo' does not exist");
110
111
        $response = new FilterResponse([]);
112
        $response->foo = false;
0 ignored issues
show
Bug Best Practice introduced by
The property foo does not exist on TraderInteractive\FilterResponse. Since you implemented __set, consider adding a @property annotation.
Loading history...
113
    }
114
}
115