1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Tesa\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\Tesa\Tokenizer\JaCompoundGroupTokenizer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\Tesa\Tokenizer\JaCompoundGroupTokenizer |
9
|
|
|
* @group onoi-tesa |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 0.1 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class JaCompoundGroupTokenizerTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testUnknownOption() { |
19
|
|
|
|
20
|
|
|
$this->assertInstanceOf( |
21
|
|
|
'\Onoi\Tesa\Tokenizer\JaCompoundGroupTokenizer', |
22
|
|
|
new JaCompoundGroupTokenizer() |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider stringProvider |
28
|
|
|
*/ |
29
|
|
|
public function testTokenize( $string, $expected ) { |
30
|
|
|
|
31
|
|
|
if ( version_compare( phpversion(), '5.4', '<' ) ) { |
32
|
|
|
$this->markTestSkipped( |
33
|
|
|
"Boo, PHP 5.3 returns with unexpected results" |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$instance = new JaCompoundGroupTokenizer(); |
38
|
|
|
|
39
|
|
|
$this->assertEquals( |
40
|
|
|
$expected, |
41
|
|
|
$instance->tokenize( $string ) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testTokenizeWithOption() { |
46
|
|
|
|
47
|
|
|
if ( version_compare( phpversion(), '5.4', '<' ) ) { |
48
|
|
|
$this->markTestSkipped( |
49
|
|
|
"Ehh, PHP 5.3 returns with unexpected results" |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$string = 'と歓声を上げていました'; |
54
|
|
|
|
55
|
|
|
$tokenizer = $this->getMockBuilder( '\Onoi\Tesa\Tokenizer\Tokenizer' ) |
56
|
|
|
->disableOriginalConstructor() |
57
|
|
|
->getMockForAbstractClass(); |
58
|
|
|
|
59
|
|
|
$tokenizer->expects( $this->once() ) |
60
|
|
|
->method( 'setOption' ); |
61
|
|
|
|
62
|
|
|
$tokenizer->expects( $this->once() ) |
63
|
|
|
->method( 'tokenize' ) |
64
|
|
|
->with( $this->equalTo( $string ) ) |
65
|
|
|
->will( $this->returnValue( array( $string ) ) ); |
66
|
|
|
|
67
|
|
|
$instance = new JaCompoundGroupTokenizer( $tokenizer ); |
68
|
|
|
|
69
|
|
|
$instance->setOption( |
70
|
|
|
JaCompoundGroupTokenizer::REGEX_EXEMPTION, |
71
|
|
|
array( 'Foo' ) |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
$this->assertEquals( |
75
|
|
|
array( '歓声', '上' ), |
76
|
|
|
$instance->tokenize( $string ) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function stringProvider() { |
81
|
|
|
|
82
|
|
|
$provider[] = array( |
|
|
|
|
83
|
|
|
'と歓声を上げていました。 十勝農業改良普及センターによりますと', |
84
|
|
|
array( '歓声', '上', '十勝農業改良普及', 'センター' ) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $provider; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
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.