3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
12 require_once 'tests/utils/ReferenceHistory.php';
14 class HistoryTest
extends \PHPUnit\Framework\TestCase
17 * @var string datastore to test write operations
19 protected static $testHistory = 'sandbox/history.php';
22 * @var ConfigManager instance
27 * @var \ReferenceHistory instance.
29 protected $refHistory = null;
32 * @var Container instance.
37 * @var HistoryController controller instance.
39 protected $controller;
42 * Before every test, instantiate a new Api with its config, plugins and links.
44 public function setUp()
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);
54 $this->controller
= new HistoryController($this->container
);
58 * After every test, remove the test datastore.
60 public function tearDown()
62 @unlink(self
::$testHistory);
66 * Test /history service without parameter.
68 public function testGetHistory()
70 $env = Environment
::mock([
71 'REQUEST_METHOD' => 'GET',
73 $request = Request
::createFromEnvironment($env);
75 $response = $this->controller
->getHistory($request, new Response());
76 $this->assertEquals(200, $response->getStatusCode());
77 $data = json_decode((string) $response->getBody(), true);
79 $this->assertEquals($this->refHistory
->count(), count($data));
81 $this->assertEquals(History
::DELETED
, $data[0]['event']);
83 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
86 $this->assertEquals(124, $data[0]['id']);
88 $this->assertEquals(History
::SETTINGS
, $data[1]['event']);
90 \DateTime
::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime
::ATOM
),
93 $this->assertNull($data[1]['id']);
95 $this->assertEquals(History
::UPDATED
, $data[2]['event']);
97 \DateTime
::createFromFormat('Ymd_His', '20170301_121214')->format(\DateTime
::ATOM
),
100 $this->assertEquals(123, $data[2]['id']);
102 $this->assertEquals(History
::CREATED
, $data[3]['event']);
104 \DateTime
::createFromFormat('Ymd_His', '20170201_121214')->format(\DateTime
::ATOM
),
107 $this->assertEquals(124, $data[3]['id']);
109 $this->assertEquals(History
::CREATED
, $data[4]['event']);
111 \DateTime
::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime
::ATOM
),
114 $this->assertEquals(123, $data[4]['id']);
118 * Test /history service with limit parameter.
120 public function testGetHistoryLimit()
122 $env = Environment
::mock([
123 'REQUEST_METHOD' => 'GET',
124 'QUERY_STRING' => 'limit=1'
126 $request = Request
::createFromEnvironment($env);
128 $response = $this->controller
->getHistory($request, new Response());
129 $this->assertEquals(200, $response->getStatusCode());
130 $data = json_decode((string) $response->getBody(), true);
132 $this->assertEquals(1, count($data));
134 $this->assertEquals(History
::DELETED
, $data[0]['event']);
136 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
139 $this->assertEquals(124, $data[0]['id']);
143 * Test /history service with offset parameter.
145 public function testGetHistoryOffset()
147 $env = Environment
::mock([
148 'REQUEST_METHOD' => 'GET',
149 'QUERY_STRING' => 'offset=4'
151 $request = Request
::createFromEnvironment($env);
153 $response = $this->controller
->getHistory($request, new Response());
154 $this->assertEquals(200, $response->getStatusCode());
155 $data = json_decode((string) $response->getBody(), true);
157 $this->assertEquals(1, count($data));
159 $this->assertEquals(History
::CREATED
, $data[0]['event']);
161 \DateTime
::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime
::ATOM
),
164 $this->assertEquals(123, $data[0]['id']);
168 * Test /history service with since parameter.
170 public function testGetHistorySince()
172 $env = Environment
::mock([
173 'REQUEST_METHOD' => 'GET',
174 'QUERY_STRING' => 'since=2017-03-03T00:00:00%2B00:00'
176 $request = Request
::createFromEnvironment($env);
178 $response = $this->controller
->getHistory($request, new Response());
179 $this->assertEquals(200, $response->getStatusCode());
180 $data = json_decode((string) $response->getBody(), true);
182 $this->assertEquals(1, count($data));
184 $this->assertEquals(History
::DELETED
, $data[0]['event']);
186 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
189 $this->assertEquals(124, $data[0]['id']);
193 * Test /history service with since parameter.
195 public function testGetHistorySinceOffsetLimit()
197 $env = Environment
::mock([
198 'REQUEST_METHOD' => 'GET',
199 'QUERY_STRING' => 'since=2017-02-01T00:00:00%2B00:00&offset=1&limit=1'
201 $request = Request
::createFromEnvironment($env);
203 $response = $this->controller
->getHistory($request, new Response());
204 $this->assertEquals(200, $response->getStatusCode());
205 $data = json_decode((string) $response->getBody(), true);
207 $this->assertEquals(1, count($data));
209 $this->assertEquals(History
::SETTINGS
, $data[0]['event']);
211 \DateTime
::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime
::ATOM
),