Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#15)
by Der Mundschenk
02:23
created

Text_Parser_Test   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 656
Duplicated Lines 16.62 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 9
Bugs 0 Features 0
Metric Value
dl 109
loc 656
rs 9.845
c 9
b 0
f 0
wmc 28
lcom 1
cbo 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *  This file is part of PHP-Typography.
4
 *
5
 *  Copyright 2015-2017 Peter Putzer.
6
 *
7
 *  This program is free software; you can redistribute it and/or
8
 *  modify it under the terms of the GNU General Public License
9
 *  as published by the Free Software Foundation; either version 2
10
 *  of the License, or ( at your option ) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 *
21
 *  @package mundschenk-at/php-typography/tests
22
 *  @license http://www.gnu.org/licenses/gpl-2.0.html
23
 */
24
25
namespace PHP_Typography\Tests;
26
27
use \PHP_Typography\Text_Parser\Token;
28
use \PHP_Typography\Text_Parser;
29
30
/**
31
 * Unit test for \PHP_Typography\Text_Parser class.
32
 *
33
 * @coversDefaultClass \PHP_Typography\Text_Parser
34
 * @usesDefaultClass \PHP_Typography\Text_Parser
35
 *
36
 * @uses PHP_Typography\Text_Parser::__construct
37
 * @uses PHP_Typography\Text_Parser\Token
38
 * @uses PHP_Typography\Strings::functions
39
 */
