Skip to content

Commit 294b751

Browse files
committed
Added PHPUnit tests.
1 parent 419066e commit 294b751

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

tests/TogglTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* The Toggl API token for the testing account.
5+
*/
6+
define('TOGGL_TEST_TOKEN', 'd37a6e2ebf3a5e2f0bb2a579725360f2');
7+
8+
// Include PHPUnit
9+
require_once 'PHPUnit/Framework/TestCase.php';
10+
11+
// Include the Toggl class file.
12+
require_once dirname(dirname(__FILE__)) . '/toggl.php';
13+
14+
class TogglTest extends PHPUnit_Framework_TestCase {
15+
protected $toggl;
16+
17+
protected function setUp() {
18+
$this->toggl = new Toggl(TOGGL_TEST_TOKEN);
19+
}
20+
21+
public function testConstruct() {
22+
$this->assertClassHasAttribute('token', 'Toggl');
23+
$this->assertSame($this->toggl->getToken(), TOGGL_TEST_TOKEN);
24+
}
25+
26+
/**
27+
* @expectedException TogglException
28+
* @expectedExceptionMessage Invalid parameters for getTimeEntries.
29+
*/
30+
public function testTimeEntriesInvalidParameters() {
31+
$this->toggl->timeEntriesLoadRecent(0, NULL);
32+
}
33+
34+
/**
35+
* @expectedException TogglException
36+
* @expectedExceptionMessage Start date cannot be after the end date.
37+
*/
38+
public function testTimeEntriesInvalidDates() {
39+
$this->toggl->timeEntriesLoadRecent(1, 0);
40+
}
41+
42+
public function testWorkspaces() {
43+
$response = $this->toggl->workspaceLoadAll();
44+
$this->assertSame($response->code, 200);
45+
$this->assertSame(count($response->data->data), 2);
46+
}
47+
48+
public function testClients() {
49+
$response = $this->toggl->clientLoadAll();
50+
$this->assertSame($response->code, 200);
51+
$this->assertSame(count($response->data->data), 3);
52+
}
53+
54+
public function testUser() {
55+
$account = $this->toggl->userLoad();
56+
$this->assertSame($account->api_token, TOGGL_TEST_TOKEN);
57+
$this->assertSame($account->fullname, 'Toggl Tester');
58+
$this->assertSame($account->email, '[email protected]');
59+
$this->assertSame($account->id, 202215);
60+
}
61+
}

toggl.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ protected function request($url, array $options = array()) {
112112
*/
113113
public function timeEntriesLoadRecent($start_date = NULL, $end_date = NULL) {
114114
if (isset($start_date) != isset($end_date)) {
115-
throw new ToggleException("Invalid parameters for getTimeEntries.");
115+
throw new TogglException("Invalid parameters for getTimeEntries.");
116116
}
117117

118118
$options = array();
@@ -209,7 +209,11 @@ public function tagLoadAll() {
209209
}
210210

211211
public function userLoad() {
212-
return $this->request($this->getURL('me'));
212+
$response = $this->request($this->getURL('me'));
213+
if (!empty($response->data->data)) {
214+
return $response->data->data;
215+
}
216+
return FALSE;
213217
}
214218

215219
public function loadAll($resource) {

0 commit comments

Comments
 (0)