MongoDbTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Cache\Backend;
  7. class MongoDbTest extends \PHPUnit\Framework\TestCase
  8. {
  9. protected $_connectionString;
  10. protected $_dbName = 'magento_integration_test';
  11. /**
  12. * @var \Magento\Framework\Cache\Backend\MongoDb|null
  13. */
  14. protected $_model = null;
  15. protected function setUp()
  16. {
  17. if (defined('MONGODB_CONNECTION_STRING')) {
  18. $this->_connectionString = MONGODB_CONNECTION_STRING;
  19. }
  20. if (empty($this->_connectionString) || !extension_loaded('mongo')) {
  21. $this->markTestSkipped(
  22. "Either 'mongo' extension is not loaded or 'MONGODB_CONNECTION_STRING' constant is not defined"
  23. );
  24. }
  25. if (defined('MONGODB_DATABASE_NAME')) {
  26. $this->_dbName = MONGODB_DATABASE_NAME;
  27. }
  28. $this->_model = new \Magento\Framework\Cache\Backend\MongoDb(
  29. ['connection_string' => $this->_connectionString, 'db' => $this->_dbName]
  30. );
  31. }
  32. protected function tearDown()
  33. {
  34. if (!empty($this->_connectionString) && extension_loaded('mongo')) {
  35. $this->_model = null;
  36. $connection = new \Mongo($this->_connectionString);
  37. $connection->dropDB($this->_dbName);
  38. }
  39. }
  40. /**
  41. * @expectedException \Zend_Cache_Exception
  42. * @expectedExceptionMessage 'db' option is not specified
  43. */
  44. public function testConstructorException()
  45. {
  46. new \Magento\Framework\Cache\Backend\MongoDb();
  47. }
  48. public function testGetIds()
  49. {
  50. $this->assertEmpty($this->_model->getIds());
  51. $this->_model->save('test data 1', 'test1');
  52. $this->_model->save('test data 2', 'test2');
  53. $this->assertEquals(['test1', 'test2'], $this->_model->getIds());
  54. }
  55. public function testGetTags()
  56. {
  57. $this->assertEmpty($this->_model->getTags());
  58. $this->_model->save('test data 1', 'test1', ['tag1', 'tag2']);
  59. $this->_model->save('test data 2', 'test2', ['tag1', 'tag3']);
  60. $actual = $this->_model->getTags();
  61. $expected = ['tag1', 'tag2', 'tag3'];
  62. $this->assertEquals($expected, $actual);
  63. }
  64. /**
  65. * @dataProvider getIdsMatchingTagsDataProvider
  66. */
  67. public function testGetIdsMatchingTags($searchTags, $expectedIds)
  68. {
  69. $this->_prepareCollection();
  70. $actualIds = $this->_model->getIdsMatchingTags($searchTags);
  71. $this->assertEquals($expectedIds, $actualIds);
  72. }
  73. public function getIdsMatchingTagsDataProvider()
  74. {
  75. return [
  76. 'one tag' => [['tag1'], ['test1', 'test2', 'test3']],
  77. 'multiple tags' => [['tag1', 'tag2'], ['test1', 'test3']]
  78. ];
  79. }
  80. /**
  81. * @dataProvider getIdsNotMatchingTagsDataProvider
  82. */
  83. public function testGetIdsNotMatchingTags($searchTags, $expectedIds)
  84. {
  85. $this->_prepareCollection();
  86. $actualIds = $this->_model->getIdsNotMatchingTags($searchTags);
  87. $this->assertEquals($expectedIds, $actualIds);
  88. }
  89. public function getIdsNotMatchingTagsDataProvider()
  90. {
  91. return [
  92. 'one tag' => [['tag2'], ['test2', 'test4', 'test5']],
  93. 'multiple tags' => [['tag1', 'tag2'], ['test4', 'test5']]
  94. ];
  95. }
  96. /**
  97. * @dataProvider getIdsMatchingAnyTagsDataProvider
  98. */
  99. public function testGetIdsMatchingAnyTags($searchTags, $expectedIds)
  100. {
  101. $this->_prepareCollection();
  102. $actualIds = $this->_model->getIdsMatchingAnyTags($searchTags);
  103. $this->assertEquals($expectedIds, $actualIds);
  104. }
  105. public function getIdsMatchingAnyTagsDataProvider()
  106. {
  107. return [
  108. 'no tags' => [[], []],
  109. 'one tag' => [['tag2'], ['test1', 'test3']],
  110. 'multiple tags' => [['tag1', 'tag2'], ['test1', 'test2', 'test3']]
  111. ];
  112. }
  113. public function testGetMetadatas()
  114. {
  115. $cacheId = 'test';
  116. $tags = ['tag_1', 'tag_2'];
  117. $this->_model->save('test data', $cacheId, $tags, 100);
  118. $actualResult = $this->_model->getMetadatas($cacheId);
  119. $this->assertArrayHasKey('expire', $actualResult);
  120. $this->assertArrayHasKey('tags', $actualResult);
  121. $this->assertArrayHasKey('mtime', $actualResult);
  122. $this->assertSame($tags, $actualResult['tags']);
  123. }
  124. /**
  125. * @param int $extraLifeTime
  126. * @param \PHPUnit\Framework\Constraint\Constraint $constraint
  127. * @dataProvider touchDataProvider
  128. */
  129. public function testTouch($extraLifeTime, \PHPUnit\Framework\Constraint\Constraint $constraint)
  130. {
  131. $cacheId = 'test';
  132. $this->_model->save('test data', $cacheId, [], 2);
  133. $this->assertGreaterThan(0, $this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been saved");
  134. $this->_model->touch($cacheId, $extraLifeTime);
  135. sleep(2);
  136. $this->assertThat($this->_model->test($cacheId), $constraint);
  137. }
  138. public function touchDataProvider()
  139. {
  140. return [
  141. 'not enough extra lifetime' => [0, $this->isFalse()],
  142. 'enough extra lifetime' => [1000, $this->logicalNot($this->isFalse())]
  143. ];
  144. }
  145. /**
  146. * @param string $data
  147. * @param int|bool|null $lifetime
  148. * @param bool $doNotTestValidity
  149. * @param string|bool $expected
  150. * @dataProvider loadDataProvider
  151. */
  152. public function testLoad($data, $lifetime, $doNotTestValidity, $expected)
  153. {
  154. $cacheId = 'test';
  155. $this->_model->save($data, $cacheId, [], $lifetime);
  156. $actualData = $this->_model->load($cacheId, $doNotTestValidity);
  157. $this->assertSame($expected, $actualData);
  158. }
  159. public function loadDataProvider()
  160. {
  161. return [
  162. 'infinite lifetime with validity' => ['test data', null, false, 'test data'],
  163. 'infinite lifetime without validity' => ['test data', null, true, 'test data'],
  164. 'zero lifetime with validity' => ['test data', 0, false, false],
  165. 'zero lifetime without validity' => ['test data', 0, true, 'test data']
  166. ];
  167. }
  168. public function testTest()
  169. {
  170. $this->assertFalse($this->_model->test('test'));
  171. $this->_model->save('test data', 'test');
  172. $this->assertNotEmpty($this->_model->test('test'), "Cache with id 'test' has not been saved");
  173. }
  174. public function testSave()
  175. {
  176. $cacheId = 'test_id';
  177. $data = 'test data';
  178. $tags = ['tag1', 'tag2'];
  179. $this->assertTrue($this->_model->save($data, $cacheId, $tags));
  180. $actualData = $this->_model->load($cacheId);
  181. $this->assertEquals($data, $actualData);
  182. $actualMetadata = $this->_model->getMetadatas($cacheId);
  183. $this->arrayHasKey('tags', $actualMetadata);
  184. $this->assertEquals($tags, $actualMetadata['tags']);
  185. }
  186. public function testRemove()
  187. {
  188. $cacheId = 'test';
  189. $this->_model->save('test data', $cacheId);
  190. $this->assertGreaterThan(0, $this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been found");
  191. $this->_model->remove($cacheId);
  192. $this->assertFalse($this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been removed");
  193. }
  194. /**
  195. * @dataProvider cleanDataProvider
  196. */
  197. public function testClean($mode, $tags, $expectedIds)
  198. {
  199. $this->_prepareCollection();
  200. $this->_model->clean($mode, $tags);
  201. $actualIds = $this->_model->getIds();
  202. $this->assertEquals($expectedIds, $actualIds);
  203. }
  204. public function cleanDataProvider()
  205. {
  206. return [
  207. 'clean all cache' => [\Zend_Cache::CLEANING_MODE_ALL, [], []],
  208. 'clean cache matching all tags' => [
  209. \Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  210. ['tag1', 'tag2'],
  211. ['test2', 'test4', 'test5'],
  212. ],
  213. 'clean cache not matching tags' => [
  214. \Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
  215. ['tag1', 'tag2'],
  216. ['test1', 'test2', 'test3'],
  217. ],
  218. 'clean cache matching any tags' => [
  219. \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  220. ['tag1', 'tag2'],
  221. ['test4', 'test5'],
  222. ]
  223. ];
  224. }
  225. public function testCleanOld()
  226. {
  227. $this->_model->save('long-living entity', 'long', [], 1000);
  228. $this->_model->save('infinite-living entity', 'infinite', [], null);
  229. $this->_model->save('short-living entity', 'short', [], 0);
  230. $this->_model->clean(\Zend_Cache::CLEANING_MODE_OLD);
  231. $expectedIds = ['long', 'infinite'];
  232. $actualIds = $this->_model->getIds();
  233. $this->assertSame($expectedIds, $actualIds);
  234. }
  235. /**
  236. * Fill the collection with data
  237. */
  238. protected function _prepareCollection()
  239. {
  240. $this->_model->save('test data 1', 'test1', ['tag1', 'tag2', 'tag3']);
  241. $this->_model->save('test data 2', 'test2', ['tag1', 'tag3']);
  242. $this->_model->save('test data 3', 'test3', ['tag2', 'tag1']);
  243. $this->_model->save('test data 4', 'test4', ['tag4', 'tag5']);
  244. $this->_model->save('test data 5', 'test5', []);
  245. }
  246. }