Completed
Push — master ( 15e850...27125e )
by mw
02:06
created

tests/phpunit/Unit/StopwordAnalyzerTest.php (2 issues)

Upgrade to new PHP Analysis Engine

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
use Onoi\Tesa\PermutableString;
7
8
/**
9
 * @covers \Onoi\Tesa\StopwordAnalyzer
10
 * @group onoi-tesa
11
 *
12
 * @license GNU GPL v2+
13
 * @since 0.1
14
 *
15
 * @author mwjames
16
 */
17
class StopwordAnalyzerTest extends \PHPUnit_Framework_TestCase {
18
19
	public function testCanConstruct() {
20
21
		$this->assertInstanceOf(
22
			'\Onoi\Tesa\StopwordAnalyzer',
23
			new StopwordAnalyzer()
24
		);
25
	}
26
27
	public function testCustomStopwordList() {
28
29
		$instance = new StopwordAnalyzer();
30
		$instance->setCustomStopwordList( array( 'zoo' => array( 'Foo' ) ) );
31
32
		$this->assertTrue(
33
			$instance->isStopWord( 'Foo' )
34
		);
35
	}
36
37
	/**
38
	 * @dataProvider defaultListStopWordsProvider
39
	 */
40
	public function testUncachedIsWordCheckForDefaultList( $word, $expected ) {
41
42
		$instance = new StopwordAnalyzer();
43
		$instance->loadListBy( StopwordAnalyzer::DEFAULT_STOPWORDLIST );
44
45
		$this->assertEquals(
46
			$expected,
47
			$instance->isStopWord( $word )
48
		);
49
	}
50
51
	/**
52
	 * @dataProvider listByLanguageStopWordsProvider
53
	 */
54
	public function testListByLanguage( $languageCode, $word, $expected ) {
55
56
		$instance = new StopwordAnalyzer();
57
		$instance->loadListByLanguage( $languageCode );
58
59
		$this->assertEquals(
60
			$expected,
61
			$instance->isStopWord( $word )
62
		);
63
	}
64
65
	public function testTryToLoadUnlistedLanguage() {
66
67
		$instance = new StopwordAnalyzer();
68
		$instance->loadListByLanguage( 'foo' );
69
70
		$this->assertFalse(
71
			$instance->isStopWord( 'Bar' )
72
		);
73
	}
74
75
	public function testToLoadFromCache() {
76
77
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
78
			->disableOriginalConstructor()
79
			->setMethods( array( 'contains', 'fetch' ) )
80
			->getMockForAbstractClass();
81
82
		$cache->expects( $this->once() )
83
			->method( 'contains' )
84
			->with( $this->stringContains( 'onoi:tesa:stopword' ) )
85
			->will( $this->returnValue( true ) );
86
87
		$cache->expects( $this->once() )
88
			->method( 'fetch' );
89
90
		$instance = new StopwordAnalyzer( $cache );
91
		$instance->loadListByLanguage( 'foo' );
92
	}
93
94
	public function defaultListStopWordsProvider() {
95
96
		$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 $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar 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.

Loading history...
97
			'Foo',
98
			false
99
		);
100
101
		// en-list
102
		$provider[] = array(
103
			'about',
104
			true
105
		);
106
107
		// de-list
108
		$provider[] = array(
109
			'über',
110
			true
111
		);
112
113
		// fr-list
114
		$provider[] = array(
115
			'avoir',
116
			true
117
		);
118
119
		// es-list
120
		$provider[] = array(
121
			'aquellos',
122
			true
123
		);
124
125
		// ja-list (not default)
126
		$provider[] = array(
127
			'かつて',
128
			false
129
		);
130
131
		return $provider;
132
	}
133
134
	public function listByLanguageStopWordsProvider() {
135
136
		// ja-list
137
		$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 $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar 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.

Loading history...
138
			'ja',
139
			'かつて',
140
			true
141
		);
142
143
		return $provider;
144
	}
145
146
}
147