PHP Classes

File: Tests/GDM/Framework/Types/StringTest.php

Recommend this page to a friend!
  Classes of Corey Sewell   GDM Types   Tests/GDM/Framework/Types/StringTest.php   Download  
File: Tests/GDM/Framework/Types/StringTest.php
Role: Unit test script
Content type: text/plain
Description: Auxiliary script
Class: GDM Types
Manipulate scalar data types as objects
Author: By
Last change: Update of Tests/GDM/Framework/Types/StringTest.php
Date: 4 months ago
Size: 20,548 bytes
 

Contents

Class file image Download
<?php namespace GDM\Framework\Types; /** * Generated by PHPUnit_SkeletonGenerator on 2014-07-03 at 22:23:09. */ class StringTest extends \PHPUnit_Framework_TestCase { /** * @var String */ protected $object; /** * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ protected function setUp() { $this->object = new String; } /** * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ protected function tearDown() { } /** * @covers GDM\Framework\Types\String::clean */ public function testClean() { $string = 'a!b@c#d$e/f%g^h&i*j(k)l-m_n+o=p[q{r]s}t;u:v"w\'x,y>z'; $aToZRange = range("a", "z"); $expected1 = implode('', $aToZRange); $stringObg1 = \GDM\Framework\Types\String::create($string)->clean(''); $this->assertEquals($expected1, (string) $stringObg1, "Clean with empty seperator failed"); $seperator = "_"; $expected2 = implode($seperator, $aToZRange); $stringObg2 = \GDM\Framework\Types\String::create($string)->clean($seperator); $this->assertEquals($expected2, (string) $stringObg2, "Clean with seperator " + $seperator + " failed"); $expected3 = 'abc'; $stringObg3 = \GDM\Framework\Types\String::create($string)->clean('', 3); $this->assertEquals($expected3, (string) $stringObg3, "Clean with empty seperator and length failed"); $expected4 = 'a_b'; $stringObg4 = \GDM\Framework\Types\String::create($string)->clean('_', 3); $this->assertEquals($expected4, (string) $stringObg4, "Clean with seperator and length failed"); } /** * @covers GDM\Framework\Types\String::compareTo */ public function testCompareTo() { $string = "string"; $stringObg = \GDM\Framework\Types\String::create($string); $this->assertTrue($stringObg->compareTo($string)); $this->assertTrue($stringObg->compareTo(strtoupper($string), false)); $this->assertFalse($stringObg->compareTo('foo')); $this->assertFalse($stringObg->compareTo(strtoupper('FOO'), false)); } /** * @covers GDM\Framework\Types\String::contains */ public function testContains() { $string = "string"; $stringObg = \GDM\Framework\Types\String::create($string); $this->assertTrue($stringObg->contains($string)); $this->assertTrue($stringObg->contains("tri")); $this->assertTrue($stringObg->contains(strtoupper($string), false)); $this->assertTrue($stringObg->contains(strtoupper("TRI"), false)); $this->assertFalse($stringObg->contains("foo")); $this->assertFalse($stringObg->contains(strtoupper("foo"), false)); } /** * @covers GDM\Framework\Types\String::toLowerCase */ public function testToLowerCase() { $string = "STRING"; $stringObg = \GDM\Framework\Types\String::create($string); $this->assertEquals(strtolower($string), (string) $stringObg->toLowerCase()); $stringObg1 = \GDM\Framework\Types\String::create($string); $this->assertNotEquals($string, (string) $stringObg1->toLowerCase()); } /** * @covers GDM\Framework\Types\String::isInt */ public function testIsInt() { $this->assertTrue(\GDM\Framework\Types\String::create("1")->isInt()); $this->assertTrue(\GDM\Framework\Types\String::create(1235)->isInt()); $this->assertFalse(\GDM\Framework\Types\String::create("string")->isInt()); $this->assertFalse(\GDM\Framework\Types\String::create(1.2345)->isInt()); } /** * @covers GDM\Framework\Types\String::stripLineBreak */ public function testStripLineBreak() { $string = "Foo".PHP_EOL."Bar\r\nFoo\nBar"; $expected = 'FooBarFooBar'; $stringObg = \GDM\Framework\Types\String::create($string)->stripLineBreak(''); $this->assertEquals($expected, (string) $stringObg); $this->assertNotEquals($string, (string) $stringObg); $expected1 = 'Foo_Bar_Foo_Bar'; $stringObg1 = \GDM\Framework\Types\String::create($string)->stripLineBreak('_'); $this->assertEquals($expected1, (string) $stringObg1); $this->assertNotEquals($string, (string) $stringObg1); } /** * @covers GDM\Framework\Types\String::trim */ public function testTrim() { $string = ' Foo '; $expected = trim($string); $stringObg = \GDM\Framework\Types\String::create($string)->trim(); $this->assertEquals("s".$expected."e", "s".$stringObg."e"); $this->assertNotEquals("s".$string."e", "s".$stringObg."e"); $string1 = '____Foo____'; $expected1 = trim($string1, '_'); $stringObg1 = \GDM\Framework\Types\String::create($string1)->trim('_'); $this->assertEquals("s".$expected1."e", "s".$stringObg1."e"); $this->assertNotEquals("s".$string1."e", "s".$stringObg1."e"); } /** * @covers GDM\Framework\Types\String::leftTrim */ public function testLeftTrim() { $string = ' Foo '; $expected = ltrim($string); $stringObg = \GDM\Framework\Types\String::create($string)->leftTrim(); $this->assertEquals("s".$expected."e", "s".$stringObg."e"); $this->assertNotEquals("s".$string."e", "s".$stringObg."e"); $string1 = '____Foo____'; $expected1 = ltrim($string1, '_'); $stringObg1 = \GDM\Framework\Types\String::create($string1)->leftTrim('_'); $this->assertEquals("s".$expected1."e", "s".$stringObg1."e"); $this->assertNotEquals("s".$string1."e", "s".$stringObg1."e"); } /** * @covers GDM\Framework\Types\String::rightTrim */ public function testRightTrim() { $string = ' Foo '; $expected = rtrim($string); $stringObg = \GDM\Framework\Types\String::create($string)->rightTrim(); $this->assertEquals("s".$expected."e", "s".$stringObg."e"); $this->assertNotEquals("s".$string."e", "s".$stringObg."e"); $string1 = '____Foo____'; $expected1 = rtrim($string1, '_'); $stringObg1 = \GDM\Framework\Types\String::create($string1)->rightTrim('_'); $this->assertEquals("s".$expected1."e", "s".$stringObg1."e"); $this->assertNotEquals("s".$string1."e", "s".$stringObg1."e"); } /** * @covers GDM\Framework\Types\String::splitByCaps */ public function testSplitByCaps() { $string = 'FooBarFOOBar'; $expected = 'Foo Bar FOO Bar'; $stringObg = \GDM\Framework\Types\String::create($string)->splitByCaps(); $this->assertEquals($expected, $stringObg); $this->assertNotEquals($string, $stringObg); $string1 = 'FooBarFOOBar'; $expected1 = 'Foo_Bar_FOO_Bar'; $stringObg1 = \GDM\Framework\Types\String::create($string1)->splitByCaps('_'); $this->assertEquals($expected1, $stringObg1); $this->assertNotEquals($string1, $stringObg1); } /** * @covers GDM\Framework\Types\String::upperCaseWords */ public function testUpperCaseWords() { $string = 'foo bar foo bar'; $expected = 'Foo Bar Foo Bar'; $stringObg = \GDM\Framework\Types\String::create($string)->upperCaseWords(); $this->assertEquals($expected, $stringObg); $this->assertNotEquals($string, $stringObg); } /** * @covers GDM\Framework\Types\String::startsWith */ public function testStartsWith() { $string = 'foo bar foo bar'; $stringObg = \GDM\Framework\Types\String::create($string); $this->assertTrue($stringObg->startsWith("foo")); $this->assertFalse($stringObg->startsWith("bar")); } /** * @covers GDM\Framework\Types\String::endsWith */ public function testEndsWith() { $stringObg = \GDM\Framework\Types\String::create('foo bar foo bar'); $this->assertTrue($stringObg->endsWith("bar")); $this->assertFalse($stringObg->endsWith("foo")); } /** * @covers GDM\Framework\Types\String::isEmail */ public function testIsEmail() { $this->assertTrue(\GDM\Framework\Types\String::create('foo@gmail.com')->isEmail(false)); $this->assertTrue(\GDM\Framework\Types\String::create('foo@gmail.com')->isEmail(true)); $this->assertFalse(\GDM\Framework\Types\String::create('foobar')->isEmail(false)); $this->assertFalse(\GDM\Framework\Types\String::create('foo@somenonexistantdomain_atkvdy843.com')->isEmail(true)); } /** * @covers GDM\Framework\Types\String::neatTruncate * @todo Implement testNeatTruncate(). */ public function testNeatTruncate() { $string = 'foo bar foo bar'; $expected = 'foo bar...'; $stringObg = \GDM\Framework\Types\String::create($string)->neatTruncate(10); $this->assertEquals($expected, (string) $stringObg); $this->assertNotEquals($string, (string) $stringObg); $string1 = 'foo bar foo bar'; $expected1 = 'foo bar foo'; $stringObg1 = \GDM\Framework\Types\String::create($string1)->neatTruncate(10, ''); $this->assertEquals($expected1, (string) $stringObg1); $this->assertNotEquals($string, (string) $stringObg1); } /** * @covers GDM\Framework\Types\String::truncate * @todo Implement testTruncate(). */ public function testTruncate() { $string = 'foo bar foo bar'; $expected = 'fo...'; $stringObg = \GDM\Framework\Types\String::create($string)->truncate(5); $this->assertEquals($expected, (string) $stringObg); $this->assertNotEquals($string, (string) $stringObg); } /** * @covers GDM\Framework\Types\String::length */ public function testLength() { $string = '12345'; $expected = 5; $stringObg = \GDM\Framework\Types\String::create($string); $this->assertEquals($expected, (string) $stringObg->length()); } /** * @covers GDM\Framework\Types\String::subString */ public function testSubString() { $string = 'abcdef'; $this->assertEquals("f", (string) (new String($string))->subString(-1)); // returns "f" $this->assertEquals("ef", (string) (new String($string))->subString(-2)); // returns "ef" $this->assertEquals("d", (string) (new String($string))->subString(-3, 1)); // returns "d" $this->assertEquals("abcde", (string) (new String($string))->subString(0, -1)); // returns "abcde" $this->assertEquals("cde", (string) (new String($string))->subString(2, -1)); // returns "cde" $this->assertFalse((new String($string))->subString(4, -4)->get()); // returns false $this->assertEquals("de", (string) (new String($string))->subString(-3, -1)); // returns "de" } /** * @covers GDM\Framework\Types\String::indexOf * @todo Implement testIndexOf(). */ public function testIndexOf() { $string = 'foobar'; $this->assertEquals(3, (new String($string))->indexOf("b")); $this->assertEquals(1, (new String($string))->indexOf("o")); $this->assertEquals(false, (new String($string))->indexOf("t")); $this->assertEquals(4, (new String($string))->indexOf("a", 2)); } /** * @covers GDM\Framework\Types\String::escapeDblQuotes * @todo Implement testEscapeDblQuotes(). */ public function testEscapeDblQuotes() { $string = 'foo"bar'; $this->assertEquals('foo\\"bar', (string) (new String($string))->escapeDblQuotes()); } /** * @covers GDM\Framework\Types\String::stripNonNums * @todo Implement testStripNonNums(). */ public function testStripNonNums() { $string = '1foo2bar3'; $this->assertEquals('123', (string) (new String($string))->stripNonNums()); } /** * @covers GDM\Framework\Types\String::replace */ public function testReplace() { $string = 'foo bar foo'; $this->assertEquals('bar', (string) (new String($string))->replace('/foo.*/', 'bar')); } /** * @covers GDM\Framework\Types\String::simpleReplace */ public function testSimpleReplace() { $string = 'foo bar foo'; $this->assertEquals('bar bar bar', (string) (new String($string))->simpleReplace('foo', 'bar')); } /** * @covers GDM\Framework\Types\String::stripSpecialChars */ public function testStripSpecialChars() { $string = 'a!b@c#d$e/f%g^h&i*j(k)l-m_n+o=p[q{r]s}t;u:v"w\'x,y>z'; $aToZRange = range("a", "z"); $expected1 = implode('', $aToZRange); $stringObg1 = \GDM\Framework\Types\String::create($string)->stripSpecialChars(''); $this->assertEquals($expected1, (string) $stringObg1, "Clean with empty seperator failed"); $seperator = "_"; $expected2 = implode($seperator, $aToZRange); $stringObg2 = \GDM\Framework\Types\String::create($string)->stripSpecialChars($seperator); $this->assertEquals($expected2, (string) $stringObg2, "Clean with seperator " + $seperator + " failed"); } /** * @covers GDM\Framework\Types\String::split * @todo Implement testSplit(). */ public function testSplit() { $string = 'foo bar foo'; $this->assertEquals(explode(' ', $string), (new String($string))->split(' ')); $this->assertEquals(explode(' ', $string, 2), (new String($string))->split(' ', 2)); } /** * @covers GDM\Framework\Types\String::toUpperCase */ public function testToUpperCase() { $string = "string"; $stringObg = \GDM\Framework\Types\String::create($string); $this->assertEquals(strtoupper($string), (string) $stringObg->toUpperCase()); $stringObg1 = \GDM\Framework\Types\String::create($string); $this->assertNotEquals($string, (string) $stringObg1->toUpperCase()); } /** * @covers GDM\Framework\Types\String::toBool */ public function testToBool() { foreach (["off", "false", "no", "0", "zero"] as $falseString) { $this->assertFalse((new String($falseString))->toBool()); } $this->assertTrue((new String('yes'))->toBool()); $this->assertTrue((new String('yup'))->toBool()); $this->assertTrue((new String('chur'))->toBool()); $this->assertTrue((new String('ok'))->toBool()); } /** * @covers GDM\Framework\Types\String::reverse */ public function testReverse() { $this->assertEquals('foobar', (new String('raboof'))->reverse()); } /** * @covers GDM\Framework\Types\String::md5 * @todo Implement testMd5(). */ public function testMd5() { $string = "foobar"; $this->assertEquals(md5($string), (new String($string))->md5()); } /** * @covers GDM\Framework\Types\String::toComment * @todo Implement testToComment(). */ public function testToComment() { $string = "foobar"; $this->assertEquals("<!-- ".$string." -->", (string) (new String($string))->toComment('text/html')); $this->assertEquals("/** ".$string." **/", (string) (new String($string))->toComment('text/javascript')); $this->assertEquals("/** ".$string." **/", (string) (new String($string))->toComment('text/css')); $this->assertEquals("/** ".$string." **/", (string) (new String($string))->toComment('application/x-javascript')); } /** * @covers GDM\Framework\Types\String::createUUID */ public function testCreateUUID() { $this->assertTrue(preg_match('/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/', (string) String::createUUID()) !== false); } /** * @covers GDM\Framework\Types\String::createRandomAlphaNumeric */ public function testCreateRandomAlphaNumeric() { $this->assertTrue(strlen((string) String::createRandomAlphaNumeric()) === 8); $this->assertTrue(strlen((string) String::createRandomAlphaNumeric(3)) === 3); $this->assertTrue(strlen((string) String::createRandomAlphaNumeric(20)) === 20); } /** * @covers GDM\Framework\Types\String::fromObject * @covers GDM\Framework\Types\Type::fromObject */ public function testFromObject() { $stringableClass = new stringableClass(); $this->assertEquals((string) $stringableClass, (string) String::create()->fromObject($stringableClass)); $this->assertEquals('GDM\Framework\Types\nonStringableClass', (string) String::create()->fromObject(new nonStringableClass())); } /** * @covers GDM\Framework\Types\String::fromBool * @covers GDM\Framework\Types\Type::fromBool */ public function testFromBool() { $this->assertEquals('true', (string) String::create()->fromBool(true)); $this->assertEquals('false', (string) String::create()->fromBool(false)); } /** * @covers GDM\Framework\Types\String::fromInt * @covers GDM\Framework\Types\Type::fromInt */ public function testFromInt() { $this->assertEquals('1234', (string) String::create()->fromInt(1234)); } /** * @covers GDM\Framework\Types\String::fromDouble * @covers GDM\Framework\Types\Type::fromDouble */ public function testFromDouble() { $this->assertEquals('1.23', (string) String::create()->fromDouble(1.23)); } /** * @covers GDM\Framework\Types\String::fromString * @covers GDM\Framework\Types\Type::fromString */ public function testFromString() { $this->assertEquals("foo bar", (string) String::create()->fromString("foo bar")); } /** * @covers GDM\Framework\Types\String::fromResource * @covers GDM\Framework\Types\Type::fromResource */ public function testFromResource() { $fh = tmpfile(); fwrite($fh, "foo bar"); fseek($fh, 0); $this->assertEquals("foo bar", (string) String::create()->fromResource($fh)); fclose($fh); } /** * @covers GDM\Framework\Types\String::get */ public function testGet() { $this->assertEquals("foo bar", (string) String::create()->fromString("foo bar")->get()); } /** * @covers GDM\Framework\Types\String::clear */ public function testClear() { $string = "foo bar"; $strObg = String::create()->fromString($string); $this->assertEquals($string, (string) $strObg); $method = new \ReflectionMethod($strObg, 'clear'); $method->setAccessible(true); $this->assertEmpty((string) $method->invoke($strObg)); } /** * @covers GDM\Framework\Types\String::create * @covers GDM\Framework\Types\Type::create * @covers GDM\Framework\Types\Type::__construct */ public function testCreate() { $this->assertEquals("foo bar", (string) String::create("foo bar")); } /** * @covers GDM\Framework\Types\Type::set */ public function testSet() { $this->assertEquals("true", (string) String::create()->set(true)); $this->assertEquals("1234", (string) String::create()->set(1234)); $this->assertEquals("1.23", (string) String::create()->set(1.23)); $this->assertEquals("foo bar", (string) String::create()->set("foo bar")); $fh = tmpfile(); fwrite($fh, "foo bar"); fseek($fh, 0); $this->assertEquals("foo bar", (string) String::create()->set($fh)); fclose($fh); $this->setExpectedException('InvalidArgumentException'); String::create()->set(new stringableClass()); $this->setExpectedException('UnexpectedValueException'); String::create()->set([]); } } if (!class_exists('\GDM\Framework\Types\stringableClass')) { class stringableClass { public function __toString() { return "http://www.example.com/foo/bar"; } } } if (!class_exists('\GDM\Framework\Types\nonStringableClass')) { class nonStringableClass { } }