From 0691cf14180d75ac755fac90f9932b3583ab95c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20G=C3=B3mez=20Vilches?= Date: Tue, 21 Mar 2017 23:27:28 +0100 Subject: [PATCH] Fix GSM 03.38 charset (#5) Fix to avoid removing valid characters Add more tests Fix travis --- .travis.yml | 4 +- README.md | 25 +++- SMSCounter.php | 316 ++++++++++++++++++++++----------------- Tests/SMSCounterTest.php | 136 +++++++++-------- composer.json | 5 +- 5 files changed, 272 insertions(+), 214 deletions(-) diff --git a/.travis.yml b/.travis.yml index be2f131..87c76fb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,6 @@ php: - hhvm before_script: - - composer install --dev + - composer install -script: phpunit +script: vendor/bin/phpunit diff --git a/README.md b/README.md index c50833c..d1d25c3 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,12 @@ Character counter for SMS Messages [![Build Status](https://img.shields.io/travis/instasent/sms-counter-php.svg?style=flat-square)](https://travis-ci.org/instasent/sms-counter-php) [![SensioLabsInsight](https://img.shields.io/sensiolabs/i/0a2fa87a-0287-46f6-b8b5-818b44a2b9f9.svg?style=flat-square)](https://insight.sensiolabs.com/projects/0a2fa87a-0287-46f6-b8b5-818b44a2b9f9) -##Usage +## Usage ```php use Instasent\SMSCounter\SMSCounter; -$smsCounter = new SMSCounter; +$smsCounter = new SMSCounter(); $smsCounter->count('some-string-to-be-counted'); ``` @@ -26,23 +26,32 @@ stdClass Object ) ``` -##Installation +You can sanitize your text to be a valid GSM 03.38 charset + +```php +use Instasent\SMSCounter\SMSCounter; + +$smsCounter = new SMSCounter(); +$smsCounter->sanitizeToGSM('dadáó'); //return dadao +``` + +## Installation `sms-counter-php` is available via [composer](http://getcomposer.org) on [packagist](https://packagist.org/packages/instasent/sms-counter-php). ```json { -"require": { -"instasent/sms-counter-php": "dev-master" -} + "require": { + "instasent/sms-counter-php": "^0.3" + } } ``` -##License +## License SMS Counter (PHP) is released under the [MIT License](LICENSE-MIT.md) -###Mentions +### Mentions * Original idea : [danxexe/sms-counter](https://github.com/danxexe/sms-counter) * Fork Idea from: [acpmasquerade/sms-counter-php](https://github.com/acpmasquerade/sms-counter-php) diff --git a/SMSCounter.php b/SMSCounter.php index db9c4d4..d77f4be 100644 --- a/SMSCounter.php +++ b/SMSCounter.php @@ -114,19 +114,19 @@ public function count($text) { $unicodeArray = $this->utf8ToUnicode($text); - # variable to catch if any ex chars while encoding detection. - $exChars = array(); + // variable to catch if any ex chars while encoding detection. + $exChars = []; $encoding = $this->detectEncoding($unicodeArray, $exChars); $length = count($unicodeArray); if ($encoding === self::GSM_7BIT_EX) { $lengthExchars = count($exChars); - # Each exchar in the GSM 7 Bit encoding takes one more space - # Hence the length increases by one char for each of those Ex chars. + // Each exchar in the GSM 7 Bit encoding takes one more space + // Hence the length increases by one char for each of those Ex chars. $length += $lengthExchars; } - # Select the per message length according to encoding and the message length + // Select the per message length according to encoding and the message length switch ($encoding) { case self::GSM_7BIT: $perMessage = self::GSM_7BIT_LEN; @@ -198,13 +198,13 @@ public function detectEncoding($text, &$exChars) */ public function utf8ToUnicode($str) { - $unicode = array(); - $values = array(); + $unicode = []; + $values = []; $lookingFor = 1; $len = strlen($str); for ($i = 0; $i < $len; $i++) { - $thisValue = ord($str[ $i ]); + $thisValue = ord($str[$i]); if ($thisValue < 128) { $unicode[] = $thisValue; @@ -223,7 +223,7 @@ public function utf8ToUnicode($str) (($values[0] % 32) * 64) + ($values[1] % 64); $unicode[] = $number; - $values = array(); + $values = []; $lookingFor = 1; } } @@ -287,7 +287,7 @@ public function removeNonGsmChars($str) /** * Replaces non GSM characters from a string. * - * @param string $str String to be replaced + * @param string $str String to be replaced * @param string $replacement String of characters to be replaced with * * @return (string|false) if replacement string is more than 1 character @@ -302,7 +302,7 @@ public function replaceNonGsmChars($str, $replacement = null) return false; } - $replacementArray = array(); + $replacementArray = []; $unicodeArray = $this->utf8ToUnicode($replacement); $replacementUnicode = array_pop($unicodeArray); @@ -346,136 +346,172 @@ public function removeAccents($str) return $str; } - $chars = array( + $chars = [ // Decompositions for Latin-1 Supplement - chr(195).chr(128) => 'A', - chr(195).chr(129) => 'A', - chr(195).chr(130) => 'A', - chr(195).chr(131) => 'A', - chr(195).chr(132) => 'A', - chr(195).chr(133) => 'A', - // chr(195).chr(135) => 'C', // Ç - chr(195).chr(136) => 'E', - chr(195).chr(137) => 'E', - chr(195).chr(138) => 'E', - chr(195).chr(139) => 'E', - chr(195).chr(140) => 'I', - chr(195).chr(141) => 'I', - chr(195).chr(142) => 'I', - chr(195).chr(143) => 'I', - // chr(195).chr(145) => 'N', // Ñ - chr(195).chr(146) => 'O', - chr(195).chr(147) => 'O', - chr(195).chr(148) => 'O', - chr(195).chr(149) => 'O', - chr(195).chr(150) => 'O', - chr(195).chr(153) => 'U', - chr(195).chr(154) => 'U', - chr(195).chr(155) => 'U', - chr(195).chr(156) => 'U', - chr(195).chr(157) => 'Y', - chr(195).chr(159) => 's', - // chr(195).chr(160) => 'a', - chr(195).chr(161) => 'a', - chr(195).chr(162) => 'a', - chr(195).chr(163) => 'a', - chr(195).chr(164) => 'a', - chr(195).chr(165) => 'a', - // chr(195).chr(167) => 'c', // ç - chr(195).chr(168) => 'e', - chr(195).chr(169) => 'e', - chr(195).chr(170) => 'e', - chr(195).chr(171) => 'e', - chr(195).chr(172) => 'i', - chr(195).chr(173) => 'i', - chr(195).chr(174) => 'i', - chr(195).chr(175) => 'i', - // chr(195).chr(177) => 'n', // ñ - chr(195).chr(178) => 'o', - chr(195).chr(179) => 'o', - chr(195).chr(180) => 'o', - chr(195).chr(181) => 'o', - chr(195).chr(182) => 'o', - chr(195).chr(182) => 'o', - chr(195).chr(185) => 'u', - chr(195).chr(186) => 'u', - chr(195).chr(187) => 'u', - chr(195).chr(188) => 'u', - chr(195).chr(189) => 'y', - chr(195).chr(191) => 'y', + 'ª' => 'a', 'º' => 'o', + 'À' => 'A', 'Á' => 'A', + 'Â' => 'A', 'Ã' => 'A', + 'È' => 'E', + 'Ê' => 'E', 'Ë' => 'E', + 'Ì' => 'I', 'Í' => 'I', + 'Î' => 'I', 'Ï' => 'I', + 'Ð' => 'D', + 'Ò' => 'O', 'Ó' => 'O', + 'Ô' => 'O', 'Õ' => 'O', + 'Ù' => 'U', + 'Ú' => 'U', 'Û' => 'U', + 'Ý' => 'Y', + 'Þ' => 'TH', + 'á' => 'a', + 'â' => 'a', 'ã' => 'a', + 'ç' => 'c', + 'ê' => 'e', 'ë' => 'e', + 'í' => 'i', + 'î' => 'i', 'ï' => 'i', + 'ð' => 'd', + 'ó' => 'o', + 'ô' => 'o', 'õ' => 'o', + 'ú' => 'u', + 'û' => 'u', + 'ý' => 'y', 'þ' => 'th', + 'ÿ' => 'y', // Decompositions for Latin Extended-A - chr(196).chr(128) => 'A', chr(196).chr(129) => 'a', - chr(196).chr(130) => 'A', chr(196).chr(131) => 'a', - chr(196).chr(132) => 'A', chr(196).chr(133) => 'a', - chr(196).chr(134) => 'C', chr(196).chr(135) => 'c', - chr(196).chr(136) => 'C', chr(196).chr(137) => 'c', - chr(196).chr(138) => 'C', chr(196).chr(139) => 'c', - chr(196).chr(140) => 'C', chr(196).chr(141) => 'c', - chr(196).chr(142) => 'D', chr(196).chr(143) => 'd', - chr(196).chr(144) => 'D', chr(196).chr(145) => 'd', - chr(196).chr(146) => 'E', chr(196).chr(147) => 'e', - chr(196).chr(148) => 'E', chr(196).chr(149) => 'e', - chr(196).chr(150) => 'E', chr(196).chr(151) => 'e', - chr(196).chr(152) => 'E', chr(196).chr(153) => 'e', - chr(196).chr(154) => 'E', chr(196).chr(155) => 'e', - chr(196).chr(156) => 'G', chr(196).chr(157) => 'g', - chr(196).chr(158) => 'G', chr(196).chr(159) => 'g', - chr(196).chr(160) => 'G', chr(196).chr(161) => 'g', - chr(196).chr(162) => 'G', chr(196).chr(163) => 'g', - chr(196).chr(164) => 'H', chr(196).chr(165) => 'h', - chr(196).chr(166) => 'H', chr(196).chr(167) => 'h', - chr(196).chr(168) => 'I', chr(196).chr(169) => 'i', - chr(196).chr(170) => 'I', chr(196).chr(171) => 'i', - chr(196).chr(172) => 'I', chr(196).chr(173) => 'i', - chr(196).chr(174) => 'I', chr(196).chr(175) => 'i', - chr(196).chr(176) => 'I', chr(196).chr(177) => 'i', - chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij', - chr(196).chr(180) => 'J', chr(196).chr(181) => 'j', - chr(196).chr(182) => 'K', chr(196).chr(183) => 'k', - chr(196).chr(184) => 'k', chr(196).chr(185) => 'L', - chr(196).chr(186) => 'l', chr(196).chr(187) => 'L', - chr(196).chr(188) => 'l', chr(196).chr(189) => 'L', - chr(196).chr(190) => 'l', chr(196).chr(191) => 'L', - chr(197).chr(128) => 'l', chr(197).chr(129) => 'L', - chr(197).chr(130) => 'l', - chr(197).chr(131) => 'N', // Ń - chr(197).chr(132) => 'n', // ń - chr(197).chr(133) => 'N', // Ņ - chr(197).chr(134) => 'n', // ņ - chr(197).chr(135) => 'N', - chr(197).chr(136) => 'n', - chr(197).chr(137) => 'N', - chr(197).chr(138) => 'n', - chr(197).chr(139) => 'N', - chr(197).chr(140) => 'O', - chr(197).chr(141) => 'o', - chr(197).chr(142) => 'O', - chr(197).chr(143) => 'o', - chr(197).chr(144) => 'O', chr(197).chr(145) => 'o', - chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe', - chr(197).chr(148) => 'R',chr(197).chr(149) => 'r', - chr(197).chr(150) => 'R',chr(197).chr(151) => 'r', - chr(197).chr(152) => 'R',chr(197).chr(153) => 'r', - chr(197).chr(154) => 'S',chr(197).chr(155) => 's', - chr(197).chr(156) => 'S',chr(197).chr(157) => 's', - chr(197).chr(158) => 'S',chr(197).chr(159) => 's', - chr(197).chr(160) => 'S', chr(197).chr(161) => 's', - chr(197).chr(162) => 'T', chr(197).chr(163) => 't', - chr(197).chr(164) => 'T', chr(197).chr(165) => 't', - chr(197).chr(166) => 'T', chr(197).chr(167) => 't', - chr(197).chr(168) => 'U', chr(197).chr(169) => 'u', - chr(197).chr(170) => 'U', chr(197).chr(171) => 'u', - chr(197).chr(172) => 'U', chr(197).chr(173) => 'u', - chr(197).chr(174) => 'U', chr(197).chr(175) => 'u', - chr(197).chr(176) => 'U', chr(197).chr(177) => 'u', - chr(197).chr(178) => 'U', chr(197).chr(179) => 'u', - chr(197).chr(180) => 'W', chr(197).chr(181) => 'w', - chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y', - chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z', - chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z', - chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z', - chr(197).chr(190) => 'z', chr(197).chr(191) => 's' - ); + 'Ā' => 'A', 'ā' => 'a', + 'Ă' => 'A', 'ă' => 'a', + 'Ą' => 'A', 'ą' => 'a', + 'Ć' => 'C', 'ć' => 'c', + 'Ĉ' => 'C', 'ĉ' => 'c', + 'Ċ' => 'C', 'ċ' => 'c', + 'Č' => 'C', 'č' => 'c', + 'Ď' => 'D', 'ď' => 'd', + 'Đ' => 'D', 'đ' => 'd', + 'Ē' => 'E', 'ē' => 'e', + 'Ĕ' => 'E', 'ĕ' => 'e', + 'Ė' => 'E', 'ė' => 'e', + 'Ę' => 'E', 'ę' => 'e', + 'Ě' => 'E', 'ě' => 'e', + 'Ĝ' => 'G', 'ĝ' => 'g', + 'Ğ' => 'G', 'ğ' => 'g', + 'Ġ' => 'G', 'ġ' => 'g', + 'Ģ' => 'G', 'ģ' => 'g', + 'Ĥ' => 'H', 'ĥ' => 'h', + 'Ħ' => 'H', 'ħ' => 'h', + 'Ĩ' => 'I', 'ĩ' => 'i', + 'Ī' => 'I', 'ī' => 'i', + 'Ĭ' => 'I', 'ĭ' => 'i', + 'Į' => 'I', 'į' => 'i', + 'İ' => 'I', 'ı' => 'i', + 'IJ' => 'IJ', 'ij' => 'ij', + 'Ĵ' => 'J', 'ĵ' => 'j', + 'Ķ' => 'K', 'ķ' => 'k', + 'ĸ' => 'k', 'Ĺ' => 'L', + 'ĺ' => 'l', 'Ļ' => 'L', + 'ļ' => 'l', 'Ľ' => 'L', + 'ľ' => 'l', 'Ŀ' => 'L', + 'ŀ' => 'l', 'Ł' => 'L', + 'ł' => 'l', 'Ń' => 'N', + 'ń' => 'n', 'Ņ' => 'N', + 'ņ' => 'n', 'Ň' => 'N', + 'ň' => 'n', 'ʼn' => 'n', + 'Ŋ' => 'N', 'ŋ' => 'n', + 'Ō' => 'O', 'ō' => 'o', + 'Ŏ' => 'O', 'ŏ' => 'o', + 'Ő' => 'O', 'ő' => 'o', + 'Œ' => 'OE', 'œ' => 'oe', + 'Ŕ' => 'R', 'ŕ' => 'r', + 'Ŗ' => 'R', 'ŗ' => 'r', + 'Ř' => 'R', 'ř' => 'r', + 'Ś' => 'S', 'ś' => 's', + 'Ŝ' => 'S', 'ŝ' => 's', + 'Ş' => 'S', 'ş' => 's', + 'Š' => 'S', 'š' => 's', + 'Ţ' => 'T', 'ţ' => 't', + 'Ť' => 'T', 'ť' => 't', + 'Ŧ' => 'T', 'ŧ' => 't', + 'Ũ' => 'U', 'ũ' => 'u', + 'Ū' => 'U', 'ū' => 'u', + 'Ŭ' => 'U', 'ŭ' => 'u', + 'Ů' => 'U', 'ů' => 'u', + 'Ű' => 'U', 'ű' => 'u', + 'Ų' => 'U', 'ų' => 'u', + 'Ŵ' => 'W', 'ŵ' => 'w', + 'Ŷ' => 'Y', 'ŷ' => 'y', + 'Ÿ' => 'Y', 'Ź' => 'Z', + 'ź' => 'z', 'Ż' => 'Z', + 'ż' => 'z', 'Ž' => 'Z', + 'ž' => 'z', 'ſ' => 's', + // Decompositions for Latin Extended-B + 'Ș' => 'S', 'ș' => 's', + 'Ț' => 'T', 'ț' => 't', + // Vowels with diacritic (Vietnamese) + // unmarked + 'Ơ' => 'O', 'ơ' => 'o', + 'Ư' => 'U', 'ư' => 'u', + // grave accent + 'Ầ' => 'A', 'ầ' => 'a', + 'Ằ' => 'A', 'ằ' => 'a', + 'Ề' => 'E', 'ề' => 'e', + 'Ồ' => 'O', 'ồ' => 'o', + 'Ờ' => 'O', 'ờ' => 'o', + 'Ừ' => 'U', 'ừ' => 'u', + 'Ỳ' => 'Y', 'ỳ' => 'y', + // hook + 'Ả' => 'A', 'ả' => 'a', + 'Ẩ' => 'A', 'ẩ' => 'a', + 'Ẳ' => 'A', 'ẳ' => 'a', + 'Ẻ' => 'E', 'ẻ' => 'e', + 'Ể' => 'E', 'ể' => 'e', + 'Ỉ' => 'I', 'ỉ' => 'i', + 'Ỏ' => 'O', 'ỏ' => 'o', + 'Ổ' => 'O', 'ổ' => 'o', + 'Ở' => 'O', 'ở' => 'o', + 'Ủ' => 'U', 'ủ' => 'u', + 'Ử' => 'U', 'ử' => 'u', + 'Ỷ' => 'Y', 'ỷ' => 'y', + // tilde + 'Ẫ' => 'A', 'ẫ' => 'a', + 'Ẵ' => 'A', 'ẵ' => 'a', + 'Ẽ' => 'E', 'ẽ' => 'e', + 'Ễ' => 'E', 'ễ' => 'e', + 'Ỗ' => 'O', 'ỗ' => 'o', + 'Ỡ' => 'O', 'ỡ' => 'o', + 'Ữ' => 'U', 'ữ' => 'u', + 'Ỹ' => 'Y', 'ỹ' => 'y', + // acute accent + 'Ấ' => 'A', 'ấ' => 'a', + 'Ắ' => 'A', 'ắ' => 'a', + 'Ế' => 'E', 'ế' => 'e', + 'Ố' => 'O', 'ố' => 'o', + 'Ớ' => 'O', 'ớ' => 'o', + 'Ứ' => 'U', 'ứ' => 'u', + // dot below + 'Ạ' => 'A', 'ạ' => 'a', + 'Ậ' => 'A', 'ậ' => 'a', + 'Ặ' => 'A', 'ặ' => 'a', + 'Ẹ' => 'E', 'ẹ' => 'e', + 'Ệ' => 'E', 'ệ' => 'e', + 'Ị' => 'I', 'ị' => 'i', + 'Ọ' => 'O', 'ọ' => 'o', + 'Ộ' => 'O', 'ộ' => 'o', + 'Ợ' => 'O', 'ợ' => 'o', + 'Ụ' => 'U', 'ụ' => 'u', + 'Ự' => 'U', 'ự' => 'u', + 'Ỵ' => 'Y', 'ỵ' => 'y', + // Vowels with diacritic (Chinese, Hanyu Pinyin) + 'ɑ' => 'a', + // macron + 'Ǖ' => 'U', 'ǖ' => 'u', + // acute accent + 'Ǘ' => 'U', 'ǘ' => 'u', + // caron + 'Ǎ' => 'A', 'ǎ' => 'a', + 'Ǐ' => 'I', 'ǐ' => 'i', + 'Ǒ' => 'O', 'ǒ' => 'o', + 'Ǔ' => 'U', 'ǔ' => 'u', + 'Ǚ' => 'U', 'ǚ' => 'u', + // grave accent + 'Ǜ' => 'U', 'ǜ' => 'u', + ]; $str = strtr($str, $chars); @@ -519,7 +555,7 @@ public function truncate($str, $limitSms) $str = mb_substr($str, 0, $limit * $limitSms); $count = $this->count($str); - $limit = $limit -1; + $limit = $limit - 1; } while ($count->messages > $limitSms); return $str; diff --git a/Tests/SMSCounterTest.php b/Tests/SMSCounterTest.php index 9472390..c225bdb 100755 --- a/Tests/SMSCounterTest.php +++ b/Tests/SMSCounterTest.php @@ -3,20 +3,21 @@ namespace Instasent\SMSCounter\Tests; use Instasent\SMSCounter\SMSCounter; +use PHPUnit\Framework\TestCase; -class SMSCounterTest extends \PHPUnit_Framework_TestCase +class SMSCounterTest extends TestCase { public function testGSM() { - $text = "a GSM Text"; + $text = 'a GSM Text'; - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $count = $smsCounter->count($text); $expected = new \stdClass(); $expected->encoding = SMSCounter::GSM_7BIT; $expected->length = 10; - $expected->per_message= 160; + $expected->per_message = 160; $expected->remaining = 150; $expected->messages = 1; @@ -25,14 +26,14 @@ public function testGSM() public function testGSMSymbols() { - $text = "a GSM +Text"; - $smsCounter = new SMSCounter; + $text = 'a GSM +Text'; + $smsCounter = new SMSCounter(); $count = $smsCounter->count($text); $expected = new \stdClass(); $expected->encoding = SMSCounter::GSM_7BIT; $expected->length = 11; - $expected->per_message= 160; + $expected->per_message = 160; $expected->remaining = 149; $expected->messages = 1; @@ -41,31 +42,31 @@ public function testGSMSymbols() public function testGSMMultiPage() { - $text = "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - - $smsCounter = new SMSCounter; + $text = '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + + $smsCounter = new SMSCounter(); $count = $smsCounter->count($text); $expected = new \stdClass(); $expected->encoding = SMSCounter::GSM_7BIT; $expected->length = 170; - $expected->per_message= 153; + $expected->per_message = 153; $expected->remaining = 153 * 2 - 170; $expected->messages = 2; @@ -74,22 +75,22 @@ public function testGSMMultiPage() public function testUnicodeMultiPage() { - $text = "`"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - $text .= "1234567890"; - - $smsCounter = new SMSCounter; + $text = '`'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + $text .= '1234567890'; + + $smsCounter = new SMSCounter(); $count = $smsCounter->count($text); $expected = new \stdClass(); $expected->encoding = SMSCounter::UTF16; $expected->length = 71; - $expected->per_message= 67; + $expected->per_message = 67; $expected->remaining = 67 * 2 - 71; $expected->messages = 2; @@ -99,7 +100,7 @@ public function testUnicodeMultiPage() public function testCarriageReturn() { $text = "\n\r"; - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $count = $smsCounter->count($text); $expected = new \stdClass(); @@ -114,14 +115,14 @@ public function testCarriageReturn() public function testUnicode() { - $text = "`"; - $smsCounter = new SMSCounter; + $text = '`'; + $smsCounter = new SMSCounter(); $count = $smsCounter->count($text); $expected = new \stdClass(); $expected->encoding = SMSCounter::UTF16; $expected->length = 1; - $expected->per_message= 70; + $expected->per_message = 70; $expected->remaining = 69; $expected->messages = 1; @@ -130,32 +131,32 @@ public function testUnicode() public function testRemoveNonGSMChars() { - $text = "áno-unicode-remaining` ñ"; - $expectedTExt = "no-unicode-remaining ñ"; + $text = 'áno-unicode-remaining` ñ'; + $expectedTExt = 'no-unicode-remaining ñ'; - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $output = $smsCounter->removeNonGsmChars($text); $this->assertEquals($expectedTExt, $output); } - public function testSanitizeToGSM() + /** + * @dataProvider dataProvider + */ + public function testSanitizeToGSM($text, $expectedText) { - $text = "Test sanitization à ñ Ç"; - $expectedTExt = "Test sanitization à ñ Ç"; - - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $output = $smsCounter->sanitizeToGSM($text); - $this->assertEquals($expectedTExt, $output); + $this->assertEquals($expectedText, $output); } public function testTruncate1SmsGSM7() { - $text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem."; - $expectedTExt = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient "; + $text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.'; + $expectedTExt = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient '; - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $output = $smsCounter->truncate($text, 1); $this->assertEquals($expectedTExt, $output); @@ -163,10 +164,10 @@ public function testTruncate1SmsGSM7() public function testTruncate2SmsGSM7() { - $text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient"; - $expectedTExt = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis d"; + $text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient'; + $expectedTExt = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis d'; - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $output = $smsCounter->truncate($text, 2); $this->assertEquals($expectedTExt, $output); @@ -174,10 +175,10 @@ public function testTruncate2SmsGSM7() public function testTruncate1SmsUnicode() { - $text = "Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa"; - $expectedTExt = "Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipisci"; + $text = 'Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa'; + $expectedTExt = 'Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipisci'; - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $output = $smsCounter->truncate($text, 1); $this->assertEquals($expectedTExt, $output); @@ -185,12 +186,21 @@ public function testTruncate1SmsUnicode() public function testTruncate2SmsUnicode() { - $text = "Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula e ☃get dolor. Aenean massa Lorem ipsum dolor sit amet, consectetuer adip eg"; - $expectedTExt = "Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula e ☃get dolor. Aenean massa Lorem "; + $text = 'Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula e ☃get dolor. Aenean massa Lorem ipsum dolor sit amet, consectetuer adip eg'; + $expectedTExt = 'Snowman shows off! ☃ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula e ☃get dolor. Aenean massa Lorem '; - $smsCounter = new SMSCounter; + $smsCounter = new SMSCounter(); $output = $smsCounter->truncate($text, 2); $this->assertEquals($expectedTExt, $output); } + + public function dataProvider() + { + return [ + ['@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞ^{}\[~]|€ÆæßÉ!\"#¤%&\'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà', '@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞ^{}\[~]|€ÆæßÉ!\"#¤%&\'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà'], + ['Lhg jjjo fx 382 64237 12299 qmecb. Ç éæ+! -[Å*_ (¡)| ?Λ^ ~£;ΩΠ¿ ÑΔ #ΓüΘ¥ñ,É øΨì] ò= Ü. @å<: ö%\'Ƥ"Ö> اΦ{ }/&Ä ùß\€ èà Ξ$äΣ.', 'Lhg jjjo fx 382 64237 12299 qmecb. Ç éæ+! -[Å*_ (¡)| ?Λ^ ~£;ΩΠ¿ ÑΔ #ΓüΘ¥ñ,É øΨì] ò= Ü. @å<: ö%\'Ƥ"Ö> اΦ{ }/&Ä ùß\€ èà Ξ$äΣ.'], + ['dadáó', 'dadao'], + ]; + } } diff --git a/composer.json b/composer.json index 51620a3..756e9f1 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,14 @@ } ], "require": { - "php": ">=5.4.0" + "php": ">=5.6.0" }, "autoload": { "psr-4": { "Instasent\\SMSCounter\\": "" } + }, + "require-dev": { + "phpunit/phpunit": "^5.0" } }