-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
MultiBackendTest.php
115 lines (100 loc) · 3.63 KB
/
MultiBackendTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Neos\Cache\Tests\Unit\Backend;
include_once(__DIR__ . '/../../BaseTestCase.php');
use Neos\Cache\Backend\MultiBackend;
use Neos\Cache\Backend\NullBackend;
use Neos\Cache\Backend\RedisBackend;
use Neos\Cache\EnvironmentConfiguration;
use Neos\Cache\Tests\BaseTestCase;
/**
*
*/
class MultiBackendTest extends BaseTestCase
{
/**
* @test
*/
public function noExceptionIsThrownIfBackendFailsToBeCreated()
{
$backendOptions = [
'backendConfigurations' => [
[
// Will fail as there shouldn't be a redis usually on that port
'backend' => RedisBackend::class,
'backendOptions' => [
'port' => '60999'
]
]
]
];
$multiBackend = new MultiBackend($this->getEnvironmentConfiguration(), $backendOptions);
// We need to trigger initialization.
$result = $multiBackend->get('foo');
self::assertFalse($result);
}
/**
* @test
*/
public function debugModeWillBubbleExceptions()
{
$this->expectException(\Throwable::class);
$backendOptions = [
'debug' => true,
'backendConfigurations' => [
[
// Will fail as there shouldn't be a redis usually on that port
'backend' => RedisBackend::class,
'backendOptions' => [
'port' => '60999'
]
]
]
];
$multiBackend = new MultiBackend($this->getEnvironmentConfiguration(), $backendOptions);
// We need to trigger initialization.
$result = $multiBackend->get('foo');
self::assertFalse($result);
}
/**
* @test
*/
public function writesToAllBackends()
{
$mockBuilder = $this->getMockBuilder(NullBackend::class);
$firstNullBackendMock = $mockBuilder->getMock();
$secondNullBackendMock = $mockBuilder->getMock();
$firstNullBackendMock->expects(self::once())->method('set')->withAnyParameters()->willReturn(null);
$secondNullBackendMock->expects(self::once())->method('set')->withAnyParameters()->willReturn(null);
$multiBackend = new MultiBackend($this->getEnvironmentConfiguration(), []);
$this->inject($multiBackend, 'backends', [$firstNullBackendMock, $secondNullBackendMock]);
$this->inject($multiBackend, 'initialized', true);
$multiBackend->set('foo', 1);
}
/**
* @test
*/
public function fallsBackToSecondaryBackend()
{
$mockBuilder = $this->getMockBuilder(NullBackend::class);
$firstNullBackendMock = $mockBuilder->getMock();
$secondNullBackendMock = $mockBuilder->getMock();
$firstNullBackendMock->expects(self::once())->method('get')->with('foo')->willThrowException(new \Exception('Backend failure'));
$secondNullBackendMock->expects(self::once())->method('get')->with('foo')->willReturn(5);
$multiBackend = new MultiBackend($this->getEnvironmentConfiguration(), []);
$this->inject($multiBackend, 'backends', [$firstNullBackendMock, $secondNullBackendMock]);
$this->inject($multiBackend, 'initialized', true);
$result = $multiBackend->get('foo');
self::assertSame(5, $result);
}
/**
* @return EnvironmentConfiguration|\PHPUnit_Framework_MockObject_MockObject
*/
public function getEnvironmentConfiguration()
{
return new EnvironmentConfiguration(
__DIR__ . '~Testing',
'vfs://Foo/',
255
);
}
}