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

@ -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']);
}
}
}
?>