|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\MeadSteve\DiceApi\Counters; |
|
4
|
|
|
|
|
5
|
|
|
use MeadSteve\DiceApi\Dice; |
|
6
|
|
|
use PhpSpec\ObjectBehavior; |
|
7
|
|
|
use Predis\Connection\ConnectionException; |
|
8
|
|
|
use Prophecy\Argument; |
|
9
|
|
|
|
|
10
|
|
|
require_once __DIR__ . "/RedisClientMock.php"; |
|
11
|
|
|
|
|
12
|
|
|
class RedisCounterSpec extends ObjectBehavior |
|
13
|
|
|
{ |
|
14
|
|
|
function let(RedisClient $redisClient) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->beConstructedWith($redisClient); |
|
17
|
|
|
$redisClient->hincrby("dice-count", Argument::any(), 1)->willReturn(true); |
|
18
|
|
|
$redisClient->hgetall("dice-count")->willReturn([]); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
function it_is_initializable() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->shouldHaveType('MeadSteve\DiceApi\Counters\RedisCounter'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
function it_counts_single_dice_as_the_correct_type(Dice $dice, RedisClient $redisClient) |
|
27
|
|
|
{ |
|
28
|
|
|
$dice->name()->willReturn(6); |
|
29
|
|
|
$this->count([$dice]); |
|
30
|
|
|
$redisClient->hincrby("dice-count", "6", 1)->shouldHaveBeenCalledTimes(1); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
function it_counts_multiple_dice_of_a_single_type(Dice $dice, RedisClient $redisClient) |
|
34
|
|
|
{ |
|
35
|
|
|
$dice->name()->willReturn(6); |
|
36
|
|
|
$this->count([$dice, $dice]); |
|
37
|
|
|
$redisClient->hincrby("dice-count", "6", 1)->shouldHaveBeenCalledTimes(2); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
function it_counts_multiple_dice_of_multiple_types(Dice $d6, Dice $d4, RedisClient $redisClient) |
|
41
|
|
|
{ |
|
42
|
|
|
$d6->name()->willReturn(6); |
|
43
|
|
|
$d4->name()->willReturn(4); |
|
44
|
|
|
$this->count([$d6, $d4, $d6]); |
|
45
|
|
|
$redisClient->hincrby("dice-count", "6", 1)->shouldHaveBeenCalledTimes(2); |
|
46
|
|
|
$redisClient->hincrby("dice-count", "4", 1)->shouldHaveBeenCalledTimes(1); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
function it_ignores_connection_errors(Dice $dice, RedisClient $redisClient) |
|
50
|
|
|
{ |
|
51
|
|
|
$dice->name()->willReturn(6); |
|
52
|
|
|
$redisClient->hincrby("dice-count", Argument::any(), 1)->willThrow(ConnectionException::class); |
|
53
|
|
|
$this->count([$dice])->shouldReturn(false); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
function it_returns_the_current_counts(RedisClient $redisClient, $diceCountData) |
|
57
|
|
|
{ |
|
58
|
|
|
$redisClient->hgetall("dice-count")->willReturn($diceCountData); |
|
59
|
|
|
$this->getCounts()->shouldReturn($diceCountData); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|