diff --git a/.travis.yml b/.travis.yml index 543c9e9..082f60c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,4 +5,6 @@ php: - 7.0 - 7.1 - nightly -before_script: composer install \ No newline at end of file +before_script: composer install +services: + - redis-server diff --git a/composer.json b/composer.json index 5210821..3b6e4b1 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "php": ">=5.5" }, "require-dev": { - "phpunit/phpunit": "^4.8.13" + "phpunit/phpunit": "^4.8.13", + "predis/predis": "1.1.*" }, "autoload": { "files": [ diff --git a/src/cache.php b/src/cache.php index 90d644b..03e9df6 100644 --- a/src/cache.php +++ b/src/cache.php @@ -5,13 +5,18 @@ class Cache { private static $redis; public static function redis($config=false) { - if(!isset(self::$redis)) { + if(empty(self::$redis)) { if($config) { - self::$redis = new \Predis\Client(Config::$redis); + self::$redis = new \Predis\Client($config); } else { self::$redis = new \Predis\Client('tcp://127.0.0.1:6379'); } } + return self::$redis; + } + + public static function reset() { + self::$redis = null; } public static function set($key, $value, $exp=600) { @@ -23,13 +28,13 @@ class Cache { } } - public static function get($key) { + public static function get($key, $default=null) { self::redis(); $data = self::$redis->get($key); if($data) { return json_decode($data); } else { - return null; + return $default; } } diff --git a/src/utils.php b/src/utils.php index 8b52147..ce4c525 100644 --- a/src/utils.php +++ b/src/utils.php @@ -1,18 +1,25 @@ assertEquals('bar', p3k\Cache::get('foo')); + p3k\Cache::reset(); + } + + public function testAutoCreate() { + p3k\Cache::set('foo', 'bar'); + $this->assertEquals('bar', p3k\Cache::get('foo')); + p3k\Cache::reset(); + } + + public function testSet() { + p3k\Cache::set('foo', 'bar', 0); + $this->assertEquals('bar', p3k\Cache::get('foo')); + $redis = p3k\Cache::redis(); + $this->assertEquals(-1, $redis->ttl('foo')); + } + + public function testSetEx() { + p3k\Cache::set('foo', 'bar', 600); + $this->assertEquals('bar', p3k\Cache::get('foo')); + $redis = p3k\Cache::redis(); + $this->assertGreaterThan(500, $redis->ttl('foo')); + } + + public function testGetExpired() { + p3k\Cache::set('foo', 'bar', 1); + sleep(1); + $this->assertEquals('default', p3k\Cache::get('foo', 'default')); + } + + public function testDelete() { + p3k\Cache::set('foo', 'bar', 600); + $this->assertEquals('bar', p3k\Cache::get('foo')); + p3k\Cache::delete('foo'); + $this->assertEquals('default', p3k\Cache::get('foo', 'default')); + } + + public function testExpire() { + p3k\Cache::set('foo', 'bar', 600); + $this->assertEquals('bar', p3k\Cache::get('foo')); + p3k\Cache::expire('foo'); + $this->assertEquals('default', p3k\Cache::get('foo', 'default')); + + p3k\Cache::set('foo', 'bar', 600); + $this->assertEquals('bar', p3k\Cache::get('foo')); + p3k\Cache::expire('foo', 1); + sleep(1); + $this->assertEquals('default', p3k\Cache::get('foo', 'default')); + } + + public function testIncr() { + p3k\Cache::delete('test1'); + p3k\Cache::incr('test1'); + $this->assertEquals(1, p3k\Cache::get('test1')); + + p3k\Cache::set('test2', 10); + p3k\Cache::incr('test2'); + $this->assertEquals(11, p3k\Cache::get('test2')); + + p3k\Cache::set('test3', 10); + p3k\Cache::incr('test3', 4); + $this->assertEquals(14, p3k\Cache::get('test3')); + } + + public function testDecr() { + p3k\Cache::delete('test4'); + p3k\Cache::decr('test4'); + $this->assertEquals(-1, p3k\Cache::get('test4')); + + p3k\Cache::set('test5', 10); + p3k\Cache::decr('test5'); + $this->assertEquals(9, p3k\Cache::get('test5')); + + p3k\Cache::set('test6', 10); + p3k\Cache::decr('test6', 4); + $this->assertEquals(6, p3k\Cache::get('test6')); + } + +} diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php new file mode 100644 index 0000000..e6ebf87 --- /dev/null +++ b/tests/UtilsTest.php @@ -0,0 +1,119 @@ +assertEquals(20, strlen($str1)); + $str2 = p3k\random_string(20); + $this->assertEquals(20, strlen($str2)); + $this->assertNotEquals($str1, $str2); + } + + public function testEndsWith() { + $this->assertFalse(p3k\str_ends_with('abcdefg', '')); + $this->assertFalse(p3k\str_ends_with('', 'abcdefg')); + $this->assertTrue(p3k\str_ends_with('abcdefg', 'efg')); + $this->assertTrue(p3k\str_ends_with('abcdefg', 'abcdefg')); + $this->assertTrue(p3k\str_ends_with('abcdefg', 'g')); + $this->assertFalse(p3k\str_ends_with('abcdefg', 'abc')); + } + + public function testSessionSetupNoCreate() { + // no session already, so this should not create one + p3k\session_setup(); + $this->assertFalse(isset($_SESSION)); + } + + public function testSessionSetupCreateFromCookie() { + // there is a session cookie, so this should initialize the session + $_COOKIE[session_name()] = '12345'; + p3k\session_setup(); + $this->assertTrue(isset($_SESSION)); + } + + public function testSessionAccess() { + $_SESSION = []; + $this->assertNull(p3k\session('foo')); + $_SESSION = []; + $this->assertEquals('default', p3k\session('foo', 'default')); + $_SESSION = ['foo'=>'bar']; + $this->assertEquals('bar', p3k\session('foo')); + } + + public function testFlash() { + $_SESSION = []; + $this->assertNull(p3k\flash('foo')); + $_SESSION = ['foo'=>'bar']; + $this->assertEquals('bar', p3k\flash('foo')); + $this->assertNull(p3k\flash('foo')); + } + + public function testE() { + $html = p3k\e('test'); + $this->assertEquals('<b>test</b>', $html); + } + + public function testK() { + $this->assertEquals('b', p3k\k(['a'=>'b'], 'a')); + $this->assertEquals('default', p3k\k(['a'=>'b'], 'z', 'default')); + $obj = new StdClass; + $obj->a = 'b'; + $this->assertEquals('b', p3k\k($obj, 'a')); + $this->assertEquals('default', p3k\k($obj, 'z', 'default')); + + $keys = ['a','b','c']; + $values = ['a'=>true, 'b'=>true, 'c'=>true]; + $this->assertTrue(p3k\k($values, $keys)); + + $keys = ['a','b','c']; + $values = ['a'=>true, 'c'=>true]; + $this->assertFalse(p3k\k($values, $keys)); + } + + public function testHTTPHeaderCase() { + $name = p3k\http_header_case('header-name'); + $this->assertEquals('Header-Name', $name); + $name = p3k\http_header_case('HEADER-NAME'); + $this->assertEquals('Header-Name', $name); + $name = p3k\http_header_case('hEaDeR-nAmE'); + $this->assertEquals('Header-Name', $name); + $name = p3k\http_header_case('host'); + $this->assertEquals('Host', $name); + $name = p3k\http_header_case('x-header-name'); + $this->assertEquals('X-Header-Name', $name); + } + + public function testHTMLToDomDocument() { + $doc = p3k\html_to_dom_document('TitleHello World'); + $this->assertEquals('DOMDocument', get_class($doc)); + $this->assertEmpty(libxml_get_errors()); + $doc = p3k\html_to_dom_document("\0this is not HTML"); + $this->assertEquals('DOMDocument', get_class($doc)); + $this->assertEmpty(libxml_get_errors()); + } + + public function testXMLToDomDocument() { + $doc = p3k\xml_to_dom_document('TitleHello World'); + $this->assertEquals('DOMDocument', get_class($doc)); + $this->assertEmpty(libxml_get_errors()); + $doc = p3k\xml_to_dom_document('TitleHello World'); + $this->assertEquals('DOMDocument', get_class($doc)); + $this->assertEmpty(libxml_get_errors()); + $doc = p3k\xml_to_dom_document("\0this is not XML"); + $this->assertEquals('DOMDocument', get_class($doc)); + $this->assertEmpty(libxml_get_errors()); + } + + public function testBase60() { + $this->assertEquals('BBBB', p3k\b10to60(p3k\b60to10('BBBB'))); + $this->assertEquals('ABCD_efg', p3k\b10to60(p3k\b60to10('ABCD_efg'))); + $this->assertEquals('Z111000', p3k\b10to60(p3k\b60to10('ZIl1O0O'))); + $this->assertEquals('0', p3k\b10to60(p3k\b60to10(',<.'))); + } + + public function testCreatesRedis() { + p3k\redis(); + $this->assertEquals('Predis\Client', get_class(p3k\redis())); + } + +}