1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onoi\Tesa\Tests; |
4
|
|
|
|
5
|
|
|
use Onoi\Tesa\Tokenizer\GenericRegExTokenizer; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @covers \Onoi\Tesa\Tokenizer\GenericRegExTokenizer |
9
|
|
|
* @group onoi-tesa |
10
|
|
|
* |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 0.1 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class GenericRegExTokenizerTest extends \PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
public function testUnknownOption() { |
19
|
|
|
|
20
|
|
|
$this->assertInstanceOf( |
21
|
|
|
'\Onoi\Tesa\Tokenizer\GenericRegExTokenizer', |
22
|
|
|
new GenericRegExTokenizer() |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider stringProvider |
28
|
|
|
*/ |
29
|
|
|
public function testTokenize( $string, $expected ) { |
30
|
|
|
|
31
|
|
|
$instance = new GenericRegExTokenizer(); |
32
|
|
|
|
33
|
|
|
$this->assertEquals( |
34
|
|
|
$expected, |
35
|
|
|
$instance->tokenize( $string ) |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testTokenizeWithEnabledExemptionList() { |
40
|
|
|
|
41
|
|
|
$string = "It's a test string (that has no);"; |
42
|
|
|
|
43
|
|
|
$tokenizer = $this->getMockBuilder( '\Onoi\Tesa\Tokenizer\Tokenizer' ) |
44
|
|
|
->disableOriginalConstructor() |
45
|
|
|
->getMockForAbstractClass(); |
46
|
|
|
|
47
|
|
|
$tokenizer->expects( $this->once() ) |
48
|
|
|
->method( 'setOption' ); |
49
|
|
|
|
50
|
|
|
$tokenizer->expects( $this->once() ) |
51
|
|
|
->method( 'tokenize' ) |
52
|
|
|
->with( $this->equalTo( $string ) ) |
53
|
|
|
->will( $this->returnValue( array( $string ) ) ); |
54
|
|
|
|
55
|
|
|
$instance = new GenericRegExTokenizer( $tokenizer ); |
56
|
|
|
|
57
|
|
|
$instance->setOption( |
58
|
|
|
GenericRegExTokenizer::REGEX_EXEMPTION, |
59
|
|
|
array( '\(', '\)', "'", ';') |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$this->assertEquals( |
63
|
|
|
array( "It's", 'a', 'test', 'string', '(that', 'has', 'no);' ), |
64
|
|
|
$instance->tokenize( $string ) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function stringProvider() { |
69
|
|
|
|
70
|
|
|
$provider[] = array( |
|
|
|
|
71
|
|
|
"It's a test string (that has no);deep meaning except0", |
72
|
|
|
array( 'It', 's', 'a', 'test', 'string', 'that', 'has', 'no', 'deep', 'meaning', 'except', '0' ) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$provider[] = array( |
76
|
|
|
"Привет, мир! Меня зовут д'Артаньян %) цуацуа123123", |
77
|
|
|
array( 'Привет', 'мир', 'Меня', 'зовут', 'д', "Артаньян", 'цуацуа', '123123' ) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
$provider[] = array( |
81
|
|
|
"[[Действует на возбудителей]] Brucella spp., Legionella pneumophila, Salmonella typhi,(за исключением остальных рифампицинов) не отмечено. ... |
82
|
|
|
|
83
|
|
|
Фармакокинетика[править | править вики-текст] |
84
|
|
|
Рифампицин хорошо всасывается из желудочно-кишечного тракта.", |
85
|
|
|
array( |
86
|
|
|
'Действует', 'на', 'возбудителей', 'Brucella', 'spp', 'Legionella', 'pneumophila', 'Salmonella', 'typhi', 'за', 'исключением', 'остальных', 'рифампицинов', |
87
|
|
|
'не', 'отмечено', 'Фармакокинетика', 'править', 'править', 'вики', 'текст', 'Рифампицин', 'хорошо', 'всасывается', 'из', 'желудочно', 'кишечного', 'тракта' |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
return $provider; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
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.