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