From 0cfc2c797950f2f89e4ce182c689b37fc11a6a91 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 28 Sep 2014 07:31:02 -0400 Subject: [PATCH] Moved tests and updated to use namespaces --- composer.lock | 2 +- src/Object.php | 15 +- tests/{classes => }/BrowserTest.php | 20 +-- tests/{classes => }/CacheTest.php | 20 +-- tests/{classes => }/ConfigTest.php | 26 ++-- tests/{classes => }/ConvertTest.php | 2 +- tests/{classes => }/DatabaseTest.php | 58 ++++---- tests/{classes => }/DateTest.php | 4 +- tests/DistanceTest.php | 75 ++++++++++ tests/{classes => }/FileTest.php | 10 +- tests/{classes => }/ModelTest.php | 48 +++---- tests/{classes => }/NumberTest.php | 4 +- tests/ObjectTest.php | 24 ++++ tests/ProfilerTest.php | 81 +++++++++++ tests/{classes => }/ResourceTest.php | 8 +- tests/{classes => }/RouterTest.php | 8 +- tests/{classes => }/SortTest.php | 6 +- tests/{classes => }/StringTest.php | 52 +++---- tests/TimeTest.php | 160 +++++++++++++++++++++ tests/ValidateTest.php | 199 +++++++++++++++++++++++++++ tests/bootstrap.php | 8 +- tests/classes/DistanceTest.php | 75 ---------- tests/classes/ObjectTest.php | 24 ---- tests/classes/ProfilerTest.php | 81 ----------- tests/classes/TimeTest.php | 160 --------------------- tests/classes/ValidateTest.php | 199 --------------------------- 26 files changed, 686 insertions(+), 683 deletions(-) rename tests/{classes => }/BrowserTest.php (69%) rename tests/{classes => }/CacheTest.php (77%) rename tests/{classes => }/ConfigTest.php (85%) rename tests/{classes => }/ConvertTest.php (93%) rename tests/{classes => }/DatabaseTest.php (73%) rename tests/{classes => }/DateTest.php (81%) create mode 100644 tests/DistanceTest.php rename tests/{classes => }/FileTest.php (82%) rename tests/{classes => }/ModelTest.php (93%) rename tests/{classes => }/NumberTest.php (87%) create mode 100644 tests/ObjectTest.php create mode 100644 tests/ProfilerTest.php rename tests/{classes => }/ResourceTest.php (87%) rename tests/{classes => }/RouterTest.php (91%) rename tests/{classes => }/SortTest.php (89%) rename tests/{classes => }/StringTest.php (53%) create mode 100644 tests/TimeTest.php create mode 100644 tests/ValidateTest.php delete mode 100644 tests/classes/DistanceTest.php delete mode 100644 tests/classes/ObjectTest.php delete mode 100644 tests/classes/ProfilerTest.php delete mode 100644 tests/classes/TimeTest.php delete mode 100644 tests/classes/ValidateTest.php diff --git a/composer.lock b/composer.lock index 111c651..ba0f3b0 100644 --- a/composer.lock +++ b/composer.lock @@ -3,7 +3,7 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "276692127a6b06339d4dcbc0bd8f0e34", + "hash": "34024ae2fc3e51c0793168dad225c937", "packages": [], "packages-dev": [ { diff --git a/src/Object.php b/src/Object.php index 589512d..b271898 100644 --- a/src/Object.php +++ b/src/Object.php @@ -122,14 +122,19 @@ class Object */ public static function getInstance($class = false) { - $class = 'Pickles\\' . $class; - - if (!isset(self::$instances[$class])) + if ($class) { - self::$instances[$class] = new $class(); + $class = 'Pickles\\' . $class; + + if (!isset(self::$instances[$class])) + { + self::$instances[$class] = new $class(); + } + + return self::$instances[$class]; } - return self::$instances[$class]; + return false; } /** diff --git a/tests/classes/BrowserTest.php b/tests/BrowserTest.php similarity index 69% rename from tests/classes/BrowserTest.php rename to tests/BrowserTest.php index 670c741..4a29c91 100644 --- a/tests/classes/BrowserTest.php +++ b/tests/BrowserTest.php @@ -1,31 +1,31 @@ assertFalse(Browser::remoteIP()); + $this->assertFalse(Pickles\Browser::remoteIP()); } public function testRemoteIPRemoteAddress() { $_SERVER['REMOTE_ADDR'] = '1.2.3.4'; - $this->assertEquals('1.2.3.4', Browser::remoteIP()); + $this->assertEquals('1.2.3.4', Pickles\Browser::remoteIP()); } public function testRemoteIPHTTPXForwardedFor() { $_SERVER['HTTP_X_FORWARDED_FOR'] = '2.3.4.5'; - $this->assertEquals('2.3.4.5', Browser::remoteIP()); + $this->assertEquals('2.3.4.5', Pickles\Browser::remoteIP()); } public function testRemoteIPHTTPClientIP() { $_SERVER['HTTP_CLIENT_IP'] = '3.4.5.6'; - $this->assertEquals('3.4.5.6', Browser::remoteIP()); + $this->assertEquals('3.4.5.6', Pickles\Browser::remoteIP()); } public function testRemoteIPWithComma() @@ -35,31 +35,31 @@ class BrowserTest extends PHPUnit_Framework_TestCase public function testStatus1xx() { - Browser::status(100); + Pickles\Browser::status(100); $this->assertTrue(in_array('Status: 100 Continue', xdebug_get_headers())); } public function testStatus2xx() { - Browser::status(200); + Pickles\Browser::status(200); $this->assertTrue(in_array('Status: 200 OK', xdebug_get_headers())); } public function testStatus3xx() { - Browser::status(300); + Pickles\Browser::status(300); $this->assertTrue(in_array('Status: 300 Multiple Choices', xdebug_get_headers())); } public function testStatus4xx() { - Browser::status(400); + Pickles\Browser::status(400); $this->assertTrue(in_array('Status: 400 Bad Request', xdebug_get_headers())); } public function testStatus5xx() { - Browser::status(500); + Pickles\Browser::status(500); $this->assertTrue(in_array('Status: 500 Internal Server Error', xdebug_get_headers())); } } diff --git a/tests/classes/CacheTest.php b/tests/CacheTest.php similarity index 77% rename from tests/classes/CacheTest.php rename to tests/CacheTest.php index 13ecbc1..364d304 100644 --- a/tests/classes/CacheTest.php +++ b/tests/CacheTest.php @@ -7,7 +7,7 @@ class CacheTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->config = Config::getInstance(); + $this->config = Pickles\Config::getInstance(); $this->config->data['pickles']['cache'] = 'mc'; $this->config->data['datasources']['mc'] = [ 'type' => 'memcache', @@ -16,18 +16,18 @@ class CacheTest extends PHPUnit_Framework_TestCase 'namespace' => 'ns', ]; - $this->cache = Cache::getInstance(); + $this->cache = Pickles\Cache::getInstance(); } public function testGetInstance() { - $this->assertInstanceOf('Cache', $this->cache); + $this->assertInstanceOf('Pickles\\Cache', $this->cache); } public function testSetAndGet() { - $key = String::random(); - $value = String::random(); + $key = Pickles\String::random(); + $value = Pickles\String::random(); $this->cache->set($key, $value); @@ -40,8 +40,8 @@ class CacheTest extends PHPUnit_Framework_TestCase for ($i = 0; $i < 5; $i++) { - $keys[] = String::random(); - $values[] = String::random(); + $keys[] = Pickles\String::random(); + $values[] = Pickles\String::random(); } foreach ($keys as $key => $key_name) @@ -56,8 +56,8 @@ class CacheTest extends PHPUnit_Framework_TestCase public function testDelete() { - $key = String::random(); - $value = String::random(); + $key = Pickles\String::random(); + $value = Pickles\String::random(); $this->cache->set($key, $value); $this->cache->delete($key); @@ -67,7 +67,7 @@ class CacheTest extends PHPUnit_Framework_TestCase public function testIncrement() { - $key = String::random(); + $key = Pickles\String::random(); $this->assertFalse($this->cache->increment($key)); diff --git a/tests/classes/ConfigTest.php b/tests/ConfigTest.php similarity index 85% rename from tests/classes/ConfigTest.php rename to tests/ConfigTest.php index e16c8d1..7749f29 100644 --- a/tests/classes/ConfigTest.php +++ b/tests/ConfigTest.php @@ -6,7 +6,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->config = Config::getInstance(); + $this->config = Pickles\Config::getInstance(); setupConfig([]); $_SERVER['REQUEST_METHOD'] = 'GET'; @@ -14,14 +14,14 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testConfigProperty() { - $config = new Config(); + $config = new Pickles\Config(); $this->assertTrue(PHPUnit_Framework_Assert::readAttribute($config, 'config')); } public function testInstanceOf() { - $this->assertInstanceOf('Config', $this->config); + $this->assertInstanceOf('Pickles\\Config', $this->config); } public function testUndefined() @@ -35,7 +35,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase 'environment' => 'local', ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertEquals('local', $config->environment); } @@ -49,7 +49,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase ], ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertEquals('local', $config->environment); } @@ -63,7 +63,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase ], ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertEquals('prod', $config->environment); } @@ -80,7 +80,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase ], ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertEquals('prod', $config->environment); } @@ -96,7 +96,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase setUpConfig(['environments' => []]); - $config = new Config(); + $config = new Pickles\Config(); } public function testProfiler() @@ -106,7 +106,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase 'pickles' => ['profiler' => true], ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertTrue($config->pickles['profiler']); } @@ -118,7 +118,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase 'pickles' => ['profiler' => ['objects', 'timers']], ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertEquals('objects,timers', $config->pickles['profiler']); } @@ -130,7 +130,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase 'security' => ['levels' => [10 => 'level']], ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertEquals(10, SECURITY_LEVEL_USER); } @@ -146,7 +146,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase 'security' => ['levels' => [10 => 'level']], ]); - $config = new Config(); + $config = new Pickles\Config(); $this->assertEquals(10, SECURITY_LEVEL_USER); } @@ -155,7 +155,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testConfigArrayMissing() { file_put_contents(SITE_PATH . 'config.php', ''); - new Config(); + new Pickles\Config(); } } diff --git a/tests/classes/ConvertTest.php b/tests/ConvertTest.php similarity index 93% rename from tests/classes/ConvertTest.php rename to tests/ConvertTest.php index c36a2fa..36314a6 100644 --- a/tests/classes/ConvertTest.php +++ b/tests/ConvertTest.php @@ -7,7 +7,7 @@ class ConvertTest extends PHPUnit_Framework_TestCase */ public function testArrayToXML($a, $b, $c) { - $this->assertEquals(Convert::arrayToXML($a, $b), $c); + $this->assertEquals(Pickles\Convert::arrayToXML($a, $b), $c); } public function providerArrayToXML() diff --git a/tests/classes/DatabaseTest.php b/tests/DatabaseTest.php similarity index 73% rename from tests/classes/DatabaseTest.php rename to tests/DatabaseTest.php index 1f19a79..48783c5 100644 --- a/tests/classes/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -4,7 +4,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase { public function testGetInstanceFalse() { - $this->assertFalse(Database::getInstance()); + $this->assertFalse(Pickles\Database::getInstance()); } /** @@ -13,9 +13,9 @@ class DatabaseTest extends PHPUnit_Framework_TestCase */ public function testGetInstanceDatasourceNotDefined() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['pickles']['datasource'] = 'bad'; - Database::getInstance(); + Pickles\Database::getInstance(); } /** @@ -24,13 +24,13 @@ class DatabaseTest extends PHPUnit_Framework_TestCase */ public function testGetInstanceDatasourceLacksDriver() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['datasources'] = [ 'bad' => [ 'type' => 'mysql', ], ]; - $this->assertInstanceOf('Database', Database::getInstance()); + $this->assertInstanceOf('Pickles\\Database', Pickles\Database::getInstance()); } /** @@ -39,7 +39,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase */ public function testOpenConfigError() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['datasources'] = [ 'bad' => [ 'type' => 'mysql', @@ -47,13 +47,13 @@ class DatabaseTest extends PHPUnit_Framework_TestCase 'database' => 'test', ], ]; - $db = Database::getInstance(); + $db = Pickles\Database::getInstance(); $db->open(); } public function testGetInstanceDatasourcesArray() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['datasources'] = [ 'mysql' => [ 'type' => 'mysql', @@ -64,28 +64,28 @@ class DatabaseTest extends PHPUnit_Framework_TestCase 'database' => 'test', ], ]; - $this->assertInstanceOf('Database', Database::getInstance()); + $this->assertInstanceOf('Pickles\\Database', Pickles\Database::getInstance()); } // Also tests the datasource being missing and selecting the first one public function testGetInstanceMySQL() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); unset($config->data['pickles']['datasource']); - $this->assertInstanceOf('Database', Database::getInstance()); + $this->assertInstanceOf('Pickles\\Database', Pickles\Database::getInstance()); } public function testOpenMySQL() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['pickles']['datasource'] = 'mysql'; - $db = Database::getInstance(); + $db = Pickles\Database::getInstance(); $db->open(); } public function testExecute() { - $db = Database::getInstance(); + $db = Pickles\Database::getInstance(); $this->assertEquals('0', $db->execute('SHOW TABLES')); } @@ -95,35 +95,35 @@ class DatabaseTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoQuery() { - $db = Database::getInstance(); + $db = Pickles\Database::getInstance(); $db->execute(' '); } public function testFetch() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['pickles']['logging'] = true; $config->data['pickles']['profiler'] = true; - $db = Database::getInstance(); + $db = Pickles\Database::getInstance(); $this->assertEquals([], $db->fetch('SELECT * FROM pickles WHERE id != ?', ['0'])); } public function testExplainNoInput() { - $config = Config::getInstance(); - $db = Database::getInstance(); + $config = Pickles\Config::getInstance(); + $db = Pickles\Database::getInstance(); $this->assertEquals([], $db->fetch('SELECT * FROM pickles WHERE id != 0')); } public function testSlowQuery() { - $db = Database::getInstance(); + $db = Pickles\Database::getInstance(); $this->assertEquals('0', $db->execute('SHOW DATABASES', null, true)); } public function testCloseMySQL() { - $db = Database::getInstance(); + $db = Pickles\Database::getInstance(); $db->open(); $this->assertTrue($db->close()); @@ -131,7 +131,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase public function testGetInstancePostgreSQL() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['pickles']['datasource'] = 'pgsql'; $config->data['datasources']['pgsql'] = [ 'type' => 'pgsql', @@ -141,7 +141,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase 'password' => '', 'database' => 'test', ]; - $this->assertInstanceOf('Database', Database::getInstance()); + $this->assertInstanceOf('Pickles\\Database', Pickles\Database::getInstance()); } /** @@ -151,14 +151,14 @@ class DatabaseTest extends PHPUnit_Framework_TestCase public function testOpenPostgreSQL() { // Also throws an exception since I don't have PostgreSQL set up - $config = Config::getInstance(); - $db = Database::getInstance(); + $config = Pickles\Config::getInstance(); + $db = Pickles\Database::getInstance(); $db->open(); } public function testGetInstanceSQLite() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['pickles']['datasource'] = 'sqlite'; $config->data['datasources']['sqlite'] = [ 'type' => 'sqlite', @@ -168,7 +168,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase 'password' => '', 'database' => 'test', ]; - $this->assertInstanceOf('Database', Database::getInstance()); + $this->assertInstanceOf('Pickles\\Database', Pickles\Database::getInstance()); } /** @@ -177,7 +177,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase */ public function testGetInstanceInvalidDriver() { - $config = Config::getInstance(); + $config = Pickles\Config::getInstance(); $config->data['pickles']['datasource'] = 'invalid'; $config->data['datasources']['invalid'] = [ 'type' => 'invalid', @@ -187,7 +187,7 @@ class DatabaseTest extends PHPUnit_Framework_TestCase 'password' => '', 'database' => 'test', ]; - Database::getInstance(); + Pickles\Database::getInstance(); } } diff --git a/tests/classes/DateTest.php b/tests/DateTest.php similarity index 81% rename from tests/classes/DateTest.php rename to tests/DateTest.php index 5752e4d..c7d3867 100644 --- a/tests/classes/DateTest.php +++ b/tests/DateTest.php @@ -7,7 +7,7 @@ class DateTest extends PHPUnit_Framework_TestCase */ public function testAge($a, $b) { - $this->assertEquals(Date::age($a), $b); + $this->assertEquals(Pickles\Date::age($a), $b); } public function providerAge() @@ -22,7 +22,7 @@ class DateTest extends PHPUnit_Framework_TestCase [date('r', $time), '25'], ['today', '0'], ['400 days ago', '1'], - [true, Date::age('1969-12-31')], + [true, Pickles\Date::age('1969-12-31')], ]; } } diff --git a/tests/DistanceTest.php b/tests/DistanceTest.php new file mode 100644 index 0000000..2de6212 --- /dev/null +++ b/tests/DistanceTest.php @@ -0,0 +1,75 @@ +assertEquals(0.621371, Pickles\Distance::kilometersToMiles(1)); + } + + public function testConvertKilometersToMeters() + { + $this->assertEquals(1000, Pickles\Distance::kilometersToMeters(1)); + } + + public function testConvertKilometersToYards() + { + $this->assertEquals(1093.61, Pickles\Distance::kilometersToYards(1)); + } + + public function testConvertMilesToKilometers() + { + $this->assertEquals(1.60934, Pickles\Distance::milesToKilometers(1)); + } + + public function testConvertMilesToMeters() + { + $this->assertEquals(1609.34, Pickles\Distance::milesToMeters(1)); + } + + public function testConvertMilesToYards() + { + $this->assertEquals(1760, Pickles\Distance::milesToYards(1)); + } + + public function testConvertMetersToKilometers() + { + $this->assertEquals(0.001, Pickles\Distance::metersToKilometers(1)); + } + + public function testConvertMetersToMiles() + { + $this->assertEquals(0.000621371, Pickles\Distance::metersToMiles(1)); + } + + public function testConvertMetersToYards() + { + $this->assertEquals(1.09361, Pickles\Distance::metersToYards(1)); + } + + public function testCalculateDistanceMiles() + { + $this->assertEquals(1003.2646776326, Pickles\Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94)); + } + + public function testCalculateDistanceKilometers() + { + $this->assertEquals(1614.5939763012, Pickles\Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94, 'kilometers')); + } + + public function testCalculateDistanceMeters() + { + $this->assertEquals(1614593.9763012, Pickles\Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94, 'meters'), '', 0.2); + } + + public function testCalculateDistanceYards() + { + $this->assertEquals(1765745.8326334, Pickles\Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94, 'yards'), '', 0.2); + } + + public function testNotEnoughUnits() + { + $this->assertFalse(Pickles\Distance::milesTo(123)); + } +} + diff --git a/tests/classes/FileTest.php b/tests/FileTest.php similarity index 82% rename from tests/classes/FileTest.php rename to tests/FileTest.php index 5e00d82..838643e 100644 --- a/tests/classes/FileTest.php +++ b/tests/FileTest.php @@ -15,7 +15,7 @@ class FileTest extends PHPUnit_Framework_TestCase public static function tearDownAfterClass() { - File::removeDirectory('/tmp/pickles-fs'); + Pickles\File::removeDirectory('/tmp/pickles-fs'); } public function testRemoveDirectory() @@ -26,7 +26,7 @@ class FileTest extends PHPUnit_Framework_TestCase touch($directory . 'test/ing'); touch($directory . 'test/test/ing'); - File::removeDirectory($directory); + Pickles\File::removeDirectory($directory); $this->assertFalse(file_exists($directory)); } @@ -38,7 +38,7 @@ class FileTest extends PHPUnit_Framework_TestCase mkdir($directory, 0777, true); touch(SITE_PATH . 'missing/slash'); - File::removeDirectory($directory); + Pickles\File::removeDirectory($directory); $this->assertFalse(file_exists($directory)); } @@ -51,11 +51,11 @@ class FileTest extends PHPUnit_Framework_TestCase mkdir($directory, 0777, true); touch($file); - File::removeDirectory($file); + Pickles\File::removeDirectory($file); $this->assertFalse(file_exists($file)); - File::removeDirectory($directory); + Pickles\File::removeDirectory($directory); } } diff --git a/tests/classes/ModelTest.php b/tests/ModelTest.php similarity index 93% rename from tests/classes/ModelTest.php rename to tests/ModelTest.php index be53da9..600a73a 100644 --- a/tests/classes/ModelTest.php +++ b/tests/ModelTest.php @@ -1,20 +1,20 @@ 'created_at']; } // MyISAM -class MyMockModel extends Model +class MyMockModel extends Pickles\Model { public $table = 'mypickles'; } @@ -24,8 +24,8 @@ class ModelTest extends PHPUnit_Framework_TestCase public static function setUpBeforeClass() { // Clears out the Config for ease of testing - Object::$instances = []; - $config = Config::getInstance(); + Pickles\Object::$instances = []; + $config = Pickles\Config::getInstance(); $config->data = [ 'pickles' => [ @@ -69,7 +69,7 @@ class ModelTest extends PHPUnit_Framework_TestCase */ public function testNoTable() { - new Model(); + new Pickles\Model(); } public function testWithoutColumns() @@ -232,7 +232,7 @@ class ModelTest extends PHPUnit_Framework_TestCase for ($i = 1; $i <= 5; $i++) { - $model->record['field' . $i] = String::random(); + $model->record['field' . $i] = Pickles\String::random(); } $model->commit(); @@ -246,7 +246,7 @@ class ModelTest extends PHPUnit_Framework_TestCase { for ($j = 1; $j <= 5; $j++) { - $model->record['field' . $j] = String::random(); + $model->record['field' . $j] = Pickles\String::random(); } $model->queue(); @@ -365,7 +365,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitSingleRecord() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(1); $model->record['field1'] = $value; $model->commit(); @@ -379,7 +379,7 @@ class ModelTest extends PHPUnit_Framework_TestCase // else it just takes a shit because the ID isn't injected back in. public function testCommitSingleRecordReplace() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(1); $model->replace = true; $model->record['field1'] = $value; @@ -389,7 +389,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitInsertPriority() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(); $model->priority = 'low'; $model->record['field1'] = $value; @@ -400,7 +400,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitInsertDelayed() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MyMockModel(); $model->delayed = true; $model->record['field1'] = $value; @@ -411,7 +411,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitInsertIgnore() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(); $model->ignore = true; $model->record['field1'] = $value; @@ -422,7 +422,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitReplacePriority() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(); $model->replace = true; $model->priority = 'low'; @@ -434,7 +434,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitReplaceDelayed() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MyMockModel(); $model->replace = true; $model->delayed = true; @@ -446,7 +446,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitReplaceIgnore() { - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(); $model->replace = true; $model->ignore = true; @@ -458,8 +458,8 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitMultipleFields() { - $value1 = String::random(); - $value2 = String::random(); + $value1 = Pickles\String::random(); + $value2 = Pickles\String::random(); $model = new MockModelWithoutColumns(1); $model->record['field1'] = $value1; $model->record['field2'] = $value2; @@ -484,7 +484,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitUpdatedID() { $_SESSION['__pickles']['security']['user_id'] = 1; - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(1); $model->record['field1'] = $value; $model->commit(); @@ -496,7 +496,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitCreatedID() { $_SESSION['__pickles']['security']['user_id'] = 1; - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(); $model->record['field1'] = $value; $id = $model->commit(); @@ -508,7 +508,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitInsertPostgreSQL() { $_SESSION['__pickles']['security']['user_id'] = 1; - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(); $model->mysql = false; $model->postgresql = true; @@ -530,7 +530,7 @@ class ModelTest extends PHPUnit_Framework_TestCase public function testCommitUpdatePostgreSQL() { $_SESSION['__pickles']['security']['user_id'] = 1; - $value = String::random(); + $value = Pickles\String::random(); $model = new MockModel(1); $model->mysql = false; $model->postgresql = true; @@ -593,7 +593,7 @@ class ModelTest extends PHPUnit_Framework_TestCase for ($i = 0; $i < 5; $i++) { - $model->record['field1'] = String::random(); + $model->record['field1'] = Pickles\String::random(); $model->record['updated_id'] = 1; $model->queue(); } @@ -611,7 +611,7 @@ class ModelTest extends PHPUnit_Framework_TestCase for ($i = 3; $i <= 5; $i++) { $model->record['id'] = $i; - $model->record['field1'] = String::random(); + $model->record['field1'] = Pickles\String::random(); $model->record['updated_id'] = 1; $model->queue(); } diff --git a/tests/classes/NumberTest.php b/tests/NumberTest.php similarity index 87% rename from tests/classes/NumberTest.php rename to tests/NumberTest.php index 361b22a..b54dbb4 100644 --- a/tests/classes/NumberTest.php +++ b/tests/NumberTest.php @@ -7,7 +7,7 @@ class NumberTest extends PHPUnit_Framework_TestCase */ public function testOrdinalIndicatorNoSuper($a, $b) { - $this->assertEquals($b, Number::ordinalIndicator($a)); + $this->assertEquals($b, Pickles\Number::ordinalIndicator($a)); } public function providerOrginalIndicatorNoSuper() @@ -29,7 +29,7 @@ class NumberTest extends PHPUnit_Framework_TestCase */ public function testOrdinalIndicatorSuper($a, $b) { - $this->assertEquals($b, Number::ordinalIndicator($a, true)); + $this->assertEquals($b, Pickles\Number::ordinalIndicator($a, true)); } public function providerOrginalIndicatorSuper() diff --git a/tests/ObjectTest.php b/tests/ObjectTest.php new file mode 100644 index 0000000..4344bf8 --- /dev/null +++ b/tests/ObjectTest.php @@ -0,0 +1,24 @@ +assertInstanceOf('Pickles\Config', PHPUnit_Framework_Assert::readAttribute($object, 'config')); + } + + public function testConstructorWithObjects() + { + $object = new Pickles\Object('cache'); + + $this->assertInstanceOf('Pickles\Cache', PHPUnit_Framework_Assert::readAttribute($object, 'cache')); + } + + public function testGetInstanceWithoutClass() + { + $this->assertFalse(Pickles\Object::getInstance()); + } +} + diff --git a/tests/ProfilerTest.php b/tests/ProfilerTest.php new file mode 100644 index 0000000..6dadbac --- /dev/null +++ b/tests/ProfilerTest.php @@ -0,0 +1,81 @@ +expectOutputRegex('//'); + + Pickles\Profiler::report(); + } + + public function testDisabledType() + { + $config = Pickles\Config::getInstance(); + $config->data['pickles']['profiler'] = false; + + $this->assertFalse(Pickles\Profiler::enabled('timers')); + } + + public function testTimerDisabled() + { + $this->assertFalse(Pickles\Profiler::timer('disabled')); + } + + public function testReportNothing() + { + $this->expectOutputRegex('/There is nothing to profile/'); + + Pickles\Profiler::report(); + } + + public function testEnabled() + { + $config = Pickles\Config::getInstance(); + $config->data['pickles']['profiler'] = true; + + $this->assertTrue(Pickles\Profiler::enabled()); + } + + public function testEnabledType() + { + $config = Pickles\Config::getInstance(); + $config->data['pickles']['profiler'] = 'timers'; + + $this->assertTrue(Pickles\Profiler::enabled('timers')); + } + + public function testLogAndTimer() + { + Pickles\Profiler::log('timer', 'timer-one'); + Pickles\Profiler::log(['foo' => 'bar']); + Pickles\Profiler::log(new Pickles\Object); + Pickles\Profiler::log('string'); + Pickles\Profiler::log(3.14, 'method', true); + Pickles\Profiler::log('timer', 'timer-one'); + } + + public function testLogQuery() + { + $explain = [ + [ + 'key' => '', + 'possible_keys' => '', + 'type' => '', + 'rows' => '', + 'Extra' => '', + ], + ]; + + Pickles\Profiler::logQuery('SELECT * FROM table;'); + Pickles\Profiler::logQuery('SELECT * FROM table WHERE column = ?;', ['foo']); + Pickles\Profiler::logQuery('SELECT * FROM table;', false, $explain); + } + + public function testTimer() + { + Pickles\Profiler::timer('timer-two'); + Pickles\Profiler::timer('timer-two'); + } +} + diff --git a/tests/classes/ResourceTest.php b/tests/ResourceTest.php similarity index 87% rename from tests/classes/ResourceTest.php rename to tests/ResourceTest.php index c0ac401..6fa429e 100644 --- a/tests/classes/ResourceTest.php +++ b/tests/ResourceTest.php @@ -4,7 +4,7 @@ $_POST['field2'] = 'short'; $_GET['field2'] = 'short'; $_REQUEST['field2'] = 'short'; -class MockParentResource extends Resource +class MockParentResource extends Pickles\Resource { public $validate = [ 'field1', @@ -24,7 +24,7 @@ class ResourceTest extends PHPUnit_Framework_TestCase { public function testAutoRun() { - $this->assertInstanceOf('Resource', new Resource(true)); + $this->assertInstanceOf('Pickles\\Resource', new Pickles\Resource(true)); } public function testAutoRunParentError() @@ -35,14 +35,14 @@ class ResourceTest extends PHPUnit_Framework_TestCase public function testSetGetReturn() { - $module = new Resource(); + $module = new Pickles\Resource(); $module->foo = 'bar'; $this->assertEquals('bar', $module->foo); } public function testGetMissing() { - $module = new Resource(); + $module = new Pickles\Resource(); $this->assertFalse($module->missing); } diff --git a/tests/classes/RouterTest.php b/tests/RouterTest.php similarity index 91% rename from tests/classes/RouterTest.php rename to tests/RouterTest.php index 84617ab..7f5863e 100644 --- a/tests/classes/RouterTest.php +++ b/tests/RouterTest.php @@ -6,7 +6,7 @@ class RouterTest extends PHPUnit_Framework_TestCase public function setUp() { - $this->config = Config::getInstance(); + $this->config = Pickles\Config::getInstance(); $this->config->data['pickles']['disabled'] = false; $this->config->data['pickles']['profiler'] = false; @@ -25,7 +25,7 @@ class RouterTest extends PHPUnit_Framework_TestCase file_put_contents(SITE_MODULE_PATH . 'secure.php', $module); - new Router(); + new Pickles\Router(); $this->assertTrue(in_array('Location: https://testsite.com/secure', xdebug_get_headers())); } @@ -39,7 +39,7 @@ class RouterTest extends PHPUnit_Framework_TestCase file_put_contents(SITE_MODULE_PATH . 'insecure.php', $module); - new Router(); + new Pickles\Router(); $this->assertTrue(in_array('Location: http://testsite.com/insecure', xdebug_get_headers())); } @@ -55,7 +55,7 @@ class RouterTest extends PHPUnit_Framework_TestCase file_put_contents(SITE_MODULE_PATH . 'validationerrors.php', $module); - new Router(); + new Pickles\Router(); $this->expectOutputString('{"status":"error","message":"The test field is required."}'); } diff --git a/tests/classes/SortTest.php b/tests/SortTest.php similarity index 89% rename from tests/classes/SortTest.php rename to tests/SortTest.php index 2f0ba46..ed916f2 100644 --- a/tests/classes/SortTest.php +++ b/tests/SortTest.php @@ -20,7 +20,7 @@ class SortTest extends PHPUnit_Framework_TestCase ['name' => 'gamma'], ]; - Sort::by('name', $shuffled); + Pickles\Sort::by('name', $shuffled); $this->assertEquals($sorted, $shuffled); } @@ -43,7 +43,7 @@ class SortTest extends PHPUnit_Framework_TestCase ['name' => 'alpha'], ]; - Sort::by('name', $shuffled, Sort::DESC); + Pickles\Sort::by('name', $shuffled, Pickles\Sort::DESC); $this->assertEquals($sorted, $shuffled); } @@ -53,7 +53,7 @@ class SortTest extends PHPUnit_Framework_TestCase $shuffled = [['foo' => 'bar', 'bar' => 'foo']]; $sorted = [['foo' => 'bar', 'bar' => 'foo']]; - Sort::by('name', $shuffled); + Pickles\Sort::by('name', $shuffled); $this->assertEquals($sorted, $shuffled); } diff --git a/tests/classes/StringTest.php b/tests/StringTest.php similarity index 53% rename from tests/classes/StringTest.php rename to tests/StringTest.php index b91112d..b1d56f0 100644 --- a/tests/classes/StringTest.php +++ b/tests/StringTest.php @@ -7,7 +7,7 @@ class StringTest extends PHPUnit_Framework_TestCase */ public function testFormatPhoneNumber($a, $b) { - $this->assertEquals(String::formatPhoneNumber($a), $b); + $this->assertEquals(Pickles\String::formatPhoneNumber($a), $b); } public function providerFormatPhoneNumber() @@ -27,35 +27,35 @@ class StringTest extends PHPUnit_Framework_TestCase public function testIsEmpty() { - $this->assertTrue(String::isEmpty('')); - $this->assertTrue(String::isEmpty(' ')); - $this->assertTrue(String::isEmpty(false)); - $this->assertTrue(String::isEmpty(null)); - $this->assertTrue(String::isEmpty(true, false)); + $this->assertTrue(Pickles\String::isEmpty('')); + $this->assertTrue(Pickles\String::isEmpty(' ')); + $this->assertTrue(Pickles\String::isEmpty(false)); + $this->assertTrue(Pickles\String::isEmpty(null)); + $this->assertTrue(Pickles\String::isEmpty(true, false)); - $this->assertFalse(String::isEmpty(0)); - $this->assertFalse(String::isEmpty('foo')); - $this->assertFalse(String::isEmpty(' bar ')); - $this->assertFalse(String::isEmpty(true)); + $this->assertFalse(Pickles\String::isEmpty(0)); + $this->assertFalse(Pickles\String::isEmpty('foo')); + $this->assertFalse(Pickles\String::isEmpty(' bar ')); + $this->assertFalse(Pickles\String::isEmpty(true)); } public function testRandom() { - $this->assertEquals(strlen(String::random()), 8); - $this->assertEquals(strlen(String::random(16)), 16); + $this->assertEquals(strlen(Pickles\String::random()), 8); + $this->assertEquals(strlen(Pickles\String::random(16)), 16); - $this->assertEquals(preg_match('/[a-z0-9]/', String::random(32, true, true)), 1); - $this->assertEquals(preg_match('/[a-z]/', String::random(32, true, false)), 1); - $this->assertEquals(preg_match('/[0-9]/', String::random(32, false, true)), 1); + $this->assertEquals(preg_match('/[a-z0-9]/', Pickles\String::random(32, true, true)), 1); + $this->assertEquals(preg_match('/[a-z]/', Pickles\String::random(32, true, false)), 1); + $this->assertEquals(preg_match('/[0-9]/', Pickles\String::random(32, false, true)), 1); - $this->assertEquals(preg_match('/[0-9]/', String::random(32, true, false)), 0); - $this->assertEquals(preg_match('/[a-z]/', String::random(32, false, true)), 0); - $this->assertEquals(preg_match('/[a-z0-9]/', String::random(32, false, false)), 0); + $this->assertEquals(preg_match('/[0-9]/', Pickles\String::random(32, true, false)), 0); + $this->assertEquals(preg_match('/[a-z]/', Pickles\String::random(32, false, true)), 0); + $this->assertEquals(preg_match('/[a-z0-9]/', Pickles\String::random(32, false, false)), 0); } public function testRandomSimilarFalse() { - $this->assertRegExp('/[a-hj-np-z2-9]{8}/', String::random(8, true, true, false)); + $this->assertRegExp('/[a-hj-np-z2-9]{8}/', Pickles\String::random(8, true, true, false)); } /** @@ -63,7 +63,7 @@ class StringTest extends PHPUnit_Framework_TestCase */ public function testTruncate($a, $b, $c, $d) { - $this->assertEquals(String::truncate($a, $b, $c), $d); + $this->assertEquals(Pickles\String::truncate($a, $b, $c), $d); } public function providerTruncate() @@ -81,7 +81,7 @@ class StringTest extends PHPUnit_Framework_TestCase */ public function testUpperWords($a, $b) { - $this->assertEquals(String::upperWords($a), $b); + $this->assertEquals(Pickles\String::upperWords($a), $b); } public function providerUpperWords() @@ -100,7 +100,7 @@ class StringTest extends PHPUnit_Framework_TestCase */ public function testGenerateSlug($a, $b) { - $this->assertEquals($b, String::generateSlug($a)); + $this->assertEquals($b, Pickles\String::generateSlug($a)); } public function providerGenerateSlug() @@ -116,10 +116,10 @@ class StringTest extends PHPUnit_Framework_TestCase public function testPluralize() { - $this->assertEquals('test', String::pluralize('test', 1, false)); - $this->assertEquals('1 test', String::pluralize('test', 1, true)); - $this->assertEquals('tests', String::pluralize('test', 2, false)); - $this->assertEquals('2 tests', String::pluralize('test', 2, true)); + $this->assertEquals('test', Pickles\String::pluralize('test', 1, false)); + $this->assertEquals('1 test', Pickles\String::pluralize('test', 1, true)); + $this->assertEquals('tests', Pickles\String::pluralize('test', 2, false)); + $this->assertEquals('2 tests', Pickles\String::pluralize('test', 2, true)); } } diff --git a/tests/TimeTest.php b/tests/TimeTest.php new file mode 100644 index 0000000..cdcc040 --- /dev/null +++ b/tests/TimeTest.php @@ -0,0 +1,160 @@ +assertEquals(18, Pickles\Time::age(date('Y-m-d', strtotime('-18 years')))); + } + + public function testAgeFutureTime() + { + $this->assertEquals(-18, Pickles\Time::age(date('Y-m-d', strtotime('18 years')))); + } + + public function testAgeWrongFormat() + { + $this->assertEquals(17, Pickles\Time::age(date('Ymd', strtotime('December 31st -18 years')))); + } + + public function testAgoJustNow() + { + $this->assertEquals('just now', Pickles\Time::ago(Pickles\Time::timestamp())); + } + + public function testAgoPastTimeSeconds() + { + $this->assertEquals('seconds ago', Pickles\Time::ago(strtotime('-30 seconds'))); + } + + public function testAgoPastTimeMinute() + { + $this->assertEquals('a minute ago', Pickles\Time::ago(strtotime('-1 minutes'))); + } + + public function testAgoPastTimeMinutes() + { + $this->assertEquals('5 minutes ago', Pickles\Time::ago(strtotime('-5 minutes'))); + } + + public function testAgoPastTimeHour() + { + $this->assertEquals('an hour ago', Pickles\Time::ago(strtotime('-1 hours'))); + } + + public function testAgoPastTimeHours() + { + $this->assertEquals('2 hours ago', Pickles\Time::ago(strtotime('-2 hours'))); + } + + public function testAgoPastTimeDay() + { + $this->assertEquals('a day ago', Pickles\Time::ago(strtotime('-1 days'))); + } + + public function testAgoPastTimeDays() + { + $this->assertEquals('2 days ago', Pickles\Time::ago(strtotime('-2 days'))); + } + + public function testAgoPastTimeWeek() + { + $this->assertEquals('a week ago', Pickles\Time::ago(strtotime('-1 weeks'))); + } + + public function testAgoPastTimeWeeks() + { + $this->assertEquals('2 weeks ago', Pickles\Time::ago(strtotime('-2 weeks'))); + } + + public function testAgoPastTimeMonth() + { + $this->assertEquals('a month ago', Pickles\Time::ago(strtotime('-1 months'))); + } + + public function testAgoPastTimeMonths() + { + $this->assertEquals('2 months ago', Pickles\Time::ago(strtotime('-2 months'))); + } + + public function testAgoPastTimeYear() + { + $this->assertEquals('a year ago', Pickles\Time::ago(strtotime('-1 years'))); + } + + public function testAgoPastTimeYears() + { + $this->assertEquals('2 years ago', Pickles\Time::ago(strtotime('-2 years'))); + } + + public function testAgoFutureTimeSeconds() + { + $this->assertEquals('seconds from now', Pickles\Time::ago(strtotime('+30 seconds'))); + } + + public function testAgoFutureTimeMinutes() + { + $this->assertEquals('5 minutes from now', Pickles\Time::ago(strtotime('+5 minutes'))); + } + + public function testAgoFutureTimeHours() + { + $this->assertEquals('an hour from now', Pickles\Time::ago(strtotime('+1 hour'))); + } + + public function testAgoFutureTimeDays() + { + $this->assertEquals('a day from now', Pickles\Time::ago(strtotime('+1 day'))); + } + + public function testAgoFutureTimeWeeks() + { + $this->assertEquals('a week from now', Pickles\Time::ago(strtotime('+1 week'))); + } + + public function testAgoFutureTimeMonths() + { + $this->assertEquals('a month from now', Pickles\Time::ago(strtotime('+1 month'))); + } + + public function testAgoFutureTimeYears() + { + $this->assertEquals('a year from now', Pickles\Time::ago(strtotime('+1 year'))); + } + + public function testTimestamp() + { + $this->assertEquals(gmdate('Y-m-d H:i:s'), Pickles\Time::timestamp()); + } + + public function testRoundUpHour() + { + $this->assertEquals('an hour ago', Pickles\Time::ago(strtotime('-59 minutes -55 seconds'))); + } + + public function testRoundUpDay() + { + $this->assertEquals('a day ago', Pickles\Time::ago(strtotime('-23 hours -55 minutes'))); + } + + public function testRoundUpWeek() + { + $this->assertEquals('a week ago', Pickles\Time::ago(strtotime('-6 days -23 hours'))); + } + + public function testRoundUpMonth() + { + $this->assertEquals('a month ago', Pickles\Time::ago(strtotime('-29 days'))); + } + + public function testRoundUpYear() + { + $this->assertEquals('a year ago', Pickles\Time::ago(strtotime('-364 days'))); + } +} + diff --git a/tests/ValidateTest.php b/tests/ValidateTest.php new file mode 100644 index 0000000..8a2cf11 --- /dev/null +++ b/tests/ValidateTest.php @@ -0,0 +1,199 @@ +assertTrue(Validate::isValid(true, ['filter:boolean' => 'error'])); +// } +// +// public function testFilterBooleanError() +// { +// $this->assertEquals(['error'], Validate::isValid(false, ['filter:boolean' => 'error'])); +// } +// +// public function testFilterEmail() +// { +// $this->assertTrue(Validate::isValid('foo@bar.com', ['filter:email' => 'error'])); +// } +// +// public function testFilterEmailError() +// { +// $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:email' => 'error'])); +// } +// +// public function testFilterFloat() +// { +// $this->assertTrue(Validate::isValid(2.231981, ['filter:float' => 'error'])); +// } +// +// public function testFilterFloatError() +// { +// $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:float' => 'error'])); +// } +// +// public function testFilterInt() +// { +// $this->assertTrue(Validate::isValid(2231981, ['filter:int' => 'error'])); +// } +// +// public function testFilterIntError() +// { +// $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:int' => 'error'])); +// } +// +// public function testFilterIP() +// { +// $this->assertTrue(Validate::isValid('2.23.19.81', ['filter:ip' => 'error'])); +// } +// +// public function testFilterIPError() +// { +// $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:ip' => 'error'])); +// } +// +// public function testFilterURL() +// { +// $this->assertTrue(Validate::isValid('http://foo.com/bar?stuff', ['filter:url' => 'error'])); +// } +// +// public function testFilterURLError() +// { +// $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:url' => 'error'])); +// } +// +// /** +// * @expectedException Exception +// * @expectedExceptionMessage Invalid validation rule, expected: "validate:boolean|email|float|int|ip|url". +// */ +// public function testFilterVarInvalidRule() +// { +// Validate::isValid('value', ['filter' => 'foo']); +// } +// +// /** +// * @expectedException Exception +// * @expectedExceptionMessage Invalid filter, expecting boolean, email, float, int, ip or url. +// */ +// public function testFilterVarInvalidFilter() +// { +// Validate::isValid('value', ['filter:foo' => 'bar']); +// } +// +// public function testLengthLessThan() +// { +// $this->assertTrue(Validate::isValid('value', ['length:<:10' => 'error'])); +// } +// +// public function testLengthLessThanError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['length:<:1' => 'error'])); +// } +// +// public function testLengthLessThanOrEqual() +// { +// $this->assertTrue(Validate::isValid('value', ['length:<=:10' => 'error'])); +// } +// +// public function testLengthLessThanOrEqualError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['length:<=:1' => 'error'])); +// } +// +// public function testLengthEqual() +// { +// $this->assertTrue(Validate::isValid('value', ['length:==:5' => 'error'])); +// } +// +// public function testLengthEqualError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['length:==:1' => 'error'])); +// } +// +// public function testLengthNotEqual() +// { +// $this->assertTrue(Validate::isValid('value', ['length:!=:1' => 'error'])); +// } +// +// public function testLengthNotEqualError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['length:!=:5' => 'error'])); +// } +// +// public function testLengthGreaterThanOrEqual() +// { +// $this->assertTrue(Validate::isValid('value', ['length:>=:1' => 'error'])); +// } +// +// public function testLengthGreaterThanOrEqualError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['length:>=:10' => 'error'])); +// } +// +// public function testLengthGreaterThan() +// { +// $this->assertTrue(Validate::isValid('value', ['length:>:1' => 'error'])); +// } +// +// public function testLengthGreaterThanError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['length:>:10' => 'error'])); +// } +// +// /** +// * @expectedException Exception +// * @expectedExceptionMessage Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer". +// */ +// public function testLengthInvalidRule() +// { +// Validate::isValid('value', ['length:16' => 'bar']); +// } +// +// /** +// * @expectedException Exception +// * @expectedExceptionMessage Invalid length value, expecting an integer. +// */ +// public function testLengthInvalidLength() +// { +// Validate::isValid('value', ['length:==:foo' => 'bar']); +// } +// +// /** +// * @expectedException Exception +// * @expectedExceptionMessage Invalid operator, expecting <, <=, ==, !=, >= or >. +// */ +// public function testLengthInvalidOperator() +// { +// Validate::isValid('value', ['length:&&:10' => 'foo']); +// } +// +// public function testRegexIs() +// { +// $this->assertTrue(Validate::isValid('value', ['regex:is:/^va7ue$/' => 'error'])); +// } +// +// public function testRegexIsError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['regex:is:/^value$/' => 'error'])); +// } +// +// public function testRegexNot() +// { +// $this->assertTrue(Validate::isValid('value', ['regex:not:/^value$/' => 'error'])); +// } +// +// public function testRegexNotError() +// { +// $this->assertEquals(['error'], Validate::isValid('value', ['regex:not:/^va7ue$/' => 'error'])); +// } +// +// /** +// * @expectedException Exception +// * @expectedExceptionMessage Invalid validation rule, expected: "regex:is|not:string". +// */ +// public function testRegexInvalidRule() +// { +// Validate::isValid('value', ['regex:/foo/' => 'bar']); +// } +// } + diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 2499300..e3b85a4 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,10 +1,5 @@ assertEquals(0.621371, Distance::kilometersToMiles(1)); - } - - public function testConvertKilometersToMeters() - { - $this->assertEquals(1000, Distance::kilometersToMeters(1)); - } - - public function testConvertKilometersToYards() - { - $this->assertEquals(1093.61, Distance::kilometersToYards(1)); - } - - public function testConvertMilesToKilometers() - { - $this->assertEquals(1.60934, Distance::milesToKilometers(1)); - } - - public function testConvertMilesToMeters() - { - $this->assertEquals(1609.34, Distance::milesToMeters(1)); - } - - public function testConvertMilesToYards() - { - $this->assertEquals(1760, Distance::milesToYards(1)); - } - - public function testConvertMetersToKilometers() - { - $this->assertEquals(0.001, Distance::metersToKilometers(1)); - } - - public function testConvertMetersToMiles() - { - $this->assertEquals(0.000621371, Distance::metersToMiles(1)); - } - - public function testConvertMetersToYards() - { - $this->assertEquals(1.09361, Distance::metersToYards(1)); - } - - public function testCalculateDistanceMiles() - { - $this->assertEquals(1003.2646776326, Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94)); - } - - public function testCalculateDistanceKilometers() - { - $this->assertEquals(1614.5939763012, Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94, 'kilometers')); - } - - public function testCalculateDistanceMeters() - { - $this->assertEquals(1614593.9763012, Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94, 'meters'), '', 0.2); - } - - public function testCalculateDistanceYards() - { - $this->assertEquals(1765745.8326334, Distance::calculateDistance(27.947222, -82.458611, 40.67, -73.94, 'yards'), '', 0.2); - } - - public function testNotEnoughUnits() - { - $this->assertFalse(Distance::milesTo(123)); - } -} - diff --git a/tests/classes/ObjectTest.php b/tests/classes/ObjectTest.php deleted file mode 100644 index 063c541..0000000 --- a/tests/classes/ObjectTest.php +++ /dev/null @@ -1,24 +0,0 @@ -assertInstanceOf('Config', PHPUnit_Framework_Assert::readAttribute($object, 'config')); - } - - public function testConstructorWithObjects() - { - $object = new Object('cache'); - - $this->assertInstanceOf('Cache', PHPUnit_Framework_Assert::readAttribute($object, 'cache')); - } - - public function testGetInstanceWithoutClass() - { - $this->assertFalse(Object::getInstance()); - } -} - diff --git a/tests/classes/ProfilerTest.php b/tests/classes/ProfilerTest.php deleted file mode 100644 index 7645db3..0000000 --- a/tests/classes/ProfilerTest.php +++ /dev/null @@ -1,81 +0,0 @@ -expectOutputRegex('//'); - - Profiler::report(); - } - - public function testDisabledType() - { - $config = Config::getInstance(); - $config->data['pickles']['profiler'] = false; - - $this->assertFalse(Profiler::enabled('timers')); - } - - public function testTimerDisabled() - { - $this->assertFalse(Profiler::timer('disabled')); - } - - public function testReportNothing() - { - $this->expectOutputRegex('/There is nothing to profile/'); - - Profiler::report(); - } - - public function testEnabled() - { - $config = Config::getInstance(); - $config->data['pickles']['profiler'] = true; - - $this->assertTrue(Profiler::enabled()); - } - - public function testEnabledType() - { - $config = Config::getInstance(); - $config->data['pickles']['profiler'] = 'timers'; - - $this->assertTrue(Profiler::enabled('timers')); - } - - public function testLogAndTimer() - { - Profiler::log('timer', 'timer-one'); - Profiler::log(['foo' => 'bar']); - Profiler::log(new Object); - Profiler::log('string'); - Profiler::log(3.14, 'method', true); - Profiler::log('timer', 'timer-one'); - } - - public function testLogQuery() - { - $explain = [ - [ - 'key' => '', - 'possible_keys' => '', - 'type' => '', - 'rows' => '', - 'Extra' => '', - ], - ]; - - Profiler::logQuery('SELECT * FROM table;'); - Profiler::logQuery('SELECT * FROM table WHERE column = ?;', ['foo']); - Profiler::logQuery('SELECT * FROM table;', false, $explain); - } - - public function testTimer() - { - Profiler::timer('timer-two'); - Profiler::timer('timer-two'); - } -} - diff --git a/tests/classes/TimeTest.php b/tests/classes/TimeTest.php deleted file mode 100644 index 82e0a03..0000000 --- a/tests/classes/TimeTest.php +++ /dev/null @@ -1,160 +0,0 @@ -assertEquals(18, Time::age(date('Y-m-d', strtotime('-18 years')))); - } - - public function testAgeFutureTime() - { - $this->assertEquals(-18, Time::age(date('Y-m-d', strtotime('18 years')))); - } - - public function testAgeWrongFormat() - { - $this->assertEquals(17, Time::age(date('Ymd', strtotime('December 31st -18 years')))); - } - - public function testAgoJustNow() - { - $this->assertEquals('just now', Time::ago(Time::timestamp())); - } - - public function testAgoPastTimeSeconds() - { - $this->assertEquals('seconds ago', Time::ago(strtotime('-30 seconds'))); - } - - public function testAgoPastTimeMinute() - { - $this->assertEquals('a minute ago', Time::ago(strtotime('-1 minutes'))); - } - - public function testAgoPastTimeMinutes() - { - $this->assertEquals('5 minutes ago', Time::ago(strtotime('-5 minutes'))); - } - - public function testAgoPastTimeHour() - { - $this->assertEquals('an hour ago', Time::ago(strtotime('-1 hours'))); - } - - public function testAgoPastTimeHours() - { - $this->assertEquals('2 hours ago', Time::ago(strtotime('-2 hours'))); - } - - public function testAgoPastTimeDay() - { - $this->assertEquals('a day ago', Time::ago(strtotime('-1 days'))); - } - - public function testAgoPastTimeDays() - { - $this->assertEquals('2 days ago', Time::ago(strtotime('-2 days'))); - } - - public function testAgoPastTimeWeek() - { - $this->assertEquals('a week ago', Time::ago(strtotime('-1 weeks'))); - } - - public function testAgoPastTimeWeeks() - { - $this->assertEquals('2 weeks ago', Time::ago(strtotime('-2 weeks'))); - } - - public function testAgoPastTimeMonth() - { - $this->assertEquals('a month ago', Time::ago(strtotime('-1 months'))); - } - - public function testAgoPastTimeMonths() - { - $this->assertEquals('2 months ago', Time::ago(strtotime('-2 months'))); - } - - public function testAgoPastTimeYear() - { - $this->assertEquals('a year ago', Time::ago(strtotime('-1 years'))); - } - - public function testAgoPastTimeYears() - { - $this->assertEquals('2 years ago', Time::ago(strtotime('-2 years'))); - } - - public function testAgoFutureTimeSeconds() - { - $this->assertEquals('seconds from now', Time::ago(strtotime('+30 seconds'))); - } - - public function testAgoFutureTimeMinutes() - { - $this->assertEquals('5 minutes from now', Time::ago(strtotime('+5 minutes'))); - } - - public function testAgoFutureTimeHours() - { - $this->assertEquals('an hour from now', Time::ago(strtotime('+1 hour'))); - } - - public function testAgoFutureTimeDays() - { - $this->assertEquals('a day from now', Time::ago(strtotime('+1 day'))); - } - - public function testAgoFutureTimeWeeks() - { - $this->assertEquals('a week from now', Time::ago(strtotime('+1 week'))); - } - - public function testAgoFutureTimeMonths() - { - $this->assertEquals('a month from now', Time::ago(strtotime('+1 month'))); - } - - public function testAgoFutureTimeYears() - { - $this->assertEquals('a year from now', Time::ago(strtotime('+1 year'))); - } - - public function testTimestamp() - { - $this->assertEquals(gmdate('Y-m-d H:i:s'), Time::timestamp()); - } - - public function testRoundUpHour() - { - $this->assertEquals('an hour ago', Time::ago(strtotime('-59 minutes -55 seconds'))); - } - - public function testRoundUpDay() - { - $this->assertEquals('a day ago', Time::ago(strtotime('-23 hours -55 minutes'))); - } - - public function testRoundUpWeek() - { - $this->assertEquals('a week ago', Time::ago(strtotime('-6 days -23 hours'))); - } - - public function testRoundUpMonth() - { - $this->assertEquals('a month ago', Time::ago(strtotime('-29 days'))); - } - - public function testRoundUpYear() - { - $this->assertEquals('a year ago', Time::ago(strtotime('-364 days'))); - } -} - diff --git a/tests/classes/ValidateTest.php b/tests/classes/ValidateTest.php deleted file mode 100644 index cf8d447..0000000 --- a/tests/classes/ValidateTest.php +++ /dev/null @@ -1,199 +0,0 @@ -assertTrue(Validate::isValid(true, ['filter:boolean' => 'error'])); - } - - public function testFilterBooleanError() - { - $this->assertEquals(['error'], Validate::isValid(false, ['filter:boolean' => 'error'])); - } - - public function testFilterEmail() - { - $this->assertTrue(Validate::isValid('foo@bar.com', ['filter:email' => 'error'])); - } - - public function testFilterEmailError() - { - $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:email' => 'error'])); - } - - public function testFilterFloat() - { - $this->assertTrue(Validate::isValid(2.231981, ['filter:float' => 'error'])); - } - - public function testFilterFloatError() - { - $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:float' => 'error'])); - } - - public function testFilterInt() - { - $this->assertTrue(Validate::isValid(2231981, ['filter:int' => 'error'])); - } - - public function testFilterIntError() - { - $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:int' => 'error'])); - } - - public function testFilterIP() - { - $this->assertTrue(Validate::isValid('2.23.19.81', ['filter:ip' => 'error'])); - } - - public function testFilterIPError() - { - $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:ip' => 'error'])); - } - - public function testFilterURL() - { - $this->assertTrue(Validate::isValid('http://foo.com/bar?stuff', ['filter:url' => 'error'])); - } - - public function testFilterURLError() - { - $this->assertEquals(['error'], Validate::isValid('invalid', ['filter:url' => 'error'])); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Invalid validation rule, expected: "validate:boolean|email|float|int|ip|url". - */ - public function testFilterVarInvalidRule() - { - Validate::isValid('value', ['filter' => 'foo']); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Invalid filter, expecting boolean, email, float, int, ip or url. - */ - public function testFilterVarInvalidFilter() - { - Validate::isValid('value', ['filter:foo' => 'bar']); - } - - public function testLengthLessThan() - { - $this->assertTrue(Validate::isValid('value', ['length:<:10' => 'error'])); - } - - public function testLengthLessThanError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['length:<:1' => 'error'])); - } - - public function testLengthLessThanOrEqual() - { - $this->assertTrue(Validate::isValid('value', ['length:<=:10' => 'error'])); - } - - public function testLengthLessThanOrEqualError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['length:<=:1' => 'error'])); - } - - public function testLengthEqual() - { - $this->assertTrue(Validate::isValid('value', ['length:==:5' => 'error'])); - } - - public function testLengthEqualError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['length:==:1' => 'error'])); - } - - public function testLengthNotEqual() - { - $this->assertTrue(Validate::isValid('value', ['length:!=:1' => 'error'])); - } - - public function testLengthNotEqualError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['length:!=:5' => 'error'])); - } - - public function testLengthGreaterThanOrEqual() - { - $this->assertTrue(Validate::isValid('value', ['length:>=:1' => 'error'])); - } - - public function testLengthGreaterThanOrEqualError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['length:>=:10' => 'error'])); - } - - public function testLengthGreaterThan() - { - $this->assertTrue(Validate::isValid('value', ['length:>:1' => 'error'])); - } - - public function testLengthGreaterThanError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['length:>:10' => 'error'])); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer". - */ - public function testLengthInvalidRule() - { - Validate::isValid('value', ['length:16' => 'bar']); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Invalid length value, expecting an integer. - */ - public function testLengthInvalidLength() - { - Validate::isValid('value', ['length:==:foo' => 'bar']); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Invalid operator, expecting <, <=, ==, !=, >= or >. - */ - public function testLengthInvalidOperator() - { - Validate::isValid('value', ['length:&&:10' => 'foo']); - } - - public function testRegexIs() - { - $this->assertTrue(Validate::isValid('value', ['regex:is:/^va7ue$/' => 'error'])); - } - - public function testRegexIsError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['regex:is:/^value$/' => 'error'])); - } - - public function testRegexNot() - { - $this->assertTrue(Validate::isValid('value', ['regex:not:/^value$/' => 'error'])); - } - - public function testRegexNotError() - { - $this->assertEquals(['error'], Validate::isValid('value', ['regex:not:/^va7ue$/' => 'error'])); - } - - /** - * @expectedException Exception - * @expectedExceptionMessage Invalid validation rule, expected: "regex:is|not:string". - */ - public function testRegexInvalidRule() - { - Validate::isValid('value', ['regex:/foo/' => 'bar']); - } -} -