40
class Text_Parser_Test extends PHP_Typography_Testcase {
41
	/**
42
	 * The Text_Parser fixture.
43
	 *
44
	 * @var Text_Parser
45
	 */
46
	protected $parser;
47
48
	/**
49
	 * Sets up the fixture, for example, opens a network connection.
50
	 * This method is called before a test is executed.
51
	 */
52
	protected function setUp() {
53
		$this->parser = new \PHP_Typography\Text_Parser();
54
	}
55
56
	/**
57
	 * Tears down the fixture, for example, closes a network connection.
58
	 * This method is called after a test is executed.
59
	 */
60
	protected function tearDown() {
61
	}
62
63
	/**
64
	 * Test constructor.
65
	 *
66
	 * @covers ::__construct
67
	 */
68
	public function test_constructor() {
69
		$parser = new Text_Parser();
70
71
		$this->assertAttributeCount( 0, 'text', $parser );
72
		$this->assertAttributeEmpty( 'current_strtoupper', $parser );
73
	}
74
75
	/**
76
	 * Test load.
77
	 *
78
	 * @covers ::load
79
	 * @covers ::tokenize
80
	 * @covers ::parse_ambiguous_token
81
	 * @covers ::is_preceeded_by
82
	 * @covers ::is_not_preceeded_by
83
	 *
84
	 * @uses ::get_all
85
	 */
86
	public function test_load() {
87
		$too_long = 'A really long string with a word that is wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmost too long.';
89
		$almost_too_long = 'A really long string with a word that is aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaalmost too long.';
90
91
		$parser = $this->parser;
92
93
		// Security check.
94
		$this->assertFalse( $parser->load( $too_long ) );
95
		$this->assertFalse( $parser->load( $still_too_long ) );
96
		$this->assertTrue( $parser->load( $almost_too_long ) );
97
98
		$interesting = 'Quoth the raven, "nevermore"! Äöüß?';
99
		$this->assertTrue( $parser->load( $interesting ) );
100
101
		$tokens = $parser->get_all();
102
103
		$this->assertCount( 13, $tokens );
104
		$this->assertArraySubset( [
105
			0 => new Token( 'Quoth', Token::WORD ),
106
		], $tokens );
107
		$this->assertArraySubset( [
108
			5 => new Token( ',', Token::PUNCTUATION ),
109
		], $tokens );
110
		$this->assertArraySubset( [
111
			11 => new Token( 'Äöüß', Token::WORD ),
112
		], $tokens );
113
114
		return $parser;
115
	}
116
117
	/**
118
	 * Test load with email address.
119
	 *
120
	 * @covers ::load
121
	 * @covers ::tokenize
122
	 * @covers ::parse_ambiguous_token
123
	 * @covers ::is_preceeded_by
124
	 * @covers ::is_not_preceeded_by
125
	 *
126
	 * @uses ::get_all
127
	 */
128
	public function test_load_email() {
129
		$parser = $this->parser;
130
131
		$string = 'Quoth the raven, "nevermore"! Please mail to [email protected].';
132
		$this->assertTrue( $parser->load( $string ) );
133
134
		$tokens = $parser->get_all();
135
		$this->assertCount( 19, $tokens );
136
137
		$this->assertArraySubset( [
138
			0 => new Token( 'Quoth', Token::WORD ),
139
		], $tokens );
140
		$this->assertArraySubset( [
141
			5 => new Token( ',', Token::PUNCTUATION ),
142
		], $tokens );
143
		$this->assertArraySubset( [
144
			17 => new Token( '[email protected]', Token::OTHER ),
145
		], $tokens );
146
147
		return $parser;
148
	}
149
150
	/**
151
	 * Test load with URL.
152
	 *
153
	 * @covers ::load
154
	 * @covers ::tokenize
155
	 * @covers ::parse_ambiguous_token
156
	 * @covers ::is_preceeded_by
157
	 * @covers ::is_not_preceeded_by
158
	 *
159
	 * @uses ::get_all
160
	 */
161
	public function test_load_url() {
162
		$parser = $this->parser;
163
164
		$string = 'Quoth the raven, "nevermore"! Please open http://example.org or foo:WordPress or foo:W@rdPress or @example or @:@:@:risk.';
165
		$this->assertTrue( $parser->load( $string ) );
166
167
		$tokens = $parser->get_all();
168
		$this->assertCount( 33, $tokens );
169
170
		$this->assertArraySubset( [
171
			0 => new Token( 'Quoth', Token::WORD ),
172
		], $tokens );
173
		$this->assertArraySubset( [
174
			5 => new Token( ',', Token::PUNCTUATION ),
175
		], $tokens );
176
		$this->assertArraySubset( [
177
			15 => new Token( 'http://example.org', Token::OTHER ),
178
		], $tokens );
179
		$this->assertArraySubset( [
180
			19 => new Token( 'foo:WordPress', Token::OTHER ),
181
		], $tokens );
182
		$this->assertArraySubset( [
183
			23 => new Token( 'foo:W@rdPress', Token::OTHER ),
184
		], $tokens );
185
		$this->assertArraySubset( [
186
			27 => new Token( '@example', Token::OTHER ),
187
		], $tokens );
188
		$this->assertArraySubset( [
189
			31 => new Token( '@:@:@:risk', Token::OTHER ),
190
		], $tokens );
191
192
		return $parser;
193
	}
194
195
	/**
196
	 * Test load with a compound word.
197
	 *
198
	 * @covers ::load
199
	 * @covers ::tokenize
200
	 * @covers ::parse_ambiguous_token
201
	 * @covers ::is_preceeded_by
202
	 * @covers ::is_not_preceeded_by
203
	 *
204
	 * @uses ::get_all
205
	 */
206
	public function test_load_compound_word() {
207
		$parser = $this->parser;
208
209
		$string = 'Some don\'t trust the captain-owner.';
210
		$this->assertTrue( $parser->load( $string ) );
211
212
		$tokens = $parser->get_all();
213
		$this->assertCount( 10, $tokens );
214
215
		$this->assertArraySubset( [
216
			0 => new Token( 'Some', Token::WORD ),
217
		], $tokens );
218
		$this->assertArraySubset( [
219
			2 => new Token( "don't", Token::OTHER ),
220
		], $tokens );
221
		$this->assertArraySubset( [
222
			8 => new Token( 'captain-owner', Token::WORD ),
223
		], $tokens );
224
225
		return $parser;
226
	}
227
228
	/**
229
	 * Test load with an invalid encoding.
230
	 *
231
	 * @covers ::load
232
	 */
233
	public function test_load_invalid_encoding() {
234
		$string = mb_convert_encoding( 'Ein längerer String im falschen Zeichensatz', 'ISO-8859-2' );
235
		$parser = $this->parser;
236
237
		$this->assertFalse( $parser->load( $string ) );
238
	}
239
240
	/**
241
	 * Test load with something that is not a string.
242
	 *
243
	 * @covers ::load
244
	 */
245
	public function test_load_not_a_string() {
246
		$parser = $this->parser;
247
248
		$this->assertFalse( $parser->load( [] ) );
249
	}
250
251
	/**
252
	 * Test reload.
253
	 *
254
	 * @covers ::reload
255
	 *
256
	 * @depends test_load
257
	 * @uses ::clear
258
	 * @uses ::get_all
259
	 * @uses ::is_not_preceeded_by
260
	 * @uses ::is_preceeded_by
261
	 * @uses ::load
262
	 * @uses ::parse_ambiguous_token
263
	 * @uses ::tokenize
264
	 * @uses ::unload
265
	 * @uses ::update
266
267
	 * @param \PHP_Typography\Text_Parser $parser The parser to use.
268
	 */
269
	public function test_reload( Text_Parser $parser ) {
270
		$tokens = $parser->get_all();
271
		$tokens[12] = $tokens[12]->with_value( '' ); // ?.
272
		$tokens[11] = $tokens[11]->with_value( '' ); // Äöüß
273
		$tokens[10] = $tokens[10]->with_value( '' ); //
274
		$tokens[9]  = $tokens[9]->with_value( $tokens[9]->value . '!' );
275
		$parser->update( $tokens );
276
277
		$this->assertTrue( $parser->reload() );
278
		$this->assertSame( 'Quoth the raven, "nevermore"!!', $parser->unload() );
279
280
		return $parser;
281
	}
282
283
	/**
284
	 * Test unload.
285
	 *
286
	 * @covers ::unload
287
	 *
288
	 * @uses ::clear
289
	 * @uses ::is_not_preceeded_by
290
	 * @uses ::is_preceeded_by
291
	 * @uses ::load
292
	 * @uses ::parse_ambiguous_token
293
	 * @uses ::tokenize
294
	 */
295
	public function test_unload() {
296
		$interesting = 'Quoth the raven, "nevermore"! Äöüß?';
297
		$parser = $this->parser;
298
299
		$this->assertTrue( $parser->load( $interesting ) );
300
301
		$result = $parser->unload();
302
303
		$this->assertSame( $interesting, $result );
304
		$this->assertNotSame( $result, $parser->unload() ); // the parser is empty now.
305
	}
306
307
	/**
308
	 * Test clear.
309
	 *
310
	 * @covers ::clear
311
	 *
312
	 * @uses ::get_all
313
	 * @uses ::is_not_preceeded_by
314
	 * @uses ::is_preceeded_by
315
	 * @uses ::load
316
	 * @uses ::parse_ambiguous_token
317
	 * @uses ::tokenize
318
	 */
319
	public function test_clear() {
320
		$parser = $this->parser;
321
		$interesting = 'Quoth the raven, "nevermore"!';
322
323
		$this->assertTrue( $parser->load( $interesting ) );
324
		$this->assertGreaterThan( 0, count( $parser->get_all() ) );
325
326
		$parser->clear();
327
		$this->assertCount( 0, $parser->get_all() );
328
	}
329
330
	/**
331
	 * Test update.
332
	 *
333
	 * @covers ::update
334
	 *
335
	 * @uses ::clear
336
	 * @uses ::get_all
337
	 * @uses ::is_not_preceeded_by
338
	 * @uses ::is_preceeded_by
339
	 * @uses ::load
340
	 * @uses ::parse_ambiguous_token
341
	 * @uses ::tokenize
342
	 * @uses ::unload
343
	 */
344
	public function test_update() {
345
		$parser = $this->parser;
346
		$interesting = 'Quoth the raven, "nevermore"! Äöüß?';
347
		$this->assertTrue( $parser->load( $interesting ) );
348
349
		$tokens = $parser->get_all();
350
		$tokens[12] = $tokens[12]->with_value( '' ); // ?.
351
		$tokens[11] = $tokens[11]->with_value( '' ); // Äöüß
352
		$tokens[10] = $tokens[10]->with_value( '' ); //
353
		$tokens[9]  = $tokens[9]->with_value( $tokens[9]->value . '!' );
354
		$parser->update( $tokens );
355
356
		$this->assertSame( 'Quoth the raven, "nevermore"!!', $parser->unload() );
357
358
		return $parser;
359
	}
360
361
	/**
362
	 * Test get_all.
363
	 *
364
	 * @covers ::get_all
365
	 *
366
	 * @uses ::load
367
	 * @uses ::is_not_preceeded_by
368
	 * @uses ::is_preceeded_by
369
	 * @uses ::parse_ambiguous_token
370
	 * @uses ::tokenize
371
	 */
372
	public function test_get_all() {
373
		$interesting = 'Quoth the raven, "nevermore"!';
374
		$parser = $this->parser;
375
		$this->assertTrue( $parser->load( $interesting ) );
376
377
		$tokens = $parser->get_all();
378
		$this->assertCount( 10, $tokens );
379
380
		return $parser;
381
	}
382
383
	/**
384
	 * Test get_spaces.
385
	 *
386
	 * @covers ::get_spaces
387
	 * @depends test_get_all
388
	 *
389
	 * @uses ::get_type
390
	 *
391
	 * @param \PHP_Typography\Text_Parser $parser The parser to use.
392
	 */
393
	public function test_get_spaces( Text_Parser $parser ) {
394
		$tokens = $parser->get_spaces();
395
		$this->assertCount( 3, $tokens );
396
397
		return $parser;
398
	}
399
400
	/**
401
	 * Test get_punctuation.
402
	 *
403
	 * @covers ::get_punctuation
404
	 * @depends test_get_all
405
	 *
406
	 * @uses ::get_type
407
	 *
408
	 * @param \PHP_Typography\Text_Parser $parser The parser to use.
409
	 */
410
	public function test_get_punctuation( Text_Parser $parser ) {
411
		$tokens = $parser->get_punctuation();
412
		$this->assertCount( 3, $tokens );
413
414
		return $parser;
415
	}
416
417
	/**
418
	 * Test get_words.
419
	 *
420
	 * @covers ::get_words
421
	 * @depends test_get_all
422
	 *
423
	 * @uses ::conforms_to_caps_policy
424
	 * @uses ::conforms_to_compounds_policy
425
	 * @uses ::conforms_to_letters_policy
426
	 * @uses ::check_policy
427
	 * @uses ::get_type
428
	 * @uses ::is_preceeded_by
429
	 * @uses ::load
430
	 * @uses ::parse_ambiguous_token
431
	 * @uses ::tokenize
432
	 *
433
	 * @param \PHP_Typography\Text_Parser $parser The parser to use.
434
	 */
435
	public function test_get_words( Text_Parser $parser ) {
436
		$tokens = $parser->get_words();
437
		$this->assertCount( 4, $tokens );
438
439
		$parser->load( 'A few m1xed W0RDS.' );
440
		$tokens = $parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, Text_Parser::NO_ALL_CAPS );
441
		$this->assertCount( 1, $tokens );
442
		$this->assertContains( new Token( 'few', Token::WORD ), $tokens, '', false, false, true );
443
444
		$tokens = $parser->get_words( Text_Parser::ALLOW_ALL_LETTERS, Text_Parser::NO_ALL_CAPS );
445
		$this->assertCount( 2, $tokens );
446
		$this->assertContains( new Token( 'few', Token::WORD ), $tokens, '', false, false, true );
447
		$this->assertContains( new Token( 'm1xed', Token::WORD ), $tokens, '', false, false, true );
448
449
		$tokens = $parser->get_words( Text_Parser::NO_ALL_LETTERS, Text_Parser::NO_ALL_CAPS );
450
		$this->assertCount( 1, $tokens );
451
		$this->assertContains( new Token( 'm1xed', Token::WORD ), $tokens, '', false, false, true );
452
453
		$tokens = $parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, Text_Parser::ALLOW_ALL_CAPS );
