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 Container instance.
38 * @var History controller instance.
40 protected $controller;
43 * Before every test, instantiate a new Api with its config, plugins and links.
45 public function setUp()
47 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
48 $this->refHistory
= new \
ReferenceHistory();
49 $this->refHistory
->write(self
::$testHistory);
50 $this->container
= new Container();
51 $this->container
['conf'] = $this->conf
;
52 $this->container
['db'] = true;
53 $this->container
['history'] = new \
History(self
::$testHistory);
55 $this->controller
= new History($this->container
);
59 * After every test, remove the test datastore.
61 public function tearDown()
63 @unlink(self
::$testHistory);
67 * Test /history service without parameter.
69 public function testGetHistory()
71 $env = Environment
::mock([
72 'REQUEST_METHOD' => 'GET',
74 $request = Request
::createFromEnvironment($env);
76 $response = $this->controller
->getHistory($request, new Response());
77 $this->assertEquals(200, $response->getStatusCode());
78 $data = json_decode((string) $response->getBody(), true);
80 $this->assertEquals($this->refHistory
->count(), count($data));
82 $this->assertEquals(\History
::DELETED
, $data[0]['event']);
84 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
87 $this->assertEquals(124, $data[0]['id']);
89 $this->assertEquals(\History
::SETTINGS
, $data[1]['event']);
91 \DateTime
::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime
::ATOM
),
94 $this->assertNull($data[1]['id']);
96 $this->assertEquals(\History
::UPDATED
, $data[2]['event']);
98 \DateTime
::createFromFormat('Ymd_His', '20170301_121214')->format(\DateTime
::ATOM
),
101 $this->assertEquals(123, $data[2]['id']);
103 $this->assertEquals(\History
::CREATED
, $data[3]['event']);
105 \DateTime
::createFromFormat('Ymd_His', '20170201_121214')->format(\DateTime
::ATOM
),
108 $this->assertEquals(124, $data[3]['id']);
110 $this->assertEquals(\History
::CREATED
, $data[4]['event']);
112 \DateTime
::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime
::ATOM
),
115 $this->assertEquals(123, $data[4]['id']);
119 * Test /history service with limit parameter.
121 public function testGetHistoryLimit()
123 $env = Environment
::mock([
124 'REQUEST_METHOD' => 'GET',
125 'QUERY_STRING' => 'limit=1'
127 $request = Request
::createFromEnvironment($env);
129 $response = $this->controller
->getHistory($request, new Response());
130 $this->assertEquals(200, $response->getStatusCode());
131 $data = json_decode((string) $response->getBody(), true);
133 $this->assertEquals(1, count($data));
135 $this->assertEquals(\History
::DELETED
, $data[0]['event']);
137 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
140 $this->assertEquals(124, $data[0]['id']);
144 * Test /history service with offset parameter.
146 public function testGetHistoryOffset()
148 $env = Environment
::mock([
149 'REQUEST_METHOD' => 'GET',
150 'QUERY_STRING' => 'offset=4'
152 $request = Request
::createFromEnvironment($env);
154 $response = $this->controller
->getHistory($request, new Response());
155 $this->assertEquals(200, $response->getStatusCode());
156 $data = json_decode((string) $response->getBody(), true);
158 $this->assertEquals(1, count($data));
160 $this->assertEquals(\History
::CREATED
, $data[0]['event']);
162 \DateTime
::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime
::ATOM
),
165 $this->assertEquals(123, $data[0]['id']);
169 * Test /history service with since parameter.
171 public function testGetHistorySince()
173 $env = Environment
::mock([
174 'REQUEST_METHOD' => 'GET',
175 'QUERY_STRING' => 'since=2017-03-03T00:00:00%2B00:00'
177 $request = Request
::createFromEnvironment($env);
179 $response = $this->controller
->getHistory($request, new Response());
180 $this->assertEquals(200, $response->getStatusCode());
181 $data = json_decode((string) $response->getBody(), true);
183 $this->assertEquals(1, count($data));
185 $this->assertEquals(\History
::DELETED
, $data[0]['event']);
187 \DateTime
::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime
::ATOM
),
190 $this->assertEquals(124, $data[0]['id']);
194 * Test /history service with since parameter.
196 public function testGetHistorySinceOffsetLimit()
198 $env = Environment
::mock([
199 'REQUEST_METHOD' => 'GET',
200 'QUERY_STRING' => 'since=2017-02-01T00:00:00%2B00:00&offset=1&limit=1'
202 $request = Request
::createFromEnvironment($env);
204 $response = $this->controller
->getHistory($request, new Response());
205 $this->assertEquals(200, $response->getStatusCode());
206 $data = json_decode((string) $response->getBody(), true);
208 $this->assertEquals(1, count($data));
210 $this->assertEquals(\History
::SETTINGS
, $data[0]['event']);
212 \DateTime
::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime
::ATOM
),