1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use TraderInteractive\Exceptions\FilterException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Booleans |
10
|
|
|
* @covers ::<private> |
11
|
|
|
*/ |
12
|
|
|
final class BooleansTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @test |
16
|
|
|
* @covers ::filter |
17
|
|
|
*/ |
18
|
|
|
public function filterBasic() |
19
|
|
|
{ |
20
|
|
|
$this->assertTrue(Booleans::filter(true)); |
21
|
|
|
$this->assertTrue(Booleans::filter(' true')); |
22
|
|
|
$this->assertTrue(Booleans::filter(' TRUE ')); |
23
|
|
|
$this->assertTrue(Booleans::filter('True ')); |
24
|
|
|
|
25
|
|
|
$this->assertFalse(Booleans::filter('false ')); |
26
|
|
|
$this->assertFalse(Booleans::filter('FALSE ')); |
27
|
|
|
$this->assertFalse(Booleans::filter(' False ')); |
28
|
|
|
$this->assertFalse(Booleans::filter(false)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @test |
33
|
|
|
* @covers ::filter |
34
|
|
|
*/ |
35
|
|
|
public function filterAllowNullIsTrueAndNullValue() |
36
|
|
|
{ |
37
|
|
|
$this->assertNull(Booleans::filter(null, true)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
* @covers ::filter |
43
|
|
|
*/ |
44
|
|
|
public function filterAllowNullIsFalseAndNullValue() |
45
|
|
|
{ |
46
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
47
|
|
|
$this->expectExceptionMessage('Value failed filtering, $allowNull is set to false'); |
48
|
|
|
$this->assertNull(Booleans::filter(null, false)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
* @covers ::filter |
54
|
|
|
*/ |
55
|
|
|
public function filterNonStringAndNonBoolValue() |
56
|
|
|
{ |
57
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
58
|
|
|
$this->expectExceptionMessage('"1" $value is not a string'); |
59
|
|
|
Booleans::filter(1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @test |
64
|
|
|
* @covers ::filter |
65
|
|
|
*/ |
66
|
|
|
public function filterInvalidString() |
67
|
|
|
{ |
68
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
69
|
|
|
$this->expectExceptionMessage("invalid is not 'true' or 'false' disregarding case and whitespace"); |
70
|
|
|
Booleans::filter('invalid'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @test |
75
|
|
|
* @covers ::filter |
76
|
|
|
*/ |
77
|
|
|
public function filterCustomTrueValues() |
78
|
|
|
{ |
79
|
|
|
$this->assertTrue(Booleans::filter('Y', false, ['y'])); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @test |
84
|
|
|
* @covers ::filter |
85
|
|
|
*/ |
86
|
|
|
public function filterCustomFalseValues() |
87
|
|
|
{ |
88
|
|
|
$this->assertFalse(Booleans::filter('0', false, ['true'], ['0'])); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @test |
93
|
|
|
* @covers ::filter |
94
|
|
|
*/ |
95
|
|
|
public function filterCustomBoolValuesInvalidString() |
96
|
|
|
{ |
97
|
|
|
$this->expectException(\TraderInteractive\Exceptions\FilterException::class); |
98
|
|
|
$this->expectExceptionMessage("true is not 'y' or '1' or 'n' or '0' disregarding case and whitespace"); |
99
|
|
|
$this->assertFalse(Booleans::filter('true', false, ['y', '1'], ['n', '0'])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Verify basic behavior of convert(). |
104
|
|
|
* |
105
|
|
|
* @test |
106
|
|
|
* @covers ::convert |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function convert() |
111
|
|
|
{ |
112
|
|
|
$this->assertSame('yes', Booleans::convert(true, 'yes', 'no')); |
113
|
|
|
$this->assertSame('bar', Booleans::convert(false, 'foo', 'bar')); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|