|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + +------------------------------------------------------------------------+ |
| 5 | + | Phalcon Framework | |
| 6 | + +------------------------------------------------------------------------+ |
| 7 | + | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | |
| 8 | + +------------------------------------------------------------------------+ |
| 9 | + | This source file is subject to the New BSD License that is bundled | |
| 10 | + | with this package in the file docs/LICENSE.txt. | |
| 11 | + | | |
| 12 | + | If you did not receive a copy of the license and are unable to | |
| 13 | + | obtain it through the world-wide-web, please send an email | |
| 14 | + | to [email protected] so we can send you a copy immediately. | |
| 15 | + +------------------------------------------------------------------------+ |
| 16 | + | Authors: Andres Gutierrez <[email protected]> | |
| 17 | + | Eduar Carvajal <[email protected]> | |
| 18 | + | Piotr Gasiorowski <[email protected]> | |
| 19 | + +------------------------------------------------------------------------+ |
| 20 | +*/ |
| 21 | + |
| 22 | +use Phalcon\Di; |
| 23 | +use Phalcon\Mvc\View\Exception; |
| 24 | +use Phalcon\Mvc\View\Simple as View; |
| 25 | +use Phalcon\Cache\Frontend\Output as FrontendCache; |
| 26 | +use Phalcon\Cache\Backend\File as BackendCache; |
| 27 | +use Phalcon\Cache\BackendInterface; |
| 28 | + |
| 29 | +class ViewSimpleTest extends PHPUnit_Framework_TestCase |
| 30 | +{ |
| 31 | + public function setUp() |
| 32 | + { |
| 33 | + foreach (new DirectoryIterator('unit-tests/cache/') as $item) { |
| 34 | + $item->isDir() or unlink($item->getPathname()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public function testSettersAndGetters() |
| 39 | + { |
| 40 | + $view = new View(); |
| 41 | + |
| 42 | + $view->foo = 'bar'; |
| 43 | + $this->assertEquals('bar', $view->foo); |
| 44 | + |
| 45 | + $this->assertEquals($view, $view->setVar('foo1', 'bar1')); |
| 46 | + $this->assertEquals('bar1', $view->getVar('foo1')); |
| 47 | + |
| 48 | + $expectedVars = array('foo2' => 'bar2', 'foo3' => 'bar3'); |
| 49 | + $this->assertEquals($view, $view->setVars($expectedVars)); |
| 50 | + $this->assertEquals('bar2', $view->foo2); |
| 51 | + $this->assertEquals('bar3', $view->foo3); |
| 52 | + $this->assertEquals($view, $view->setVars($expectedVars, false)); |
| 53 | + |
| 54 | + $this->assertEquals($view, $view->setParamToView('foo4', 'bar4')); |
| 55 | + |
| 56 | + $expectedParamsToView = array('foo2' => 'bar2', 'foo3' => 'bar3', 'foo4' => 'bar4'); |
| 57 | + $this->assertEquals($expectedParamsToView, $view->getParamsToView()); |
| 58 | + |
| 59 | + $this->assertEquals($view, $view->setContent('<h1>hello</h1>')); |
| 60 | + $this->assertEquals('<h1>hello</h1>', $view->getContent()); |
| 61 | + |
| 62 | + $view->setViewsDir('unit-tests/views/'); |
| 63 | + $this->assertEquals('unit-tests/views/', $view->getViewsDir()); |
| 64 | + |
| 65 | + $expectedCacheOptions = array("lifetime" => 86400, "key" => "simple-cache"); |
| 66 | + $this->assertEmpty($view->getCacheOptions()); |
| 67 | + $this->assertEquals($view, $view->setCacheOptions($expectedCacheOptions)); |
| 68 | + $this->assertEquals($expectedCacheOptions, $view->getCacheOptions()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testMissingView() |
| 72 | + { |
| 73 | + $this->setExpectedException('Phalcon\Mvc\View\Exception'); |
| 74 | + $view = new View; |
| 75 | + $view->setViewsDir('unit-tests/views/'); |
| 76 | + $view->render('test1/index'); |
| 77 | + } |
| 78 | + |
| 79 | + public function testRenderStandard() |
| 80 | + { |
| 81 | + $view = new View; |
| 82 | + $view->setViewsDir('unit-tests/views/'); |
| 83 | + |
| 84 | + $this->assertEquals('here', $view->render('test2/index')); |
| 85 | + $this->assertEquals('here', $view->getContent()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testRenderWithVariables() |
| 89 | + { |
| 90 | + $view = new View; |
| 91 | + $view->setViewsDir('unit-tests/views/'); |
| 92 | + |
| 93 | + $this->assertEquals('here', $view->render('test3/other')); |
| 94 | + |
| 95 | + $view->setParamToView('a_cool_var', 'le-this'); |
| 96 | + $this->assertEquals('<p>le-this</p>', $view->render('test3/another')); |
| 97 | + } |
| 98 | + |
| 99 | + public function testRenderWithRegisteredEngine() |
| 100 | + { |
| 101 | + $view = new View; |
| 102 | + $view->setDI(new Di); |
| 103 | + $view->setViewsDir('unit-tests/views/'); |
| 104 | + $view->setParamToView('name', 'FooBar'); |
| 105 | + $view->registerEngines(array('.mhtml' => 'Phalcon\\Mvc\\View\\Engine\\Volt')); |
| 106 | + |
| 107 | + $this->assertEquals('Hello FooBar', $view->render('test4/index')); |
| 108 | + } |
| 109 | + |
| 110 | + public function testRenderWithPartials() |
| 111 | + { |
| 112 | + $view = new View; |
| 113 | + $view->setViewsDir('unit-tests/views/'); |
| 114 | + $expectedParams = array('cool_var' => 'FooBar'); |
| 115 | + |
| 116 | + ob_start(); |
| 117 | + $view->partial('partials/_partial1', $expectedParams); |
| 118 | + ob_clean(); |
| 119 | + $this->assertEquals('Hey, this is a partial, also FooBar', $view->getContent()); |
| 120 | + |
| 121 | + $view->setVars($expectedParams); |
| 122 | + $this->assertEquals('Hey, this is a partial, also FooBar', $view->render('test5/index')); |
| 123 | + $this->assertEquals('Hey, this is a partial, also FooBar<br />Hey, this is a second partial, also FooBar', $view->render('test9/index')); |
| 124 | + } |
| 125 | + |
| 126 | + public function testRenderWithCache() |
| 127 | + { |
| 128 | + // Create cache at first run |
| 129 | + $view = new View; |
| 130 | + $view->setViewsDir('unit-tests/views/'); |
| 131 | + |
| 132 | + $this->assertFalse($view->getCache()); // No cache before DI is set |
| 133 | + $view->setDI($this->_getDI()); |
| 134 | + $this->assertEquals($view, $view->cache(array('key' => 'view_simple_cache'))); |
| 135 | + |
| 136 | + $cache = $view->getCache(); |
| 137 | + $this->assertInstanceOf('Phalcon\Cache\BackendInterface', $cache); |
| 138 | + if (($cache instanceof BackendInterface) == false) |
| 139 | + $this->fail('Expected BackendInterface'); |
| 140 | + |
| 141 | + $timeNow = time(); |
| 142 | + $view->setParamToView('a_cool_var', $timeNow); |
| 143 | + $this->assertEquals("<p>$timeNow</p>", $view->render('test3/coolVar')); |
| 144 | + unset($view, $cache); |
| 145 | + |
| 146 | + // Re-use the cached contents |
| 147 | + $view = new View; |
| 148 | + $view->setViewsDir('unit-tests/views/'); |
| 149 | + $view->setDI($this->_getDI()); |
| 150 | + $view->cache(array('key' => 'view_simple_cache')); |
| 151 | + $this->assertEquals("<p>$timeNow</p>", $view->render('test3/coolVar')); |
| 152 | + |
| 153 | + // Cache should expire |
| 154 | + $this->assertEquals("<p></p>", @$view->render('test3/coolVar')); |
| 155 | + } |
| 156 | + |
| 157 | + // Setup viewCache service and DI |
| 158 | + private function _getDI() |
| 159 | + { |
| 160 | + $di = new Di; |
| 161 | + $frontendCache = new FrontendCache(array('lifetime' => 2)); |
| 162 | + $backendCache = new BackendCache($frontendCache, array('cacheDir' => 'unit-tests/cache/')); |
| 163 | + $di->set('viewCache', $backendCache); |
| 164 | + return $di; |
| 165 | + } |
| 166 | + |
| 167 | + public function tearDown() |
| 168 | + { |
| 169 | + foreach (new DirectoryIterator('unit-tests/cache/') as $item) |
| 170 | + { |
| 171 | + $item->isDir() or unlink($item->getPathname()); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments