Skip to content

Commit 3003df5

Browse files
committed
Fix redis store tests
1 parent c36fc04 commit 3003df5

2 files changed

Lines changed: 42 additions & 28 deletions

File tree

tests/src/SimpleSAML/Store/RedisStoreTest.php

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace SimpleSAML\Test\Store;
66

7-
use PHPUnit\Framework\{MockObject\MockObject, TestCase};
7+
use PHPUnit\Framework\TestCase;
88
use Predis\Client;
99
use SimpleSAML\{Configuration, Store};
1010

@@ -21,8 +21,8 @@
2121
*/
2222
class RedisStoreTest extends TestCase
2323
{
24-
/** @var \PHPUnit\Framework\MockObject\MockObject */
25-
protected MockObject $mocked_redis;
24+
/** @var \Predis\Client */
25+
protected Client $client;
2626

2727
/** @var \SimpleSAML\Store\RedisStore */
2828
protected Store\RedisStore $store;
@@ -37,24 +37,43 @@ protected function setUp(): void
3737
{
3838
$this->config = [];
3939

40-
$this->mocked_redis = $this->getMockBuilder(Client::class)
41-
->onlyMethods(['get', 'set', 'setex', 'del', 'disconnect', '__destruct'])
42-
->disableOriginalConstructor()
43-
->getMock();
44-
45-
$this->mocked_redis->method('get')
46-
->will($this->returnCallback([$this, 'getMocked']));
47-
48-
$this->mocked_redis->method('set')
49-
->will($this->returnCallback([$this, 'setMocked']));
50-
51-
$this->mocked_redis->method('setex')
52-
->will($this->returnCallback([$this, 'setexMocked']));
53-
54-
$this->mocked_redis->method('del')
55-
->will($this->returnCallback([$this, 'delMocked']));
56-
57-
$this->store = new Store\RedisStore($this->mocked_redis);
40+
$this->client = new class ($this) extends Client
41+
{
42+
public function __construct(
43+
protected TestCase $unitTest
44+
) {
45+
}
46+
47+
public function __deconstruct()
48+
{
49+
}
50+
51+
public function disconnect(): void
52+
{
53+
}
54+
55+
public function get(string $str): ?string
56+
{
57+
return $this->unitTest->getMocked($str);
58+
}
59+
60+
public function set(string $str, mixed $value): void
61+
{
62+
$this->unitTest->setMocked($str, $value);
63+
}
64+
65+
public function setEx(string $str, int $expire, mixed $value): void
66+
{
67+
$this->unitTest->setExMocked($str, $expire, $value);
68+
}
69+
70+
public function del(string $str): void
71+
{
72+
$this->unitTest->delMocked($str);
73+
}
74+
};
75+
76+
$this->store = new Store\RedisStore($this->client);
5877
}
5978

6079

@@ -72,7 +91,7 @@ public function getMocked(string $key): ?string
7291
* @param string $key
7392
* @param mixed $value
7493
*/
75-
public function setMocked(string $key, $value): void
94+
public function setMocked(string $key, mixed $value): void
7695
{
7796
$this->config[$key] = $value;
7897
}
@@ -83,7 +102,7 @@ public function setMocked(string $key, $value): void
83102
* @param int $expire
84103
* @param mixed $value
85104
*/
86-
public function setexMocked(string $key, int $expire, $value): void
105+
public function setexMocked(string $key, int $expire, mixed $value): void
87106
{
88107
// Testing expiring data is more trouble than it's worth for now
89108
$this->setMocked($key, $value);

tests/src/SimpleSAML/Store/StoreFactoryTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace SimpleSAML\Test;
66

77
use PHPUnit\Framework\{MockObject\MockObject, TestCase};
8-
use Predis\Client;
98
use ReflectionClass;
109
use SimpleSAML\{Configuration, Store};
1110
use SimpleSAML\Error\CriticalConfigurationError;
@@ -88,10 +87,6 @@ public function redisStore(): void
8887

8988
/** @psalm-var \SimpleSAML\Store\RedisStore $store */
9089
$store = StoreFactory::getInstance($storeType);
91-
$store->redis = $this->getMockBuilder(Client::class)
92-
->onlyMethods(['get', 'set', 'setex', 'del', 'disconnect', '__destruct'])
93-
->disableOriginalConstructor()
94-
->getMock();
9590

9691
$this->assertInstanceOf(Store\RedisStore::class, $store);
9792
}

0 commit comments

Comments
 (0)