testWithCacheMiss()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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