Passed
Push — master ( 8615ef...5e52dd )
by Tim
02:51
created

LocalCacheAdapterTest::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 29
rs 9.7998
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Cache\LocalCacheAdapterTest
5
*
6
* NOTICE OF LICENSE
7
*
8
* This source file is subject to the Open Software License (OSL 3.0)
9
* that is available through the world-wide-web at this URL:
10
* http://opensource.org/licenses/osl-3.0.php
11
*
12
* PHP version 5
13
*
14
* @author    Tim Wagner <[email protected]>
15
* @copyright 2021 TechDivision GmbH <[email protected]>
16
* @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
* @link      https://github.com/techdivision/import-cache
18
* @link      http://www.techdivision.com
19
*/
20
21
namespace TechDivision\Import\Cache;
22
23
use Ramsey\Uuid\Uuid;
24
use PHPUnit\Framework\TestCase;
25
use TechDivision\Import\Cache\Utils\CacheKeyUtil;
26
use TechDivision\Import\Cache\Configuration\ConfigurationInterface;
27
28
/**
29
 * Test class for the local cache adapter.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2021 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-cache
35
 * @link      http://www.techdivision.com
36
 */
37
class LocalCacheAdapterTest extends TestCase
38
{
39
40
    /**
41
     * The exportable trait that has to be tested.
42
     *
43
     * @var \TechDivision\Import\Cache\LocalCacheAdapter
44
     */
45
    protected $localCacheAdapter;
46
47
    /**
48
     * Sets up the fixture, for example, open a network connection.
49
     * This method is called before a test is executed.
50
     *
51
     * @return void
52
     * @see \PHPUnit\Framework\TestCase::setUp()
53
     */
54
    protected function setUp()
55
    {
56
57
        // create the mock configuration
58
        $mockConfiguration = $this->getMockBuilder(ConfigurationInterface::class)
59
            ->setMethods(array('getSerial'))
60
            ->getMockForAbstractClass();
61
        // mock the methods
62
        $mockConfiguration
63
            ->expects($this->any())
64
            ->method('getSerial')
65
            ->willReturn(Uuid::uuid4()->toString());
66
67
        // create a new cache key instance
68
        $cacheKeyUtil = new CacheKeyUtil($mockConfiguration);
69
70
        // initialize the cache adapter we want to test
71
        $this->localCacheAdapter = new LocalCacheAdapter($cacheKeyUtil);
72
73
        // load the test data
74
        $values = $this->provideTestdata();
75
        // pre-initialize the cacha adapter with the values
76
        foreach ($values as $value) {
77
            // explode key and reference
78
            list ($uniqueKey, $ref) = $value;
79
            // prepare the array with the references
80
            $references = array($ref => $uniqueKey);
81
            // add the value to the cache
82
            $this->localCacheAdapter->toCache($uniqueKey, $value, $references);
83
        }
84
    }
85
86
    /**
87
     * Test the removeCache() method.
88
     *
89
     * @param string $uniqueKey The unique key of the item that has to be removed from the cache
90
     * @param string $ref       The reference of the item that has also to be removed
91
     *
92
     * @return void
93
     * @dataProvider provideTestdata
94
     */
95
    public function testRemoveCache(string $uniqueKey, string $ref)
96
    {
97
98
        // remove the value from the cache
99
        $this->localCacheAdapter->removeCache($uniqueKey);
100
101
        // query whether or not the value is NOT available either per unique key or reference
102
        $this->assertFalse($this->localCacheAdapter->isCached($uniqueKey));
103
        $this->assertFalse($this->localCacheAdapter->isCached($ref));
104
    }
105
106
    /**
107
     * Test the toCache() method.
108
     *
109
     * @param string $uniqueKey The unique key of the item that has to be cached
110
     * @param string $ref       The reference of the item that has to be cached
111
     *
112
     * @return void
113
     * @dataProvider provideTestdata
114
     */
115
    public function testToCache(string $uniqueKey, string $ref)
116
    {
117
118
        // create the array with thereferences
119
        $references = array($ref => $uniqueKey);
120
121
        // add the value to the cache
122
        $this->localCacheAdapter->toCache($uniqueKey, array($uniqueKey, $ref), $references, array(), true);
123
124
        // query whether or not the value is available either per unique key or reference
125
        $this->assertTrue($this->localCacheAdapter->isCached($uniqueKey));
126
        $this->assertTrue($this->localCacheAdapter->isCached($ref));;
127
    }
128
129
    /**
130
     * Data provider for the methods that has to be tested.
131
     *
132
     * @return \Generator The generator instance
133
     */
134
    public function provideTestdata()
135
    {
136
        for ($i = 0; $i < 10; $i++) {
137
            yield array("key-$i", "ref-$i");
138
        }
139
    }
140
}
141