1<?php
2
3namespace Vanderlee\Sentence\Tests;
4
5use PHPUnit_Framework_TestCase;
6use Vanderlee\Sentence\Sentence;
7
8/**
9 * @coversDefaultClass \Vanderlee\Sentence\Sentence
10 */
11class SentenceTest extends PHPUnit_Framework_TestCase
12{
13
14    /**
15     * @var Sentence
16     */
17    protected $object;
18
19    /**
20     * Sets up the fixture, for example, opens a network connection.
21     * This method is called before a test is executed.
22     */
23    protected function setUp()
24    {
25        $this->object = new \Vanderlee\Sentence\Sentence();
26    }
27
28    /**
29     * @covers ::count
30     */
31    public function testCountEmpty()
32    {
33        $this->assertSame(0, $this->object->count(''));
34        $this->assertSame(0, $this->object->count(' '));
35        $this->assertSame(0, $this->object->count("\n"));
36    }
37
38    /**
39     * @covers ::count
40     */
41    public function testCountWord()
42    {
43        $this->assertSame(1, $this->object->count('Hello'));
44        $this->assertSame(1, $this->object->count('Hello.'));
45        $this->assertSame(1, $this->object->count('Hello...'));
46        $this->assertSame(1, $this->object->count('Hello!'));
47        $this->assertSame(1, $this->object->count('Hello?'));
48        $this->assertSame(1, $this->object->count('Hello?!'));
49    }
50
51    /**
52     * @covers ::count
53     */
54    public function testCountTwoWords()
55    {
56        $this->assertSame(1, $this->object->count('Hello world'));
57        $this->assertSame(1, $this->object->count('Hello world.'));
58        $this->assertSame(1, $this->object->count('Hello world...'));
59        $this->assertSame(1, $this->object->count('Hello world!'));
60        $this->assertSame(1, $this->object->count('Hello world?'));
61        $this->assertSame(1, $this->object->count('Hello world?!'));
62    }
63
64    /**
65     * @covers ::count
66     */
67    public function testCountMultipleWords()
68    {
69        $this->assertSame(2, $this->object->count('Hello world. Are you there'));
70        $this->assertSame(2, $this->object->count('Hello world. Are you there?'));
71        $this->assertSame(1, $this->object->count('Hello world, Are you there?'));
72        $this->assertSame(1, $this->object->count('Hello world: Are you there?'));
73        $this->assertSame(1, $this->object->count('Hello world... Are you there?'));
74    }
75
76    /**
77     * @covers ::count
78     */
79    public function testCountLinebreaks()
80    {
81        $this->assertSame(2, $this->object->count("Hello world...\rAre you there?"));
82        $this->assertSame(2, $this->object->count("Hello world...\nAre you there?"));
83        $this->assertSame(2, $this->object->count("Hello world...\r\nAre you there?"));
84        $this->assertSame(2, $this->object->count("Hello world...\r\n\rAre you there?"));
85        $this->assertSame(2, $this->object->count("Hello world...\n\r\nAre you there?"));
86        $this->assertSame(2, $this->object->count("Hello world...\n\nAre you there?"));
87        $this->assertSame(2, $this->object->count("Hello world...\r\rAre you there?"));
88    }
89
90    /**
91     * @covers ::count
92     */
93    public function testCountAbreviations()
94    {
95        $this->assertSame(1, $this->object->count("Hello mr. Smith."));
96        $this->assertSame(1, $this->object->count("Hello, OMG Kittens!"));
97        $this->assertSame(1, $this->object->count("Hello, abbrev. Kittens!"));
98        $this->assertSame(1, $this->object->count("Hello, O.M.G. Kittens!"));
99        $this->assertSame(1, $this->object->count("Last week, former director of the A.B.C. John B. Smith was fired."));
100        $this->assertSame(1, $this->object->count("Mr. Smith was not available for comment.."));
101    }
102
103    /**
104     * @covers ::count
105     */
106    public function testCountMultiplePunctuation()
107    {
108        $this->assertSame(2, $this->object->count("Hello there. Brave new world."));
109        $this->assertSame(1, $this->object->count("Hello there... Brave new world."));
110        $this->assertSame(2, $this->object->count("Hello there?... Brave new world."));
111        $this->assertSame(2, $this->object->count("Hello there!... Brave new world."));
112        $this->assertSame(2, $this->object->count("Hello there!!! Brave new world."));
113        $this->assertSame(2, $this->object->count("Hello there??? Brave new world."));
114    }
115
116    /**
117     * @covers ::count
118     */
119    public function testCountOneWordSentences()
120    {
121        $this->assertSame(2, $this->object->count("You? Smith?"));
122        $this->assertSame(2, $this->object->count("You there? Smith?"));
123        $this->assertSame(1, $this->object->count("You mr. Smith?"));
124        $this->assertSame(2, $this->object->count("Are you there. Mister Smith?"));
125        $this->assertSame(2, $this->object->count("Are you there. Smith, sir?"));
126        $this->assertSame(2, $this->object->count("Are you there. Mr. Smith?"));
127    }
128
129    /**
130     * @covers ::split
131     */
132    public function testSplitEmpty()
133    {
134        $this->assertSame([], $this->object->split(''));
135        $this->assertSame([], $this->object->split(' '));
136        $this->assertSame([], $this->object->split("\n"));
137    }
138
139    /**
140     * @covers ::cleanupUnicode
141     */
142    public function testCleanupUnicode()
143    {
144        $this->assertSame(['Fix "these" quotes'], $this->object->split('Fix "these" quotes'));
145        $this->assertSame(['Fix "these" quotes'], $this->object->split("Fix \xC2\xABthese\xC2\xAB quotes"));
146    }
147
148    /**
149     * @covers ::split
150     */
151    public function testSplitWord()
152    {
153        $this->assertSame(['Hello'], $this->object->split('Hello'));
154        $this->assertSame(['Hello.'], $this->object->split('Hello.'));
155        $this->assertSame(['Hello...'], $this->object->split('Hello...'));
156        $this->assertSame(['Hello!'], $this->object->split('Hello!'));
157        $this->assertSame(['Hello?'], $this->object->split('Hello?'));
158        $this->assertSame(['Hello?!'], $this->object->split('Hello?!'));
159    }
160
161    /**
162     * @covers ::split
163     */
164    public function testSplitMultipleWords()
165    {
166        $this->assertSame(['Hello world.', ' Are you there'], $this->object->split('Hello world. Are you there'));
167        $this->assertSame(['Hello world.', ' Are you there?'], $this->object->split('Hello world. Are you there?'));
168        $this->assertSame(['Hello world.', 'Are you there'], $this->object->split('Hello world. Are you there', Sentence::SPLIT_TRIM));
169        $this->assertSame(['Hello world.', 'Are you there?'], $this->object->split('Hello world. Are you there?', Sentence::SPLIT_TRIM));
170        $this->assertSame(['Hello world, Are you there?'], $this->object->split('Hello world, Are you there?'));
171        $this->assertSame(['Hello world: Are you there?'], $this->object->split('Hello world: Are you there?'));
172        $this->assertSame(['Hello world... Are you there?'], $this->object->split('Hello world... Are you there?'));
173    }
174
175    /**
176     * @covers ::split
177     */
178    public function testSplitLinebreaks()
179    {
180        $this->assertSame(["Hello world...\r", "Are you there?"], $this->object->split("Hello world...\rAre you there?"));
181        $this->assertSame(["Hello world...\n", " Are you there?"], $this->object->split("Hello world...\n Are you there?"));
182        $this->assertSame(["Hello world...\n", "Are you there?"], $this->object->split("Hello world...\nAre you there?"));
183        $this->assertSame(["Hello world...\r\n", "Are you there?"], $this->object->split("Hello world...\r\nAre you there?"));
184        $this->assertSame(["Hello world...\r\n\r", "Are you there?"], $this->object->split("Hello world...\r\n\rAre you there?"));
185        $this->assertSame(["Hello world...\n\r\n", "Are you there?"], $this->object->split("Hello world...\n\r\nAre you there?"));
186        $this->assertSame(["Hello world...\n\n", "Are you there?"], $this->object->split("Hello world...\n\nAre you there?"));
187        $this->assertSame(["Hello world...\r\r", "Are you there?"], $this->object->split("Hello world...\r\rAre you there?"));
188    }
189
190    /**
191     * @covers ::split
192     */
193    public function testSplitAbreviations()
194    {
195//		$this->markTestIncomplete('This test has not been implemented yet.');
196        $this->assertSame(['Hello mr. Smith.'], $this->object->split("Hello mr. Smith."));
197        $this->assertSame(['Hello, OMG Kittens!'], $this->object->split("Hello, OMG Kittens!"));
198        $this->assertSame(['Hello, abbrev. Kittens!'], $this->object->split("Hello, abbrev. Kittens!"));
199        $this->assertSame(['Hello, O.M.G. Kittens!'], $this->object->split("Hello, O.M.G. Kittens!"));
200        $this->assertSame(['Last week, former director of the A.B.C. John B. Smith was fired.'], $this->object->split("Last week, former director of the A.B.C. John B. Smith was fired."));
201        $this->assertSame(['Mr. Smith was not available for comment..'], $this->object->split("Mr. Smith was not available for comment.."));
202        $this->assertSame(['Hello mr. Smith.', ' Are you there?'], $this->object->split("Hello mr. Smith. Are you there?"));
203    }
204
205    /**
206     * @covers ::split
207     */
208    public function testSplitOneWordSentences()
209    {
210        $this->assertSame(["You?", " Smith?"], $this->object->split("You? Smith?"));
211        $this->assertSame(["You there?", " Smith?"], $this->object->split("You there? Smith?"));
212        $this->assertSame(["You mr. Smith?"], $this->object->split("You mr. Smith?"));
213        $this->assertSame(["Are you there.", " Mister Smith?"], $this->object->split("Are you there. Mister Smith?"));
214        $this->assertSame(["Are you there.", " Smith, sir?"], $this->object->split("Are you there. Smith, sir?"));
215        $this->assertSame(["Are you there.", " Mr. Smith?"], $this->object->split("Are you there. Mr. Smith?"));
216    }
217
218    /**
219     * @covers ::split
220     */
221    public function testSplitParenthesis()
222    {
223        $this->assertSame(["You there (not here!).", " Mister Smith"], $this->object->split("You there (not here!). Mister Smith"));
224        $this->assertSame(["You (not him!) here.", " Mister Smith"], $this->object->split("You (not him!) here. Mister Smith"));
225        $this->assertSame(["(What!) you here.", " Mister Smith"], $this->object->split("(What!) you here. Mister Smith"));
226        $this->assertSame(["You there (not here).", " Mister Smith"], $this->object->split("You there (not here). Mister Smith"));
227        $this->assertSame(["You (not him) here.", " Mister Smith"], $this->object->split("You (not him) here. Mister Smith"));
228        $this->assertSame(["(What) you here.", " Mister Smith"], $this->object->split("(What) you here. Mister Smith"));
229    }
230
231    /**
232     * @covers ::split
233     */
234    public function testSentenceWithNumericValues()
235    {
236        $this->assertSame(1, $this->object->count("The price is £25.50, including postage and packing."));
237        $this->assertSame(1, $this->object->count("The price is 25.50, including postage and packing."));
238        $this->assertSame(1, $this->object->count("I went true to size at 10.5 cms."));
239        $this->assertSame(2, $this->object->count("The prices are £25.50 or £27.50, including postage and packing. I went true to size at 10.5 cms."));
240    }
241
242    /**
243     * @covers ::floatNumberClean
244     * @covers ::floatNumberRevert
245     *
246     * @dataProvider dataSplit
247     *
248     * @param string[] $expected
249     * @param string   $text
250     *
251     * @return void
252     */
253    public function testSplit(array $expected, string $text)
254    {
255        $this->assertSame($expected, $this->object->split($text));
256        $this->assertSame(count($expected), $this->object->count($text));
257    }
258
259    public function dataSplit()
260    {
261        return [
262            'repeat 2'                            => [
263                [
264                    'He got £2.',
265                    ' He lost £2.',
266                    ' He had £2.',
267                ],
268                'He got £2. He lost £2. He had £2.',
269            ],
270            'times'                               => [
271                [
272                    'If at 8:00 pm, do something, there is a good chance that by 8:45 pm we do something else.',
273                    ' This is another sentence',
274                ],
275                'If at 8:00 pm, do something, there is a good chance that by 8:45 pm we do something else. This is another sentence',
276            ],
277            'lead/trailing zeroes'                => [
278                [
279                    'Number 00.20 it is',
280                ],
281                'Number 00.20 it is',
282            ],
283            'Bug report #15; ))) -1 index offset' => [
284                [
285                    ')))',
286                ],
287                ')))',
288            ],
289            'Price'                               => [
290                [
291                    'The price is 25.50, including postage and packing.',
292                ],
293                'The price is 25.50, including postage and packing.',
294            ],
295            'Recursive replacement'               => [
296                [
297                    'From 11 to 12.',
298                    ' From 11 to 15.',
299                ],
300                'From 11 to 12. From 11 to 15.',
301            ],
302        ];
303    }
304}
305