|
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\Fixes\Node_Fixes; |
|
26
|
|
|
|
|
27
|
|
|
use \PHP_Typography\Fixes\Node_Fixes\Dewidow_Fix; |
|
28
|
|
|
use \PHP_Typography\Settings; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Dewidow_Fix unit test. |
|
32
|
|
|
* |
|
33
|
|
|
* @coversDefaultClass \PHP_Typography\Fixes\Node_Fixes\Dewidow_Fix |
|
34
|
|
|
* @usesDefaultClass \PHP_Typography\Fixes\Node_Fixes\Dewidow_Fix |
|
35
|
|
|
* |
|
36
|
|
|
* @uses ::__construct |
|
37
|
|
|
* @uses PHP_Typography\Arrays |
|
38
|
|
|
* @uses PHP_Typography\DOM |
|
39
|
|
|
* @uses PHP_Typography\Settings |
|
40
|
|
|
* @uses PHP_Typography\Settings\Dash_Style |
|
41
|
|
|
* @uses PHP_Typography\Settings\Quote_Style |
|
42
|
|
|
* @uses PHP_Typography\Settings\Simple_Dashes |
|
43
|
|
|
* @uses PHP_Typography\Settings\Simple_Quotes |
|
44
|
|
|
* @uses PHP_Typography\Strings |
|
45
|
|
|
*/ |
|
46
|
|
|
class Dewidow_Fix_Test extends Node_Fix_Testcase { |
|
47
|
|
|
/** |
|
48
|
|
|
* Sets up the fixture, for example, opens a network connection. |
|
49
|
|
|
* This method is called before a test is executed. |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function setUp() { // @codingStandardsIgnoreLine |
|
52
|
|
|
parent::setUp(); |
|
53
|
|
|
|
|
54
|
|
|
$this->fix = new Dewidow_Fix(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Provide data for testing dewidowing. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
|
|
public function provide_dewidow_data() { |
|
63
|
|
|
return [ |
|
64
|
|
|
[ 'bla foo b', 'bla foo b', 3, 2 ], |
|
65
|
|
|
[ 'bla foo b', 'bla foo b', 3, 2 ], // don't replace thin space... |
|
66
|
|
|
[ 'bla foo b', 'bla foo b', 3, 2 ], // ... or hair space. |
|
67
|
|
|
[ 'bla foo bar', 'bla foo bar', 2, 2 ], |
|
68
|
|
|
[ 'bla foo bär...', 'bla foo bär...', 3, 3 ], |
|
69
|
|
|
[ 'bla foo bär…!', 'bla foo bär…!', 3, 3 ], |
|
70
|
|
|
[ 'bla foo bär­!', 'bla foo bär!', 3, 3 ], |
|
71
|
|
|
[ 'bla foo b­är!', 'bla foo bär!', 3, 3 ], |
|
72
|
|
|
[ 'bla foo b​är!', 'bla foo bär!', 3, 3 ], |
|
73
|
|
|
[ 'bla foo bär...', 'bla foo bär...', 3, 3 ], |
|
74
|
|
|
[ 'bla föö​bar s', 'bla föö​bar s', 3, 2 ], |
|
75
|
|
|
[ 'bla foo​bar s', 'bla foo​bar s', 2, 2 ], |
|
76
|
|
|
[ 'bla foo­bar', 'bla foo­bar', 3, 3 ], // ­ not matched. |
|
77
|
|
|
[ 'bla foo­bar bar', 'bla foo­bar bar', 3, 3 ], // ­ not matched, but syllable after is. |
|
78
|
|
|
[ 'bla foo​bar bar', 'bla foo​bar bar', 3, 3 ], |
|
79
|
|
|
[ 'bla foo bar bar', 'bla foo bar bar', 3, 3 ], // widow not replaced because the would pull too many letters from previous. |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Test apply. |
|
85
|
|
|
* |
|
86
|
|
|
* @covers ::apply |
|
87
|
|
|
* |
|
88
|
|
|
* @dataProvider provide_dewidow_data |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $html HTML input. |
|
91
|
|
|
* @param string $result Expected result. |
|
92
|
|
|
* @param int $max_pull Maximum number of pulled characters. |
|
93
|
|
|
* @param int $max_length Maximum word length for dewidowing. |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
public function test_apply( $html, $result, $max_pull, $max_length ) { |
|
|
|
|
|
|
96
|
|
|
$this->s->set_dewidow( true ); |
|
97
|
|
|
$this->s->set_max_dewidow_pull( $max_pull ); |
|
98
|
|
|
$this->s->set_max_dewidow_length( $max_length ); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertFixResultSame( $html, $result ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Test dewidow. |
|
105
|
|
|
* |
|
106
|
|
|
* @covers ::apply |
|
107
|
|
|
* |
|
108
|
|
|
* @dataProvider provide_dewidow_data |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $html HTML input. |
|
111
|
|
|
* @param string $result Expected result. |
|
112
|
|
|
* @param int $max_pull Maximum number of pulled characters. |
|
113
|
|
|
* @param int $max_length Maximum word length for dewidowing. |
|
114
|
|
|
*/ |
|
115
|
|
View Code Duplication |
public function test_apply_off( $html, $result, $max_pull, $max_length ) { |
|
|
|
|
|
|
116
|
|
|
$this->s->set_dewidow( false ); |
|
117
|
|
|
$this->s->set_max_dewidow_pull( $max_pull ); |
|
118
|
|
|
$this->s->set_max_dewidow_length( $max_length ); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertFixResultSame( $html, $html ); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.