Skip to content

Commit 798d77d

Browse files
author
Martin Smeeckaert
committed
add breakpoint tests
1 parent a7cb878 commit 798d77d

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace spec\Coduo\PHPHumanizer\String;
4+
5+
use Coduo\PHPHumanizer\String\WordBreakpoint;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
9+
class WordBreakpointSpec extends ObjectBehavior
10+
{
11+
12+
function it_breakpoint()
13+
{
14+
$text = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.';
15+
16+
$this->calculatePosition($text, 2)->shouldReturn(5);
17+
$this->calculatePosition($text, 4)->shouldReturn(5);
18+
$this->calculatePosition($text, 5)->shouldReturn(5);
19+
$this->calculatePosition($text, 10)->shouldReturn(11);
20+
$this->calculatePosition($text, 20)->shouldReturn(22);
21+
$this->calculatePosition($text, -2)->shouldReturn(-2);
22+
$this->calculatePosition($text, 0)->shouldReturn(5);
23+
}
24+
25+
}

src/Coduo/PHPHumanizer/String/WordBreakpoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class WordBreakpoint implements Breakpoint
77
public function calculatePosition($text, $charactersCount)
88
{
99
$length = $charactersCount;
10-
if (false !== ($breakpoint = mb_strpos($text, ' ', $charactersCount))) {
10+
if ($charactersCount >= 0 && false !== ($breakpoint = mb_strpos($text, ' ', $charactersCount))) {
1111
$length = $breakpoint;
1212
}
1313
return $length;

0 commit comments

Comments
 (0)