454
		$this->assertCount( 2, $tokens );
455
		$this->assertContains( new Token( 'A', Token::WORD ), $tokens, '', false, false, true );
456
		$this->assertContains( new Token( 'few', Token::WORD ), $tokens, '', false, false, true );
457
458
		$tokens = $parser->get_words( Text_Parser::ALLOW_ALL_LETTERS, Text_Parser::ALLOW_ALL_CAPS );
459
		$this->assertCount( 4, $tokens );
460
		$this->assertContains( new Token( 'A', Token::WORD ), $tokens, '', false, false, true );
461
		$this->assertContains( new Token( 'few', Token::WORD ), $tokens, '', false, false, true );
462
		$this->assertContains( new Token( 'm1xed', Token::WORD ), $tokens, '', false, false, true );
463
		$this->assertContains( new Token( 'W0RDS', Token::WORD ), $tokens, '', false, false, true );
464
465
		$tokens = $parser->get_words( Text_Parser::NO_ALL_LETTERS, Text_Parser::ALLOW_ALL_CAPS );
466
		$this->assertCount( 2, $tokens );
467
		$this->assertContains( new Token( 'm1xed', Token::WORD ), $tokens, '', false, false, true );
468
		$this->assertContains( new Token( 'W0RDS', Token::WORD ), $tokens, '', false, false, true );
