1<?php
2
3/**
4 * This file is part of the Phalcon.
5 *
6 * (c) Phalcon Team <team@phalconphp.com>
7 *
8 * For the full copyright and license information, please view the LICENSE.txt
9 * file that was distributed with this source code.
10 */
11
12namespace Phalcon\Test\Integration\Mvc\View;
13
14use Phalcon\Di;
15use IntegrationTester;
16use Phalcon\Mvc\View\Simple;
17use Phalcon\Cache\Backend\File;
18use Phalcon\Cache\Frontend\Output;
19use PHPUnit\Framework\SkippedTestError;
20
21/**
22 * Phalcon\Test\Integration\Mvc\View\SimpleCest
23 * Tests the Phalcon\Mvc\View\Simple component
24 *
25 * @package Phalcon\Test\Integration\Mvc\View
26 */
27class SimpleCest
28{
29    public function testSetVars(IntegrationTester $I)
30    {
31        $I->wantToTest('Set and get View vars');
32
33        $view = new Simple;
34        $view->setViewsDir(PATH_DATA . 'views/');
35
36        $I->assertNull($view->getVar('some_var'));
37        $some_var = time();
38        $view->setParamToView('some_var', $some_var);
39
40
41        $I->assertNull($view->getVar('another_var'));
42        $another_var = uniqid();
43        $view->setVar('another_var', $another_var);
44
45        $I->assertEquals($some_var, $view->getVar('some_var'));
46        $I->assertEquals($another_var, $view->getVar('another_var'));
47    }
48
49    public function testRenderWithCache(IntegrationTester $I)
50    {
51        $I->wantToTest('Render by using simple view with cache');
52
53        if (PHP_MAJOR_VERSION == 7) {
54            throw new SkippedTestError(
55                'Skipped in view of the experimental support for PHP 7.'
56            );
57        }
58
59        // Create cache at first run
60        $view = new Simple;
61        codecept_debug(gettype($view->getParamsToView()));
62        $view->setViewsDir(PATH_DATA . 'views/');
63
64        // No cache before DI is set
65        $I->assertFalse($view->getCache());
66
67        $view->setDI($this->getDi());
68        $I->assertEquals($view, $view->cache(['key' => 'view_simple_cache']));
69
70        $cache = $view->getCache();
71        $I->assertInstanceOf('Phalcon\Cache\BackendInterface', $cache);
72
73        $timeNow = time();
74        $view->setParamToView('a_cool_var', $timeNow);
75
76        $I->assertEquals("<p>$timeNow</p>", rtrim($view->render('test3/coolVar')));
77
78        $I->amInPath(PATH_CACHE);
79        $I->seeFileFound('view_simple_cache');
80        $I->seeInThisFile("<p>$timeNow</p>");
81
82        unset($view, $cache);
83
84        // Re-use the cached contents
85        $view = new Simple;
86        $view->setViewsDir(PATH_DATA . 'views/');
87        $view->setDI($this->getDi());
88        $view->cache(['key' => 'view_simple_cache']);
89
90        $I->assertEmpty($view->getContent());
91        $I->assertEquals("<p>$timeNow</p>", rtrim($view->render('test3/coolVar')));
92
93        $I->assertNotEmpty($view->getContent());
94        $I->assertEquals("<p></p>", rtrim($view->render('test3/coolVar')));
95
96        $I->deleteFile('view_simple_cache');
97    }
98
99    /**
100     * Setup viewCache service and DI
101     * @return Di
102     */
103    protected function getDi()
104    {
105        $di = new Di;
106
107        $di->set('viewCache', function () {
108            return new File(new Output(['lifetime' => 2]), ['cacheDir' => PATH_CACHE]);
109        });
110
111        return $di;
112    }
113}
114