aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api
diff options
context:
space:
mode:
Diffstat (limited to 'tests/api')
-rw-r--r--tests/api/controllers/HistoryTest.php221
1 files changed, 221 insertions, 0 deletions
diff --git a/tests/api/controllers/HistoryTest.php b/tests/api/controllers/HistoryTest.php
new file mode 100644
index 00000000..21e9c0ba
--- /dev/null
+++ b/tests/api/controllers/HistoryTest.php
@@ -0,0 +1,221 @@
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
6
7use Shaarli\Config\ConfigManager;
8use Slim\Container;
9use Slim\Http\Environment;
10use Slim\Http\Request;
11use Slim\Http\Response;
12
13require_once 'tests/utils/ReferenceHistory.php';
14
15class HistoryTest extends \PHPUnit_Framework_TestCase
16{
17 /**
18 * @var string datastore to test write operations
19 */
20 protected static $testHistory = 'sandbox/history.php';
21
22 /**
23 * @var ConfigManager instance
24 */
25 protected $conf;
26
27 /**
28 * @var \ReferenceHistory instance.
29 */
30 protected $refHistory = null;
31
32 /**
33 * @var \History instance.
34 */
35 protected $history;
36
37 /**
38 * @var Container instance.
39 */
40 protected $container;
41
42 /**
43 * @var History controller instance.
44 */
45 protected $controller;
46
47 /**
48 * Before every test, instantiate a new Api with its config, plugins and links.
49 */
50 public function setUp()
51 {
52 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
53 $this->refHistory = new \ReferenceHistory();
54 $this->refHistory->write(self::$testHistory);
55 $this->conf->set('resource.history', self::$testHistory);
56 $this->container = new Container();
57 $this->container['conf'] = $this->conf;
58 $this->container['db'] = true;
59
60 $this->controller = new History($this->container);
61 }
62
63 /**
64 * After every test, remove the test datastore.
65 */
66 public function tearDown()
67 {
68 @unlink(self::$testHistory);
69 }
70
71 /**
72 * Test /history service without parameter.
73 */
74 public function testGetHistory()
75 {
76 $env = Environment::mock([
77 'REQUEST_METHOD' => 'GET',
78 ]);
79 $request = Request::createFromEnvironment($env);
80
81 $response = $this->controller->getHistory($request, new Response());
82 $this->assertEquals(200, $response->getStatusCode());
83 $data = json_decode((string) $response->getBody(), true);
84
85 $this->assertEquals($this->refHistory->count(), count($data));
86
87 $this->assertEquals(\History::DELETED, $data[0]['event']);
88 $this->assertEquals(
89 \DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
90 $data[0]['datetime']
91 );
92 $this->assertEquals(124, $data[0]['id']);
93
94 $this->assertEquals(\History::SETTINGS, $data[1]['event']);
95 $this->assertEquals(
96 \DateTime::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime::ATOM),
97 $data[1]['datetime']
98 );
99 $this->assertNull($data[1]['id']);
100
101 $this->assertEquals(\History::UPDATED, $data[2]['event']);
102 $this->assertEquals(
103 \DateTime::createFromFormat('Ymd_His', '20170301_121214')->format(\DateTime::ATOM),
104 $data[2]['datetime']
105 );
106 $this->assertEquals(123, $data[2]['id']);
107
108 $this->assertEquals(\History::CREATED, $data[3]['event']);
109 $this->assertEquals(
110 \DateTime::createFromFormat('Ymd_His', '20170201_121214')->format(\DateTime::ATOM),
111 $data[3]['datetime']
112 );
113 $this->assertEquals(124, $data[3]['id']);
114
115 $this->assertEquals(\History::CREATED, $data[4]['event']);
116 $this->assertEquals(
117 \DateTime::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime::ATOM),
118 $data[4]['datetime']
119 );
120 $this->assertEquals(123, $data[4]['id']);
121 }
122
123 /**
124 * Test /history service with limit parameter.
125 */
126 public function testGetHistoryLimit()
127 {
128 $env = Environment::mock([
129 'REQUEST_METHOD' => 'GET',
130 'QUERY_STRING' => 'limit=1'
131 ]);
132 $request = Request::createFromEnvironment($env);
133
134 $response = $this->controller->getHistory($request, new Response());
135 $this->assertEquals(200, $response->getStatusCode());
136 $data = json_decode((string) $response->getBody(), true);
137
138 $this->assertEquals(1, count($data));
139
140 $this->assertEquals(\History::DELETED, $data[0]['event']);
141 $this->assertEquals(
142 \DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
143 $data[0]['datetime']
144 );
145 $this->assertEquals(124, $data[0]['id']);
146 }
147
148 /**
149 * Test /history service with offset parameter.
150 */
151 public function testGetHistoryOffset()
152 {
153 $env = Environment::mock([
154 'REQUEST_METHOD' => 'GET',
155 'QUERY_STRING' => 'offset=4'
156 ]);
157 $request = Request::createFromEnvironment($env);
158
159 $response = $this->controller->getHistory($request, new Response());
160 $this->assertEquals(200, $response->getStatusCode());
161 $data = json_decode((string) $response->getBody(), true);
162
163 $this->assertEquals(1, count($data));
164
165 $this->assertEquals(\History::CREATED, $data[0]['event']);
166 $this->assertEquals(
167 \DateTime::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime::ATOM),
168 $data[0]['datetime']
169 );
170 $this->assertEquals(123, $data[0]['id']);
171 }
172
173 /**
174 * Test /history service with since parameter.
175 */
176 public function testGetHistorySince()
177 {
178 $env = Environment::mock([
179 'REQUEST_METHOD' => 'GET',
180 'QUERY_STRING' => 'since=2017-03-03T00:00:00%2B00:00'
181 ]);
182 $request = Request::createFromEnvironment($env);
183
184 $response = $this->controller->getHistory($request, new Response());
185 $this->assertEquals(200, $response->getStatusCode());
186 $data = json_decode((string) $response->getBody(), true);
187
188 $this->assertEquals(1, count($data));
189
190 $this->assertEquals(\History::DELETED, $data[0]['event']);
191 $this->assertEquals(
192 \DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
193 $data[0]['datetime']
194 );
195 $this->assertEquals(124, $data[0]['id']);
196 }
197
198 /**
199 * Test /history service with since parameter.
200 */
201 public function testGetHistorySinceOffsetLimit()
202 {
203 $env = Environment::mock([
204 'REQUEST_METHOD' => 'GET',
205 'QUERY_STRING' => 'since=2017-02-01T00:00:00%2B00:00&offset=1&limit=1'
206 ]);
207 $request = Request::createFromEnvironment($env);
208
209 $response = $this->controller->getHistory($request, new Response());
210 $this->assertEquals(200, $response->getStatusCode());
211 $data = json_decode((string) $response->getBody(), true);
212
213 $this->assertEquals(1, count($data));
214
215 $this->assertEquals(\History::SETTINGS, $data[0]['event']);
216 $this->assertEquals(
217 \DateTime::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime::ATOM),
218 $data[0]['datetime']
219 );
220 }
221}