469
470
		$tokens = $parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, Text_Parser::REQUIRE_ALL_CAPS );
471
		$this->assertCount( 1, $tokens );
472
		$this->assertContains( new Token( 'A', Token::WORD ), $tokens, '', false, false, true );
473
474
		$tokens = $parser->get_words( Text_Parser::ALLOW_ALL_LETTERS, Text_Parser::REQUIRE_ALL_CAPS );
475
		$this->assertCount( 2, $tokens );
476
		$this->assertContains( new Token( 'A', Token::WORD ), $tokens, '', false, false, true );
477
		$this->assertContains( new Token( 'W0RDS', Token::WORD ), $tokens, '', false, false, true );
478
479
		$tokens = $parser->get_words( Text_Parser::NO_ALL_LETTERS, Text_Parser::REQUIRE_ALL_CAPS );
480
		$this->assertCount( 1, $tokens );
481
		$this->assertContains( new Token( 'W0RDS', Token::WORD ), $tokens, '', false, false, true );
482
	}
483
484
	/**
485
	 * Providate data for testing conforms_to_letters_policy.
486
	 *
487
	 * @return array
488
	 */
489
	public function provide_conforms_to_letters_policy_data() {
490
		return [
491
			[ 'simple',   Token::WORD, Text_Parser::ALLOW_ALL_LETTERS, true ],
492
			[ 'SIMPLE',   Token::WORD, Text_Parser::ALLOW_ALL_LETTERS, true ],
493
			[ 'simple',   Token::WORD, Text_Parser::NO_ALL_LETTERS, false ],
494
			[ 'simple99', Token::WORD, Text_Parser::NO_ALL_LETTERS, true ],
495
			[ 'simple',   Token::WORD, Text_Parser::REQUIRE_ALL_LETTERS, true ],
496
			[ 'SIMPLE',   Token::WORD, Text_Parser::REQUIRE_ALL_LETTERS, true ],
497
			[ 'SIMPLE99', Token::WORD, Text_Parser::REQUIRE_ALL_LETTERS, false ],
498
			[ 'simple99', Token::WORD, Text_Parser::ALLOW_ALL_LETTERS, true ],
499
			[ 'SIM-PLE',  Token::WORD, Text_Parser::ALLOW_ALL_LETTERS, true ],
500
			[ 'sim-ple',  Token::WORD, Text_Parser::NO_ALL_LETTERS, true ],
501
			[ 'SIM-PLE',  Token::WORD, Text_Parser::NO_ALL_LETTERS, true ],
502
			[ 'sim-ple',  Token::WORD, Text_Parser::REQUIRE_ALL_LETTERS, false ],
503
			[ 'SIM-PLE',  Token::WORD, Text_Parser::REQUIRE_ALL_LETTERS, false ],
504
		];
505
	}
