Completed
Push — master ( 32dcda...951284 )
by
unknown
06:45 queued 10s
created

testTermFallbackCacheFactoryCachesInMemory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 9.0472
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare( strict_types = 1 );
4
namespace Wikibase\Lib\Tests\TermFallbackCache;
5
6
use BagOStuff;
7
use CachedBagOStuff;
8
use IBufferingStatsdDataFactory;
9
use Iterator;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Log\LoggerInterface;
12
use Wikibase\Lib\SimpleCacheWithBagOStuff;
13
use Wikibase\Lib\StatsdRecordingSimpleCache;
14
use Wikibase\Lib\TermFallbackCache\TermFallbackCacheServiceFactory;
15
use Wikibase\Lib\TermFallbackCacheFactory;
16
17
/**
18
 * @covers \Wikibase\Lib\TermFallbackCacheFactory
19
 *
20
 * @group Wikibase
21
 *
22
 * @license GPL-2.0-or-later
23
 */
24
class TermFallbackCacheFactoryTest extends TestCase {
25
26
	/**
27
	 * @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
28
	 */
29
	private $mockLogger;
30
31
	/**
32
	 * @var IBufferingStatsdDataFactory|\PHPUnit\Framework\MockObject\MockObject
33
	 */
34
	private $mockIBufferingStatsdDataFactory;
35
36
	public function setUp(): void {
37
		$this->mockLogger = $this->createMock( LoggerInterface::class );
38
		$this->mockIBufferingStatsdDataFactory = $this->createMock( IBufferingStatsdDataFactory::class );
39
40
		parent::setUp();
41
	}
42
43
	public function testTermFallbackCacheFactoryCachesInMemory(): void {
44
		$sharedCacheType = CACHE_DB;
45
		$cacheSecret = 'secret';
46
47
		$mockSharedCache = $this->createMock( BagOStuff::class );
48
		$mockInMemoryCache = $this->createMock( CachedBagOStuff::class );
49
		$mockCache = $this->createMock( SimpleCacheWithBagOStuff::class );
50
		$mockStatsdRecordingSimpleCache = $this->createMock( StatsdRecordingSimpleCache::class );
51
52
		$serviceFactory = $this->createMock( TermFallbackCacheServiceFactory::class );
53
54
		$serviceFactory->expects( $this->once() )
55
			->method( 'newSharedCache' )
56
			->with( $sharedCacheType )
57
			->willReturn( $mockSharedCache );
58
59
		$serviceFactory->expects( $this->once() )
60
			->method( 'newInMemoryCache' )
61
			->with( $mockSharedCache )
62
			->willReturn( $mockInMemoryCache );
63
64
		$mockCache->expects( $this->once() )
65
			->method( 'setLogger' )
66
			->with( $this->mockLogger );
67
		$serviceFactory->expects( $this->once() )
68
			->method( 'newCache' )
69
			->with( $mockInMemoryCache, 'wikibase.repo.formatter.', $cacheSecret )
70
			->willReturn( $mockCache );
71
72
		$serviceFactory->expects( $this->once() )
73
			->method( 'newStatsdRecordingCache' )
74
			->with(
75
				$mockCache,
76
				$this->mockIBufferingStatsdDataFactory,
77
				[
78
					'miss' => 'wikibase.repo.formatterCache.miss',
79
					'hit' => 'wikibase.repo.formatterCache.hit',
80
				]
81
			)
82
			->willReturn( $mockStatsdRecordingSimpleCache );
83
84
		$factory = new TermFallbackCacheFactory(
85
			$sharedCacheType,
86
			$this->mockLogger,
87
			$this->mockIBufferingStatsdDataFactory,
88
			$cacheSecret,
89
			$serviceFactory,
90
			null
91
		);
92
93
		$this->assertInstanceOf( StatsdRecordingSimpleCache::class, $factory->getTermFallbackCache() );
94
	}
95
96
	public function testTermFallbackCacheFactoryDoesNotCacheInMemoryRedundantly(): void {
97
		$sharedCacheType = CACHE_NONE;
98
		$cacheSecret = 'secret';
99
100
		$mockSharedCache = $this->createMock( CachedBagOStuff::class );
101
		$mockCache = $this->createMock( SimpleCacheWithBagOStuff::class );
102
		$mockStatsdRecordingSimpleCache = $this->createMock( StatsdRecordingSimpleCache::class );
103
104
		$serviceFactory = $this->createMock( TermFallbackCacheServiceFactory::class );
105
106
		$serviceFactory->expects( $this->once() )
107
			->method( 'newSharedCache' )
108
			->with( $sharedCacheType )
109
			->willReturn( $mockSharedCache );
110
111
		$serviceFactory
112
			->expects( $this->never() )
113
			->method( 'newInMemoryCache' );
114
115
		$mockCache->expects( $this->once() )
116
			->method( 'setLogger' )
117
			->with( $this->mockLogger );
118
		$serviceFactory->expects( $this->once() )
119
			->method( 'newCache' )
120
			->with( $mockSharedCache, 'wikibase.repo.formatter.', $cacheSecret )
121
			->willReturn( $mockCache );
122
123
		$serviceFactory->expects( $this->once() )
124
			->method( 'newStatsdRecordingCache' )
125
			->with(
126
				$mockCache,
127
				$this->mockIBufferingStatsdDataFactory,
128
				[
129
					'miss' => 'wikibase.repo.formatterCache.miss',
130
					'hit' => 'wikibase.repo.formatterCache.hit',
131
				]
132
			)
133
			->willReturn( $mockStatsdRecordingSimpleCache );
134
135
		$factory = new TermFallbackCacheFactory(
136
			$sharedCacheType,
137
			$this->mockLogger,
138
			$this->mockIBufferingStatsdDataFactory,
139
			$cacheSecret,
140
			$serviceFactory,
141
			null
142
		);
143
144
		$this->assertInstanceOf( StatsdRecordingSimpleCache::class, $factory->getTermFallbackCache() );
145
	}
146
147
	/**
148
	 * @dataProvider provideVersionTestData
149
	 */
150
	public function testTermFallbackCacheFactoryLeveragesVersionCorrectly( $version, $cacheKey ): void {
151
		$sharedCacheType = CACHE_DB;
152
		$cacheSecret = 'secret';
153
154
		$mockInMemoryCache = $this->createMock( CachedBagOStuff::class );
155
156
		$serviceFactory = $this->createMock( TermFallbackCacheServiceFactory::class );
157
158
		$serviceFactory
159
			->expects( $this->once() )
160
			->method( 'newInMemoryCache' )
161
			->willReturn( $mockInMemoryCache );
162
		$serviceFactory->expects( $this->once() )
163
			->method( 'newCache' )
164
			->with( $mockInMemoryCache, $cacheKey, $cacheSecret );
165
166
		$factory = new TermFallbackCacheFactory(
167
			$sharedCacheType,
168
			$this->mockLogger,
169
			$this->mockIBufferingStatsdDataFactory,
170
			$cacheSecret,
171
			$serviceFactory,
172
			$version
173
		);
174
175
		$this->assertInstanceOf( StatsdRecordingSimpleCache::class, $factory->getTermFallbackCache() );
176
	}
177
178
	public function provideVersionTestData(): Iterator {
179
		yield 'no version' => [ null, 'wikibase.repo.formatter.' ];
180
		yield 'with version' => [ 5, 'wikibase.repo.formatter.5.' ];
181
	}
182
183
}
184