Any closer and it would be done

Had to rework the Model class a bit, there's some weirdness happening and I'm
unsure if it's part of the rewrite or always been busted. Won't really know for
sure until I start porting sites over to it I suppose.
This commit is contained in:
Joshua Sherman 2014-01-20 01:06:22 -05:00
parent db6e169f7b
commit 54cb6dfe83
2 changed files with 147 additions and 23 deletions

View file

@ -1194,8 +1194,6 @@ class Model extends Object
$sql .= ', ' . $values; $sql .= ', ' . $values;
} }
$record_field_count = count($record);
foreach ($record as $variable => $value) foreach ($record as $variable => $value)
{ {
$input_parameters[] = (is_array($value) ? json_encode($value) : $value); $input_parameters[] = (is_array($value) ? json_encode($value) : $value);
@ -1205,19 +1203,12 @@ class Model extends Object
if ($this->columns['created_at'] != false) if ($this->columns['created_at'] != false)
{ {
$input_parameters[] = Time::timestamp(); $input_parameters[] = Time::timestamp();
$record_field_count++;
} }
// @todo Check if the column was passed in // @todo Check if the column was passed in
if ($this->columns['created_id'] != false && isset($_SESSION['__pickles']['security']['user_id'])) if ($this->columns['created_id'] != false && isset($_SESSION['__pickles']['security']['user_id']))
{ {
$input_parameters[] = $_SESSION['__pickles']['security']['user_id']; $input_parameters[] = $_SESSION['__pickles']['security']['user_id'];
$record_field_count++;
}
if ($record_field_count != $field_count)
{
throw new Exception('Record does not match the excepted field count.');
} }
} }
} }
@ -1402,9 +1393,7 @@ class Model extends Object
// Executes the query // Executes the query
if ($this->postgresql && $update == false) if ($this->postgresql && $update == false)
{ {
$results = $this->db->fetch($sql, $input_parameters); return $this->db->fetch($sql, $input_parameters);
return $results[0][$this->columns['id']];
} }
else else
{ {

View file

@ -374,15 +374,17 @@ class ModelTest extends PHPUnit_Framework_TestCase
$this->assertEquals($value, $model->record['field1']); $this->assertEquals($value, $model->record['field1']);
} }
// Handles filling coverage gaps but isn't a reliable test. Would need to
// test against a table without a UID column so we can see this in action,
// else it just takes a shit because the ID isn't injected back in.
public function testCommitSingleRecordReplace() public function testCommitSingleRecordReplace()
{ {
# $value = String::random(); $value = String::random();
# $model = new MockModel(1); $model = new MockModel(1);
# $model->replace = true; $model->replace = true;
# $model->record['field1'] = $value; $model->record['field1'] = $value;
# $model->commit(); $model->commit();
# $model = new MockModel(1); $model = new MockModel(1);
# $this->assertEquals($value, $model->record['field1']);
} }
public function testCommitInsertPriority() public function testCommitInsertPriority()
@ -454,20 +456,119 @@ class ModelTest extends PHPUnit_Framework_TestCase
$this->assertEquals($value, $model->record['field1']); $this->assertEquals($value, $model->record['field1']);
} }
public function testCommitMultipleFields()
{
$value1 = String::random();
$value2 = String::random();
$model = new MockModelWithoutColumns(1);
$model->record['field1'] = $value1;
$model->record['field2'] = $value2;
$model->commit();
$model = new MockModelWithoutColumns(1);
$this->assertEquals($value1, $model->record['field1']);
$this->assertEquals($value2, $model->record['field2']);
}
public function testCommitIncrement()
{
$model = new MockModelWithoutColumns(1);
$model->record['field1'] = 100;;
$model->commit();
$model = new MockModelWithoutColumns(1);
$model->record['field1'] = '++';
$model->commit();
$model = new MockModelWithoutColumns(1);
$this->assertEquals(101, $model->record['field1']);
}
public function testCommitUpdatedID()
{
$_SESSION['__pickles']['security']['user_id'] = 1;
$value = String::random();
$model = new MockModel(1);
$model->record['field1'] = $value;
$model->commit();
$model = new MockModel(1);
$this->assertEquals($value, $model->record['field1']);
$this->assertEquals(1, $model->record['updated_id']);
}
public function testCommitCreatedID()
{
$_SESSION['__pickles']['security']['user_id'] = 1;
$value = String::random();
$model = new MockModel();
$model->record['field1'] = $value;
$id = $model->commit();
$model = new MockModel($id);
$this->assertEquals(1, $model->record['created_id']);
}
// Doesn't test against actual PostgreSQL instance, just for valid syntax
public function testCommitInsertPostgreSQL()
{
$_SESSION['__pickles']['security']['user_id'] = 1;
$value = String::random();
$model = new MockModel();
$model->mysql = false;
$model->postgresql = true;
$model->record['field1'] = $value;
try
{
$model->commit();
}
catch (Exception $e)
{
}
$this->assertRegExp('/RETURNING id/', $model->db->results->queryString);
}
// Doesn't test against actual PostgreSQL instance, just for valid syntax
public function testCommitUpdatePostgreSQL()
{
$_SESSION['__pickles']['security']['user_id'] = 1;
$value = String::random();
$model = new MockModel(1);
$model->mysql = false;
$model->postgresql = true;
$model->record['field1'] = $value;
try
{
$model->commit();
}
catch (Exception $e)
{
}
$model = new MockModel(1);
$this->assertEquals($value, $model->record['field1']);
}
public function testCommitNothing()
{
$model = new MockModel();
$this->assertFalse($model->commit());
}
public function testDeleteLogical() public function testDeleteLogical()
{ {
$_SESSION['__pickles']['security']['user_id'] = 1; $_SESSION['__pickles']['security']['user_id'] = 1;
$model = new MockModel(1); $model = new MockModel(1);
$model->delete(); $model->delete();
$model = new MockModelWithoutColumns(1); $model = new MockModel(1);
$this->assertEquals(1, $model->record['is_deleted']); $this->assertEquals([], $model->record);
} }
public function testDeleteActual() public function testDeleteActual()
{ {
$model = new MockModelWithoutColumns(1); $model = new MockModelWithoutColumns(2);
$model->delete(); $model->delete();
$model = new MockModelWithoutColumns(1); $model = new MockModelWithoutColumns(2);
$this->assertEquals(0, $model->count()); $this->assertEquals(0, $model->count());
} }
@ -482,6 +583,40 @@ class ModelTest extends PHPUnit_Framework_TestCase
$model = new MockModel(); $model = new MockModel();
$this->assertFalse($model->loadParameters('')); $this->assertFalse($model->loadParameters(''));
} }
public function testMultipleQueueInsert()
{
$_SESSION['__pickles']['security']['user_id'] = 1;
$model = new MockModel('count');
$count = $model->record['count'];
$model = new MockModel();
for ($i = 0; $i < 5; $i++)
{
$model->record['field1'] = String::random();
$model->record['updated_id'] = 1;
$model->queue();
}
$model->commit();
$model = new MockModel('count');
$this->assertEquals($count + 5, $model->record['count']);
}
# public function testMultipleQueueUpdate()
# {
# $model = new MockModel(['conditions' => ['id <=' => 5]]);
#
# var_dump($model->records);
#
# # while ($model->walk())
# # {
# # $model->record['field1'] = String::random();
# # $model->queue();
# # }
#
# # $model->commit();
# }
} }
?> ?>