1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Tesa\Tests\StopwordAnalyzer; |
4
|
|
|
|
5
|
|
|
use Onoi\Tesa\StopwordAnalyzer\CdbStopwordAnalyzer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\Tesa\StopwordAnalyzer\CdbStopwordAnalyzer |
9
|
|
|
* @group onoi-tesa |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 0.1 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class CdbStopwordAnalyzerTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testTryToCreateCdbByLanguageOnInvalidLanguageThrowsException() { |
19
|
|
|
|
20
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
21
|
|
|
|
22
|
|
|
CdbStopwordAnalyzer::createCdbByLanguage( |
23
|
|
|
CdbStopwordAnalyzer::getLocation(), |
24
|
|
|
'foo' |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testTryToCreateCdbByLanguageOnInvalidJsonIndexThrowsException() { |
29
|
|
|
|
30
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
31
|
|
|
|
32
|
|
|
CdbStopwordAnalyzer::createCdbByLanguage( |
33
|
|
|
__DIR__ . '/../../Fixtures/StopwordAnalyzer/', |
34
|
|
|
'missingindex' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testTryToCreateCdbByLanguageOnInvalidJsonThrowsException() { |
39
|
|
|
|
40
|
|
|
$this->setExpectedException( 'RuntimeException' ); |
41
|
|
|
|
42
|
|
|
CdbStopwordAnalyzer::createCdbByLanguage( |
43
|
|
|
__DIR__ . '/../../Fixtures/StopwordAnalyzer/', |
44
|
|
|
'invalid' |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider languageProvider |
50
|
|
|
*/ |
51
|
|
|
public function testCreateCdbByLanguage( $languageCode ) { |
52
|
|
|
|
53
|
|
|
$res = CdbStopwordAnalyzer::createCdbByLanguage( |
54
|
|
|
CdbStopwordAnalyzer::getLocation(), |
55
|
|
|
$languageCode |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
$this->assertTrue( |
59
|
|
|
$res |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @dataProvider stopWordProvider |
65
|
|
|
*/ |
66
|
|
|
public function testIsStopWord( $languageCode , $word, $expected ) { |
67
|
|
|
|
68
|
|
|
$instane = new CdbStopwordAnalyzer( |
69
|
|
|
CdbStopwordAnalyzer::getTargetByLanguage( $languageCode ) |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$this->assertEquals( |
73
|
|
|
$expected, |
74
|
|
|
$instane->isStopWord( $word ) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function languageProvider() { |
79
|
|
|
|
80
|
|
|
$provider[] = array( |
|
|
|
|
81
|
|
|
'en', |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$provider[] = array( |
85
|
|
|
'de' |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$provider[] = array( |
89
|
|
|
'ja' |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$provider[] = array( |
93
|
|
|
'zh' |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$provider[] = array( |
97
|
|
|
'es' |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$provider[] = array( |
101
|
|
|
'fr' |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
$provider[] = array( |
105
|
|
|
'pt' |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$provider[] = array( |
109
|
|
|
'pt-br' |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
return $provider; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function stopWordProvider() { |
116
|
|
|
|
117
|
|
|
$provider[] = array( |
|
|
|
|
118
|
|
|
'en', |
119
|
|
|
'Foo', |
120
|
|
|
false |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$provider[] = array( |
124
|
|
|
'en', |
125
|
|
|
'the', |
126
|
|
|
true |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$provider[] = array( |
130
|
|
|
'ja', |
131
|
|
|
'それぞれ', |
132
|
|
|
true |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
$provider[] = array( |
136
|
|
|
'zh', |
137
|
|
|
'不单', |
138
|
|
|
true |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$provider[] = array( |
142
|
|
|
'es', |
143
|
|
|
'arriba', |
144
|
|
|
true |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$provider[] = array( |
148
|
|
|
'fr', |
149
|
|
|
'devrait', |
150
|
|
|
true |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$provider[] = array( |
154
|
|
|
'pt', |
155
|
|
|
'conhecido', |
156
|
|
|
true |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$provider[] = array( |
160
|
|
|
'pt-br', |
161
|
|
|
'mediante', |
162
|
|
|
true |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
return $provider; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
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.