HistoryTest.php 6.7 KB

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