diff --git a/.travis.yml b/.travis.yml index bc6af93..7454350 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,8 @@ before_script: - echo "extension = memcached.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - echo "extension = redis.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - echo "zend_extension = test_helpers.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini + - mysql -e 'create database test;' + - mysql test < tests/schema.sql - mkdir -p vendors/build/logs script: diff --git a/classes/Database.php b/classes/Database.php index 41358af..290d014 100644 --- a/classes/Database.php +++ b/classes/Database.php @@ -163,7 +163,6 @@ class Database extends Object if (!isset($datasource['driver'])) { - var_Dump($datasource); throw new Exception('The specified datasource lacks a driver.'); } @@ -315,7 +314,7 @@ class Database extends Object { $this->open(); - if ($this->config->pickles['logging'] === true) + if (isset($this->config->pickles['logging']) && $this->config->pickles['logging']) { $loggable_query = $sql; diff --git a/classes/Log.php b/classes/Log.php index e97b2d0..a474688 100644 --- a/classes/Log.php +++ b/classes/Log.php @@ -118,7 +118,7 @@ class Log { $config = Config::getInstance(); - if ($config->pickles['logging'] === true) + if (isset($config->pickles['logging']) && $config->pickles['logging']) { $log_path = LOG_PATH . date('Y/m/d/', ($time == false ? time() : $time)); diff --git a/classes/Model.php b/classes/Model.php index 24bf5e3..dfeb9ce 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -333,12 +333,6 @@ class Model extends Object 'is_deleted' => 'is_deleted', ]; - // Grabs the config columns if no columns are set - if ($this->columns === null && isset($this->db->columns)) - { - $this->columns = $this->db->columns; - } - // Sets all but the `id` column to false if ($this->columns === false) { @@ -594,7 +588,7 @@ class Model extends Object (count($this->input_parameters) == 0 ? null : $this->input_parameters) ); - if (isset($partial_cache)) + if (isset($partial_cache) && count($this->records) > 1) { $records = array_merge($partial_cache, $this->records); @@ -639,7 +633,10 @@ class Model extends Object // @todo Move to Memcached extension and switch to use setMulti() foreach ($this->records as $record) { - $this->cache->set(strtoupper($this->model) . '-' . $record['id'], $record); + if (isset($record['id'])) + { + $this->cache->set(strtoupper($this->model) . '-' . $record['id'], $record); + } } } } @@ -1680,25 +1677,6 @@ class Model extends Object return false; } - /** - * Unescape String - * - * Assuming magic quotes is turned on, strips slashes from the string. - * - * @access protected - * @param string $value string to be unescaped - * @return string unescaped string - */ - protected function unescape($value) - { - if (get_magic_quotes_gpc()) - { - $value = stripslashes($value); - } - - return $value; - } - /** * Field Values * diff --git a/classes/Profiler.php b/classes/Profiler.php index 6693216..ed2c976 100644 --- a/classes/Profiler.php +++ b/classes/Profiler.php @@ -78,7 +78,7 @@ class Profiler public static function enabled(/* polymorphic */) { $config = Config::getInstance(); - $config = $config->pickles['profiler']; + $config = isset($config->pickles['profiler']) ? $config->pickles['profiler'] : false; // Checks if we're set to boolean true if ($config === true) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3ef39e0..e466aff 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -44,4 +44,15 @@ function setUpRequest($request, $method = 'GET') $_REQUEST['request'] = $request; } +function setUpConfig($config) +{ + file_put_contents( + SITE_PATH . 'config.php', + '' + ); +} + +`mysql -e 'TRUNCATE TABLE test.pickles;'`; +`echo 'flush_all' | nc localhost 11211`; + ?> diff --git a/tests/classes/ConfigTest.php b/tests/classes/ConfigTest.php index 2a56d24..3aa377c 100644 --- a/tests/classes/ConfigTest.php +++ b/tests/classes/ConfigTest.php @@ -2,25 +2,16 @@ class ConfigTest extends PHPUnit_Framework_TestCase { - private $config_file; private $config; public function setUp() { - $this->config_file = SITE_PATH . 'config.php'; $this->config = Config::getInstance(); - $this->createConfigFile([]); + setupConfig([]); $_SERVER['REQUEST_METHOD'] = 'GET'; } - private function createConfigFile($config) - { - $config = ''; - - file_put_contents($this->config_file, $config); - } - public function testConfigProperty() { $config = new Config(); @@ -40,7 +31,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testDefinedEnvironment() { - $this->createConfigFile([ + setUpConfig([ 'environment' => 'local', ]); @@ -51,7 +42,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testMultipleEnvironmentsByIP() { - $this->createConfigFile([ + setUpConfig([ 'environments' => [ 'local' => '127.0.0.1', 'prod' => '123.456.789.0', @@ -65,7 +56,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testMultipleEnvironmentsByRegex() { - $this->createConfigFile([ + setUpConfig([ 'environments' => [ 'local' => '/^local\.testsite\.com$/', 'prod' => '/^testsite\.com$/', @@ -82,7 +73,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase unset($_SERVER['REQUEST_METHOD']); $_SERVER['argv'][1] = 'prod'; - $this->createConfigFile([ + setUpConfig([ 'environments' => [ 'local' => '127.0.0.1', 'prod' => '123.456.789.0', @@ -103,14 +94,14 @@ class ConfigTest extends PHPUnit_Framework_TestCase unset($_SERVER['REQUEST_METHOD']); $_SERVER['argc'] = 1; - $this->createConfigFile(['environments' => []]); + setUpConfig(['environments' => []]); $config = new Config(); } public function testProfiler() { - $this->createConfigFile([ + setUpConfig([ 'environment' => 'local', 'pickles' => ['profiler' => true], ]); @@ -122,7 +113,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testProfilerArray() { - $this->createConfigFile([ + setUpConfig([ 'environment' => 'local', 'pickles' => ['profiler' => ['objects', 'timers']], ]); @@ -134,7 +125,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testProfilerForceTrue() { - $this->createConfigFile([ + setUpConfig([ 'environment' => 'local', 'pickles' => ['profiler' => ['unknown']], ]); @@ -146,7 +137,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase public function testSecurityConstant() { - $this->createConfigFile([ + setUpConfig([ 'environment' => 'local', 'security' => ['levels' => [10 => 'level']], ]); @@ -162,7 +153,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase */ public function testSecurityConstantAlreadyDefined() { - $this->createConfigFile([ + setUpConfig([ 'environment' => 'local', 'security' => ['levels' => [10 => 'level']], ]); @@ -175,7 +166,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase // This test is just for coverage public function testConfigArrayMissing() { - file_put_contents($this->config_file, ''); + file_put_contents(SITE_PATH . 'config.php', ''); new Config(); } } diff --git a/tests/classes/ModelTest.php b/tests/classes/ModelTest.php index ec47da9..53c2cd2 100644 --- a/tests/classes/ModelTest.php +++ b/tests/classes/ModelTest.php @@ -1,7 +1,59 @@ 'created_at']; +} + class ModelTest extends PHPUnit_Framework_TestCase { + public static function setUpBeforeClass() + { + $config = Config::getInstance(); + + $config->data = [ + 'pickles' => [ + 'datasource' => 'mysql', + 'cache' => 'memcache', + ], + 'datasources' => [ + 'mysql' => [ + 'type' => 'mysql', + 'driver' => 'pdo_mysql', + 'hostname' => 'localhost', + 'username' => '', + 'password' => '', + 'database' => 'test', + 'cache' => true, + ], + 'memcache' => [ + 'type' => 'memcache', + 'hostname' => 'localhost', + 'port' => 11211, + 'namespace' => '', + ], + ], + ]; + + for ($i = 0; $i < 5; $i++) + { + $model = new MockModel(); + $model->record['field1'] = 'one'; + $model->record['field2'] = 'two'; + $model->record['field3'] = 'three'; + $model->record['field4'] = 'four'; + $model->record['field5'] = 'five'; + $model->commit(); + } + } + /** * @expectedException Exception * @expectedExceptionMessage You must set the table variable @@ -10,6 +62,114 @@ class ModelTest extends PHPUnit_Framework_TestCase { new Model(); } + + public function testWithoutColumns() + { + $model = new MockModelWithoutColumns(); + $columns = PHPUnit_Framework_Assert::readAttribute($model, 'columns'); + + $this->assertFalse($columns['is_deleted']); + } + + public function testWithColumns() + { + $model = new MockModel(); + $columns = PHPUnit_Framework_Assert::readAttribute($model, 'columns'); + + $this->assertEquals('is_deleted', $columns['is_deleted']); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage You cannot pass in 2 query parameter arrays + */ + public function testDoubleArray() + { + $model = new MockModel(['foo' => 'bar'], ['test' => 'ing']); + } + + public function testFetchInt() + { + $model = new MockModel(1); + $this->assertEquals(1, $model->count()); + $this->assertEquals(1, $model->record['id']); + } + + public function testFetchIntArray() + { + $model = new MockModel([1, 2, 3]); + $this->assertEquals(3, $model->count()); + } + + public function testFetchConditionsID() + { + $model = new MockModel(['conditions' => ['id' => 1]]); + $this->assertEquals(1, $model->count()); + $this->assertEquals(1, $model->record['id']); + } + + public function testFetchCount() + { + $model = new MockModel('count'); + $this->assertEquals(5, $model->record['count']); + } + + public function testFetchCountConditions() + { + $model = new MockModel('count', ['conditions' => ['id' => [1, 3, 5]]]); + $this->assertEquals(3, $model->record['count']); + } + + public function testSort() + { + $model = new MockModel(); + $this->assertTrue($model->sort('id')); + } + + public function testShuffle() + { + $model = new MockModel(); + $this->assertTrue($model->shuffle()); + } + + public function testNextPrev() + { + $model = new MockModel('all'); + $model->next(); + $this->assertEquals(2, $model->record['id']); + $model->prev(); + $this->assertEquals(1, $model->record['id']); + } + + public function testLastFirst() + { + $model = new MockModel('all'); + $model->last(); + $this->assertEquals(5, $model->record['id']); + $model->first(); + $this->assertEquals(1, $model->record['id']); + } + + public function testEndReset() + { + $model = new MockModel('all'); + $model->end(); + $this->assertEquals(5, $model->record['id']); + $model->reset(); + $this->assertEquals(1, $model->record['id']); + } + + public function testWalk() + { + $model = new MockModel('all'); + $expected = 0; + + while ($model->walk()) + { + $expected++; + $this->assertEquals($expected, $model->record['id']); + } + } } ?> diff --git a/tests/schema.sql b/tests/schema.sql new file mode 100644 index 0000000..1810adc --- /dev/null +++ b/tests/schema.sql @@ -0,0 +1,16 @@ +CREATE TABLE `pickles` ( + `id` int(1) unsigned NOT NULL AUTO_INCREMENT, + `field1` varchar(100) COLLATE utf8_unicode_ci NOT NULL, + `field2` varchar(100) COLLATE utf8_unicode_ci NOT NULL, + `field3` varchar(100) COLLATE utf8_unicode_ci NOT NULL, + `field4` varchar(100) COLLATE utf8_unicode_ci NOT NULL, + `field5` varchar(100) COLLATE utf8_unicode_ci NOT NULL, + `created_id` int(1) unsigned DEFAULT NULL, + `created_at` datetime NOT NULL, + `updated_id` int(1) unsigned DEFAULT NULL, + `updated_at` datetime DEFAULT NULL, + `deleted_id` int(1) unsigned DEFAULT NULL, + `deleted_at` datetime DEFAULT NULL, + `is_deleted` tinyint(1) unsigned DEFAULT '0', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;