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

Make Stringy\Stringy OO, and add Stringy\StaticStringy

This commit is contained in:
Daniel St. Jules
2013-07-25 01:57:13 -04:00
parent 26ee99743c
commit ffe9d769d7
6 changed files with 1544 additions and 804 deletions

View File

@@ -0,0 +1,455 @@
<?php
class CommonTest extends PHPUnit_Framework_TestCase
{
public function stringsForUpperCaseFirst()
{
$testData = array(
array('Test', 'Test'),
array('Test', 'test'),
array('1a', '1a'),
array('Σ test', 'σ test', 'UTF-8'),
array(' σ test', ' σ test', 'UTF-8')
);
return $testData;
}
public function stringsForLowerCaseFirst()
{
$testData = array(
array('test', 'Test'),
array('test', 'test'),
array('1a', '1a'),
array('σ test', 'Σ test', 'UTF-8'),
array(' Σ test', ' Σ test', 'UTF-8')
);
return $testData;
}
public function stringsForCamelize()
{
$testData = array(
array('camelCase', 'CamelCase'),
array('camelCase', 'Camel-Case'),
array('camelCase', 'camel case'),
array('camelCase', 'camel -case'),
array('camelCase', 'camel - case'),
array('camelCase', 'camel_case'),
array('camelCTest', 'camel c test'),
array('stringWith1Number', 'string_with1number'),
array('stringWith22Numbers', 'string-with-2-2 numbers'),
array('1Camel2Case', '1camel2case'),
array('camelΣase', 'camel σase', 'UTF-8'),
array('στανιλCase', 'Στανιλ case', 'UTF-8'),
array('σamelCase', 'σamel Case', 'UTF-8')
);
return $testData;
}
public function stringsForUpperCamelize()
{
$testData = array(
array('CamelCase', 'camelCase'),
array('CamelCase', 'Camel-Case'),
array('CamelCase', 'camel case'),
array('CamelCase', 'camel -case'),
array('CamelCase', 'camel - case'),
array('CamelCase', 'camel_case'),
array('CamelCTest', 'camel c test'),
array('StringWith1Number', 'string_with1number'),
array('StringWith22Numbers', 'string-with-2-2 numbers'),
array('1Camel2Case', '1camel2case'),
array('CamelΣase', 'camel σase', 'UTF-8'),
array('ΣτανιλCase', 'στανιλ case', 'UTF-8'),
array('ΣamelCase', 'Σamel Case', 'UTF-8')
);
return $testData;
}
public function stringsForDasherize()
{
$testData = array(
array('test-case', 'testCase'),
array('test-case', 'Test-Case'),
array('test-case', 'test case'),
array('-test-case', '-test -case'),
array('test-case', 'test - case'),
array('test-case', 'test_case'),
array('test-c-test', 'test c test'),
array('test-d-case', 'TestDCase'),
array('test-c-c-test', 'TestCCTest'),
array('string-with1number', 'string_with1number'),
array('string-with-2-2-numbers', 'String-with_2_2 numbers'),
array('1test2case', '1test2case'),
array('dash-σase', 'dash Σase', 'UTF-8'),
array('στανιλ-case', 'Στανιλ case', 'UTF-8'),
array('σash-case', 'Σash Case', 'UTF-8')
);
return $testData;
}
public function stringsForUnderscored()
{
$testData = array(
array('test_case', 'testCase'),
array('test_case', 'Test-Case'),
array('test_case', 'test case'),
array('test_case', 'test -case'),
array('_test_case', '-test - case'),
array('test_case', 'test_case'),
array('test_c_test', ' test c test'),
array('test_u_case', 'TestUCase'),
array('test_c_c_test', 'TestCCTest'),
array('string_with1number', 'string_with1number'),
array('string_with_2_2_numbers', 'String-with_2_2 numbers'),
array('1test2case', '1test2case'),
array('test_σase', 'test Σase', 'UTF-8'),
array('στανιλ_case', 'Στανιλ case', 'UTF-8'),
array('σash_case', 'Σash Case', 'UTF-8')
);
return $testData;
}
public function stringsForSwapCase()
{
$testData = array(
array('TESTcASE', 'testCase'),
array('tEST-cASE', 'Test-Case'),
array(' - σASH cASE', ' - Σash Case', 'UTF-8'),
array('νΤΑΝΙΛ', 'Ντανιλ', 'UTF-8')
);
return $testData;
}
public function stringsForTitleize()
{
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
$testData = array(
array('Testing The Method', 'testing the method'),
array('Testing the Method', 'testing the method', $ignore, 'UTF-8'),
array('I Like to Watch DVDs at Home', 'i like to watch DVDs at home',
$ignore, 'UTF-8'),
array('Θα Ήθελα Να Φύγει', ' Θα ήθελα να φύγει ', null, 'UTF-8')
);
return $testData;
}
public function stringsForHumanize()
{
$testData = array(
array('Author', 'author_id'),
array('Test user', ' _test_user_'),
array('Συγγραφέας', ' συγγραφέας_id ', 'UTF-8')
);
return $testData;
}
public function stringsForTidy()
{
$testData = array(
array('"I see..."', '“I see…”'),
array("'This too'", "This too"),
array('test-dash', 'test—dash'),
array('Ο συγγραφέας είπε...', 'Ο συγγραφέας είπε…')
);
return $testData;
}
public function stringsForCollapseWhitespace()
{
$testData = array(
array('foo bar', ' foo bar '),
array('test string', 'test string'),
array('Ο συγγραφέας', ' Ο συγγραφέας '),
array('123', ' 123 '),
array('', ' '),
array('', ''),
);
return $testData;
}
public function stringsForStandardize()
{
$testData = array(
array('foo bar', 'fòô bàř'),
array(' TEST ', ' ŤÉŚŢ '),
array('φ = z = 3', 'φ = ź = 3')
);
return $testData;
}
public function stringsForPad()
{
$testData = array(
// $length <= $str
array('foo bar', 'foo bar', -1),
array('foo bar', 'foo bar', 7),
array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'),
// right
array('foo bar ', 'foo bar', 9),
array('foo bar_*', 'foo bar', 9, '_*', 'right'),
array('foo bar_*_', 'foo bar', 10, '_*', 'right'),
array('fòô bàř ', 'fòô bàř', 9, ' ', 'right', 'UTF-8'),
array('fòô bàř¬ø', 'fòô bàř', 9, '¬ø', 'right', 'UTF-8'),
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'right', 'UTF-8'),
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'right', 'UTF-8'),
// left
array(' foo bar', 'foo bar', 9, ' ', 'left'),
array('_*foo bar', 'foo bar', 9, '_*', 'left'),
array('_*_foo bar', 'foo bar', 10, '_*', 'left'),
array(' fòô bàř', 'fòô bàř', 9, ' ', 'left', 'UTF-8'),
array('¬øfòô bàř', 'fòô bàř', 9, '¬ø', 'left', 'UTF-8'),
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'left', 'UTF-8'),
// both
array('foo bar ', 'foo bar', 8, ' ', 'both'),
array(' foo bar ', 'foo bar', 9, ' ', 'both'),
array('fòô bàř ', 'fòô bàř', 8, ' ', 'both', 'UTF-8'),
array(' fòô bàř ', 'fòô bàř', 9, ' ', 'both', 'UTF-8'),
array('fòô bàř¬', 'fòô bàř', 8, '¬ø', 'both', 'UTF-8'),
array('¬fòô bàř¬', 'fòô bàř', 9, '¬ø', 'both', 'UTF-8'),
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'both', 'UTF-8'),
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬ø', 'both', 'UTF-8'),
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'both', 'UTF-8'),
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'both', 'UTF-8'),
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8')
);
return $testData;
}
public function stringsForPadLeft()
{
$testData = array(
array(' foo bar', 'foo bar', 9),
array('_*_foo bar', 'foo bar', 10, '_*'),
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
);
return $testData;
}
public function stringsForPadRight()
{
$testData = array(
array('foo bar ', 'foo bar', 9),
array('foo bar_*_', 'foo bar', 10, '_*'),
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
);
return $testData;
}
public function stringsForPadBoth()
{
$testData = array(
array('foo bar ', 'foo bar', 8),
array(' foo bar ', 'foo bar', 9, ' '),
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
);
return $testData;
}
public function stringsForStartsWith()
{
$testData = array(
array(true, 'foo bars', 'foo bar'),
array(true, 'FOO bars', 'foo bar', false),
array(true, 'FOO bars', 'foo BAR', false),
array(true, 'FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'),
array(true, 'fòô bàřs', 'fòô BÀŘ', false, 'UTF-8'),
array(false, 'foo bar', 'bar'),
array(false, 'foo bar', 'foo bars'),
array(false, 'FOO bar', 'foo bars'),
array(false, 'FOO bars', 'foo BAR'),
array(false, 'FÒÔ bàřs', 'fòô bàř', true, 'UTF-8'),
array(false, 'fòô bàřs', 'fòô BÀŘ', true, 'UTF-8'),
);
return $testData;
}
public function stringsForEndsWith()
{
$testData = array(
array(true, 'foo bars', 'o bars'),
array(true, 'FOO bars', 'o bars', false),
array(true, 'FOO bars', 'o BARs', false),
array(true, 'FÒÔ bàřs', 'ô bàřs', false, 'UTF-8'),
array(true, 'fòô bàřs', 'ô BÀŘs', false, 'UTF-8'),
array(false, 'foo bar', 'foo'),
array(false, 'foo bar', 'foo bars'),
array(false, 'FOO bar', 'foo bars'),
array(false, 'FOO bars', 'foo BARS'),
array(false, 'FÒÔ bàřs', 'fòô bàřs', true, 'UTF-8'),
array(false, 'fòô bàřs', 'fòô BÀŘS', true, 'UTF-8'),
);
return $testData;
}
public function stringsForToSpaces()
{
$testData = array(
array(' foo bar ', ' foo bar '),
array(' foo bar ', ' foo bar ', 5),
array(' foo bar ', ' foo bar ', 2),
array('foobar', ' foo bar ', 0),
array(" foo\n bar", " foo\n bar"),
array(" fòô\n bàř", " fòô\n bàř")
);
return $testData;
}
public function stringsForToTabs()
{
$testData = array(
array(' foo bar ', ' foo bar '),
array(' foo bar ', ' foo bar ', 5),
array(' foo bar ', ' foo bar ', 2),
array(" foo\n bar", " foo\n bar"),
array(" fòô\n bàř", " fòô\n bàř")
);
return $testData;
}
public function stringsForSlugify()
{
$testData = array(
array('foo-bar', ' foo bar '),
array('foo-dbar', " Foo d'Bar "),
array('a-string-with-dashes', 'A string-with-dashes'),
array('using-strings-like-foo-bar', 'Using strings like fòô bàř'),
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
array('numbers-1234', 'numbers 1234')
);
return $testData;
}
public function stringsForContains()
{
$testData = array(
array(true, 'This string contains foo bar', 'foo bar'),
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'),
array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 'UTF-8'),
array(false, 'This string contains foo bar', 'Foo bar'),
array(false, 'This string contains foo bar', 'foobar'),
array(false, 'This string contains foo bar', 'foo bar '),
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', 'UTF-8'),
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', 'UTF-8')
);
return $testData;
}
public function stringsForSurround()
{
$testData = array(
array('__foobar__', 'foobar', '__'),
array('test', 'test', ''),
array('**', '', '*'),
array('¬fòô bàř¬', 'fòô bàř', '¬'),
array('ßå∆˚ test ßå∆˚', ' test ', 'ßå∆˚')
);
return $testData;
}
public function stringsForInsert()
{
$testData = array(
array('foo bar', 'oo bar', 'f', 0),
array('foo bar', 'f bar', 'oo', 1),
array('f bar', 'f bar', 'oo', 20),
array('foo bar', 'foo ba', 'r', 6),
array('fòô bàř', 'òô bàř', 'f', 0, 'UTF-8'),
array('fòô bàř', 'f bàř', 'òô', 1, 'UTF-8'),
array('fòô bàř', 'fòô bà', 'ř', 6, 'UTF-8')
);
return $testData;
}
public function stringsForSafeTruncate()
{
$testData = array(
array('Test foo bar', 'Test foo bar', 12),
array('Test foo', 'Test foo bar', 11),
array('Test foo', 'Test foo bar', 8),
array('Test', 'Test foo bar', 7),
array('Test', 'Test foo bar', 4),
array('Test foo bar', 'Test foo bar', 12, '...'),
array('Test foo...', 'Test foo bar', 11, '...'),
array('Test...', 'Test foo bar', 8, '...'),
array('Test...', 'Test foo bar', 7, '...'),
array('...', 'Test foo bar', 4, '...'),
array('Test....', 'Test foo bar', 11, '....'),
array('Test fòô bàř', 'Test fòô bàř', 12, '', 'UTF-8'),
array('Test fòô', 'Test fòô bàř', 11, '', 'UTF-8'),
array('Test fòô', 'Test fòô bàř', 8, '', 'UTF-8'),
array('Test', 'Test fòô bàř', 7, '', 'UTF-8'),
array('Test', 'Test fòô bàř', 4, '', 'UTF-8'),
array('Test fòô bàř', 'Test fòô bàř', 12, 'ϰϰ', 'UTF-8'),
array('Test fòôϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'),
array('Testϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'),
array('Testϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'),
array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8')
);
return $testData;
}
public function stringsForReverse()
{
$testData = array(
array('', ''),
array('raboof', 'foobar'),
array('řàbôòf', 'fòôbàř', 'UTF-8'),
array('řàb ôòf', 'fòô bàř', 'UTF-8'),
array('∂∆ ˚åß', 'ßå˚ ∆∂', 'UTF-8')
);
return $testData;
}
public function stringsForShuffle()
{
$testData = array(
array('foo bar'),
array('∂∆ ˚åß', 'UTF-8'),
array('å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', '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() {
$this->assertTrue(true);
}
}

View File

@@ -0,0 +1,262 @@
<?php
$base = realpath(dirname(__FILE__) . '/../..');
require("$base/src/Stringy/StaticStringy.php");
use Stringy\StaticStringy as S;
class StaticStringyTestCase extends CommonTest
{
/**
* @dataProvider stringsForUpperCaseFirst
*/
public function testUpperCaseFirst($expected, $str, $encoding = null)
{
$result = S::upperCaseFirst($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForLowerCaseFirst
*/
public function testLowerCaseFirst($expected, $str, $encoding = null)
{
$result = S::lowerCaseFirst($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCamelize
*/
public function testCamelize($expected, $str, $encoding = null)
{
$result = S::camelize($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForUpperCamelize
*/
public function testUpperCamelize($expected, $str, $encoding = null)
{
$result = S::upperCamelize($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForDasherize
*/
public function testDasherize($expected, $str, $encoding = null)
{
$result = S::dasherize($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForUnderscored
*/
public function testUnderscored($expected, $str, $encoding = null)
{
$result = S::underscored($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSwapCase
*/
public function testSwapCase($expected, $str, $encoding = null)
{
$result = S::swapCase($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForTitleize
*/
public function testTitleize($expected, $str, $ignore = null,
$encoding = null)
{
$result = S::titleize($str, $ignore, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForHumanize
*/
public function testHumanize($expected, $str, $encoding = null)
{
$result = S::humanize($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForTidy
*/
public function testTidy($expected, $str)
{
$result = S::tidy($str);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForCollapseWhitespace
*/
public function testCollapseWhitespace($expected, $str)
{
$result = S::collapseWhitespace($str);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForStandardize
*/
public function testStandardize($expected, $str)
{
$result = S::standardize($str);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPad
*/
public function testPad($expected, $str, $length, $padStr = ' ',
$padType = 'right', $encoding = null)
{
$result = S::pad($str, $length, $padStr, $padType, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPadLeft
*/
public function testPadLeft($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padLeft($str, $length, $padStr, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPadRight
*/
public function testPadRight($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padRight($str, $length, $padStr, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForPadBoth
*/
public function testPadBoth($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padBoth($str, $length, $padStr, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForStartsWith
*/
public function testStartsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$result = S::startsWith($str, $substring, $caseSensitive, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForEndsWith
*/
public function testEndsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$result = S::endsWith($str, $substring, $caseSensitive, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForToSpaces
*/
public function testToSpaces($expected, $str, $tabLength = 4)
{
$result = S::toSpaces($str, $tabLength);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForToTabs
*/
public function testToTabs($expected, $str, $tabLength = 4)
{
$result = S::toTabs($str, $tabLength);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSlugify
*/
public function testSlugify($expected, $str)
{
$result = S::slugify($str);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForContains
*/
public function testContains($expected, $haystack, $needle, $encoding = null)
{
$result = S::contains($haystack, $needle, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSurround
*/
public function testSurround($expected, $str, $substring)
{
$result = S::surround($str, $substring);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForInsert
*/
public function testInsert($expected, $str, $substring, $index,
$encoding = null)
{
$result = S::insert($str, $substring, $index, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForSafeTruncate
*/
public function testSafeTruncate($expected, $str, $length, $substring = '',
$encoding = null)
{
$result = S::safeTruncate($str, $length, $substring, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForReverse
*/
public function testReverse($expected, $str, $encoding = null)
{
$result = S::reverse($str, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForShuffle
*/
public function testShuffle($str, $encoding = null)
{
// We'll just make sure that the chars are present before/after shuffle
$result = S::shuffle($str, $encoding);
$this->assertEquals(count_chars($str), count_chars($result));
}
}

View File

@@ -5,704 +5,258 @@ require("$base/src/Stringy/Stringy.php");
use Stringy\Stringy as S;
class StringyTestCase extends PHPUnit_Framework_TestCase
class StringyTestCase extends CommonTest
{
/**
* @dataProvider stringsForUpperCaseFirst
*/
public function testUpperCaseFirst($expected, $string, $encoding = null)
public function testUpperCaseFirst($expected, $str, $encoding = null)
{
$result = S::upperCaseFirst($string, $encoding);
$result = S::create($str, $encoding)->upperCaseFirst();
$this->assertEquals($expected, $result);
}
public function stringsForUpperCaseFirst()
{
$testData = array(
array('Test', 'Test'),
array('Test', 'test'),
array('1a', '1a'),
array('Σ test', 'σ test', 'UTF-8'),
array(' σ test', ' σ test', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForLowerCaseFirst
*/
public function testLowerCaseFirst($expected, $string, $encoding = null)
public function testLowerCaseFirst($expected, $str, $encoding = null)
{
$result = S::lowerCaseFirst($string, $encoding);
$result = S::create($str, $encoding)->lowerCaseFirst();
$this->assertEquals($expected, $result);
}
public function stringsForLowerCaseFirst()
{
$testData = array(
array('test', 'Test'),
array('test', 'test'),
array('1a', '1a'),
array('σ test', 'Σ test', 'UTF-8'),
array(' Σ test', ' Σ test', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForCamelize
*/
public function testCamelize($expected, $string, $encoding = null)
public function testCamelize($expected, $str, $encoding = null)
{
$result = S::camelize($string, $encoding);
$result = S::create($str, $encoding)->camelize();
$this->assertEquals($expected, $result);
}
public function stringsForCamelize()
{
$testData = array(
array('camelCase', 'CamelCase'),
array('camelCase', 'Camel-Case'),
array('camelCase', 'camel case'),
array('camelCase', 'camel -case'),
array('camelCase', 'camel - case'),
array('camelCase', 'camel_case'),
array('camelCTest', 'camel c test'),
array('stringWith1Number', 'string_with1number'),
array('stringWith22Numbers', 'string-with-2-2 numbers'),
array('1Camel2Case', '1camel2case'),
array('camelΣase', 'camel σase', 'UTF-8'),
array('στανιλCase', 'Στανιλ case', 'UTF-8'),
array('σamelCase', 'σamel Case', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForUpperCamelize
*/
public function testUpperCamelize($expected, $string, $encoding = null)
public function testUpperCamelize($expected, $str, $encoding = null)
{
$result = S::upperCamelize($string, $encoding);
$result = S::create($str, $encoding)->upperCamelize();
$this->assertEquals($expected, $result);
}
public function stringsForUpperCamelize()
{
$testData = array(
array('CamelCase', 'camelCase'),
array('CamelCase', 'Camel-Case'),
array('CamelCase', 'camel case'),
array('CamelCase', 'camel -case'),
array('CamelCase', 'camel - case'),
array('CamelCase', 'camel_case'),
array('CamelCTest', 'camel c test'),
array('StringWith1Number', 'string_with1number'),
array('StringWith22Numbers', 'string-with-2-2 numbers'),
array('1Camel2Case', '1camel2case'),
array('CamelΣase', 'camel σase', 'UTF-8'),
array('ΣτανιλCase', 'στανιλ case', 'UTF-8'),
array('ΣamelCase', 'Σamel Case', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForDasherize
*/
public function testDasherize($expected, $string, $encoding = null)
public function testDasherize($expected, $str, $encoding = null)
{
$result = S::dasherize($string, $encoding);
$result = S::create($str, $encoding)->dasherize();
$this->assertEquals($expected, $result);
}
public function stringsForDasherize()
{
$testData = array(
array('test-case', 'testCase'),
array('test-case', 'Test-Case'),
array('test-case', 'test case'),
array('-test-case', '-test -case'),
array('test-case', 'test - case'),
array('test-case', 'test_case'),
array('test-c-test', 'test c test'),
array('test-d-case', 'TestDCase'),
array('test-c-c-test', 'TestCCTest'),
array('string-with1number', 'string_with1number'),
array('string-with-2-2-numbers', 'String-with_2_2 numbers'),
array('1test2case', '1test2case'),
array('dash-σase', 'dash Σase', 'UTF-8'),
array('στανιλ-case', 'Στανιλ case', 'UTF-8'),
array('σash-case', 'Σash Case', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForUnderscored
*/
public function testUnderscored($expected, $string, $encoding = null)
public function testUnderscored($expected, $str, $encoding = null)
{
$result = S::underscored($string, $encoding);
$result = S::create($str, $encoding)->underscored();
$this->assertEquals($expected, $result);
}
public function stringsForUnderscored()
{
$testData = array(
array('test_case', 'testCase'),
array('test_case', 'Test-Case'),
array('test_case', 'test case'),
array('test_case', 'test -case'),
array('_test_case', '-test - case'),
array('test_case', 'test_case'),
array('test_c_test', ' test c test'),
array('test_u_case', 'TestUCase'),
array('test_c_c_test', 'TestCCTest'),
array('string_with1number', 'string_with1number'),
array('string_with_2_2_numbers', 'String-with_2_2 numbers'),
array('1test2case', '1test2case'),
array('test_σase', 'test Σase', 'UTF-8'),
array('στανιλ_case', 'Στανιλ case', 'UTF-8'),
array('σash_case', 'Σash Case', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForSwapCase
*/
public function testSwapCase($expected, $string, $encoding = null)
public function testSwapCase($expected, $str, $encoding = null)
{
$result = S::swapCase($string, $encoding);
$result = S::create($str, $encoding)->swapCase();
$this->assertEquals($expected, $result);
}
public function stringsForSwapCase()
{
$testData = array(
array('TESTcASE', 'testCase'),
array('tEST-cASE', 'Test-Case'),
array(' - σASH cASE', ' - Σash Case', 'UTF-8'),
array('νΤΑΝΙΛ', 'Ντανιλ', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForTitleize
*/
public function testTitleize($expected, $string, $ignore = null,
public function testTitleize($expected, $str, $ignore = null,
$encoding = null)
{
$result = S::titleize($string, $ignore, $encoding);
$result = S::create($str, $encoding)->titleize($ignore);
$this->assertEquals($expected, $result);
}
public function stringsForTitleize()
{
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
$testData = array(
array('Testing The Method', 'testing the method'),
array('Testing the Method', 'testing the method', $ignore, 'UTF-8'),
array('I Like to Watch DVDs at Home', 'i like to watch DVDs at home',
$ignore, 'UTF-8'),
array('Θα Ήθελα Να Φύγει', ' Θα ήθελα να φύγει ', null, 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForHumanize
*/
public function testHumanize($expected, $string, $encoding = null)
public function testHumanize($expected, $str, $encoding = null)
{
$result = S::humanize($string, $encoding);
$result = S::create($str, $encoding)->humanize();
$this->assertEquals($expected, $result);
}
public function stringsForHumanize()
{
$testData = array(
array('Author', 'author_id'),
array('Test user', ' _test_user_'),
array('Συγγραφέας', ' συγγραφέας_id ', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForTidy
*/
public function testTidy($expected, $string)
public function testTidy($expected, $str)
{
$result = S::tidy($string);
$result = S::create($str)->tidy();
$this->assertEquals($expected, $result);
}
public function stringsForTidy()
{
$testData = array(
array('"I see..."', '“I see…”'),
array("'This too'", "This too"),
array('test-dash', 'test—dash'),
array('Ο συγγραφέας είπε...', 'Ο συγγραφέας είπε…')
);
return $testData;
}
/**
* @dataProvider stringsForCollapseWhitespace
*/
public function testCollapseWhitespace($expected, $string)
public function testCollapseWhitespace($expected, $str)
{
$result = S::collapseWhitespace($string);
$result = S::create($str)->collapseWhitespace();
$this->assertEquals($expected, $result);
}
public function stringsForCollapseWhitespace()
{
$testData = array(
array('foo bar', ' foo bar '),
array('test string', 'test string'),
array('Ο συγγραφέας', ' Ο συγγραφέας '),
array('123', ' 123 '),
array('', ' '),
array('', ''),
);
return $testData;
}
/**
* @dataProvider stringsForStandardize
*/
public function testStandardize($expected, $string)
public function testStandardize($expected, $str)
{
$result = S::standardize($string);
$result = S::create($str)->standardize();
$this->assertEquals($expected, $result);
}
public function stringsForStandardize()
{
$testData = array(
array('foo bar', 'fòô bàř'),
array(' TEST ', ' ŤÉŚŢ '),
array('φ = z = 3', 'φ = ź = 3')
);
return $testData;
}
/**
* @dataProvider stringsForPad
*/
public function testPad($expected, $string, $length, $padStr = ' ',
public function testPad($expected, $str, $length, $padStr = ' ',
$padType = 'right', $encoding = null)
{
$result = S::pad($string, $length, $padStr, $padType, $encoding);
$result = S::create($str, $encoding)->pad($length, $padStr, $padType);
$this->assertEquals($expected, $result);
}
public function stringsForPad()
{
$testData = array(
// $length <= $str
array('foo bar', 'foo bar', -1),
array('foo bar', 'foo bar', 7),
array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'),
// right
array('foo bar ', 'foo bar', 9),
array('foo bar_*', 'foo bar', 9, '_*', 'right'),
array('foo bar_*_', 'foo bar', 10, '_*', 'right'),
array('fòô bàř ', 'fòô bàř', 9, ' ', 'right', 'UTF-8'),
array('fòô bàř¬ø', 'fòô bàř', 9, '¬ø', 'right', 'UTF-8'),
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'right', 'UTF-8'),
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'right', 'UTF-8'),
// left
array(' foo bar', 'foo bar', 9, ' ', 'left'),
array('_*foo bar', 'foo bar', 9, '_*', 'left'),
array('_*_foo bar', 'foo bar', 10, '_*', 'left'),
array(' fòô bàř', 'fòô bàř', 9, ' ', 'left', 'UTF-8'),
array('¬øfòô bàř', 'fòô bàř', 9, '¬ø', 'left', 'UTF-8'),
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'left', 'UTF-8'),
// both
array('foo bar ', 'foo bar', 8, ' ', 'both'),
array(' foo bar ', 'foo bar', 9, ' ', 'both'),
array('fòô bàř ', 'fòô bàř', 8, ' ', 'both', 'UTF-8'),
array(' fòô bàř ', 'fòô bàř', 9, ' ', 'both', 'UTF-8'),
array('fòô bàř¬', 'fòô bàř', 8, '¬ø', 'both', 'UTF-8'),
array('¬fòô bàř¬', 'fòô bàř', 9, '¬ø', 'both', 'UTF-8'),
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'both', 'UTF-8'),
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬ø', 'both', 'UTF-8'),
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'both', 'UTF-8'),
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'both', 'UTF-8'),
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForPadLeft
*/
public function testPadLeft($expected, $string, $length, $padStr = ' ',
public function testPadLeft($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padLeft($string, $length, $padStr, $encoding);
$result = S::create($str, $encoding)->padLeft($length, $padStr);
$this->assertEquals($expected, $result);
}
public function stringsForPadLeft()
{
$testData = array(
array(' foo bar', 'foo bar', 9),
array('_*_foo bar', 'foo bar', 10, '_*'),
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
);
return $testData;
}
/**
* @dataProvider stringsForPadRight
*/
public function testPadRight($expected, $string, $length, $padStr = ' ',
public function testPadRight($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padRight($string, $length, $padStr, $encoding);
$result = S::create($str, $encoding)->padRight($length, $padStr);
$this->assertEquals($expected, $result);
}
public function stringsForPadRight()
{
$testData = array(
array('foo bar ', 'foo bar', 9),
array('foo bar_*_', 'foo bar', 10, '_*'),
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
);
return $testData;
}
/**
* @dataProvider stringsForPadBoth
*/
public function testPadBoth($expected, $string, $length, $padStr = ' ',
public function testPadBoth($expected, $str, $length, $padStr = ' ',
$encoding = null)
{
$result = S::padBoth($string, $length, $padStr, $encoding);
$result = S::create($str, $encoding)->padBoth($length, $padStr);
$this->assertEquals($expected, $result);
}
public function stringsForPadBoth()
{
$testData = array(
array('foo bar ', 'foo bar', 8),
array(' foo bar ', 'foo bar', 9, ' '),
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForStartsWith
*/
public function testStartsWith($expected, $string, $substring,
public function testStartsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$result = S::startsWith($string, $substring, $caseSensitive, $encoding);
$result = S::create($str, $encoding)->startsWith($substring, $caseSensitive);
$this->assertEquals($expected, $result);
}
public function stringsForStartsWith()
{
$testData = array(
array(true, 'foo bars', 'foo bar'),
array(true, 'FOO bars', 'foo bar', false),
array(true, 'FOO bars', 'foo BAR', false),
array(true, 'FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'),
array(true, 'fòô bàřs', 'fòô BÀŘ', false, 'UTF-8'),
array(false, 'foo bar', 'bar'),
array(false, 'foo bar', 'foo bars'),
array(false, 'FOO bar', 'foo bars'),
array(false, 'FOO bars', 'foo BAR'),
array(false, 'FÒÔ bàřs', 'fòô bàř', true, 'UTF-8'),
array(false, 'fòô bàřs', 'fòô BÀŘ', true, 'UTF-8'),
);
return $testData;
}
/**
* @dataProvider stringsForEndsWith
*/
public function testEndsWith($expected, $string, $substring,
public function testEndsWith($expected, $str, $substring,
$caseSensitive = true, $encoding = null)
{
$result = S::endsWith($string, $substring, $caseSensitive, $encoding);
$result = S::create($str, $encoding)->endsWith($substring, $caseSensitive);
$this->assertEquals($expected, $result);
}
public function stringsForEndsWith()
{
$testData = array(
array(true, 'foo bars', 'o bars'),
array(true, 'FOO bars', 'o bars', false),
array(true, 'FOO bars', 'o BARs', false),
array(true, 'FÒÔ bàřs', 'ô bàřs', false, 'UTF-8'),
array(true, 'fòô bàřs', 'ô BÀŘs', false, 'UTF-8'),
array(false, 'foo bar', 'foo'),
array(false, 'foo bar', 'foo bars'),
array(false, 'FOO bar', 'foo bars'),
array(false, 'FOO bars', 'foo BARS'),
array(false, 'FÒÔ bàřs', 'fòô bàřs', true, 'UTF-8'),
array(false, 'fòô bàřs', 'fòô BÀŘS', true, 'UTF-8'),
);
return $testData;
}
/**
* @dataProvider stringsForToSpaces
*/
public function testToSpaces($expected, $string, $tabLength = 4)
public function testToSpaces($expected, $str, $tabLength = 4)
{
$result = S::toSpaces($string, $tabLength);
$result = S::create($str)->toSpaces($tabLength);
$this->assertEquals($expected, $result);
}
public function stringsForToSpaces()
{
$testData = array(
array(' foo bar ', ' foo bar '),
array(' foo bar ', ' foo bar ', 5),
array(' foo bar ', ' foo bar ', 2),
array('foobar', ' foo bar ', 0),
array(" foo\n bar", " foo\n bar"),
array(" fòô\n bàř", " fòô\n bàř")
);
return $testData;
}
/**
* @dataProvider stringsForToTabs
*/
public function testToTabs($expected, $string, $tabLength = 4)
public function testToTabs($expected, $str, $tabLength = 4)
{
$result = S::toTabs($string, $tabLength);
$result = S::create($str)->toTabs($tabLength);
$this->assertEquals($expected, $result);
}
public function stringsForToTabs()
{
$testData = array(
array(' foo bar ', ' foo bar '),
array(' foo bar ', ' foo bar ', 5),
array(' foo bar ', ' foo bar ', 2),
array(" foo\n bar", " foo\n bar"),
array(" fòô\n bàř", " fòô\n bàř")
);
return $testData;
}
/**
* @dataProvider stringsForSlugify
*/
public function testSlugify($expected, $string)
public function testSlugify($expected, $str)
{
$result = S::slugify($string);
$result = S::create($str)->slugify();
$this->assertEquals($expected, $result);
}
public function stringsForSlugify()
{
$testData = array(
array('foo-bar', ' foo bar '),
array('foo-dbar', " Foo d'Bar "),
array('a-string-with-dashes', 'A string-with-dashes'),
array('using-strings-like-foo-bar', 'Using strings like fòô bàř'),
array('unrecognized-chars-like', 'unrecognized chars like συγγρ'),
array('numbers-1234', 'numbers 1234')
);
return $testData;
}
/**
* @dataProvider stringsForContains
*/
public function testContains($expected, $haystack, $needle, $encoding = null)
{
$result = S::contains($haystack, $needle, $encoding);
$result = S::create($haystack, $encoding)->contains($needle);
$this->assertEquals($expected, $result);
}
public function stringsForContains()
{
$testData = array(
array(true, 'This string contains foo bar', 'foo bar'),
array(true, '12398!@(*%!@# @!%#*&^%', ' @!%#*&^%'),
array(true, 'Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å´¥©', 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'å˚ ∆', 'UTF-8'),
array(true, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'øœ¬', 'UTF-8'),
array(false, 'This string contains foo bar', 'Foo bar'),
array(false, 'This string contains foo bar', 'foobar'),
array(false, 'This string contains foo bar', 'foo bar '),
array(false, 'Ο συγγραφέας είπε', ' συγγραφέας ', 'UTF-8'),
array(false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', ' ßå˚', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForSurround
*/
public function testSurround($expected, $string, $substring)
public function testSurround($expected, $str, $substring)
{
$result = S::surround($string, $substring);
$result = S::create($str)->surround($substring);
$this->assertEquals($expected, $result);
}
public function stringsForSurround()
{
$testData = array(
array('__foobar__', 'foobar', '__'),
array('test', 'test', ''),
array('**', '', '*'),
array('¬fòô bàř¬', 'fòô bàř', '¬'),
array('ßå∆˚ test ßå∆˚', ' test ', 'ßå∆˚')
);
return $testData;
}
/**
* @dataProvider stringsForInsert
*/
public function testInsert($expected, $string, $substring, $index,
public function testInsert($expected, $str, $substring, $index,
$encoding = null)
{
$result = S::insert($string, $substring, $index, $encoding);
$result = S::create($str, $encoding)->insert($substring, $index);
$this->assertEquals($expected, $result);
}
public function stringsForInsert()
{
$testData = array(
array('foo bar', 'oo bar', 'f', 0),
array('foo bar', 'f bar', 'oo', 1),
array('f bar', 'f bar', 'oo', 20),
array('foo bar', 'foo ba', 'r', 6),
array('fòô bàř', 'òô bàř', 'f', 0, 'UTF-8'),
array('fòô bàř', 'f bàř', 'òô', 1, 'UTF-8'),
array('fòô bàř', 'fòô bà', 'ř', 6, 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForSafeTruncate
*/
public function testSafeTruncate($expected, $string, $length, $substring = '',
public function testSafeTruncate($expected, $str, $length, $substring = '',
$encoding = null)
{
$result = S::safeTruncate($string, $length, $substring, $encoding);
$result = S::create($str, $encoding)->safeTruncate($length, $substring);
$this->assertEquals($expected, $result);
}
public function stringsForSafeTruncate()
{
$testData = array(
array('Test foo bar', 'Test foo bar', 12),
array('Test foo', 'Test foo bar', 11),
array('Test foo', 'Test foo bar', 8),
array('Test', 'Test foo bar', 7),
array('Test', 'Test foo bar', 4),
array('Test foo bar', 'Test foo bar', 12, '...'),
array('Test foo...', 'Test foo bar', 11, '...'),
array('Test...', 'Test foo bar', 8, '...'),
array('Test...', 'Test foo bar', 7, '...'),
array('...', 'Test foo bar', 4, '...'),
array('Test....', 'Test foo bar', 11, '....'),
array('Test fòô bàř', 'Test fòô bàř', 12, '', 'UTF-8'),
array('Test fòô', 'Test fòô bàř', 11, '', 'UTF-8'),
array('Test fòô', 'Test fòô bàř', 8, '', 'UTF-8'),
array('Test', 'Test fòô bàř', 7, '', 'UTF-8'),
array('Test', 'Test fòô bàř', 4, '', 'UTF-8'),
array('Test fòô bàř', 'Test fòô bàř', 12, 'ϰϰ', 'UTF-8'),
array('Test fòôϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'),
array('Testϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'),
array('Testϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'),
array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForReverse
*/
public function testReverse($expected, $string, $encoding = null)
public function testReverse($expected, $str, $encoding = null)
{
$result = S::reverse($string, $encoding);
$result = S::create($str, $encoding)->reverse();
$this->assertEquals($expected, $result);
}
public function stringsForReverse()
{
$testData = array(
array('', ''),
array('raboof', 'foobar'),
array('řàbôòf', 'fòôbàř', 'UTF-8'),
array('řàb ôòf', 'fòô bàř', 'UTF-8'),
array('∂∆ ˚åß', 'ßå˚ ∆∂', 'UTF-8')
);
return $testData;
}
/**
* @dataProvider stringsForShuffle
*/
public function testShuffle($string, $encoding = null)
public function testShuffle($str, $encoding = null)
{
// We'll just make sure that the chars are present before/after shuffle
$result = S::shuffle($string, $encoding);
$this->assertEquals(count_chars($string), count_chars($result));
$result = S::create($str, $encoding)->shuffle();
$this->assertEquals(count_chars($str), count_chars($result));
}
public function stringsForShuffle()
{
$testData = array(
array('foo bar'),
array('∂∆ ˚åß', 'UTF-8'),
array('å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', 'UTF-8')
);
return $testData;
}
}