Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests stubs for ViewSimple #1560

Merged
merged 10 commits into from
Nov 26, 2013
Next Next commit
Test stubs for ViewSimple
  • Loading branch information
WooDzu committed Nov 16, 2013
commit 7aa4c2f1813f7252bda8cf36b50e510634b082b4
1 change: 0 additions & 1 deletion unit-tests/TODO.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
- Tests for foreign keys cascade in the ORM
- Tests for many-to-many relations
- Tests for Phalcon\Mvc\View\Simple
- Tests for Phalcon\Mvc\View::partial
- Tests for macros in Volt
- Tests for +=, -=, *=, /=, ++, -- in Volt
154 changes: 154 additions & 0 deletions unit-tests/ViewSimpleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

use Phalcon\Di;
use Phalcon\Mvc\View\Exception;
use Phalcon\Mvc\View\Simple as View;
use Phalcon\Cache\Frontend\Output as FrontendCache;
use Phalcon\Cache\Backend\File as BackendCache;
use Phalcon\Cache\BackendInterface;

class ViewSimpleTest extends PHPUnit_Framework_TestCase
{
public function setUp()
{
foreach (new DirectoryIterator('unit-tests/cache/') as $item) {
$item->isDir() or unlink($item->getPathname());
}
}

public function testSettersAndGetters()
{
$view = new View();

$view->foo = 'bar';
$this->assertEquals('bar', $view->foo);

$this->assertEquals($view, $view->setVar('foo1', 'bar1'));
$this->assertEquals('bar1', $view->getVar('foo1'));

$expectedVars = array('foo2' => 'bar2', 'foo3' => 'bar3');
$this->assertEquals($view, $view->setVars($expectedVars));
$this->assertEquals('bar2', $view->foo2);
$this->assertEquals('bar3', $view->foo3);
$this->assertEquals($view, $view->setVars($expectedVars, false));

$this->assertEquals($view, $view->setParamToView('foo4', 'bar4'));

$expectedParamsToView = array('foo2' => 'bar2', 'foo3' => 'bar3', 'foo4' => 'bar4');
$this->assertEquals($expectedParamsToView, $view->getParamsToView());

$this->assertEquals($view, $view->setContent('<h1>hello</h1>'));
$this->assertEquals('<h1>hello</h1>', $view->getContent());

$view->setViewsDir('unit-tests/views/');
$this->assertEquals('unit-tests/views/', $view->getViewsDir());

$expectedCacheOptions = array("lifetime" => 86400, "key" => "simple-cache");
$this->assertEmpty($view->getCacheOptions());
$this->assertEquals($view, $view->setCacheOptions($expectedCacheOptions));
$this->assertEquals($expectedCacheOptions, $view->getCacheOptions());
}

/**
* @expectedException Exception
*/
public function testMissingView()
{
$view = new View;
$view->setViewsDir('unit-tests/views/');
$view->render('test1/index');
}

public function testRenderStandard()
{
$view = new View;
$view->setViewsDir('unit-tests/views/');

$this->assertEquals('here', $view->render('test2/index'));
$this->assertEquals('here', $view->getContent());
}

public function testRenderWithVariables()
{
$view = new View;
$view->setViewsDir('unit-tests/views/');

$this->assertEquals('here', $view->render('test3/other'));

$view->setParamToView('a_cool_var', 'le-this');
$this->assertEquals('<p>le-this</p>', $view->render('test3/another'));
}

public function testRenderWithRegisteredEngine()
{
$view = new View;
$view->setDI(new Di);
$view->setViewsDir('unit-tests/views/');
$view->setParamToView('name', 'FooBar');
$view->registerEngines(array('.mhtml' => 'Phalcon\\Mvc\\View\\Engine\\Volt'));

$this->assertEquals('Hello FooBar', $view->render('test4/index'));
}

public function testRenderWithPartials()
{
$view = new View;
$view->setViewsDir('unit-tests/views/');
$expectedParams = array('cool_var' => 'FooBar');

$view->partial('partials/_partial1', $expectedParams);
$this->assertEquals('Hey, this is a partial, also FooBar', $view->getContent());

$view->setVars($expectedParams);
$this->assertEquals('Hey, this is a partial, also FooBar', $view->render('test5/index'));
$this->assertEquals('Hey, this is a partial, also FooBar<br />Hey, this is a second partial, also FooBar', $view->render('test9/index'));
}

public function testRenderWithCache()
{
// Create cache at first run
$view = new View;
$view->setViewsDir('unit-tests/views/');

$this->assertFalse($view->getCache()); // No cache before DI is set
$view->setDI($this->_getDI());
$this->assertEquals($view, $view->cache(['key' => 'view_simple_cache']));

$cache = $view->getCache();
$this->assertInstanceOf('Phalcon\Cache\BackendInterface', $cache);
if (($cache instanceof BackendInterface) == false)
$this->fail('Expected BackendInterface');

$timeNow = time();
$view->setParamToView('a_cool_var', $timeNow);
$this->assertEquals("<p>$timeNow</p>", $view->render('test3/coolVar'));
unset($view, $cache);

// Re-use the cached contents
$view = new View;
$view->setViewsDir('unit-tests/views/');
$view->setDI($this->_getDI());
$view->cache(array('key' => 'view_simple_cache'));
$this->assertEquals("<p>$timeNow</p>", $view->render('test3/coolVar'));

// Cache should expire
$this->assertEquals("<p></p>", @$view->render('test3/coolVar'));
}

// Setup viewCache service and DI
private function _getDI()
{
$di = new Di;
$frontendCache = new FrontendCache(array('lifetime' => 2));
$backendCache = new BackendCache($frontendCache, array('cacheDir' => 'unit-tests/cache/'));
$di->set('viewCache', $backendCache);
return $di;
}

public function tearDown()
{
foreach (new DirectoryIterator('unit-tests/cache/') as $item) {
$item->isDir() or unlink($item->getPathname());
}
}
}
1 change: 1 addition & 0 deletions unit-tests/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<file>unit-tests/ViewEnginesTest.php</file>
<!--<file>unit-tests/ViewEnginesVoltTest.php</file>-->
<file>unit-tests/ViewCacheTest.php</file>
<file>unit-tests/ViewSimpleTest.php</file>

<!-- Other components -->
<file>unit-tests/ControllersTest.php</file>
Expand Down