Browse Source

more tests

pull/1/head
Aaron Parecki 7 years ago
parent
commit
f7d3815616
No known key found for this signature in database GPG Key ID: 276C2817346D6056
6 changed files with 235 additions and 16 deletions
  1. +3
    -1
      .travis.yml
  2. +2
    -1
      composer.json
  3. +9
    -4
      src/cache.php
  4. +17
    -10
      src/utils.php
  5. +85
    -0
      tests/CacheTest.php
  6. +119
    -0
      tests/UtilsTest.php

+ 3
- 1
.travis.yml View File

@ -5,4 +5,6 @@ php:
- 7.0 - 7.0
- 7.1 - 7.1
- nightly - nightly
before_script: composer install
before_script: composer install
services:
- redis-server

+ 2
- 1
composer.json View File

@ -14,7 +14,8 @@
"php": ">=5.5" "php": ">=5.5"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^4.8.13"
"phpunit/phpunit": "^4.8.13",
"predis/predis": "1.1.*"
}, },
"autoload": { "autoload": {
"files": [ "files": [

+ 9
- 4
src/cache.php View File

@ -5,13 +5,18 @@ class Cache {
private static $redis; private static $redis;
public static function redis($config=false) { public static function redis($config=false) {
if(!isset(self::$redis)) {
if(empty(self::$redis)) {
if($config) { if($config) {
self::$redis = new \Predis\Client(Config::$redis);
self::$redis = new \Predis\Client($config);
} else { } else {
self::$redis = new \Predis\Client('tcp://127.0.0.1:6379'); 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) { 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(); self::redis();
$data = self::$redis->get($key); $data = self::$redis->get($key);
if($data) { if($data) {
return json_decode($data); return json_decode($data);
} else { } else {
return null;
return $default;
} }
} }

+ 17
- 10
src/utils.php View File

@ -1,18 +1,25 @@
<?php <?php
namespace p3k; namespace p3k;
use DOMDocument, IMagick, Exception;
use Config, ORM;
function redis() { function redis() {
static $client = false;
if(!$client)
$client = new Predis\Client(Config::$redis);
static $client;
if(empty($client))
$client = new \Predis\Client(class_exists('Config') ? Config::$redis : 'tcp://127.0.0.1:6379');
return $client; return $client;
} }
function bs() function bs()
{ {
static $pheanstalk; static $pheanstalk;
if(!isset($pheanstalk))
$pheanstalk = new Pheanstalk\Pheanstalk(Config::$beanstalkServer, Config::$beanstalkPort);
if(empty($pheanstalk)) {
if(class_exists('Config'))
$pheanstalk = new \Pheanstalk\Pheanstalk(Config::$beanstalkServer, Config::$beanstalkPort);
else
$pheanstalk = new \Pheanstalk\Pheanstalk('127.0.0.1', 11300);
}
return $pheanstalk; return $pheanstalk;
} }
@ -65,15 +72,15 @@ function str_ends_with($haystack, $needle) {
function session_setup($create=false, $lifetime=2592000) { function session_setup($create=false, $lifetime=2592000) {
if($create || isset($_COOKIE[session_name()])) { if($create || isset($_COOKIE[session_name()])) {
session_set_cookie_params($lifetime); session_set_cookie_params($lifetime);
session_start();
@session_start();
} }
} }
function session($key) {
if(array_key_exists($key, $_SESSION))
function session($key, $default=null) {
if(isset($_SESSION) && array_key_exists($key, $_SESSION))
return $_SESSION[$key]; return $_SESSION[$key];
else else
return null;
return $default;
} }
function flash($key) { function flash($key) {
@ -86,7 +93,7 @@ function flash($key) {
function http_header_case($str) { function http_header_case($str) {
$str = str_replace('-', ' ', $str); $str = str_replace('-', ' ', $str);
$str = ucwords($str);
$str = ucwords(strtolower($str));
$str = str_replace(' ', '-', $str); $str = str_replace(' ', '-', $str);
return $str; return $str;
} }

+ 85
- 0
tests/CacheTest.php View File

@ -0,0 +1,85 @@
<?php
class CacheTest extends PHPUnit_Framework_TestCase {
public function testCreateFromConfig() {
p3k\Cache::redis('tcp://127.0.0.1:6379');
p3k\Cache::set('foo', 'bar');
$this->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'));
}
}

+ 119
- 0
tests/UtilsTest.php View File

@ -0,0 +1,119 @@
<?php
class UtilsTest extends PHPUnit_Framework_TestCase {
public function testRandomString() {
$str1 = p3k\random_string(20);
$this->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('<b>test</b>');
$this->assertEquals('&lt;b&gt;test&lt;/b&gt;', $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('<html><head><title>Title</title></head><body>Hello World</body></html>');
$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('<html><head><title>Title</title></head><body>Hello World</body></html>');
$this->assertEquals('DOMDocument', get_class($doc));
$this->assertEmpty(libxml_get_errors());
$doc = p3k\xml_to_dom_document('<html><title>Title</title></head><body>Hello World</html>');
$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()));
}
}

Loading…
Cancel
Save