Skip to content

Commit bd52908

Browse files
author
Matias Melograno
committed
updated tests with changes in redisWrapper
1 parent 9ed72d1 commit bd52908

File tree

9 files changed

+155
-114
lines changed

9 files changed

+155
-114
lines changed

phpunit.xml.dist

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,54 @@
1212
<testsuite name="splitio">
1313
<directory suffix="Test.php">tests/</directory>
1414
</testsuite>
15-
<testsuite name="redis-adapter">
16-
<file>tests/Suite/Redis/CacheInterfacesTest.php</file>
15+
<testsuite name="adapter">
16+
<directory suffix="Test.php">tests/Suite/Adapter</directory>
17+
</testsuite>
18+
<testsuite name="attributes">
19+
<directory suffix="Test.php">tests/Suite/Attributes/</directory>
1720
</testsuite>
1821
<testsuite name="common">
1922
<directory suffix="Test.php">tests/Suite/Common/</directory>
2023
</testsuite>
2124
<testsuite name="component">
2225
<directory suffix="Test.php">tests/Suite/Component/</directory>
26+
</testsuite>
27+
<testsuite name="dynamic_configurations">
28+
<directory suffix="Test.php">tests/Suite/DynamicConfigurations/</directory>
2329
</testsuite>
2430
<testsuite name="engine">
2531
<directory suffix="Test.php">tests/Suite/Engine/</directory>
2632
</testsuite>
33+
<testsuite name="input_validation">
34+
<directory suffix="Test.php">tests/Suite/InputValidation/</directory>
35+
</testsuite>
2736
<testsuite name="matchers">
2837
<directory suffix="Test.php">tests/Suite/Matchers/</directory>
2938
</testsuite>
3039
<testsuite name="metrics">
3140
<directory suffix="Test.php">tests/Suite/Metrics/</directory>
3241
</testsuite>
42+
<testsuite name="redis">
43+
<directory suffix="Test.php">tests/Suite/Redis/</directory>
44+
</testsuite>
3345
<testsuite name="sdk">
3446
<directory suffix="Test.php">tests/Suite/Sdk/</directory>
3547
</testsuite>
36-
<testsuite name="attributes">
37-
<directory suffix="Test.php">tests/Suite/Attributes/</directory>
38-
</testsuite>
39-
<testsuite name="input_validation">
40-
<directory suffix="Test.php">tests/Suite/InputValidation/</directory>
41-
</testsuite>
4248

4349

4450

4551
<testsuite name="integration">
52+
<directory suffix="Test.php">tests/Suite/Adapter/</directory>
53+
<directory suffix="Test.php">tests/Suite/Attributes/</directory>
4654
<directory suffix="Test.php">tests/Suite/Common/</directory>
4755
<directory suffix="Test.php">tests/Suite/Component/</directory>
56+
<directory suffix="Test.php">tests/Suite/DynamicConfigurations/</directory>
4857
<directory suffix="Test.php">tests/Suite/Engine/</directory>
58+
<directory suffix="Test.php">tests/Suite/InputValidation/</directory>
4959
<directory suffix="Test.php">tests/Suite/Matchers/</directory>
5060
<directory suffix="Test.php">tests/Suite/Metrics/</directory>
61+
<directory suffix="Test.php">tests/Suite/Redis/</directory>
5162
<directory suffix="Test.php">tests/Suite/Sdk/</directory>
52-
<directory suffix="Test.php">tests/Suite/Adapter/</directory>
53-
<directory suffix="Test.php">tests/Suite/Attributes/</directory>
54-
<directory suffix="Test.php">tests/Suite/InputValidation/</directory>
55-
<directory suffix="Test.php">tests/Suite/DynamicConfigurations/</directory>
5663
</testsuite>
5764
</testsuites>
5865