506
507
	/**
508
	 * Test conforms_to_letters_policy.
509
	 *
510
	 * @covers ::conforms_to_letters_policy
511
	 * @covers ::check_policy
512
	 * @dataProvider provide_conforms_to_letters_policy_data
513
	 *
514
	 * @uses ::load
515
	 * @uses ::is_preceeded_by
516
	 * @uses ::parse_ambiguous_token
517
	 * @uses ::tokenize
518
	 *
519
	 * @param string $value  Token value.
520
	 * @param int    $type   Token type.
521
	 * @param int    $policy Letters policy.
522
	 * @param bool   $result Expected result.
523
	 */
524
	public function test_conforms_to_letters_policy( $value, $type, $policy, $result ) {
525
		$parser = $this->parser;
526
		$token  = new Token( $value, $type );
527
528
		$this->assertSame( $result, $this->invokeMethod( $parser, 'conforms_to_letters_policy', [ $token, $policy ] ) );
529
	}
530
531
	/**
532
	 * Providate data for testing conforms_to_caps_policy.
533
	 *
534
	 * @return array
535
	 */
536
	public function provide_conforms_to_caps_policy_data() {
537
		return [
538
			[ 'simple',   Token::WORD, Text_Parser::ALLOW_ALL_CAPS, true ],
539
			[ 'SIMPLE',   Token::WORD, Text_Parser::ALLOW_ALL_CAPS, true ],
540
			[ 'simple',   Token::WORD, Text_Parser::NO_ALL_CAPS, true ],
541
			[ 'simple99', Token::WORD, Text_Parser::NO_ALL_CAPS, true ],
542
			[ 'simple',   Token::WORD, Text_Parser::REQUIRE_ALL_CAPS, false ],
543
			[ 'SIMPLE',   Token::WORD, Text_Parser::REQUIRE_ALL_CAPS, true ],
544
			[ 'SIMPLE99', Token::WORD, Text_Parser::REQUIRE_ALL_CAPS, true ],
545
			[ 'simple99', Token::WORD, Text_Parser::ALLOW_ALL_CAPS, true ],
546
			[ 'SIM-PLE',  Token::WORD, Text_Parser::ALLOW_ALL_CAPS, true ],
547
			[ 'sim-ple',  Token::WORD, Text_Parser::NO_ALL_CAPS, true ],
548
			[ 'SIM-PLE',  Token::WORD, Text_Parser::NO_ALL_CAPS, false ],
549
			[ 'sim-ple',  Token::WORD, Text_Parser::REQUIRE_ALL_CAPS, false ],
550
			[ 'SIM-PLE',  Token::WORD, Text_Parser::REQUIRE_ALL_CAPS, true ],
551
		];
552
	}
