Skip to content

Commit

Permalink
Update task tests. Update Main controller tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
wadmiraal committed Sep 8, 2014
1 parent 55c9857 commit bb87621
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 10 deletions.
29 changes: 25 additions & 4 deletions src/Tudu/Controller/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,39 @@ public function inboxAction(Application $app, Request $request)
// Update the sender message ID.
$todo->addParticipant($email->getFromAddress(), $email->getFromName(), $email->getMessageID());

// If any new persons were Cc'ed, add them as well.
$recipients = $email->getRecipients();

if (!empty($recipients)) {
foreach ($recipients as $recipient) {
$todo->addParticipant($recipient['address'], $recipient['name'], $email->getMessageID());
}
}

// Extract the action and parameter.
list($action, $parameter) = Parser::extractAction($email->getBody());

switch ($action) {
case Parser::ADD:
// @todo
$todo->addTask($parameter);
break;

case Parser::DELETE:
// @todo
try {
$todo->removeTask($parameter);
} catch (\Exception $e) {}
break;

case Parser::DONE:
try {
$todo->setTaskState($parameter, true);
} catch (\Exception $e) {}
break;

case Parser::RESET:
// @todo
try {
$todo->setTaskState($parameter, false);
} catch (\Exception $e) {}
break;

default:
Expand All @@ -101,9 +120,11 @@ public function inboxAction(Application $app, Request $request)
break;
}

$todo->save();

Notifier::notify($todo);

return new Response('Todo list with id ' . $todo->getID() . ' updated', 200);
return new Response('Todo list updated, [id:' . $todo->getID() . ']', 200);
break;

default:
Expand Down
18 changes: 17 additions & 1 deletion src/Tudu/Model/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function loadFromDBRow(array $row)
}

/**
* Save a participant.
* Save a task.
*/
public function save()
{
Expand Down Expand Up @@ -272,4 +272,20 @@ public function save()

$stmt->execute();
}

/**
* Remove the task.
*/
public function remove()
{
$stmt = $this->connection->prepare("
DELETE FROM tasks
WHERE todo_id = :todo_id AND num = :num
");

$stmt->bindValue('todo_id', $this->todoID);
$stmt->bindValue('num', (int) $this->num);

$stmt->execute();
}
}
32 changes: 29 additions & 3 deletions src/Tudu/Model/Todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public function addParticipant($email, $name, $lastMessageID = null)
if (!empty($this->participants)) {
foreach ($this->participants as $participant) {
if ($participant->getEmail() === $email) {
if ($lastMessageID) {
$participant->setLastMessageID($lastMessageID);
}
return;
}
}
Expand Down Expand Up @@ -320,7 +323,7 @@ public function getTasks() {
*
* @return Tudu\Model\Task
*
* @throw \OutOfBoundsException
* @throw \OutOfRangeException
*/
public function getTask($num)
{
Expand All @@ -330,9 +333,32 @@ public function getTask($num)
return $task;
}
}
} else {
throw new \OutOfBoundsException("The task $num does not exist.");
}

throw new \OutOfRangeException("The task $num does not exist.");
}

/**
* Remove a specific task.
*
* @param int $num
* The number of the task.
*
* @throw \OutOfRangeException
*/
public function removeTask($num)
{
if (!empty($this->tasks)) {
foreach ($this->tasks as $i => $task) {
if ($task->getNum() == $num) {
$task->remove();
unset($this->tasks[$i]);
return;
}
}
}

throw new \OutOfRangeException("The task $num does not exist.");
}

/**
Expand Down
153 changes: 151 additions & 2 deletions src/Tudu/Tests/Controller/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ protected function tearDown()
}

/**
* Text CloudMailin "new" list callback.
* Text CloudMailin inbox list callback.
*/
public function testCloudMailinNewInbox()
public function testCloudMailinInbox()
{
$client = $this->createClient();
$tests = array(
Expand Down Expand Up @@ -104,6 +104,7 @@ public function testCloudMailinNewInbox()
),
),
);

foreach ($tests as $label => $data) {
$crawler = $client->request('POST', '/inbox', $data['post']);
$this->assertTrue($client->getResponse()->isSuccessful(), "Request is successful.");
Expand Down Expand Up @@ -132,5 +133,153 @@ public function testCloudMailinNewInbox()

$this->assertEquals($tasks, $data['expected']['tasks'], 'Added correct tasks.');
}

$todo = new Todo($this->model->getDBDriver());
$todo->addParticipant('test@bot.com', 'Test Bot');
$todo->setTitle('My new list');
$todo->setOwner('test@bot.com');
$todo->addTask('Do stuff');
$todo->save();

