CachedJsonLdDataValueFormatterTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testWithCacheHit() 0 12 1
A testWithCacheMiss() 0 14 1
1
<?php
2
3
namespace PPP\Wikidata\ValueFormatters;
4
5
use DataValues\StringValue;
6
use Doctrine\Common\Cache\ArrayCache;
7
use PPP\Wikidata\Cache\JsonLdDataValueFormatterCache;
8
9
/**
10
 * @covers PPP\Wikidata\ValueFormatters\CachedJsonLdDataValueFormatter
11
 *
12
 * @licence AGPLv3+
13
 * @author Thomas Pellissier Tanon
14
 */
15
class CachedJsonLdDataValueFormatterTest extends \PHPUnit_Framework_TestCase {
16
17
	public function testWithCacheHit() {
18
		$formatterMock = $this->getMock('PPP\Wikidata\ValueFormatters\JsonLd\JsonLdDataValueFormatter');
19
		$formatterMock->expects($this->never())
20
			->method('format');
21
22
		$cache = new JsonLdDataValueFormatterCache(new ArrayCache(), 'foo');
23
		$cache->save(new StringValue('foo'), (object) array('bar' => 1));
24
25
		$formatter = new CachedJsonLdDataValueFormatter($formatterMock, $cache);
26
27
		$this->assertEquals((object) array('bar' => 1), $formatter->format(new StringValue('foo')));
28
	}
29
30
	public function testWithCacheMiss() {
31
		$formatterMock = $this->getMock('PPP\Wikidata\ValueFormatters\JsonLd\JsonLdDataValueFormatter');
32
		$formatterMock->expects($this->once())
33
			->method('format')
34
			->with($this->equalTo(new StringValue('foo')))
35
			->will($this->returnValue((object) array('bar' => 1)));
36
37
		$cache = new JsonLdDataValueFormatterCache(new ArrayCache(), 'foo');
38
39
		$formatter = new CachedJsonLdDataValueFormatter($formatterMock, $cache);
40
41
		$this->assertEquals((object) array('bar' => 1), $formatter->format(new StringValue('foo')));
42
		$this->assertTrue($cache->contains(new StringValue('foo')));
43
	}
44
}
45