553
554
	/**
555
	 * Test conforms_to_caps_policy.
556
	 *
557
	 * @covers ::conforms_to_caps_policy
558
	 * @covers ::check_policy
559
	 * @dataProvider provide_conforms_to_caps_policy_data
560
	 *
561
	 * @uses ::load
562
	 * @uses ::is_preceeded_by
563
	 * @uses ::parse_ambiguous_token
564
	 * @uses ::tokenize
565
	 *
566
	 * @param string $value  Token value.
567
	 * @param int    $type   Token type.
568
	 * @param int    $policy All caps policy.
569
	 * @param bool   $result Expected result.
570
	 */
571
	public function test_conforms_to_caps_policy( $value, $type, $policy, $result ) {
572
		$parser = $this->parser;
573
		$parser->load( $value ); // Ensure that encoding can be determined.
574
575
		$token  = new Token( $value, $type );
576
577
		$this->assertSame( $result, $this->invokeMethod( $parser, 'conforms_to_caps_policy', [ $token, $policy ] ) );
578
	}
579
580
	/**
581
	 * Providate data for testing conforms_to_compounds_policy.
582
	 *
583
	 * @return array
584
	 */
585
	public function provide_conforms_to_compounds_policy() {
586
		return [
587
			[ 'simple',   Token::WORD, Text_Parser::ALLOW_COMPOUNDS, true ],
588
			[ 'SIMPLE',   Token::WORD, Text_Parser::ALLOW_COMPOUNDS, true ],
589
			[ 'simple',   Token::WORD, Text_Parser::NO_COMPOUNDS, true ],
590
			[ 'simple99', Token::WORD, Text_Parser::NO_COMPOUNDS, true ],
591
			[ 'simple',   Token::WORD, Text_Parser::REQUIRE_COMPOUNDS, false ],
592
			[ 'SIMPLE',   Token::WORD, Text_Parser::REQUIRE_COMPOUNDS, false ],
593
			[ 'SIMPLE99', Token::WORD, Text_Parser::REQUIRE_COMPOUNDS, false ],
594
			[ 'simple99', Token::WORD, Text_Parser::ALLOW_COMPOUNDS, true ],
595
			[ 'SIM-PLE',  Token::WORD, Text_Parser::ALLOW_COMPOUNDS, true ],
596
			[ 'sim-ple',  Token::WORD, Text_Parser::NO_COMPOUNDS, false ],
597
			[ 'SIM-PLE',  Token::WORD, Text_Parser::NO_COMPOUNDS, false ],
598
			[ 'sim-ple',  Token::WORD, Text_Parser::REQUIRE_COMPOUNDS, true ],
599
			[ 'SIM-PLE',  Token::WORD, Text_Parser::REQUIRE_COMPOUNDS, true ],
600
		];
601
	}
