These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | $instance->setCustomStopwordList( array( 'zoo' => array( 'Foo' ) ) ); |
||
30 | |||
31 | $this->assertTrue( |
||
32 | $instance->isStopWord( 'Foo' ) |
||
33 | ); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @dataProvider defaultListStopWordsProvider |
||
38 | */ |
||
39 | public function testUncachedIsWordCheckForDefaultList( $word, $expected ) { |
||
40 | |||
41 | $instance = new StopwordAnalyzer(); |
||
42 | $instance->loadListBy( StopwordAnalyzer::DEFAULT_STOPWORDLIST ); |
||
43 | |||
44 | $this->assertEquals( |
||
45 | $expected, |
||
46 | $instance->isStopWord( $word ) |
||
47 | ); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @dataProvider listByLanguageStopWordsProvider |
||
52 | */ |
||
53 | public function testListByLanguage( $languageCode, $word, $expected ) { |
||
54 | |||
55 | $instance = new StopwordAnalyzer(); |
||
56 | $instance->loadListByLanguage( $languageCode ); |
||
57 | |||
58 | $this->assertEquals( |
||
59 | $expected, |
||
60 | $instance->isStopWord( $word ) |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | public function testTryToLoadUnlistedLanguage() { |
||
65 | |||
66 | $instance = new StopwordAnalyzer(); |
||
67 | $instance->loadListByLanguage( 'foo' ); |
||
68 | |||
69 | $this->assertFalse( |
||
70 | $instance->isStopWord( 'Bar' ) |
||
71 | ); |
||
72 | } |
||
73 | |||
74 | public function testToLoadFromCache() { |
||
75 | |||
76 | $cache = $this->getMockBuilder( '\Onoi\Cache\Cache' ) |
||
77 | ->disableOriginalConstructor() |
||
78 | ->setMethods( array( 'contains', 'fetch' ) ) |
||
79 | ->getMockForAbstractClass(); |
||
80 | |||
81 | $cache->expects( $this->once() ) |
||
82 | ->method( 'contains' ) |
||
83 | ->with( $this->stringContains( 'onoi:tesa:stopword' ) ) |
||
84 | ->will( $this->returnValue( true ) ); |
||
85 | |||
86 | $cache->expects( $this->once() ) |
||
87 | ->method( 'fetch' ); |
||
88 | |||
89 | $instance = new StopwordAnalyzer( $cache ); |
||
90 | $instance->loadListByLanguage( 'foo' ); |
||
91 | } |
||
92 | |||
93 | public function defaultListStopWordsProvider() { |
||
94 | |||
95 | $provider[] = array( |
||
0 ignored issues
–
show
|
|||
96 | 'Foo', |
||
97 | false |
||
98 | ); |
||
99 | |||
100 | // en-list |
||
101 | $provider[] = array( |
||
102 | 'about', |
||
103 | true |
||
104 | ); |
||
105 | |||
106 | // de-list |
||
107 | $provider[] = array( |
||
108 | 'über', |
||
109 | true |
||
110 | ); |
||
111 | |||
112 | // fr-list |
||
113 | $provider[] = array( |
||
114 | 'avoir', |
||
115 | true |
||
116 | ); |
||
117 | |||
118 | // es-list |
||
119 | $provider[] = array( |
||
120 | 'aquellos', |
||
121 | true |
||
122 | ); |
||
123 | |||
124 | // ja-list (not default) |
||
125 | $provider[] = array( |
||
126 | 'かつて', |
||
127 | false |
||
128 | ); |
||
129 | |||
130 | return $provider; |
||
131 | } |
||
132 | |||
133 | public function listByLanguageStopWordsProvider() { |
||
134 | |||
135 | // ja-list |
||
136 | $provider[] = array( |
||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.
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: foreach ($collection as $item) {
$myArray['foo'] = $item->getFoo();
if ($item->hasBar()) {
$myArray['bar'] = $item->getBar();
}
// do something with $myArray
}
As you can see in this example, the array 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.
Loading history...
|
|||
137 | 'ja', |
||
138 | 'かつて', |
||
139 | true |
||
140 | ); |
||
141 | |||
142 | return $provider; |
||
143 | } |
||
144 | |||
145 | } |
||
146 |
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.