AbstractViewWithCacheTest::buildMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Web;
3
4
use Fwlib\Cache\Handler\PhpArray as PhpArrayCacheHandler;
5
use Fwlib\Util\Common\Env as EnvUtil;
6
use Fwlib\Util\UtilContainer;
7
use Fwlib\Web\AbstractViewWithCache;
8
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
9
use PHPUnit_Framework_MockObject_MockObject as MockObject;
10
11
/**
12
 * @copyright   Copyright 2013-2015 Fwolf
13
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
14
 */
15
class AbstractViewWithCacheTest extends PHPUnitTestCase
16
{
17
    /**
18
     * @var EnvUtil
19
     */
20
    protected static $envUtilBackup = null;
21
22
    /**
23
     * @var string
24
     */
25
    protected static $requestUri = '';
26
27
28
    /**
29
     * @return MockObject | AbstractViewWithCache
30
     */
31
    protected function buildMock()
32
    {
33
        $mock = $this->getMock(
34
            AbstractViewWithCache::class,
35
            ['getCacheLifetime', 'getOutputBody']
36
        );
37
38
        // Long enough lifetime for test
39
        $mock->expects($this->any())
40
            ->method('getCacheLifetime')
41
            ->willReturn(60);
42
43
        // Mock un-cached output, remove header and footer, only body part
44
        // left, and use microtime to simulate output content, because their
45
        // value are different each time run.
46
        $this->reflectionSet($mock, 'outputParts', ['body']);
47
        $mock->expects($this->any())
48
            ->method('getOutputBody')
49
            ->will($this->returnCallback(function () {
50
                $datetimeUtil = UtilContainer::getInstance()->getDatetime();
51
                return $datetimeUtil->getMicroTime();
52
            }));
53
54
        /** @var AbstractViewWithCache $mock */
55
        $mock->setCacheHandler(new PhpArrayCacheHandler);
56
57
        return $mock;
58
    }
59
60
61 View Code Duplication
    public static function setUpBeforeClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        $utilContainer = UtilContainer::getInstance();
64
        self::$envUtilBackup = $utilContainer->getEnv();
65
66
        $testCase = new self;
67
        $envUtil = $testCase->getMock(EnvUtil::class, ['getServer']);
68
        $envUtil->expects($testCase->any())
69
            ->method('getServer')
70
            ->willReturnCallback(function() {
71
                return self::$requestUri;
72
            });
73
74
        $utilContainer->register('Env', $envUtil);
75
    }
76
77
78
    public static function tearDownAfterClass()
79
    {
80
        $utilContainer = UtilContainer::getInstance();
81
        $utilContainer->register('Env', self::$envUtilBackup);
82
    }
83
84
85
    public function testForceRefreshCache()
86
    {
87
        $view = $this->buildMock();
88
        $view->setUseCache(true);
89
90
        $view->setForceRefreshCache(false);
91
        $foo = $view->getOutput();
92
        $bar = $view->getOutput();
93
        $this->assertEquals($foo, $bar);
94
95
        $view->setForceRefreshCache(true);
96
        $bar = $view->getOutput();
97
        $this->assertNotEquals($foo, $bar);
98
    }
99
100
101
    public function testGetOutput()
102
    {
103
        $view = $this->buildMock();
104
105
        $view->setUseCache(false);
106
        $this->assertFalse($view->isUseCache());
107
108
        self::$requestUri = '';
109
110
        // Without cache
111
        $foo = $view->getOutput();
112
        $bar = $view->getOutput();
113
        $this->assertNotEquals($foo, $bar);
114
115
        // With cache
116
        $view->setUseCache(true);
117
        $this->assertTrue($view->isUseCache());
118
        $foo = $view->getOutput();
119
        $bar = $view->getOutput();
120
        $this->assertEquals($foo, $bar);
121
122
        // Change cache key, will got different result
123
        self::$requestUri = 'test.php?a=1&b=';
124
        $bar = $view->getOutput();
125
        $this->assertNotEquals($foo, $bar);
126
    }
127
}
128