Skip to content

Commit aa14791

Browse files
committed
refactoring and finished BloomFilter integration tests from client
1 parent 1f4b831 commit aa14791

18 files changed

+212
-72
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <[email protected]>
5+
* @copyright 2019 Rafael Campoy <[email protected]>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\Client;
14+
15+
use Averias\RedisBloom\Adapter\RedisClientAdapterInterface;
16+
use Averias\RedisBloom\Exception\ResponseException;
17+
use Averias\RedisBloom\Parser\ParserTrait;
18+
19+
class BaseRedisBloomClient
20+
{
21+
use ParserTrait;
22+
23+
/** @var RedisClientAdapterInterface */
24+
protected $redisClientAdapter;
25+
26+
public function __construct(RedisClientAdapterInterface $redisClientAdapter)
27+
{
28+
$this->redisClientAdapter = $redisClientAdapter;
29+
}
30+
31+
/**
32+
* @param string $command
33+
* @param string $key
34+
* @param array $params
35+
* @return mixed
36+
* @throws ResponseException
37+
*/
38+
protected function executeBloomCommand(string $command, string $key, array $params = [])
39+
{
40+
$response = $this->redisClientAdapter->executeBloomCommand($command, $key, $params);
41+
return $this->parseResponse($command, $response);
42+
}
43+
}

src/RedisBloomClient/Client/RedisBloomClient.php

+1-25
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,12 @@
1414

1515
use Averias\RedisBloom\Command\Traits\BloomCommandTrait;
1616
use Averias\RedisBloom\Exception\ResponseException;
17-
use Averias\RedisBloom\Adapter\RedisClientAdapterInterface;
18-
use Averias\RedisBloom\Parser\ParserTrait;
1917
use Averias\RedisBloom\Validator\InputValidatorTrait;
2018

21-
class RedisBloomClient implements RedisBloomClientInterface
19+
class RedisBloomClient extends BaseRedisBloomClient implements RedisBloomClientInterface
2220
{
2321
use BloomCommandTrait;
2422
use InputValidatorTrait;
25-
use ParserTrait;
26-
27-
/** @var RedisClientAdapterInterface */
28-
protected $redisClientAdapter;
29-
30-
public function __construct(RedisClientAdapterInterface $redisClientAdapter)
31-
{
32-
$this->redisClientAdapter = $redisClientAdapter;
33-
}
3423

3524
/**
3625
* @param string $commandName
@@ -53,17 +42,4 @@ public function __call(string $name, array $arguments)
5342
{
5443
return $this->redisClientAdapter->executeCommandByName($name, $arguments);
5544
}
56-
57-
/**
58-
* @param string $command
59-
* @param string $key
60-
* @param array $params
61-
* @return mixed
62-
* @throws ResponseException
63-
*/
64-
protected function executeBloomCommand(string $command, string $key, array $params = [])
65-
{
66-
$response = $this->redisClientAdapter->executeBloomCommand($command, $key, $params);
67-
return $this->parseResponse($command, $response);
68-
}
6945
}

src/RedisBloomClient/DataTypes/BaseDataType.php

+3-19
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,21 @@
1313
namespace Averias\RedisBloom\DataTypes;
1414

1515
use Averias\RedisBloom\Adapter\RedisClientAdapterInterface;
16-
use Averias\RedisBloom\Exception\ResponseException;
16+
use Averias\RedisBloom\Client\BaseRedisBloomClient;
1717
use Averias\RedisBloom\Parser\ParserTrait;
1818
use Averias\RedisBloom\Validator\InputValidatorTrait;
1919

20-
class BaseDataType
20+
class BaseDataType extends BaseRedisBloomClient
2121
{
2222
use InputValidatorTrait;
2323
use ParserTrait;
2424

2525
/** @var string */
2626
protected $name;
2727

28-
/** @var RedisClientAdapterInterface */
29-
protected $redisClientAdapter;
30-
3128
public function __construct(string $filterName, RedisClientAdapterInterface $redisClientAdapter)
3229
{
30+
parent::__construct($redisClientAdapter);
3331
$this->name = $filterName;
34-
$this->redisClientAdapter = $redisClientAdapter;
35-
}
36-
37-
/**
38-
* @param string $command
39-
* @param string $key
40-
* @param array $params
41-
* @return mixed
42-
* @throws ResponseException
43-
*/
44-
protected function executeBloomCommand(string $command, string $key, array $params = [])
45-
{
46-
$response = $this->redisClientAdapter->executeBloomCommand($command, $key, $params);
47-
return $this->parseResponse($command, $response);
4832
}
4933
}

src/RedisBloomClient/Factory/RedisBloomClientFactoryInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212

1313
namespace Averias\RedisBloom\Factory;
1414

