Skip to content

Commit

Permalink
updates tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Seago committed Oct 3, 2014
1 parent 5b84d95 commit 05df235
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 39 deletions.
10 changes: 9 additions & 1 deletion MysqlModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,15 @@ public function set($assignments, $collection, $where=null)
foreach ($key as $name) {
array_push($fields, ':'.$name);
}
$sql = "INSERT INTO $collection (".self::stringify($key, true, '`').") VALUES(".implode(',', $fields).")";
$sql = "INSERT INTO $collection (".self::stringify($key, true, '`').") VALUES (".implode(',', $fields).") ON DUPLICATE KEY UPDATE ";
$first = true;
foreach ($key as $field) {
if (!$first) {
$sql .= ',';
}
$sql .= "`$field`=VALUES(`$field`)";
$first = false;
}

if (!is_null($where)) {
// Initializes $where and $params
Expand Down
19 changes: 10 additions & 9 deletions spec/Devtools/MysqlModelSpec.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php

namespace spec\Devtools;
<?php namespace spec\Devtools;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
Expand Down Expand Up @@ -38,13 +36,13 @@ function it_should_perform_PDO_queries(\PDO $connectionMock, \PDOStatement $stmt
)
)->willReturn(true);

$stmtMock->fetch(\PDO::FETCH_ASSOC)
$stmtMock->fetchAll(\PDO::FETCH_ASSOC)
->willReturn(
array('user_name' => 'seagoj')
);

$this->get('user_name', 'users', array('userid' => 1))
->shouldReturn('seagoj');
->shouldReturn(['user_name' => 'seagoj']);
}

function it_should_return_multiple_values(\PDO $connectionMock, \PDOStatement $stmtMock)
Expand All @@ -59,7 +57,7 @@ function it_should_return_multiple_values(\PDO $connectionMock, \PDOStatement $s
)
)->willReturn(true);

$stmtMock->fetch(\PDO::FETCH_ASSOC)
$stmtMock->fetchAll(\PDO::FETCH_ASSOC)
->willReturn(
array(
'user_name' => 'seagoj',
Expand All @@ -73,8 +71,9 @@ function it_should_return_multiple_values(\PDO $connectionMock, \PDOStatement $s

function it_should_set_a_value(\PDO $connectionMock, \PDOStatement $stmtMock)
{
$sql = "INSERT INTO users (`user_name`,`last_name`) VALUES (:user_name,:last_name) ON DUPLICATE KEY UPDATE `user_name`=VALUES(`user_name`),`last_name`=VALUES(`last_name`)";
$connectionMock->prepare(
"INSERT INTO users (`user_name`,`last_name`) VALUES(:user_name,:last_name)"
$sql
)->willReturn($stmtMock);

$stmtMock->execute(
Expand All @@ -84,7 +83,7 @@ function it_should_set_a_value(\PDO $connectionMock, \PDOStatement $stmtMock)
)
)->willReturn(true);

$stmtMock->fetch(\PDO::FETCH_ASSOC)
$stmtMock->fetchAll(\PDO::FETCH_ASSOC)
->willReturn(
array(
'userid' => 1000
Expand All @@ -98,7 +97,9 @@ function it_should_set_a_value(\PDO $connectionMock, \PDOStatement $stmtMock)
),
'users'
)->shouldReturn(
1000
array(
'userid' => 1000
)
);
}
}
29 changes: 0 additions & 29 deletions spec/Devtools/ResponseSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,35 +133,6 @@ function it_is_serializable()
);
}

function it_loads_data_from_model(\Devtools\MysqlModel $modelMock)
{
$modelMock
->query(
'select `user_name`, `last_name` from users where `user_id`=:user_id',
['user_id' => 1],
true
)
->willReturn(['user_name' =>'seagoj', 'last_name' => 'Seago']);

$this
->load(
'select `user_name`, `last_name` from users where `user_id`=:user_id',
['user_id' => 1]
)
->shouldReturn(['user_name' => 'seagoj', 'last_name' => 'Seago']);
$this->json()->shouldReturn(
json_encode(
[
'status' => 'OK',
'request' => $_REQUEST,
'message' => '',
'user_name' => 'seagoj',
'last_name' => 'Seago'
]
)
);
}

function it_sets_and_gets_magically()
{
$this->param1 = 'val1';
Expand Down

0 comments on commit 05df235

Please sign in to comment.