src/SplitIO/Component/Cache/Storage/Adapter/PRedis.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,11 @@ public function getItem($key)
239239
*/
240240
public function getItems(array $keys = array())
241241
{
242-
$values = $this->client->mget($keys);
243242
$toReturn = array();
243+
if (count($keys) == 0) {
244+
return $toReturn;
245+
}
246+
$values = $this->client->mget($keys);
244247
foreach ($keys as $index => $key) {
245248
$toReturn[$key] = new Item($key);
246249
if (!is_null($values[$index])) {

src/SplitIO/Component/Cache/Storage/Adapter/SafeRedisWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getItems(array $keys = array())
5353
try {
5454
return $this->cacheAdapter->getItems($keys);
5555
} catch (\Exception $e) {
56-
Di::getLogger()->critical("An error occurred getting " . $keys . " from redis.");
56+
Di::getLogger()->critical("An error occurred getting " . json_encode($keys) . " from redis.");
5757
Di::getLogger()->critical($e->getMessage());
5858
Di::getLogger()->critical($e->getTraceAsString());
5959
return array();

src/SplitIO/Component/Initialization/CacheTrait.php

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,21 @@ class CacheTrait
88
{
99
public static function addCache($adapter, array $options)
1010
{
11-
switch ($adapter) {
12-
case 'filesystem':
13-
$adapter_config = array(
14-
'name' => 'filesystem',
15-
'options' => array(
16-
'path'=> isset($options['filesystem-path']) ? $options['filesystem-path'] : null
17-
)
18-
);
19-
break;
20-
case 'predis':
21-
$adapter_config = array(
22-
'name' => 'predis',
23-
'options' => array(
24-
'options' => isset($options['predis-options'])
25-
? $options['predis-options'] : null,
26-
'parameters' => isset($options['predis-parameters'])
27-
? $options['predis-parameters'] : null,
28-
'sentinels' => isset($options['predis-sentinels'])
29-
? $options['predis-sentinels'] : null,
30-
'clusterNodes' => isset($options['predis-clusterNodes'])
31-
? $options['predis-clusterNodes'] : null,
32-
'distributedStrategy' => isset($options['predis-distributedStrategy'])
33-
? $options['predis-distributedStrategy'] : null,
34-
)
35-
);
36-
break;
37-
case 'redis':
38-
default:
39-
$adapter_config = array(
40-
'name' => 'redis',
41-
'options' => array(
42-
'host' => isset($options['redis-host']) ? $options['redis-host'] : null,
43-
'port' => isset($options['redis-port']) ? $options['redis-port'] : null,
44-
'password' => isset($options['redis-pass']) ? $options['redis-pass'] : null,
45-
'timeout' => isset($options['redis-timeout']) ? $options['redis-timeout'] : null,
46-
)
47-
);
48-
break;
49-
}
11+
$adapter_config = array(
12+
'name' => 'predis',
13+
'options' => array(
14+
'options' => isset($options['predis-options'])
15+
? $options['predis-options'] : null,
16+
'parameters' => isset($options['predis-parameters'])
17+
? $options['predis-parameters'] : null,
18+
'sentinels' => isset($options['predis-sentinels'])
19+
? $options['predis-sentinels'] : null,
20+
'clusterNodes' => isset($options['predis-clusterNodes'])
21+
? $options['predis-clusterNodes'] : null,
22+
'distributedStrategy' => isset($options['predis-distributedStrategy'])
23+
? $options['predis-distributedStrategy'] : null,
24+
)
25+
);
5026

5127
ServiceProvider::registerCache(new Pool(array('adapter' => $adapter_config)));
5228
}

src/SplitIO/Sdk/Factory/SplitFactory.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ public function __construct($apiKey, array $options = array())
4646

4747
private function doBUR()
4848
{
49-
$ready = (isset($this->options['ready']) && $this->options['ready'] > 0) ? $this->options['ready'] : null;
49+
/*
50+
Deprecated
51+
$ready = (isset($this->options['ready']) && $this->options['ready'] > 0) ? $this->options['ready'] : null;
5052
51-
//Block Until Ready
52-
if ($ready) {
53-
if (!$this->blockUntilReady($ready)) {
54-
throw new TimeOutException("Cache data is not ready yet");
53+
//Block Until Ready
54+
if ($ready) {
55+
if (!$this->blockUntilReady($ready)) {
56+
throw new TimeOutException("Cache data is not ready yet");
57+
}
5558
}
56-
}
59+
*/
5760
}
5861

5962
/**

tests/Suite/Redis/CacheInterfacesTest.php

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
use SplitIO\Component\Log\Handler\Stdout;
1111
use SplitIO\Component\Log\Logger;
1212
use SplitIO\Component\Log\LogLevelEnum;
13+
use SplitIO\Sdk\Events\EventDTO;
14+
use SplitIO\Sdk\Events\EventQueueMessage;
15+
use SplitIO\Sdk\QueueMetadataMessage;
16+
17+
use SplitIO\Test\Utils;
1318

1419
class CacheInterfacesTest extends \PHPUnit\Framework\TestCase
1520
{
@@ -31,18 +36,21 @@ public function testDiLog()
3136
public function testDiCache()
3237
{
3338
try {
34-
$cachePoolAdapter = array(
35-
'name' => 'redis',
36-
'options' => array(
37-
'host' => REDIS_HOST,
38-
'port' => REDIS_PORT,
39-
)
39+
$parameters = array(
40+
'host' => REDIS_HOST,
41+
'port' => REDIS_PORT,
4042
);
4143

42-
$cachePool = new Pool(array( 'adapter' => $cachePoolAdapter ));
44+
$cachePool = new Pool(array('adapter' => array(
45+
'name' => 'predis',
46+
'options' => array(
47+
'options' => array('prefix' => TEST_PREFIX),
48+
'parameters' => $parameters,
49+
),
50+
)));
4351
Di::getInstance()->setCache($cachePool);
4452
} catch (\Exception $e) {
45-
$this->assertTrue(false, "Error setting cache on Di");
53+
$this->assertTrue(false, "Error setting cache on Di". $e);
4654
}
4755

4856
$this->assertTrue(true);
@@ -57,6 +65,7 @@ public function testSplitCacheInterface()
5765
$splitChanges = file_get_contents(__DIR__."/../../files/splitChanges.json");
5866
$this->assertJson($splitChanges);
5967

68+
Utils\Utils::addSplitsInCache($splitChanges);
6069
$splitCache = new SplitCache();
6170

6271
$splitChanges = json_decode($splitChanges, true);
@@ -65,14 +74,7 @@ public function testSplitCacheInterface()
6574

6675
$splitName = $split['name'];
6776

68-
$this->assertTrue($splitCache->addSplit($splitName, json_encode($split)));
69-
7077
$this->assertEquals(strlen(json_encode($split)), strlen($splitCache->getSplit($splitName)));
71-
72-
$this->assertTrue($splitCache->removeSplit($splitName));
73-
74-
$this->assertTrue($splitCache->setChangeNumber($splitChanges['till']));
75-
7678
$this->assertEquals($splitChanges['till'], $splitCache->getChangeNumber());
7779
}
7880

@@ -82,48 +84,19 @@ public function testSplitCacheInterface()
8284
public function testSegmentCacheInterface()
8385
{
8486
$segmentChanges = file_get_contents(__DIR__."/../../files/segmentEmployeesChanges.json");
85-
8687
$this->assertJson($segmentChanges);
8788

88-
$segmentData = json_decode($segmentChanges, true);
89+
Utils\Utils::addSegmentsInCache($segmentChanges);
8990

91+
$segmentData = json_decode($segmentChanges, true);
9092
$segmentName = $segmentData['name'];
91-
9293
$segmentCache = new SegmentCache();
93-
94-
$this->assertArrayHasKey('fake_user_id_4', $segmentCache->addToSegment($segmentName, $segmentData['added']));
95-
96-
$removedResult = $segmentCache->removeFromSegment($segmentName, $segmentData['removed']);
97-
$this->assertArrayHasKey('fake_user_id_6', $removedResult);
98-
99-
$this->assertTrue($segmentCache->setChangeNumber($segmentName, $segmentData['till']));
94+
$this->assertTrue(boolval($segmentCache->isInSegment($segmentName, "fake_user_id_4")));
95+
$this->assertFalse(boolval($segmentCache->isInSegment($segmentName, "fake_user_id_4_")));
10096

10197
$this->assertEquals($segmentData['till'], $segmentCache->getChangeNumber($segmentName));
10298
}
10399

104-
/**
105-
* @depends testDiLog
106-
* @depends testDiCache
107-
*/
108-
public function testBlockUntilReadyCacheInterface()
109-
{
110-
$dateTimeUTC = new \DateTime("now", new \DateTimeZone("UTC"));
111-
$deltaTime = 100;
112-
113-
$splitsTimestamp = $dateTimeUTC->getTimestamp();
114-
$segmentsTimestamp = $dateTimeUTC->getTimestamp() + $deltaTime;
115-
116-
$bur = new BlockUntilReadyCache();
117-
$bur->setReadySplits($splitsTimestamp);
118-
$bur->setReadySegments($segmentsTimestamp);
119-
120-
//Checking
121-
$this->assertEquals($splitsTimestamp, $bur->getReadySplits());
122-
$this->assertEquals($segmentsTimestamp, $bur->getReadySegments());
123-
124-
$this->assertEquals(min($splitsTimestamp, $segmentsTimestamp), $bur->getReadyCheckpoint());
125-
}
126-
127100
/**
128101
* @depends testDiLog
129102
* @depends testDiCache
@@ -135,9 +108,8 @@ public function testEventsCache()
135108
$eventType = "some_event_type";
136109
$value = 0.0;
137110

138-
$eventDTO = new EventDTO($key, $trafficType, $eventType, $value);
139-
$eventMessageMetadata = new EventQueueMetadataMessage();
140-
$eventQueueMessage = new EventQueueMessage($eventMessageMetadata, $eventDTO);
111+
$eventDTO = new EventDTO($key, $trafficType, $eventType, $value, null);
112+
$eventQueueMessage = new EventQueueMessage(new QueueMetadataMessage(), $eventDTO);
141113

142114
$this->assertTrue(EventsCache::addEvent($eventQueueMessage));
143115
}
@@ -160,8 +132,7 @@ public function testEventsCacheWithProperties()
160132
);
161133

162134
$eventDTO = new EventDTO($key, $trafficType, $eventType, $value, $properties);
163-
$eventMessageMetadata = new EventQueueMetadataMessage();
164-
$eventQueueMessage = new EventQueueMessage($eventMessageMetadata, $eventDTO);
135+
$eventQueueMessage = new EventQueueMessage(new QueueMetadataMessage(), $eventDTO);
165136

166137
$this->assertTrue(EventsCache::addEvent($eventQueueMessage));
167138

tests/Suite/Redis/PRedisReadOnlyMock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(PRedis $predis)
1414
}
1515

1616

17-
/**
17+
/**
1818
* @param string $key
1919
* @return \SplitIO\Component\Cache\Item
2020
*/
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
namespace SplitIO\Test\Suite\Redis;
3+
4+
use ReflectionClass;
5+
6+
7+
use SplitIO\Component\Cache\Storage\Adapter\SafeRedisWrapper;
8+
use SplitIO\Component\Cache\Storage\Adapter\PRedis;
9+
10+
class SafeRedisWrapperTest extends \PHPUnit\Framework\TestCase
11+
{
12+
public function testAllMethodsException()
13+
{
14+
// Set redis-library client mock
15+
$cachePoolMethods = array('get', 'mget', 'rpush', 'keys', 'sismember', 'expire', 'getOptions');
16+
$predisMock = $this
17+
->getMockBuilder('\Predis\Client')
18+
->disableOriginalConstructor()
19+
->setMethods($cachePoolMethods)
20+
->getMock();
21+
22+
foreach ($cachePoolMethods as $method) {
23+
$predisMock->method($method)
24+
->will($this->throwException(new \Exception()));
25+
}
26+
27+
$predisMock->method('getOptions')
28+
->willReturn(array());
29+
30+
/*
31+
$reflectionPRedis = new ReflectionClass('SplitIO\Component\Cache\Storage\Adapter\PRedis');
32+
$parameters = array(
33+
'host' => REDIS_HOST,
34+
'port' => REDIS_PORT,
35+
);
36+
$predisAdapter = new PRedis(array(
37+
'parameters' => array(
38+
'scheme' => 'tcp',
39+
'host' => 'localhost',
40+
'port' => 6379,
41+
'timeout' => 881,
42+
'database' => 0
43+
)
44+
));
45+
$property = $reflectionPRedis->getProperty('client');
46+
$property->setAccessible(true);
47+
$property->setValue($predisMock, $predisMock);
48+
49+
// $predisAdapter->client = predisMock;
50+
51+
$safeRedisWrapper = new SafeRedisWrapper($reflectionPRedis);
52+
*/
53+
54+
$predisAdapter = new PRedis(array(
55+
'parameters' => array(
56+
'scheme' => 'tcp',
57+
'host' => 'localhost',
58+
'port' => 6379,
59+
)
60+
));
61+
$refObject = new \ReflectionObject($predisAdapter);
62+
$refProperty = $refObject->getProperty('client');
63+
$refProperty->setAccessible(true);
64+
$refProperty->setValue($predisAdapter, $predisMock);
65+
$safeRedisWrapper = new SafeRedisWrapper($predisAdapter);
66+
67+
$this->assertEquals(false, $safeRedisWrapper->getItem("some")->get());
68+
$this->assertEquals(array(), $safeRedisWrapper->getItems(array("some")));
69+
$this->assertEquals(false, $safeRedisWrapper->isOnList("some", "another"));
70+
$this->assertEquals(array(), $safeRedisWrapper->getKeys("some"));
71+
$this->assertEquals(0, $safeRedisWrapper->rightPushQueue("some", "another"));
72+
$this->assertEquals(false, $safeRedisWrapper->expireKey("some", 12345));
73+
}
74+
}

0 commit comments

Comments
 (0)