MongoDbTest.php 9.12 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\Cache\Backend;

class MongoDbTest extends \PHPUnit\Framework\TestCase
{
    protected $_connectionString;

    protected $_dbName = 'magento_integration_test';

    /**
     * @var \Magento\Framework\Cache\Backend\MongoDb|null
     */
    protected $_model = null;

    protected function setUp()
    {
        if (defined('MONGODB_CONNECTION_STRING')) {
            $this->_connectionString = MONGODB_CONNECTION_STRING;
        }
        if (empty($this->_connectionString) || !extension_loaded('mongo')) {
            $this->markTestSkipped(
                "Either 'mongo' extension is not loaded or 'MONGODB_CONNECTION_STRING' constant is not defined"
            );
        }
        if (defined('MONGODB_DATABASE_NAME')) {
            $this->_dbName = MONGODB_DATABASE_NAME;
        }
        $this->_model = new \Magento\Framework\Cache\Backend\MongoDb(
            ['connection_string' => $this->_connectionString, 'db' => $this->_dbName]
        );
    }

    protected function tearDown()
    {
        if (!empty($this->_connectionString) && extension_loaded('mongo')) {
            $this->_model = null;
            $connection = new \Mongo($this->_connectionString);
            $connection->dropDB($this->_dbName);
        }
    }

    /**
     * @expectedException \Zend_Cache_Exception
     * @expectedExceptionMessage 'db' option is not specified
     */
    public function testConstructorException()
    {
        new \Magento\Framework\Cache\Backend\MongoDb();
    }

    public function testGetIds()
    {
        $this->assertEmpty($this->_model->getIds());
        $this->_model->save('test data 1', 'test1');
        $this->_model->save('test data 2', 'test2');
        $this->assertEquals(['test1', 'test2'], $this->_model->getIds());
    }

    public function testGetTags()
    {
        $this->assertEmpty($this->_model->getTags());
        $this->_model->save('test data 1', 'test1', ['tag1', 'tag2']);
        $this->_model->save('test data 2', 'test2', ['tag1', 'tag3']);
        $actual = $this->_model->getTags();
        $expected = ['tag1', 'tag2', 'tag3'];
        $this->assertEquals($expected, $actual);
    }

    /**
     * @dataProvider getIdsMatchingTagsDataProvider
     */
    public function testGetIdsMatchingTags($searchTags, $expectedIds)
    {
        $this->_prepareCollection();
        $actualIds = $this->_model->getIdsMatchingTags($searchTags);
        $this->assertEquals($expectedIds, $actualIds);
    }

    public function getIdsMatchingTagsDataProvider()
    {
        return [
            'one tag' => [['tag1'], ['test1', 'test2', 'test3']],
            'multiple tags' => [['tag1', 'tag2'], ['test1', 'test3']]
        ];
    }

    /**
     * @dataProvider getIdsNotMatchingTagsDataProvider
     */
    public function testGetIdsNotMatchingTags($searchTags, $expectedIds)
    {
        $this->_prepareCollection();
        $actualIds = $this->_model->getIdsNotMatchingTags($searchTags);
        $this->assertEquals($expectedIds, $actualIds);
    }

    public function getIdsNotMatchingTagsDataProvider()
    {
        return [
            'one tag' => [['tag2'], ['test2', 'test4', 'test5']],
            'multiple tags' => [['tag1', 'tag2'], ['test4', 'test5']]
        ];
    }

    /**
     * @dataProvider getIdsMatchingAnyTagsDataProvider
     */
    public function testGetIdsMatchingAnyTags($searchTags, $expectedIds)
    {
        $this->_prepareCollection();
        $actualIds = $this->_model->getIdsMatchingAnyTags($searchTags);
        $this->assertEquals($expectedIds, $actualIds);
    }

    public function getIdsMatchingAnyTagsDataProvider()
    {
        return [
            'no tags' => [[], []],
            'one tag' => [['tag2'], ['test1', 'test3']],
            'multiple tags' => [['tag1', 'tag2'], ['test1', 'test2', 'test3']]
        ];
    }

    public function testGetMetadatas()
    {
        $cacheId = 'test';
        $tags = ['tag_1', 'tag_2'];
        $this->_model->save('test data', $cacheId, $tags, 100);
        $actualResult = $this->_model->getMetadatas($cacheId);
        $this->assertArrayHasKey('expire', $actualResult);
        $this->assertArrayHasKey('tags', $actualResult);
        $this->assertArrayHasKey('mtime', $actualResult);
        $this->assertSame($tags, $actualResult['tags']);
    }