$client = $this->createClient();
$tests = array(
'Add a new task' => array(
'post' => array(
'plain' => 'Add: This is a new task',
'headers' => array(
'From' => 'Test Bot <test@bot.com>',
'To' => 'please-reply@tuduapp.com',
'Cc' => 'Jimmy <jimmy@test.com>',
'Message-ID' => 'message--id',
'Subject' => 'My new list [id:' . $todo->getID() . ']',
),
),
'expected' => array(
'tasks' => array(
'Do stuff' => false,
'This is a new task' => false,
),
'participants' => array(
array(
'email' => 'test@bot.com',
'name' => 'Test Bot',
),
array(
'email' => 'jimmy@test.com',
'name' => 'Jimmy',
),
),
),
),
'Set a task as done' => array(
'post' => array(
'plain' => 'Done 2',
'headers' => array(
'From' => 'Test Bot <test@bot.com>',
'To' => 'please-reply@tuduapp.com',
'Cc' => '',
'Message-ID' => 'message--id',
'Subject' => 'My new list [id:' . $todo->getID() . ']',
),
),
'expected' => array(
'tasks' => array(
'Do stuff' => false,
'This is a new task' => true,
),
'participants' => array(
array(
'email' => 'test@bot.com',
'name' => 'Test Bot',
),
array(
'email' => 'jimmy@test.com',
'name' => 'Jimmy',
),
),
),
),
'Set a task as "not" done' => array(
'post' => array(
'plain' => 'Reset 2',
'headers' => array(
'From' => 'Test Bot <test@bot.com>',
'To' => 'please-reply@tuduapp.com',
'Cc' => '',
'Message-ID' => 'message--id',
'Subject' => 'My new list [id:' . $todo->getID() . ']',
),
),
'expected' => array(
'tasks' => array(
'Do stuff' => false,
'This is a new task' => false,
),
'participants' => array(
array(
'email' => 'test@bot.com',
'name' => 'Test Bot',
),
array(
'email' => 'jimmy@test.com',
'name' => 'Jimmy',
),
),
),
),
'Delete a task' => array(
'post' => array(
'plain' => 'Delete 1',
'headers' => array(
'From' => 'Test Bot <test@bot.com>',
'To' => 'please-reply@tuduapp.com',
'Cc' => '',
'Message-ID' => 'message--id',
'Subject' => 'My new list [id:' . $todo->getID() . ']',
),
),
'expected' => array(
'tasks' => array(
'This is a new task' => false,
),
'participants' => array(
array(
'email' => 'test@bot.com',
'name' => 'Test Bot',
),
array(
'email' => 'jimmy@test.com',
'name' => 'Jimmy',
),
),
),
),
);

foreach ($tests as $label => $data) {
$crawler = $client->request('POST', '/inbox', $data['post']);
$this->assertTrue($client->getResponse()->isSuccessful(), "Request is successful.");
$this->assertContains('Todo list updated', $client->getResponse()->getContent(), "The response contains the message saying the list was created.");

$todo2 = new Todo($this->model->getDBDriver());
$todo2->load($todo->getID());

$participants = array();
foreach ($todo2->getParticipants() as $participant) {
$participants[] = array(
'email' => $participant->getEmail(),
'name' => $participant->getName(),
);
}

$this->assertEquals($participants, $data['expected']['participants'], 'Added correct participants.');

$tasks = array();
foreach ($todo2->getTasks() as $task) {
$tasks[$task->getTask()] = $task->getDone();
}

$this->assertEquals($tasks, $data['expected']['tasks'], 'Added correct tasks.');
}
}
}
13 changes: 13 additions & 0 deletions src/Tudu/Tests/Model/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,18 @@ public function testDB()
$rows = $stmt->fetchAll();
$task2->loadFromDBRow($rows[0]);
$this->assertEquals($task, $task2, 'Loading loads the same attributes.');

// Test removing.
$task2->remove();
$stmt = $this->connection->prepare("
SELECT *
FROM tasks
WHERE todo_id = :id AND num = :num
");
$stmt->bindValue('id', $task->getTodoID());
$stmt->bindValue('num', $task->getNum());
$stmt->execute();
$rows = $stmt->fetchAll();
$this->assertEquals(0, count($rows), 'Removing the task deletes it from the database.');
}
}
12 changes: 12 additions & 0 deletions src/Tudu/Tests/Model/TodoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,17 @@ public function testDB()
// Check.
$this->assertEquals(false, $todo2->getTask(1)->getDone(), 'Test setting a task done state.');
$this->assertEquals(true, $todo2->getTask(2)->getDone(), 'Test setting a task done state.');

// Deleting tasks.
$todo->removeTask(1);
$todo->save();

$this->setExpectedException('OutOfRangeException', 'The task 1 does not exist.');
$todo->getTask(1);

$todo2->load($todo->getID());

$this->setExpectedException('OutOfRangeException', 'The task 1 does not exist.');
$todo2->getTask(1);
}
}

0 comments on commit bb87621

Please sign in to comment.