Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
MongoDbTest Class Reference
Inheritance diagram for MongoDbTest:

Public Member Functions

 testConstructorException ()
 
 testGetIds ()
 
 testGetTags ()
 
 testGetIdsMatchingTags ($searchTags, $expectedIds)
 
 getIdsMatchingTagsDataProvider ()
 
 testGetIdsNotMatchingTags ($searchTags, $expectedIds)
 
 getIdsNotMatchingTagsDataProvider ()
 
 testGetIdsMatchingAnyTags ($searchTags, $expectedIds)
 
 getIdsMatchingAnyTagsDataProvider ()
 
 testGetMetadatas ()
 
 testTouch ($extraLifeTime, \PHPUnit\Framework\Constraint\Constraint $constraint)
 
 touchDataProvider ()
 
 testLoad ($data, $lifetime, $doNotTestValidity, $expected)
 
 loadDataProvider ()
 
 testTest ()
 
 testSave ()
 
 testRemove ()
 
 testClean ($mode, $tags, $expectedIds)
 
 cleanDataProvider ()
 
 testCleanOld ()
 

Protected Member Functions

 setUp ()
 
 tearDown ()
 
 _prepareCollection ()
 

Protected Attributes

 $_connectionString
 
 $_dbName = 'magento_integration_test'
 
 $_model = null
 

Detailed Description

Definition at line 8 of file MongoDbTest.php.

Member Function Documentation

◆ _prepareCollection()

_prepareCollection ( )
protected

Fill the collection with data

Definition at line 266 of file MongoDbTest.php.

267  {
268  $this->_model->save('test data 1', 'test1', ['tag1', 'tag2', 'tag3']);
269  $this->_model->save('test data 2', 'test2', ['tag1', 'tag3']);
270  $this->_model->save('test data 3', 'test3', ['tag2', 'tag1']);
271  $this->_model->save('test data 4', 'test4', ['tag4', 'tag5']);
272  $this->_model->save('test data 5', 'test5', []);
273  }

◆ cleanDataProvider()

cleanDataProvider ( )

Definition at line 230 of file MongoDbTest.php.

231  {
232  return [
233  'clean all cache' => [\Zend_Cache::CLEANING_MODE_ALL, [], []],
234  'clean cache matching all tags' => [
236  ['tag1', 'tag2'],
237  ['test2', 'test4', 'test5'],
238  ],
239  'clean cache not matching tags' => [
241  ['tag1', 'tag2'],
242  ['test1', 'test2', 'test3'],
243  ],
244  'clean cache matching any tags' => [
246  ['tag1', 'tag2'],
247  ['test4', 'test5'],
248  ]
249  ];
250  }
const CLEANING_MODE_NOT_MATCHING_TAG
Definition: Cache.php:75
const CLEANING_MODE_ALL
Definition: Cache.php:72
const CLEANING_MODE_MATCHING_ANY_TAG
Definition: Cache.php:76
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74

◆ getIdsMatchingAnyTagsDataProvider()

getIdsMatchingAnyTagsDataProvider ( )

Definition at line 119 of file MongoDbTest.php.

120  {
121  return [
122  'no tags' => [[], []],
123  'one tag' => [['tag2'], ['test1', 'test3']],
124  'multiple tags' => [['tag1', 'tag2'], ['test1', 'test2', 'test3']]
125  ];
126  }

◆ getIdsMatchingTagsDataProvider()

getIdsMatchingTagsDataProvider ( )

Definition at line 83 of file MongoDbTest.php.

84  {
85  return [
86  'one tag' => [['tag1'], ['test1', 'test2', 'test3']],
87  'multiple tags' => [['tag1', 'tag2'], ['test1', 'test3']]
88  ];
89  }

◆ getIdsNotMatchingTagsDataProvider()

getIdsNotMatchingTagsDataProvider ( )

Definition at line 101 of file MongoDbTest.php.

102  {
103  return [
104  'one tag' => [['tag2'], ['test2', 'test4', 'test5']],
105  'multiple tags' => [['tag1', 'tag2'], ['test4', 'test5']]
106  ];
107  }

◆ loadDataProvider()

loadDataProvider ( )

Definition at line 178 of file MongoDbTest.php.

