|
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
|
|
|
|