1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Cmp\Cache\Decorator; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Decorator\CacheDecorator; |
6
|
|
|
use Cmp\Cache\Exceptions\NotFoundException; |
7
|
|
|
use PhpSpec\ObjectBehavior; |
8
|
|
|
use Prophecy\Argument; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\LogLevel; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LoggerCacheDecoratorSpec |
14
|
|
|
* |
15
|
|
|
* @package spec\Cmp\Cache\Application |
16
|
|
|
* @mixin \Cmp\Cache\Decorator\LoggerCache |
17
|
|
|
*/ |
18
|
|
|
class LoggerCacheSpec extends ObjectBehavior |
19
|
|
|
{ |
20
|
|
|
function let(CacheDecorator $decorated, LoggerInterface $logger) |
21
|
|
|
{ |
22
|
|
|
$this->beConstructedWith($decorated, $withExceptions = true, $logger, LogLevel::CRITICAL); |
23
|
|
|
$decorated->getDecoratedCache()->willReturn($decorated->getWrappedObject()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
function it_is_initializable() |
27
|
|
|
{ |
28
|
|
|
$this->shouldHaveType('\Cmp\Cache\Decorator\LoggerCache'); |
29
|
|
|
$this->shouldHaveType('\Cmp\Cache\Cache'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function it_can_get_the_inner_decorated_cache(CacheDecorator $anotherDecorator, CacheDecorator $decorated) |
33
|
|
|
{ |
34
|
|
|
$this->beConstructedWith($anotherDecorator); |
35
|
|
|
$anotherDecorator->getDecoratedCache()->willReturn($decorated); |
36
|
|
|
|
37
|
|
|
$this->getDecoratedCache()->shouldReturn($decorated); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function it_delegates_set_operations_correctly(CacheDecorator $decorated) |
41
|
|
|
{ |
42
|
|
|
$decorated->set('one', 'foo', 10)->willReturn(true); |
43
|
|
|
|
44
|
|
|
$exception = new \Exception("Server's on fire!"); |
45
|
|
|
$decorated->set('two', 'bar')->willThrow($exception); |
46
|
|
|
|
47
|
|
|
$this->set('one', 'foo', 10)->shouldBe(true); |
48
|
|
|
$this->shouldThrow('\Cmp\Cache\Exceptions\BackendOperationFailedException')->duringSet('two', 'bar'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function it_can_work_in_silent_mode(CacheDecorator $decorated) |
52
|
|
|
{ |
53
|
|
|
$withExceptions = false; |
54
|
|
|
$this->beConstructedWith($decorated, $withExceptions); |
55
|
|
|
|
56
|
|
|
$exception = new \Exception("Server's on fire!"); |
57
|
|
|
$decorated->set('foo', 'bar')->willThrow($exception); |
58
|
|
|
|
59
|
|
|
$this->set('foo', 'bar')->shouldReturn(false); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
function it_delegates_get_operations_correctly(CacheDecorator $decorated) |
63
|
|
|
{ |
64
|
|
|
$decorated->get('foo', null)->willReturn('bar'); |
65
|
|
|
|
66
|
|
|
$this->get('foo')->shouldReturn('bar'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
function it_delegates_demand_operations_correctly(CacheDecorator $decorated, LoggerInterface $logger) |
70
|
|
|
{ |
71
|
|
|
$exception = new NotFoundException('foo'); |
72
|
|
|
$decorated->demand('foo')->willThrow($exception); |
73
|
|
|
|
74
|
|
|
$this->shouldThrow($exception)->duringDemand('foo'); |
75
|
|
|
|
76
|
|
|
$logger->log(LogLevel::CRITICAL, Argument::containingString("cache operation failed"), [ |
77
|
|
|
'cache' => get_class($decorated), |
78
|
|
|
'decorated' => get_class($decorated->getWrappedObject()), |
79
|
|
|
'exception' => $exception, |
80
|
|
|
'method' => Argument::containingString('demand'), |
81
|
|
|
'arguments' => ['foo'], |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
function it_delegates_has_operations_correctly(CacheDecorator $decorated) |
86
|
|
|
{ |
87
|
|
|
$decorated->has('foo')->willReturn(true); |
88
|
|
|
|
89
|
|
|
$this->has('foo')->shouldReturn(true); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
function it_delegates_delete_operations_correctly(CacheDecorator $decorated) |
93
|
|
|
{ |
94
|
|
|
$decorated->delete('foo')->willReturn(false); |
95
|
|
|
|
96
|
|
|
$this->delete('foo')->shouldReturn(false); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
function it_delegates_flush_operations_correctly(CacheDecorator $decorated) |
100
|
|
|
{ |
101
|
|
|
$decorated->flush()->willReturn(false); |
102
|
|
|
|
103
|
|
|
$this->flush()->shouldReturn(false); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function it_delegates_set_items_operations_correctly(CacheDecorator $decorated) |
107
|
|
|
{ |
108
|
|
|
$decorated->setItems(['foo' => 'bar'], 10)->willReturn(true); |
109
|
|
|
|
110
|
|
|
$this->setItems(['foo' => 'bar'], 10)->shouldReturn(true); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
function it_delegates_get_items_operations_correctly(CacheDecorator $decorated) |
114
|
|
|
{ |
115
|
|
|
$decorated->getItems(['foo'])->willReturn(['foo' => 'bar']); |
116
|
|
|
|
117
|
|
|
$this->getItems(['foo'])->shouldReturn(['foo' => 'bar']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
function it_delegates_get_time_to_live_operations_correctly(CacheDecorator $decorated) |
121
|
|
|
{ |
122
|
|
|
$decorated->getTimeToLive('foo')->willReturn(false); |
123
|
|
|
|
124
|
|
|
$this->getTimeToLive('foo')->shouldReturn(false); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|