Welcome to mirror list, hosted at ThFree Co, Russian Federation.

LoaderCacheTest.php « Loader « Translation « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9fc4f43f5a8a806d8c8edb23a8073760fb70bc62 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Unit\Translation\Loader;

use Matomo\Cache\Backend\ArrayCache;
use Matomo\Cache\Lazy;
use Piwik\Translation\Loader\LoaderCache;

/**
 * @group Translation
 */
class LoaderCacheTest extends \PHPUnit\Framework\TestCase
{
    public function test_shouldNotLoad_ifInCache()
    {
        $cache = $this->getMock('Matomo\Cache\Lazy', array(), array(), '', false);
        $cache->expects($this->any())
            ->method('fetch')
            ->willReturn(array('translations!'));
        $wrappedLoader = $this->getMockForAbstractClass('Piwik\Translation\Loader\LoaderInterface');
        $wrappedLoader->expects($this->never())
            ->method('load');

        $loader = new LoaderCache($wrappedLoader, $cache);
        $translations = $loader->load('en', array('foo'));

        $this->assertEquals(array('translations!'), $translations);
    }

    public function test_shouldLoad_ifNotInCache()
    {
        $cache = $this->getMock('Matomo\Cache\Lazy', array(), array(), '', false);
        $cache->expects($this->any())
            ->method('fetch')
            ->willReturn(null);
        $wrappedLoader = $this->getMockForAbstractClass('Piwik\Translation\Loader\LoaderInterface');
        $wrappedLoader->expects($this->once())
            ->method('load')
            ->with('en', array('foo'))
            ->willReturn(array('translations!'));

        $loader = new LoaderCache($wrappedLoader, $cache);
        $translations = $loader->load('en', array('foo'));

        $this->assertEquals(array('translations!'), $translations);
    }

    public function test_shouldReLoad_ifDifferentDirectories()
    {
        $cache = new Lazy(new ArrayCache());

        $wrappedLoader = $this->getMockForAbstractClass('Piwik\Translation\Loader\LoaderInterface');
        $wrappedLoader->expects($this->exactly(2))
            ->method('load')
            ->willReturn(array('translations!'));

        $loader = new LoaderCache($wrappedLoader, $cache);

        // Should call the wrapped loader only once
        $loader->load('en', array('foo'));
        $loader->load('en', array('foo'));

        // Should call the wrapped loader a second time
        $loader->load('en', array('foo', 'bar'));
    }
}