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
|
|
|
*/ |
19
|
|
|
public function construct() |
20
|
|
|
{ |
21
|
|
|
$value = ['foo' => 'bar']; |
22
|
|
|
$errors = []; |
23
|
|
|
$unknowns = ['other' => 'unknown']; |
24
|
|
|
|
25
|
|
|
$response = new FilterResponse($value, $errors, $unknowns); |
26
|
|
|
|
27
|
|
|
$this->assertSame(true, $response->success); |
28
|
|
|
$this->assertSame($value, $response->filteredValue); |
29
|
|
|
$this->assertSame($errors, $response->errors); |
30
|
|
|
$this->assertSame(null, $response->errorMessage); |
31
|
|
|
$this->assertSame($unknowns, $response->unknowns); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
* @covers ::__construct |
37
|
|
|
*/ |
38
|
|
|
public function constructWithErrors() |
39
|
|
|
{ |
40
|
|
|
$value = ['foo' => 'bar']; |
41
|
|
|
$errors = ['something bad happened', 'and something else too']; |
42
|
|
|
$unknowns = ['other' => 'unknown']; |
43
|
|
|
|
44
|
|
|
$response = new FilterResponse($value, $errors, $unknowns); |
45
|
|
|
|
46
|
|
|
$this->assertSame(false, $response->success); |
47
|
|
|
$this->assertSame($value, $response->filteredValue); |
48
|
|
|
$this->assertSame($errors, $response->errors); |
49
|
|
|
$this->assertSame("something bad happened\nand something else too", $response->errorMessage); |
50
|
|
|
$this->assertSame($unknowns, $response->unknowns); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @test |
55
|
|
|
* @covers ::__construct |
56
|
|
|
*/ |
57
|
|
|
public function constructDefault() |
58
|
|
|
{ |
59
|
|
|
$input = ['filtered' => 'input']; |
60
|
|
|
|
61
|
|
|
$response = new FilterResponse($input); |
62
|
|
|
|
63
|
|
|
$this->assertSame(true, $response->success); |
64
|
|
|
$this->assertSame($input, $response->filteredValue); |
65
|
|
|
$this->assertSame([], $response->errors); |
66
|
|
|
$this->assertSame(null, $response->errorMessage); |
67
|
|
|
$this->assertSame([], $response->unknowns); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @test |
72
|
|
|
* @covers ::__construct |
73
|
|
|
*/ |
74
|
|
|
public function gettingInvalidPropertyThrowsException() |
75
|
|
|
{ |
76
|
|
|
$this->expectException(InvalidArgumentException::class); |
77
|
|
|
$this->expectExceptionMessage("Property 'foo' does not exist"); |
78
|
|
|
|
79
|
|
|
$response = new FilterResponse([]); |
80
|
|
|
$response->foo; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @test |
85
|
|
|
* @covers ::__construct |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function settingValidPropertyThrowsAnException() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->expectException(ReadOnlyViolationException::class); |
90
|
|
|
$this->expectExceptionMessage("Property 'success' is read-only"); |
91
|
|
|
|
92
|
|
|
$response = new FilterResponse([]); |
93
|
|
|
$response->success = false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @test |
98
|
|
|
* @covers ::__construct |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function settingInvalidPropertyThrowsAnException() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$this->expectException(InvalidArgumentException::class); |
103
|
|
|
$this->expectExceptionMessage("Property 'foo' does not exist"); |
104
|
|
|
|
105
|
|
|
$response = new FilterResponse([]); |
106
|
|
|
$response->foo = false; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.