4 namespace Shaarli\Api\Controllers
;
7 use Shaarli\Config\ConfigManager
;
9 use Slim\Http\Environment
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
13 require_once 'tests/utils/ReferenceHistory.php';
15 class HistoryTest
extends \PHPUnit_Framework_TestCase
18 * @var string datastore to test write operations
20 protected static $testHistory = 'sandbox/history.php';
23 * @var ConfigManager instance
28 * @var \ReferenceHistory instance.
30 protected $refHistory = null;
33 * @var \History instance.
38 * @var Container instance.
43 * @var History controller instance.
45 protected $controller;
48 * Before every test, instantiate a new Api with its config, plugins and links.
50 public function setUp()
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;
60 $this->controller
= new History($this->container
);
64 * After every test, remove the test datastore.
66 public function tearDown()
68 @unlink(self
::$testHistory);
72 * Test /history service without parameter.
74 public function testGetHistory()
76 $env = Environment
::mock([
77 'REQUEST_METHOD' => 'GET',
79 $request = Request
::createFromEnvironment($env);
81 $response = $this->controller
->getHistory($request, new Response());
82 $this->assertEquals(200, $response->getStatusCode());
83 $data = json_decode((string) $response->getBody(), true);
85 $this->assertEquals($this->refHistory
->count(), count($data));
87 $this->assertEquals(\History
::DELETED
, $data[0]['event']);
89 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
92 $this->assertEquals(124, $data[0]['id']);
94 $this->assertEquals(\History
::SETTINGS
, $data[1]['event']);
96 \DateTime
::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime
::ATOM
),
99 $this->assertNull($data[1]['id']);
101 $this->assertEquals(\History
::UPDATED
, $data[2]['event']);
103 \DateTime
::createFromFormat('Ymd_His', '20170301_121214')->format(\DateTime
::ATOM
),
106 $this->assertEquals(123, $data[2]['id']);
108 $this->assertEquals(\History
::CREATED
, $data[3]['event']);
110 \DateTime
::createFromFormat('Ymd_His', '20170201_121214')->format(\DateTime
::ATOM
),
113 $this->assertEquals(124, $data[3]['id']);
115 $this->assertEquals(\History
::CREATED
, $data[4]['event']);
117 \DateTime
::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime
::ATOM
),
120 $this->assertEquals(123, $data[4]['id']);
124 * Test /history service with limit parameter.
126 public function testGetHistoryLimit()
128 $env = Environment
::mock([
129 'REQUEST_METHOD' => 'GET',
130 'QUERY_STRING' => 'limit=1'
132 $request = Request
::createFromEnvironment($env);
134 $response = $this->controller
->getHistory($request, new Response());
135 $this->assertEquals(200, $response->getStatusCode());
136 $data = json_decode((string) $response->getBody(), true);
138 $this->assertEquals(1, count($data));
140 $this->assertEquals(\History
::DELETED
, $data[0]['event']);
142 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
145 $this->assertEquals(124, $data[0]['id']);
149 * Test /history service with offset parameter.
151 public function testGetHistoryOffset()
153 $env = Environment
::mock([
154 'REQUEST_METHOD' => 'GET',
155 'QUERY_STRING' => 'offset=4'
157 $request = Request
::createFromEnvironment($env);
159 $response = $this->controller
->getHistory($request, new Response());
160 $this->assertEquals(200, $response->getStatusCode());
161 $data = json_decode((string) $response->getBody(), true);
163 $this->assertEquals(1, count($data));
165 $this->assertEquals(\History
::CREATED
, $data[0]['event']);
167 \DateTime
::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime
::ATOM
),
170 $this->assertEquals(123, $data[0]['id']);
174 * Test /history service with since parameter.
176 public function testGetHistorySince()
178 $env = Environment
::mock([
179 'REQUEST_METHOD' => 'GET',
180 'QUERY_STRING' => 'since=2017-03-03T00:00:00%2B00:00'
182 $request = Request
::createFromEnvironment($env);
184 $response = $this->controller
->getHistory($request, new Response());
185 $this->assertEquals(200, $response->getStatusCode());
186 $data = json_decode((string) $response->getBody(), true);
188 $this->assertEquals(1, count($data));
190 $this->assertEquals(\History
::DELETED
, $data[0]['event']);
192 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
195 $this->assertEquals(124, $data[0]['id']);
199 * Test /history service with since parameter.
201 public function testGetHistorySinceOffsetLimit()
203 $env = Environment
::mock([
204 'REQUEST_METHOD' => 'GET',
205 'QUERY_STRING' => 'since=2017-02-01T00:00:00%2B00:00&offset=1&limit=1'
207 $request = Request
::createFromEnvironment($env);
209 $response = $this->controller
->getHistory($request, new Response());
210 $this->assertEquals(200, $response->getStatusCode());
211 $data = json_decode((string) $response->getBody(), true);
213 $this->assertEquals(1, count($data));
215 $this->assertEquals(\History
::SETTINGS
, $data[0]['event']);
217 \DateTime
::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime
::ATOM
),