Skip to content

Commit 2ea2b35

Browse files
committed
#22 - Add isEmpty test for strings
1 parent 923b758 commit 2ea2b35

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/TgUtils/Utils.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,15 @@ public static function findBy($list, $attr, $value) {
101101
}
102102
return $rc;
103103
}
104-
}
104+
105+
/**
106+
* Test whether the given string is NULL or empty after trimming.
107+
* @param mixed $s - string or NULL to be tested
108+
* @return boolean TRUE when string is empty
109+
*/
110+
public static function isEmpty($s) {
111+
if ($s == NULL) return TRUE;
112+
return strlen(trim($s)) == 0;
113+
}
114+
115+
}

tests/TgUtils/UtilsTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace TgUtils;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
/**
8+
* Tests some utility methods.
9+
* @author ralph
10+
*
11+
*/
12+
class UtilsTest extends TestCase {
13+
14+
public function testIsEmptyNull(): void {
15+
$this->assertTrue(Utils::isEmpty(NULL));
16+
}
17+
18+
public function testIsEmptyZero(): void {
19+
$this->assertTrue(Utils::isEmpty(''));
20+
}
21+
22+
public function testIsEmptySpaces(): void {
23+
$this->assertTrue(Utils::isEmpty(' '));
24+
}
25+
26+
public function testIsEmptyString(): void {
27+
$this->assertFalse(Utils::isEmpty('a'));
28+
}
29+
30+
}
31+

0 commit comments

Comments
 (0)