1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Tesa\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\Tesa\StopwordAnalyzer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\Tesa\StopwordAnalyzer |
9
|
|
|
* @group onoi-tesa |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 0.1 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class StopwordAnalyzerTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
19
|
|
|
|
20
|
|
|
$this->assertInstanceOf( |
21
|
|
|
'\Onoi\Tesa\StopwordAnalyzer', |
22
|
|
|
new StopwordAnalyzer() |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testCustomStopwordList() { |
27
|
|
|
|
28
|
|
|
$instance = new StopwordAnalyzer(); |
29
|
|
|
|
30
|
|
|
$instance->setCustomStopwordList( |
31
|
|
|
array( |
32
|
|
|
'zoo' => array( 'Foo' ), |
33
|
|
|
'doo' => array( 'bar' ) |
34
|
|
|
) |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
$this->assertTrue( |
38
|
|
|
$instance->isStopWord( 'Foo' ) |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$this->assertEquals( |
42
|
|
|
array( 'zoo', 'doo' ), |
43
|
|
|
$instance->getLanguageList() |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @dataProvider defaultListStopWordsProvider |
49
|
|
|
*/ |
50
|
|
|
public function testUncachedIsWordCheckForDefaultList( $word, $expected ) { |
51
|
|
|
|
52
|
|
|
$instance = new StopwordAnalyzer(); |
53
|
|
|
$instance->loadListByDefaultLanguages(); |
54
|
|
|
|
55
|
|
|
$this->assertEquals( |
56
|
|
|
$expected, |
57
|
|
|
$instance->isStopWord( $word ) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @dataProvider listByLanguageStopWordsProvider |
63
|
|
|
*/ |
64
|
|
|
public function testListByLanguage( $languageCode, $word, $expected ) { |
65
|
|
|
|
66
|
|
|
$instance = new StopwordAnalyzer(); |
67
|
|
|
$instance->loadListByLanguage( $languageCode ); |
68
|
|
|
|
69
|
|
|
$this->assertEquals( |
70
|
|
|
$expected, |
71
|
|
|
$instance->isStopWord( $word ) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->assertEquals( |
75
|
|
|
(array)$languageCode, |
76
|
|
|
$instance->getLanguageList() |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @dataProvider listByLanguageStopWordsProvider |
82
|
|
|
*/ |
83
|
|
|
public function testListFromCustomLocation( $languageCode, $word, $expected ) { |
84
|
|
|
|
85
|
|
|
$instance = new StopwordAnalyzer(); |
86
|
|
|
$instance->loadListFromCustomLocation( __DIR__ . '/../../../data/stopwords/', $languageCode ); |
87
|
|
|
|
88
|
|
|
$this->assertEquals( |
89
|
|
|
$expected, |
90
|
|
|
$instance->isStopWord( $word ) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testTryToLoadUnlistedLanguage() { |
95
|
|
|
|
96
|
|
|
$instance = new StopwordAnalyzer(); |
97
|
|
|
$instance->loadListByLanguage( 'foo' ); |
98
|
|
|
|
99
|
|
|
$this->assertFalse( |
100
|
|
|
$instance->isStopWord( 'Bar' ) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testToLoadFromCache() { |
105
|
|
|
|
106
|
|
|
$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
107
|
|
|
->disableOriginalConstructor() |
108
|
|
|
->setMethods( array( 'contains', 'fetch' ) ) |
109
|
|
|
->getMockForAbstractClass(); |
110
|
|
|
|
111
|
|
|
$cache->expects( $this->once() ) |
112
|
|
|
->method( 'contains' ) |
113
|
|
|
->with( $this->stringContains( 'onoi:tesa:stopword' ) ) |
114
|
|
|
->will( $this->returnValue( true ) ); |
115
|
|
|
|
116
|
|
|
$cache->expects( $this->once() ) |
117
|
|
|
->method( 'fetch' ); |
118
|
|
|
|
119
|
|
|
$instance = new StopwordAnalyzer( $cache ); |
120
|
|
|
$instance->loadListByLanguage( 'foo' ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function defaultListStopWordsProvider() { |
124
|
|
|
|
125
|
|
|
$provider[] = array( |
|
|
|
|
126
|
|
|
'Foo', |
127
|
|
|
false |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
// en-list |
131
|
|
|
$provider[] = array( |
132
|
|
|
'about', |
133
|
|
|
true |
134
|
|
|
); |
135
|
|
|
|
136
|
|
|
// de-list |
137
|
|
|
$provider[] = array( |
138
|
|
|
'über', |
139
|
|
|
true |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
// fr-list |
143
|
|
|
$provider[] = array( |
144
|
|
|
'avoir', |
145
|
|
|
true |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
// es-list |
149
|
|
|
$provider[] = array( |
150
|
|
|
'aquellos', |
151
|
|
|
true |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
// ja-list (not default) |
155
|
|
|
$provider[] = array( |
156
|
|
|
'かつて', |
157
|
|
|
false |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
return $provider; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function listByLanguageStopWordsProvider() { |
164
|
|
|
|
165
|
|
|
// ja-list |
166
|
|
|
$provider[] = array( |
|
|
|
|
167
|
|
|
'ja', |
168
|
|
|
'かつて', |
169
|
|
|
true |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return $provider; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
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.