|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onoi\Tesa\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Onoi\Tesa\Tokenizer\PunctuationRegExTokenizer; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @covers \Onoi\Tesa\Tokenizer\PunctuationRegExTokenizer |
|
9
|
|
|
* @group onoi-tesa |
|
10
|
|
|
* |
|
11
|
|
|
* @license GNU GPL v2+ |
|
12
|
|
|
* @since 0.1 |
|
13
|
|
|
* |
|
14
|
|
|
* @author mwjames |
|
15
|
|
|
*/ |
|
16
|
|
|
class PunctuationRegExTokenizerTest extends \PHPUnit_Framework_TestCase { |
|
17
|
|
|
|
|
18
|
|
|
public function testCanConstruct() { |
|
19
|
|
|
|
|
20
|
|
|
$this->assertInstanceOf( |
|
21
|
|
|
'\Onoi\Tesa\Tokenizer\PunctuationRegExTokenizer', |
|
22
|
|
|
new PunctuationRegExTokenizer() |
|
23
|
|
|
); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @dataProvider stringProvider |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testTokenize( $string, $patternExemption, $expected ) { |
|
30
|
|
|
|
|
31
|
|
|
$instance = new PunctuationRegExTokenizer(); |
|
32
|
|
|
|
|
33
|
|
|
$instance->setOption( |
|
34
|
|
|
PunctuationRegExTokenizer::REGEX_EXEMPTION, |
|
35
|
|
|
$patternExemption |
|
36
|
|
|
); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals( |
|
39
|
|
|
$expected, |
|
40
|
|
|
$instance->tokenize( $string ) |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertTrue( |
|
44
|
|
|
$instance->isWordTokenizer() |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function testisWordTokenizerFromInheritTokenizer() { |
|
49
|
|
|
|
|
50
|
|
|
$tokenizer = $this->getMockBuilder( '\Onoi\Tesa\Tokenizer\Tokenizer' ) |
|
51
|
|
|
->disableOriginalConstructor() |
|
52
|
|
|
->getMockForAbstractClass(); |
|
53
|
|
|
|
|
54
|
|
|
$tokenizer->expects( $this->once() ) |
|
55
|
|
|
->method( 'isWordTokenizer' ) |
|
56
|
|
|
->will( $this->returnValue( false ) ); |
|
57
|
|
|
|
|
58
|
|
|
$instance = new PunctuationRegExTokenizer( $tokenizer ); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertFalse( |
|
61
|
|
|
$instance->isWordTokenizer() |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function testTokenizeWithOption() { |
|
66
|
|
|
|
|
67
|
|
|
$string = '123, 345'; |
|
68
|
|
|
|
|
69
|
|
|
$tokenizer = $this->getMockBuilder( '\Onoi\Tesa\Tokenizer\Tokenizer' ) |
|
70
|
|
|
->disableOriginalConstructor() |
|
71
|
|
|
->getMockForAbstractClass(); |
|
72
|
|
|
|
|
73
|
|
|
$tokenizer->expects( $this->once() ) |
|
74
|
|
|
->method( 'setOption' ); |
|
75
|
|
|
|
|
76
|
|
|
$tokenizer->expects( $this->once() ) |
|
77
|
|
|
->method( 'tokenize' ) |
|
78
|
|
|
->with( $this->equalTo( $string ) ) |
|
79
|
|
|
->will( $this->returnValue( array( $string ) ) ); |
|
80
|
|
|
|
|
81
|
|
|
$instance = new PunctuationRegExTokenizer( $tokenizer ); |
|
82
|
|
|
|
|
83
|
|
|
$instance->setOption( |
|
84
|
|
|
PunctuationRegExTokenizer::REGEX_EXEMPTION, |
|
85
|
|
|
array( ',' ) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertEquals( |
|
89
|
|
|
array( '123,', '345' ), |
|
90
|
|
|
$instance->tokenize( $string ) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function stringProvider() { |
|
95
|
|
|
|
|
96
|
|
|
$provider[] = array( |
|
|
|
|
|
|
97
|
|
|
'123, 345^456&[foo:bar]', |
|
98
|
|
|
'', |
|
99
|
|
|
array( '123', '345', '456', 'foo', 'bar' ) |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
$provider[] = array( |
|
103
|
|
|
'123, 345^456&[foo:bar]', |
|
104
|
|
|
array( ',', '&' ), |
|
105
|
|
|
array( '123,', '345', '456&', 'foo', 'bar' ) |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
$provider[] = array( |
|
109
|
|
|
'123, 345^456&[foo:bar] 3.', |
|
110
|
|
|
array( ',', '&' ), |
|
111
|
|
|
array( '123,', '345', '456&', 'foo', 'bar', '3' ) |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
|
|
return $provider; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |
|
118
|
|
|
|
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.