602
603
	/**
604
	 * Test conforms_to_compounds_policy.
605
	 *
606
	 * @covers ::conforms_to_compounds_policy
607
	 * @covers ::check_policy
608
	 * @dataProvider provide_conforms_to_compounds_policy
609
	 *
610
	 * @uses ::load
611
	 * @uses ::is_preceeded_by
612
	 * @uses ::parse_ambiguous_token
613
	 * @uses ::tokenize
614
	 *
615
	 * @param string $value  Token value.
616
	 * @param int    $type   Token type.
617
	 * @param int    $policy Compounds policy.
618
	 * @param bool   $result Expected result.
619
	 */
620
	public function test_conforms_to_compounds_policy( $value, $type, $policy, $result ) {
621
		$parser = $this->parser;
622
		$parser->load( $value ); // Ensure that encoding can be determined.
623
624
		$token  = new Token( $value, $type );
625
626
		$this->assertSame( $result, $this->invokeMethod( $parser, 'conforms_to_compounds_policy', [ $token, $policy ] ) );
627
	}
628
629
	/**
630
	 * Test get_words.
631
	 *
632
	 * @covers ::get_words
633
	 * @depends test_get_all
634
	 *
635
	 * @uses ::clear
636
	 * @uses ::is_preceeded_by
637
	 * @uses ::load
638
	 * @uses ::parse_ambiguous_token
639
	 * @uses ::tokenize
640
	 * @uses ::unload
641
	 *
642
	 * @param \PHP_Typography\Text_Parser $parser The parser to use.
643
	 */
644
	public function test_get_words_unloaded( Text_Parser $parser ) {
645
		$parser->load( 'A few m1xed W0RDS.' );
646
		$parser->unload();
647
648
		$tokens = $parser->get_words( Text_Parser::REQUIRE_ALL_LETTERS, Text_Parser::NO_ALL_CAPS );
649
		$this->assertCount( 0, $tokens );
650
		$this->assertSame( [], $tokens );
651
652
		return $parser;
653
	}
654
655
	/**
656
	 * Test get_other.
657
	 *
658
	 * @covers ::get_other
659
	 * @depends test_get_all
660
	 *
661
	 * @uses ::get_type
662
	 *
663
	 * @param \PHP_Typography\Text_Parser $parser The parser to use.
664
	 */
665
	public function test_get_other( Text_Parser $parser ) {
666
		$tokens = $parser->get_other();
667
		$this->assertCount( 0, $tokens );
668
669
		return $parser;
670
	}
671
672
	/**
673
	 * Test get_type.
674
	 *
675
	 * @covers ::get_type
676
	 * @depends test_get_all
677
	 *
678
	 * @uses ::get_all
679
	 * @uses ::is_preceeded_by
680
	 * @uses ::load
681
	 * @uses ::parse_ambiguous_token
682
	 * @uses ::tokenize
683
	 *
684
	 * @param \PHP_Typography\Text_Parser $parser The parser to use.
685
	 */
686
	public function test_get_type( Text_Parser $parser ) {
687
		$parser->load( 'A few m1xed W0RDS.' );
688
689
		$words = [];
690
		$tokens = $parser->get_all();
691
		foreach ( $tokens as $index => $token ) {
692
			if ( Token::WORD === $token->type ) {
693
				$words[ $index ] = $token;
694
			}
695
		}
696
697
		$this->assertArraySubset( $words, $parser->get_type( Token::WORD ) );
698
	}
699
}
700