1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-28 07:20:04 +02:00

Add count() and replace()

This commit is contained in:
Daniel St. Jules
2013-07-28 23:21:44 -04:00
parent 87e86d3bf6
commit b1cdc7603f
6 changed files with 219 additions and 60 deletions

View File

@@ -796,6 +796,42 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForCount()
{
$testData = array(
array(0, '', 'foo'),
array(0, 'foo', 'bar'),
array(1, 'foo bar', 'foo'),
array(2, 'foo bar', 'o'),
array(0, '', 'fòô', 'UTF-8'),
array(0, 'fòô', 'bàř', 'UTF-8'),
array(1, 'fòô bàř', 'fòô', 'UTF-8'),
array(2, 'fôòô bàř', 'ô', 'UTF-8')
);
return $testData;
}
public function stringsForReplace()
{
$testData = array(
array('', '', '', ''),
array('foo', '', '', 'foo'),
array('foo bar', 'foo bar', '', ''),
array('bar', 'foo bar', 'foo ', ''),
array('far bar', 'foo bar', 'foo', 'far'),
array('bar bar', 'foo bar foo bar', 'foo ', ''),
array('', '', '', '', 'UTF-8'),
array('fòô', '', '', 'fòô', 'UTF-8'),
array('fòô bàř', 'fòô bàř', '', '', 'UTF-8'),
array('bàř', 'fòô bàř', 'fòô ', '', 'UTF-8'),
array('far bàř', 'fòô bàř', 'fòô', 'far', 'UTF-8'),
array('bàř bàř', 'fòô bàř fòô bàř', 'fòô ', '', 'UTF-8'),
);
return $testData;
}
// A test is required so as not to throw an error
// This is a lot cleaner than using PHPUnit's mocks to spy
public function test() {

View File

@@ -435,4 +435,23 @@ class StaticStringyTestCase extends CommonTest
$result = S::isUpperCase($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCount
*/
public function testCount($expected, $str, $substring, $encoding = null)
{
$result = S::count($str, $substring, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForReplace
*/
public function testReplace($expected, $str, $search, $replace,
$encoding = null)
{
$result = S::replace($str, $search, $replace, $encoding);
$this->assertEquals($expected, $result);
}
}

View File

@@ -501,4 +501,27 @@ class StringyTestCase extends CommonTest
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForCount
*/
public function testCount($expected, $str, $substring, $encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->count($substring);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
/**
* @dataProvider stringsForReplace
*/
public function testReplace($expected, $str, $search, $replace,
$encoding = null)
{
$stringy = S::create($str, $encoding);
$result = $stringy->replace($search, $replace);
$this->assertEquals($expected, $result);
$this->assertEquals($str, $stringy);
}
}