    /**
     * @param int $extraLifeTime
     * @param \PHPUnit\Framework\Constraint\Constraint $constraint
     * @dataProvider touchDataProvider
     */
    public function testTouch($extraLifeTime, \PHPUnit\Framework\Constraint\Constraint $constraint)
    {
        $cacheId = 'test';
        $this->_model->save('test data', $cacheId, [], 2);
        $this->assertGreaterThan(0, $this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been saved");
        $this->_model->touch($cacheId, $extraLifeTime);
        sleep(2);
        $this->assertThat($this->_model->test($cacheId), $constraint);
    }

    public function touchDataProvider()
    {
        return [
            'not enough extra lifetime' => [0, $this->isFalse()],
            'enough extra lifetime' => [1000, $this->logicalNot($this->isFalse())]
        ];
    }

    /**
     * @param string $data
     * @param int|bool|null $lifetime
     * @param bool $doNotTestValidity
     * @param string|bool $expected
     * @dataProvider loadDataProvider
     */
    public function testLoad($data, $lifetime, $doNotTestValidity, $expected)
    {
        $cacheId = 'test';
        $this->_model->save($data, $cacheId, [], $lifetime);
        $actualData = $this->_model->load($cacheId, $doNotTestValidity);
        $this->assertSame($expected, $actualData);
    }

    public function loadDataProvider()
    {
        return [
            'infinite lifetime with validity' => ['test data', null, false, 'test data'],
            'infinite lifetime without validity' => ['test data', null, true, 'test data'],
            'zero lifetime with validity' => ['test data', 0, false, false],
            'zero lifetime without validity' => ['test data', 0, true, 'test data']
        ];
    }

    public function testTest()
    {
        $this->assertFalse($this->_model->test('test'));
        $this->_model->save('test data', 'test');
        $this->assertNotEmpty($this->_model->test('test'), "Cache with id 'test' has not been saved");
    }

    public function testSave()
    {
        $cacheId = 'test_id';
        $data = 'test data';
        $tags = ['tag1', 'tag2'];

        $this->assertTrue($this->_model->save($data, $cacheId, $tags));
        $actualData = $this->_model->load($cacheId);
        $this->assertEquals($data, $actualData);
        $actualMetadata = $this->_model->getMetadatas($cacheId);
        $this->arrayHasKey('tags', $actualMetadata);
        $this->assertEquals($tags, $actualMetadata['tags']);
    }

    public function testRemove()
    {
        $cacheId = 'test';
        $this->_model->save('test data', $cacheId);
        $this->assertGreaterThan(0, $this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been found");
        $this->_model->remove($cacheId);
        $this->assertFalse($this->_model->test($cacheId), "Cache with id '{$cacheId}' has not been removed");
    }

    /**
     * @dataProvider cleanDataProvider
     */
    public function testClean($mode, $tags, $expectedIds)
    {
        $this->_prepareCollection();

        $this->_model->clean($mode, $tags);
        $actualIds = $this->_model->getIds();
        $this->assertEquals($expectedIds, $actualIds);
    }

    public function cleanDataProvider()
    {
        return [
            'clean all cache' => [\Zend_Cache::CLEANING_MODE_ALL, [], []],
            'clean cache matching all tags' => [
                \Zend_Cache::CLEANING_MODE_MATCHING_TAG,
                ['tag1', 'tag2'],
                ['test2', 'test4', 'test5'],
            ],
            'clean cache not matching tags' => [
                \Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG,
                ['tag1', 'tag2'],
                ['test1', 'test2', 'test3'],
            ],
            'clean cache matching any tags' => [
                \Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
                ['tag1', 'tag2'],
                ['test4', 'test5'],
            ]
        ];
    }

    public function testCleanOld()
    {
        $this->_model->save('long-living entity', 'long', [], 1000);
        $this->_model->save('infinite-living entity', 'infinite', [], null);
        $this->_model->save('short-living entity', 'short', [], 0);
        $this->_model->clean(\Zend_Cache::CLEANING_MODE_OLD);
        $expectedIds = ['long', 'infinite'];
        $actualIds = $this->_model->getIds();
        $this->assertSame($expectedIds, $actualIds);
    }

    /**
     * Fill the collection with data
     */
    protected function _prepareCollection()
    {
        $this->_model->save('test data 1', 'test1', ['tag1', 'tag2', 'tag3']);
        $this->_model->save('test data 2', 'test2', ['tag1', 'tag3']);
        $this->_model->save('test data 3', 'test3', ['tag2', 'tag1']);
        $this->_model->save('test data 4', 'test4', ['tag4', 'tag5']);
        $this->_model->save('test data 5', 'test5', []);
    }
}