ArrayUtilTest.php
1 year ago
StringUtilTest.php
1 year ago
TimeUnitTest.php
1 year ago
TimeUtilTest.php
1 year ago
TimeValueTest.php
1 year ago
UrlUtilTest.php
1 year ago
StringUtilTest.php
82 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WonderPush\Util; |
| 4 | |
| 5 | class StringUtilTest extends \WonderPush\TestCase { |
| 6 | |
| 7 | public function testBeginsWith() { |
| 8 | $this->assertTrue(StringUtil::beginsWith('', '')); |
| 9 | $this->assertTrue(StringUtil::beginsWith('foo', '')); |
| 10 | $this->assertFalse(StringUtil::beginsWith('foo', 'bar')); |
| 11 | $this->assertFalse(StringUtil::beginsWith('', 'bar')); |
| 12 | |
| 13 | $this->assertFalse(StringUtil::beginsWith('foo', 'o')); |
| 14 | $this->assertFalse(StringUtil::beginsWith('foo', 'oo')); |
| 15 | $this->assertTrue(StringUtil::beginsWith('foo', 'f')); |
| 16 | $this->assertTrue(StringUtil::beginsWith('foo', 'fo')); |
| 17 | $this->assertTrue(StringUtil::beginsWith('foo', 'foo')); |
| 18 | $this->assertFalse(StringUtil::beginsWith('foo', 'foob')); |
| 19 | $this->assertFalse(StringUtil::beginsWith('foo', 'fooba')); |
| 20 | $this->assertFalse(StringUtil::beginsWith('foo', 'foobar')); |
| 21 | } |
| 22 | |
| 23 | public function testEndsWith() { |
| 24 | $this->assertTrue(StringUtil::endsWith('', '')); |
| 25 | $this->assertTrue(StringUtil::endsWith('foo', '')); |
| 26 | $this->assertFalse(StringUtil::endsWith('foo', 'bar')); |
| 27 | $this->assertFalse(StringUtil::endsWith('', 'bar')); |
| 28 | |
| 29 | $this->assertFalse(StringUtil::endsWith('foo', 'f')); |
| 30 | $this->assertFalse(StringUtil::endsWith('foo', 'fo')); |
| 31 | $this->assertTrue(StringUtil::endsWith('foo', 'foo')); |
| 32 | $this->assertTrue(StringUtil::endsWith('foo', 'oo')); |
| 33 | $this->assertTrue(StringUtil::endsWith('foo', 'o')); |
| 34 | $this->assertFalse(StringUtil::endsWith('foo', 'foobar')); |
| 35 | $this->assertFalse(StringUtil::endsWith('foo', 'oobar')); |
| 36 | $this->assertFalse(StringUtil::endsWith('foo', 'obar')); |
| 37 | $this->assertFalse(StringUtil::endsWith('foo', 'bar')); |
| 38 | $this->assertFalse(StringUtil::endsWith('foo', 'ar')); |
| 39 | $this->assertFalse(StringUtil::endsWith('foo', 'r')); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @expectedException \PHPUnit_Framework_Error_Warning |
| 44 | */ |
| 45 | public function testContainsEmptyNeedle() { |
| 46 | StringUtil::contains('', ''); |
| 47 | } |
| 48 | |
| 49 | public function testContains() { |
| 50 | $this->assertFalse(StringUtil::contains('', 'foo')); |
| 51 | $this->assertTrue(StringUtil::contains('foo', 'f')); |
| 52 | $this->assertTrue(StringUtil::contains('foo', 'o')); |
| 53 | $this->assertTrue(StringUtil::contains('foo', 'oo')); |
| 54 | $this->assertTrue(StringUtil::contains('foo', 'foo')); |
| 55 | $this->assertFalse(StringUtil::contains('foo', 'foob')); |
| 56 | $this->assertFalse(StringUtil::contains('foo', 'fooba')); |
| 57 | $this->assertFalse(StringUtil::contains('foo', 'foobar')); |
| 58 | $this->assertFalse(StringUtil::contains('foo', 'rfoo')); |
| 59 | $this->assertFalse(StringUtil::contains('foo', 'arfoo')); |
| 60 | $this->assertFalse(StringUtil::contains('foo', 'barfoo')); |
| 61 | } |
| 62 | |
| 63 | public function testFormat() { |
| 64 | $this->assertEquals('foobarbaz', StringUtil::format('foo{0}baz', 'bar')); |
| 65 | $this->assertEquals('foobarbazqux', StringUtil::format('foo{0}baz{1}', 'bar', 'qux')); |
| 66 | $this->assertEquals('foobarbazqux', StringUtil::format('foo{0}baz{1}', array('bar', 'qux'))); |
| 67 | $this->assertEquals('foobarbazqux', StringUtil::format('foo{0}baz{1}', (object)array('bar', 'qux'))); |
| 68 | $this->assertEquals('foobarbazqux', StringUtil::format('foo{BAR}baz{QUX}', array('BAR' => 'bar', 'QUX' => 'qux'))); |
| 69 | $this->assertEquals('foobarbazqux', StringUtil::format('foo{BAR}baz{QUX}', (object)array('BAR' => 'bar', 'QUX' => 'qux'))); |
| 70 | |
| 71 | /** @noinspection PhpParamsInspection */ |
| 72 | $this->assertEquals('foobarbaz{1}', StringUtil::format('foo{0}baz{1}', 'bar')); |
| 73 | $this->assertEquals('foobarbazqux{qux}', StringUtil::format('foo{BAR}baz{QUX}{qux}', array('BAR' => 'bar', 'QUX' => 'qux'))); |
| 74 | $this->assertEquals('foobarbazqux{qux}', StringUtil::format('foo{BAR}baz{QUX}{qux}', (object)array('BAR' => 'bar', 'QUX' => 'qux'))); |
| 75 | |
| 76 | // Ignores any further arguments if an array was given first |
| 77 | $this->assertEquals('foobarbaz{QUX}', StringUtil::format('foo{BAR}baz{QUX}', array('BAR' => 'bar'), array('QUX' => 'qux'))); |
| 78 | $this->assertEquals('foobarbaz{QUX}', StringUtil::format('foo{BAR}baz{QUX}', (object)array('BAR' => 'bar'), (object)array('QUX' => 'qux'))); |
| 79 | } |
| 80 | |
| 81 | } |
| 82 |