15+
use Averias\RedisBloom\Adapter\RedisClientAdapterInterface;
1516
use Averias\RedisBloom\Client\RedisBloomClientInterface;
17+
use Averias\RedisBloom\DataTypes\BloomFilter;
1618

1719
interface RedisBloomClientFactoryInterface
1820
{
21+
public function getAdapter(array $config = []): RedisClientAdapterInterface;
1922
public function createClient(array $config): RedisBloomClientInterface;
23+
public function createBloomFilter(string $filterName, array $config = []): BloomFilter;
2024
}

src/RedisBloomClient/Parser/Request/BloomFilterInsertOptionalParams.php

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BloomFilterInsertOptionalParams implements ParserInterface
2323
/**
2424
* @param $optionalParams
2525
* @return array
26+
* @throws ResponseException
2627
*/
2728
public function parse($optionalParams)
2829
{

src/RedisBloomClient/Parser/Response/ArrayOfIntegerToBool.php

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
class ArrayOfIntegerToBool extends BaseResponseParser implements ParserInterface
1818
{
19+
/**
20+
* @param $response
21+
* @return mixed
22+
*/
1923
public function parse($response)
2024
{
2125
foreach ($response as $key => $item) {

src/RedisBloomClient/Parser/Response/BaseResponseParser.php

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
class BaseResponseParser
1616
{
17+
/**
18+
* @param int $data
19+
* @return bool
20+
*/
1721
public function convertIntegerToBool(int $data)
1822
{
1923
return (bool) $data;

src/RedisBloomClient/Parser/Response/IntegerToBool.php

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
class IntegerToBool extends BaseResponseParser implements ParserInterface
1818
{
19+
/**
20+
* @param $response
21+
* @return bool
22+
*/
1923
public function parse($response)
2024
{
2125
return $this->convertIntegerToBool($response);

src/RedisBloomClient/Parser/Response/OkToTrue.php

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
class OkToTrue implements ParserInterface
1919
{
20+
/**
21+
* @param $response
22+
* @return bool
23+
* @throws ResponseException
24+
*/
2025
public function parse($response): bool
2126
{
2227
if (!is_string($response)) {

tests/Integration/BaseTestIntegration.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public static function tearDownAfterClass(): void
3939
}
4040
}
4141

42-
protected static function getReBloomClientConfig()
42+
/**
43+
* @return array
44+
*/
45+
protected static function getReBloomClientConfig(): array
4346
{
4447
return [
4548
Connection::HOST => REDIS_TEST_SERVER,

tests/Integration/Command/Traits/BloomFilter/BloomFilterAddCommandTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class BloomFilterAddCommandTest extends BaseTestIntegration
2323
* @param $item
2424
* @param bool $expectedResult
2525
*/
26-
public function testAddItemSuccessfully(string $key, $item, bool $expectedResult)
26+
public function testAddItemSuccessfully(string $key, $item, bool $expectedResult): void
2727
{
2828
$result = static::$reBloomClient->bloomFilterAdd($key, $item);
2929
$this->assertSame($expectedResult, $result);
@@ -34,13 +34,13 @@ public function testAddItemSuccessfully(string $key, $item, bool $expectedResult
3434
* @param string $key
3535
* @param $item
3636
*/
37-
public function testAddItemException(string $key, $item)
37+
public function testAddItemException(string $key, $item): void
3838
{
3939
$this->expectException(ResponseException::class);
4040
static::$reBloomClient->bloomFilterAdd($key, $item);
4141
}
4242

43-
public function getSuccessDataProvider()
43+
public function getSuccessDataProvider(): array
4444
{
4545
return [
4646
['key-add1', 12, true],

tests/Integration/Command/Traits/BloomFilter/BloomFilterExistsCommandTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ public static function setUpBeforeClass():void
2929
* @param string $key
3030
* @param $item
3131
*/
32-
public function testExistsItem(string $key, $item)
32+
public function testExistsItem(string $key, $item): void
3333
{
3434
$result = static::$reBloomClient->bloomFilterExists($key, $item);
3535
$this->assertTrue($result);
3636
}
3737

38-
public function testDoesntExistItem()
38+
public function testDoesntExistItem(): void
3939
{
4040
$result = static::$reBloomClient->bloomFilterExists(Keys::DEFAULT_KEY, 'bar');
4141
$this->assertFalse($result);
4242
}
4343

44-
public function testNonExistentKey()
44+
public function testNonExistentKey(): void
4545
{
4646
$result = static::$reBloomClient->bloomFilterExists('nonexistent-key', 'bar');
4747
$this->assertFalse($result);
@@ -52,13 +52,13 @@ public function testNonExistentKey()
5252
* @param string $key
5353
* @param $item
5454
*/
55-
public function testExistsItemException(string $key, $item)
55+
public function testExistsItemException(string $key, $item): void
5656
{
5757
$this->expectException(ResponseException::class);
5858
static::$reBloomClient->bloomFilterExists($key, $item);
5959
}
6060

61-
public function getExistsDataProvider()
61+
public function getExistsDataProvider(): array
6262
{
6363
return [
6464
[Keys::DEFAULT_KEY, 'foo'],
@@ -69,7 +69,7 @@ public function getExistsDataProvider()
6969
];
7070
}
7171

72-
public function getDataProviderForException()
72+
public function getDataProviderForException(): array
7373
{
7474
return [
7575
[Keys::DEFAULT_KEY, [[1, 2]]],

tests/Integration/Command/Traits/BloomFilter/BloomFilterInsertCommandTest.php

+14-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ class BloomFilterInsertCommandTest extends BaseTestIntegration
2525
* @param array $options
2626
* @param array $expectedResult
2727
*/
28-
public function testInsertSuccessfullyWithNoOptions(string $key, array $items, array $options, array $expectedResult)
29-
{
28+
public function testInsertSuccessfullyWithNoOptions(
29+
string $key,
30+
array $items,
31+
array $options,
32+
array $expectedResult
33+
): void {
3034
$result = static::$reBloomClient->bloomFilterInsert($key, $items, $options);
3135
$this->assertSame($expectedResult, $result);
3236
}
@@ -38,8 +42,12 @@ public function testInsertSuccessfullyWithNoOptions(string $key, array $items, a
3842
* @param array $options
3943
* @param array $expectedResult
4044
*/
41-
public function testInsertSuccessfullyWithOptions(string $key, array $items, array $options, array $expectedResult)
42-
{
45+
public function testInsertSuccessfullyWithOptions(
46+
string $key,
47+
array $items,
48+
array $options,
49+
array $expectedResult
50+
): void {
4351
$result = static::$reBloomClient->bloomFilterInsert($key, $items, $options);
4452
$this->assertSame($expectedResult, $result);
4553
}
@@ -50,7 +58,7 @@ public function testInsertSuccessfullyWithOptions(string $key, array $items, arr
5058
* @param array $items
5159
* @param array $options
5260
*/
53-
public function testInsertException(string $key, array $items, array $options)
61+
public function testInsertException(string $key, array $items, array $options): void
5462
{
5563
$this->expectException(ResponseException::class);
5664
static::$reBloomClient->bloomFilterInsert($key, $items, $options);
@@ -112,7 +120,7 @@ public function getSuccessWithOptionsDataProvider(): array
112120
];
113121
}
114122

115-
public function getDataProviderForException()
123+
public function getDataProviderForException(): array
116124
{
117125
return [
118126
[ // fails cause with NOCREATE filter must exist
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* @project phpredis-bloom
4+
* @author Rafael Campoy <[email protected]>
5+
* @copyright 2019 Rafael Campoy <[email protected]>
6+
* @license MIT
7+
* @link https://github.com/averias/php-rejson
8+
*
9+
* Copyright and license information, is included in
10+
* the LICENSE file that is distributed with this source code.
11+
*/
12+
13+
namespace Averias\RedisBloom\Tests\Integration\Command\Traits\BloomFilter;
14+
15+
use Averias\RedisBloom\Enum\Keys;
16+
use Averias\RedisBloom\Exception\ResponseException;
17+
use Averias\RedisBloom\Tests\BaseTestIntegration;
18+
19+
class BloomFilterLoadChunkTest extends BaseTestIntegration
20+
{
21+
protected static $data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
22+
23+
public static function setUpBeforeClass():void
24+
{
25+
parent::setUpBeforeClass();
26+
static::$reBloomClient->bloomFilterMultiAdd(Keys::DEFAULT_KEY, ...self::$data);
27+
}
28+
29+
public function testLoadChunk(): void
30+
{
31+
$iterator = 0;
32+
while (true) {
33+
list ($iterator, $data) = $result = static::$reBloomClient->bloomFilterScanDump(
34+
Keys::DEFAULT_KEY,
35+
$iterator
36+
);
37+
if ($iterator == 0) {
38+
break;
39+
}
40+
$result = static::$reBloomClient->bloomFilterLoadChunk('copy-filter', $iterator, $data);
41+
$this->assertTrue($result);
42+
}
43+
44+
$copiedData = static::$reBloomClient->bloomFilterMultiExists('copy-filter', ...self::$data);
45+
foreach ($copiedData as $item) {
46+
$this->assertTrue($item);
47+
}
48+
}
49+
50+
public function testLoadChunkException(): void
51+
{
52+
$this->expectException(ResponseException::class);
53+
static::$reBloomClient->bloomFilterLoadChunk('nonexistent', 556, 1);
54+
}
55+
}

0 commit comments

Comments
 (0)