179  {
180  return [
181  'infinite lifetime with validity' => ['test data', null, false, 'test data'],
182  'infinite lifetime without validity' => ['test data', null, true, 'test data'],
183  'zero lifetime with validity' => ['test data', 0, false, false],
184  'zero lifetime without validity' => ['test data', 0, true, 'test data']
185  ];
186  }

◆ setUp()

setUp ( )
protected

Definition at line 19 of file MongoDbTest.php.

20  {
21  if (defined('MONGODB_CONNECTION_STRING')) {
22  $this->_connectionString = MONGODB_CONNECTION_STRING;
23  }
24  if (empty($this->_connectionString) || !extension_loaded('mongo')) {
25  $this->markTestSkipped(
26  "Either 'mongo' extension is not loaded or 'MONGODB_CONNECTION_STRING' constant is not defined"
27  );
28  }
29  if (defined('MONGODB_DATABASE_NAME')) {
30  $this->_dbName = MONGODB_DATABASE_NAME;
31  }
32  $this->_model = new \Magento\Framework\Cache\Backend\MongoDb(
33  ['connection_string' => $this->_connectionString, 'db' => $this->_dbName]
34  );
35  }

◆ tearDown()

tearDown ( )
protected

Definition at line 37 of file MongoDbTest.php.

38  {
39  if (!empty($this->_connectionString) && extension_loaded('mongo')) {
40  $this->_model = null;
41  $connection = new \Mongo($this->_connectionString);
42  $connection->dropDB($this->_dbName);
43  }
44  }
$connection
Definition: bulk.php:13

◆ testClean()

testClean (   $mode,
  $tags,
  $expectedIds 
)

@dataProvider cleanDataProvider

Definition at line 221 of file MongoDbTest.php.

