Completed
Push — master ( 9401a4...2cc7a2 )
by mw
13:59
created

PunctuationRegExTokenizerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 102
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 7 1
A testTokenize() 0 18 1
A testisWordTokenizerFromInheritTokenizer() 0 16 1
B testTokenizeWithOption() 0 28 1
A stringProvider() 0 22 1
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(
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
			'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