Working on tests for the Model class

Fixed some bugs, got MySQL setup for Travis.
This commit is contained in:
Joshua Sherman 2014-01-16 17:06:31 -05:00
parent 200988eecf
commit 51467a60f7
9 changed files with 209 additions and 52 deletions

View file

@ -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:

View file

@ -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;

View file

@ -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));

View file

@ -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);
@ -638,12 +632,15 @@ class Model extends Object
{
// @todo Move to Memcached extension and switch to use setMulti()
foreach ($this->records as $record)
{
if (isset($record['id']))
{
$this->cache->set(strtoupper($this->model) . '-' . $record['id'], $record);
}
}
}
}
}
$index_records = in_array($type_or_parameters, ['list', 'indexed']);
@ -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
*

View file

@ -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)

View file

@ -44,4 +44,15 @@ function setUpRequest($request, $method = 'GET')
$_REQUEST['request'] = $request;
}
function setUpConfig($config)
{
file_put_contents(
SITE_PATH . 'config.php',
'<?php $config = ' . var_export($config, true) . '; ?>'
);
}
`mysql -e 'TRUNCATE TABLE test.pickles;'`;
`echo 'flush_all' | nc localhost 11211`;
?>

View file

@ -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 = '<?php $config = ' . var_export($config, true) . '; ?>';
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();
}
}

View file

@ -1,7 +1,59 @@
<?php
class MockModelWithoutColumns extends Model
{
protected $table = 'pickles';
protected $columns = false;
}
class MockModel extends Model
{
protected $table = 'pickles';
protected $columns = ['created_at' => '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']);
}
}
}
?>

16
tests/schema.sql Normal file
View file

@ -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;