UtilStringTest.php 4.17 KB
Newer Older
Scott Vivian committed
1
<?php
Scott committed
2
require_once QA_INCLUDE_DIR.'util/string.php';
Scott Vivian committed
3 4 5 6 7 8 9

class UtilStringTest extends PHPUnit_Framework_TestCase
{
	private $strBasic = 'So I tied an onion to my belt, which was the style at the time.';
	private $strAccents = 'Țĥé qũīçĶ ßřǭŴƞ Ƒöŧ ǰÙƢƥş ØƲĘŕ ƮĦȨ ĿÆƶȳ Ƌơǥ';
	private $blockWordString = 't*d o*n b*t style';

10
	public function test__qa_string_to_words()
Scott Vivian committed
11 12 13 14 15 16 17 18 19 20 21
	{
		$test1 = qa_string_to_words($this->strBasic);
		$expected1 = array('so', 'i', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time');

		$test2 = qa_string_to_words($this->strBasic, false);
		$expected2 = array('So', 'I', 'tied', 'an', 'onion', 'to', 'my', 'belt', 'which', 'was', 'the', 'style', 'at', 'the', 'time');

		$this->assertEquals($expected1, $test1);
		$this->assertEquals($expected2, $test2);
	}

22
	public function test__qa_string_remove_accents()
Scott Vivian committed
23 24 25 26 27 28 29
	{
		$test = qa_string_remove_accents($this->strAccents);
		$expected = 'The quicK ssroWn Fot jUOIps OVEr THE LAEzy Dog';

		$this->assertEquals($expected, $test);
	}

30
	public function test__qa_tags_to_tagstring()
Scott Vivian committed
31 32 33 34 35 36 37
	{
		$test = qa_tags_to_tagstring( array('Hello', 'World') );
		$expected = 'Hello,World';

		$this->assertEquals($expected, $test);
	}

38
	public function test__qa_tagstring_to_tags()
Scott Vivian committed
39 40 41 42 43 44 45
	{
		$test = qa_tagstring_to_tags('hello,world');
		$expected = array('hello', 'world');

		$this->assertEquals($expected, $test);
	}

46
	public function test__qa_shorten_string_line()
Scott Vivian committed
47 48 49 50 51 52 53 54 55 56
	{
		// qa_shorten_string_line ($string, $length)

		$test = qa_shorten_string_line($this->strBasic, 30);

		$this->assertStringStartsWith('So I tied', $test);
		$this->assertStringEndsWith('time.', $test);
		$this->assertNotFalse(strpos($test, '...'));
	}

57
	public function test__qa_block_words_explode()
Scott Vivian committed
58 59 60 61 62 63 64
	{
		$test = qa_block_words_explode($this->blockWordString);
		$expected = array('t*d', 'o*n', 'b*t', 'style');

		$this->assertEquals($expected, $test);
	}

65
	public function test__qa_block_words_to_preg()
Scott Vivian committed
66 67 68 69 70 71 72
	{
		$test = qa_block_words_to_preg($this->blockWordString);
		$expected = '(?<= )t[^ ]*d(?= )|(?<= )o[^ ]*n(?= )|(?<= )b[^ ]*t(?= )|(?<= )style(?= )';

		$this->assertEquals($expected, $test);
	}

73
	public function test__qa_block_words_match_all()
Scott Vivian committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	{
		$test1 = qa_block_words_match_all('onion belt', '');

		$wordpreg = qa_block_words_to_preg($this->blockWordString);
		$test2 = qa_block_words_match_all('tried an ocean boat', $wordpreg);
		// matches are returned as array of [offset] => [length]
		$expected = array(
			 0 => 5, // tried
			 9 => 5, // ocean
			15 => 4, // boat
		);

		$this->assertEmpty($test1);
		$this->assertEquals($expected, $test2);
	}

90
	public function test__qa_block_words_replace()
Scott Vivian committed
91 92 93 94 95 96 97 98
	{
		$wordpreg = qa_block_words_to_preg($this->blockWordString);
		$test = qa_block_words_replace('tired of my ocean boat style', $wordpreg);
		$expected = '***** of my ***** **** *****';

		$this->assertEquals($expected, $test);
	}

99
	public function test__qa_random_alphanum()
Scott Vivian committed
100 101 102 103 104 105 106
	{
		$len = 50;
		$test = qa_random_alphanum($len);

		$this->assertEquals(strlen($test), $len);
	}

107
	public function test__qa_email_validate()
Scott Vivian committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	{
		$goodEmails = array(
			'hello@example.com',
			'q.a@question2answer.org',
			'example@newdomain.app'
		);
		$badEmails = array(
			'nobody@nowhere',
			'pokémon@example.com',
			'email @ with spaces',
			'some random string',
		);

		foreach ($goodEmails as $email) {
			$this->assertTrue( qa_email_validate($email) );
		}
		foreach ($badEmails as $email)
			$this->assertFalse( qa_email_validate($email) );
	}

128
	public function test__qa_strlen()
Scott Vivian committed
129 130 131 132 133 134
	{
		$test = qa_strlen($this->strAccents);

		$this->assertEquals($test, 43);
	}

135
	public function test__qa_strtolower()
Scott Vivian committed
136 137 138 139 140 141
	{
		$test = qa_strtolower('hElLo WoRld');

		$this->assertEquals($test, 'hello world');
	}

142
	public function test__qa_substr()
Scott Vivian committed
143 144 145 146 147 148
	{
		$test = qa_substr($this->strBasic, 5, 24);

		$this->assertEquals($test, 'tied an onion to my belt');
	}

149
	public function test__qa_string_matches_one()
Scott Vivian committed
150 151 152 153 154 155 156 157
	{
		$matches = array( 'dyed', 'shallot', 'belt', 'fashion' );
		$nonMatches = array( 'dyed', 'shallot', 'buckle', 'fashion' );

		$this->assertTrue( qa_string_matches_one($this->strBasic, $matches) );
		$this->assertFalse( qa_string_matches_one($this->strBasic, $nonMatches) );
	}
}