Skip to content

Commit

Permalink
Any closer and it would be done
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshtronic committed Jan 20, 2014
1 parent db6e169 commit 54cb6df
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 23 deletions.
13 changes: 1 addition & 12 deletions classes/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1194,8 +1194,6 @@ public function commit()
$sql .= ', ' . $values;
}

$record_field_count = count($record);

foreach ($record as $variable => $value)
{
$input_parameters[] = (is_array($value) ? json_encode($value) : $value);
Expand All @@ -1205,19 +1203,12 @@ public function commit()
if ($this->columns['created_at'] != false)
{
$input_parameters[] = Time::timestamp();
$record_field_count++;
}

// @todo Check if the column was passed in
if ($this->columns['created_id'] != false && isset($_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.');
}
}
}
Expand Down Expand Up @@ -1402,9 +1393,7 @@ public function commit()
// Executes the query
if ($this->postgresql && $update == false)
{
$results = $this->db->fetch($sql, $input_parameters);

return $results[0][$this->columns['id']];
return $this->db->fetch($sql, $input_parameters);
}
else
{
Expand Down
157 changes: 146 additions & 11 deletions tests/classes/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,17 @@ public function testCommitSingleRecord()
$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()
{
# $value = String::random();
# $model = new MockModel(1);
# $model->replace = true;
# $model->record['field1'] = $value;
# $model->commit();
# $model = new MockModel(1);
# $this->assertEquals($value, $model->record['field1']);
$value = String::random();
$model = new MockModel(1);
$model->replace = true;
$model->record['field1'] = $value;
$model->commit();
$model = new MockModel(1);
}

public function testCommitInsertPriority()
Expand Down Expand Up @@ -454,20 +456,119 @@ public function testCommitReplaceIgnore()
$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()
{
$_SESSION['__pickles']['security']['user_id'] = 1;
$model = new MockModel(1);
$model->delete();
$model = new MockModelWithoutColumns(1);
$this->assertEquals(1, $model->record['is_deleted']);
$model = new MockModel(1);
$this->assertEquals([], $model->record);
}

public function testDeleteActual()
{
$model = new MockModelWithoutColumns(1);
$model = new MockModelWithoutColumns(2);
$model->delete();
$model = new MockModelWithoutColumns(1);
$model = new MockModelWithoutColumns(2);
$this->assertEquals(0, $model->count());
}

Expand All @@ -482,6 +583,40 @@ public function testLoadParametersWithString()
$model = new MockModel();
$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();
# }
}

?>

0 comments on commit 54cb6df

Please sign in to comment.