222  {
223  $this->_prepareCollection();
224 
225  $this->_model->clean($mode, $tags);
226  $actualIds = $this->_model->getIds();
227  $this->assertEquals($expectedIds, $actualIds);
228  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ testCleanOld()

testCleanOld ( )

Definition at line 252 of file MongoDbTest.php.

253  {
254  $this->_model->save('long-living entity', 'long', [], 1000);
255  $this->_model->save('infinite-living entity', 'infinite', [], null);
256  $this->_model->save('short-living entity', 'short', [], 0);
257  $this->_model->clean(\Zend_Cache::CLEANING_MODE_OLD);
258  $expectedIds = ['long', 'infinite'];
259  $actualIds = $this->_model->getIds();
260  $this->assertSame($expectedIds, $actualIds);
261  }
const CLEANING_MODE_OLD
Definition: Cache.php:73

◆ testConstructorException()

testConstructorException ( )

@expectedException \Zend_Cache_Exception @expectedExceptionMessage 'db' option is not specified

Definition at line 50 of file MongoDbTest.php.

51  {
52  new \Magento\Framework\Cache\Backend\MongoDb();
53  }

◆ testGetIds()

testGetIds ( )

Definition at line 55 of file MongoDbTest.php.

56  {
57  $this->assertEmpty($this->_model->getIds());
58  $this->_model->save('test data 1', 'test1');
59  $this->_model->save('test data 2', 'test2');
60  $this->assertEquals(['test1', 'test2'], $this->_model->getIds());
61  }

◆ testGetIdsMatchingAnyTags()

testGetIdsMatchingAnyTags (   $searchTags,
  $expectedIds 
)

@dataProvider getIdsMatchingAnyTagsDataProvider

Definition at line 112 of file MongoDbTest.php.

113  {
114  $this->_prepareCollection();
115  $actualIds = $this->_model->getIdsMatchingAnyTags($searchTags);
116  $this->assertEquals($expectedIds, $actualIds);
117  }

◆ testGetIdsMatchingTags()

testGetIdsMatchingTags (   $searchTags,
  $expectedIds 
)

@dataProvider getIdsMatchingTagsDataProvider

Definition at line 76 of file MongoDbTest.php.

77  {
78  $this->_prepareCollection();
79  $actualIds = $this->_model->getIdsMatchingTags($searchTags);
80  $this->assertEquals($expectedIds, $actualIds);
81  }

◆ testGetIdsNotMatchingTags()

testGetIdsNotMatchingTags (   $searchTags,
  $expectedIds 
)

@dataProvider getIdsNotMatchingTagsDataProvider

Definition at line 94 of file MongoDbTest.php.

95  {
96  $this->_prepareCollection();
97  $actualIds = $this->_model->getIdsNotMatchingTags($searchTags);
98  $this->assertEquals($expectedIds, $actualIds);
99  }

◆ testGetMetadatas()

testGetMetadatas ( )

Definition at line 128 of file MongoDbTest.php.

129  {
130  $cacheId = 'test';
131  $tags = ['tag_1', 'tag_2'];
132  $this->_model->save('test data', $cacheId, $tags, 100);
133  $actualResult = $this->_model->getMetadatas($cacheId);
134  $this->assertArrayHasKey('expire', $actualResult);
135  $this->assertArrayHasKey('tags', $actualResult);
136  $this->assertArrayHasKey('mtime', $actualResult);
137  $this->assertSame($tags, $actualResult['tags']);
138  }

◆ testGetTags()

testGetTags ( )

Definition at line 63 of file MongoDbTest.php.

64  {
65  $this->assertEmpty($this->_model->getTags());
66  $this->_model->save('test data 1', 'test1', ['tag1', 'tag2']);
67  $this->_model->save('test data 2', 'test2', ['tag1', 'tag3']);
68  $actual = $this->_model->getTags();
69  $expected = ['tag1', 'tag2', 'tag3'];
70  $this->assertEquals($expected, $actual);
71  }

◆ testLoad()

testLoad (   $data,
  $lifetime,
  $doNotTestValidity,
  $expected 
)
Parameters
string$data
int | bool | null$lifetime
bool$doNotTestValidity
string | bool$expected@dataProvider loadDataProvider

Definition at line 170 of file MongoDbTest.php.

171  {
172  $cacheId = 'test';
173  $this->_model->save($data, $cacheId, [], $lifetime);
174  $actualData = $this->_model->load($cacheId, $doNotTestValidity);
175  $this->assertSame($expected, $actualData);
176  }

◆ testRemove()

testRemove ( )

Definition at line 209 of file MongoDbTest.php.

210  {
211  $cacheId = 'test';
212  $this->_model->save('test data', $cacheId);
213  $this->assertGreaterThan(0, $this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been found");
214  $this->_model->remove($cacheId);
215  $this->assertFalse($this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been removed");
216  }

◆ testSave()

testSave ( )

Definition at line 195 of file MongoDbTest.php.

196  {
197  $cacheId = 'test_id';
198  $data = 'test data';
199  $tags = ['tag1', 'tag2'];
200 
201  $this->assertTrue($this->_model->save($data, $cacheId, $tags));
202  $actualData = $this->_model->load($cacheId);
203  $this->assertEquals($data, $actualData);
204  $actualMetadata = $this->_model->getMetadatas($cacheId);
205  $this->arrayHasKey('tags', $actualMetadata);
206  $this->assertEquals($tags, $actualMetadata['tags']);
207  }

◆ testTest()

testTest ( )

Definition at line 188 of file MongoDbTest.php.

189  {
190  $this->assertFalse($this->_model->test('test'));
191  $this->_model->save('test data', 'test');
192  $this->assertNotEmpty($this->_model->test('test'), "Cache with id 'test' has not been saved");
193  }

◆ testTouch()

testTouch (   $extraLifeTime,
\PHPUnit\Framework\Constraint\Constraint  $constraint 
)
Parameters
int$extraLifeTime
\PHPUnit\Framework\Constraint\Constraint$constraint@dataProvider touchDataProvider

Definition at line 145 of file MongoDbTest.php.

146  {
147  $cacheId = 'test';
148  $this->_model->save('test data', $cacheId, [], 2);
149  $this->assertGreaterThan(0, $this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been saved");
150  $this->_model->touch($cacheId, $extraLifeTime);
151  sleep(2);
152  $this->assertThat($this->_model->test($cacheId), $constraint);
153  }

◆ touchDataProvider()

touchDataProvider ( )

Definition at line 155 of file MongoDbTest.php.

156  {
157  return [
158  'not enough extra lifetime' => [0, $this->isFalse()],
159  'enough extra lifetime' => [1000, $this->logicalNot($this->isFalse())]
160  ];
161  }

Field Documentation

◆ $_connectionString

$_connectionString
protected

Definition at line 10 of file MongoDbTest.php.

◆ $_dbName

$_dbName = 'magento_integration_test'
protected

Definition at line 12 of file MongoDbTest.php.

◆ $_model

$_model = null
protected

Definition at line 17 of file MongoDbTest.php.


The documentation for this class was generated from the following file: