1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Remi\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\Remi\FilteredRecord; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\Remi\FilteredRecord |
9
|
|
|
* @group onoi-remi |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 0.1 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class FilteredRecordTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
19
|
|
|
|
20
|
|
|
$this->assertInstanceOf( |
21
|
|
|
'\Onoi\Remi\FilteredRecord', |
22
|
|
|
new FilteredRecord() |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider setterGetterProvider |
28
|
|
|
*/ |
29
|
|
|
public function testSetterGetter( $set, $append, $expected ) { |
30
|
|
|
|
31
|
|
|
$instance = new FilteredRecord(); |
32
|
|
|
|
33
|
|
|
$instance->set( 'foo', $set ); |
34
|
|
|
$instance->append( 'foo', $append ); |
35
|
|
|
|
36
|
|
|
$this->assertTrue( |
37
|
|
|
$instance->has( 'foo' ) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$this->assertEquals( |
41
|
|
|
$expected, |
42
|
|
|
$instance->get( 'foo' ) |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
$instance->delete( 'foo' ); |
46
|
|
|
|
47
|
|
|
$this->assertFalse( |
48
|
|
|
$instance->has( 'foo' ) |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @dataProvider setterGetterProvider |
54
|
|
|
*/ |
55
|
|
|
public function testGetRecordFields( $set, $append, $expected ) { |
56
|
|
|
|
57
|
|
|
$instance = new FilteredRecord(); |
58
|
|
|
|
59
|
|
|
$instance->set( 'foo', $set ); |
60
|
|
|
$instance->append( 'foo', $append ); |
61
|
|
|
|
62
|
|
|
$this->assertEquals( |
63
|
|
|
array( |
64
|
|
|
'foo' => $expected |
65
|
|
|
), |
66
|
|
|
$instance->getRecordFields() |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->assertInternalType( |
70
|
|
|
'string', |
71
|
|
|
$instance->asJsonString() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testRedactedFields() { |
76
|
|
|
|
77
|
|
|
$instance = new FilteredRecord(); |
78
|
|
|
$instance->setRedactedFields( array( 'bar' ) ); |
79
|
|
|
|
80
|
|
|
$instance->set( 'bar', 42 ); |
81
|
|
|
|
82
|
|
|
$this->assertFalse( |
83
|
|
|
$instance->has( 'bar' ) |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
$instance->append( 'bar', 'foobar' ); |
87
|
|
|
|
88
|
|
|
$this->assertFalse( |
89
|
|
|
$instance->has( 'bar' ) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->assertEmpty( |
93
|
|
|
$instance->getRecordFields() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testUnknownRecordKeyFieldThrowsException() { |
98
|
|
|
|
99
|
|
|
$instance = new FilteredRecord(); |
100
|
|
|
|
101
|
|
|
$this->setExpectedException( 'InvalidArgumentException' ); |
102
|
|
|
$instance->get( 'foo' ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function setterGetterProvider() { |
106
|
|
|
|
107
|
|
|
$provider[] = array( |
|
|
|
|
108
|
|
|
'catch', |
109
|
|
|
22, |
110
|
|
|
'catch22' |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$provider[] = array( |
114
|
|
|
'', |
115
|
|
|
22, |
116
|
|
|
'22' |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$provider[] = array( |
120
|
|
|
array( 'catch' ), |
121
|
|
|
22, |
122
|
|
|
array( 'catch', 22 ) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$provider[] = array( |
126
|
|
|
array( 'catch' ), |
127
|
|
|
array( 22 ), |
128
|
|
|
array( 'catch', array( 22 ) ) |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return $provider; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.