From e0dbfef54fd15d9aefe7f478099d16c81475a1b6 Mon Sep 17 00:00:00 2001 From: WooDzu Date: Sun, 17 Nov 2013 18:13:58 +0000 Subject: [PATCH 1/5] Stubs for Phalcon\Mvc\View --- unit-tests/ViewTest.php | 116 +++++++++++++++++++++++++--- unit-tests/views/html5.phtml | 1 + unit-tests/views/test15/index.phtml | 1 + 3 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 unit-tests/views/html5.phtml create mode 100644 unit-tests/views/test15/index.phtml diff --git a/unit-tests/ViewTest.php b/unit-tests/ViewTest.php index 6f4bca64ea9..3dff3c5396b 100644 --- a/unit-tests/ViewTest.php +++ b/unit-tests/ViewTest.php @@ -46,15 +46,70 @@ public function getLevels() class ViewTest extends PHPUnit_Framework_TestCase { + public function testSettersAndGetters() + { + $view = new View(); + + $this->assertEquals($view, $view->setBasePath(__DIR__.'/../')); + + $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('

hello

')); + $this->assertEquals('

hello

', $view->getContent()); + + $this->assertEquals($view, $view->setViewsDir('unit-tests/views/')); + $this->assertEquals('unit-tests/views/', $view->getViewsDir()); + + $this->assertEquals($view, $view->setLayoutsDir('unit-tests/views/layouts/')); + $this->assertEquals('unit-tests/views/layouts/', $view->getLayoutsDir()); + + $this->assertEquals($view, $view->setPartialsDir('unit-tests/views/partials/')); + $this->assertEquals('unit-tests/views/partials/', $view->getPartialsDir()); + + $this->assertEquals($view, $view->disableLevel(View::LEVEL_MAIN_LAYOUT)); + $this->assertEquals($view, $view->setRenderLevel(View::LEVEL_ACTION_VIEW)); + $this->assertEquals(View::LEVEL_ACTION_VIEW, $view->getRenderLevel()); + + $this->assertEquals($view, $view->setMainView('html5')); + $this->assertEquals('html5', $view->getMainView()); + + $this->assertEquals($view, $view->setLayout('test2')); + $this->assertEquals('test2', $view->getLayout()); + + $this->assertEquals($view, $view->setTemplateBefore('before')); + $this->assertEquals($view, $view->setTemplateAfter('after')); + $this->assertEquals($view, $view->cleanTemplateBefore()); + $this->assertEquals($view, $view->cleanTemplateAfter()); + + $view->start(); + $view->render('test2', 'index'); + $view->finish(); + + $this->assertEquals('test2', $view->getControllerName()); + $this->assertEquals('index', $view->getActionName()); + } public function testStandardRender() { $view = new View(); $view->setBasePath(__DIR__.'/../'); - $view->setViewsDir('unit-tests/views/'); - $this->assertEquals($view->getViewsDir(), 'unit-tests/views/'); //Standard Render $view->start(); @@ -67,7 +122,7 @@ public function testStandardRender() $view->finish(); $this->assertEquals($view->getContent(), 'lolhere'.PHP_EOL); - //Variables + //Variables $view->setParamToView('a_cool_var', 'le-this'); $view->start(); @@ -130,7 +185,7 @@ public function testStandardRender() public function testOverrideLayout() { - $view = new Phalcon\Mvc\View(); + $view = new View(); $view->setViewsDir('unit-tests/views/'); @@ -142,15 +197,27 @@ public function testOverrideLayout() $this->assertEquals($view->getContent(), 'Well, this is the view content: here.' . PHP_EOL); } + /** + * @covers \Phalcon\Mvc\View::setMainView + */ + public function testOverrideMainView() + { + $view = new View(); + $view->setViewsDir('unit-tests/views/'); + $view->setMainView('html5'); + + $view->start(); + $view->render('test2', 'index'); + $view->finish(); + $this->assertEquals($view->getContent(), 'here' . PHP_EOL); + } + public function testPartials() { - $view = new Phalcon\Mvc\View(); + $view = new View(); $view->setBasePath(__DIR__.'/../'); - $view->setViewsDir('unit-tests/views/'); - $this->assertEquals($view->getViewsDir(), 'unit-tests/views/'); - $view->setParamToView('cool_var', 'le-this'); $view->start(); @@ -168,7 +235,7 @@ public function testPartials() public function testGetRender() { - $view = new Phalcon\Mvc\View(); + $view = new View(); $view->setViewsDir('unit-tests/views/'); @@ -179,7 +246,7 @@ public function testGetRender() protected function _getViewDisabled($level=null) { - $view = new Phalcon\Mvc\View(); + $view = new View(); $view->setViewsDir('unit-tests/views/'); @@ -233,9 +300,36 @@ public function testDisableLevels() $this->assertEquals($view->getContent(), '
Action
'); } + /** + * @covers \Phalcon\Mvc\View::getActiveRenderPath + */ + public function testGetActiveRenderPath() + { + $di = new \Phalcon\Di; + $view = new View; + $listener = new ViewAfterRenderListener; + $eventsManager = new \Phalcon\Events\Manager; + + $di->set('view', $view); + $eventsManager->attach('view', $listener); + + $view->setEventsManager($eventsManager); + $view->setBasePath(__DIR__.'/../'); + $view->setViewsDir('unit-tests/views/'); + $view->setRenderLevel(View::LEVEL_ACTION_VIEW); + + $view->start(); + $view->render('test15', 'index'); + $view->finish(); + + $expectedPath = realpath('unit-tests/views/'); + $this->assertEquals($expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml', + realpath($view->getContent())); + } + public function testIssue907() { - $view = new \Phalcon\Mvc\View(); + $view = new View(); $view->setBasePath(__DIR__.'/../'); $view->setViewsDir('unit-tests/views/'); diff --git a/unit-tests/views/html5.phtml b/unit-tests/views/html5.phtml new file mode 100644 index 00000000000..09833136aff --- /dev/null +++ b/unit-tests/views/html5.phtml @@ -0,0 +1 @@ +getContent() ?> diff --git a/unit-tests/views/test15/index.phtml b/unit-tests/views/test15/index.phtml new file mode 100644 index 00000000000..ce0ac617920 --- /dev/null +++ b/unit-tests/views/test15/index.phtml @@ -0,0 +1 @@ +view->getActiveRenderPath(); ?> \ No newline at end of file From af6d16808a08abf45f6f1ade951b1c93422d202e Mon Sep 17 00:00:00 2001 From: WooDzu Date: Sun, 17 Nov 2013 18:33:21 +0000 Subject: [PATCH 2/5] Stubs for partials --- unit-tests/TODO.txt | 1 - unit-tests/ViewTest.php | 31 +++++++++++++++++++++++ unit-tests/views/partials/_partial3.phtml | 1 + unit-tests/views/test5/missing.phtml | 1 + unit-tests/views/test5/subpartial.phtml | 1 + 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 unit-tests/views/partials/_partial3.phtml create mode 100644 unit-tests/views/test5/missing.phtml create mode 100644 unit-tests/views/test5/subpartial.phtml diff --git a/unit-tests/TODO.txt b/unit-tests/TODO.txt index 3efa2c3d1a7..aaab78084c9 100644 --- a/unit-tests/TODO.txt +++ b/unit-tests/TODO.txt @@ -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 diff --git a/unit-tests/ViewTest.php b/unit-tests/ViewTest.php index 3dff3c5396b..53c0d56d154 100644 --- a/unit-tests/ViewTest.php +++ b/unit-tests/ViewTest.php @@ -220,19 +220,50 @@ public function testPartials() $view->setViewsDir('unit-tests/views/'); $view->setParamToView('cool_var', 'le-this'); + // Single partial $view->start(); $view->render('test5', 'index'); $view->finish(); $this->assertEquals($view->getContent(), 'Hey, this is a partial, also le-this' . PHP_EOL); + // Multiple partials $view->start(); $view->render('test9', 'index'); $view->finish(); $this->assertEquals($view->getContent(), 'Hey, this is a partial, also le-this
Hey, this is a second partial, also le-this' . PHP_EOL); + + // A partial within other partial + $view->start(); + $view->render('test5', 'subpartial'); + $view->finish(); + + $this->assertEquals('Including Hey, this is a partial, also le-this' . PHP_EOL, $view->getContent()); + + // Single partial in overridden main view + $view->setMainView('html5'); + $view->start(); + $view->render('test5', 'index'); + $view->finish(); + + $this->assertEquals('Hey, this is a partial, also le-this' . PHP_EOL, $view->getContent()); + } + public function testMissingPartial() + { + $this->setExpectedException('Phalcon\Mvc\View\Exception'); + + $view = new View(); + $view->setBasePath(__DIR__.'/../'); + $view->setViewsDir('unit-tests/views/'); + + $view->start(); + $view->render('test5', 'missing'); + $view->finish(); + } + public function testGetRender() { $view = new View(); diff --git a/unit-tests/views/partials/_partial3.phtml b/unit-tests/views/partials/_partial3.phtml new file mode 100644 index 00000000000..e28b2be59b1 --- /dev/null +++ b/unit-tests/views/partials/_partial3.phtml @@ -0,0 +1 @@ +Including partial("partials/_partial1") ?> \ No newline at end of file diff --git a/unit-tests/views/test5/missing.phtml b/unit-tests/views/test5/missing.phtml new file mode 100644 index 00000000000..8722a15abc9 --- /dev/null +++ b/unit-tests/views/test5/missing.phtml @@ -0,0 +1 @@ +partial("partials/missing"); ?> \ No newline at end of file diff --git a/unit-tests/views/test5/subpartial.phtml b/unit-tests/views/test5/subpartial.phtml new file mode 100644 index 00000000000..ac5a02bd0c3 --- /dev/null +++ b/unit-tests/views/test5/subpartial.phtml @@ -0,0 +1 @@ +partial("partials/_partial3"); ?> \ No newline at end of file From be2596a7adbae1a8602f4afd7d5a2a0e991fb045 Mon Sep 17 00:00:00 2001 From: WooDzu Date: Sun, 17 Nov 2013 18:48:09 +0000 Subject: [PATCH 3/5] debug travis =] --- unit-tests/ViewTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unit-tests/ViewTest.php b/unit-tests/ViewTest.php index 53c0d56d154..f3a3a4a53e6 100644 --- a/unit-tests/ViewTest.php +++ b/unit-tests/ViewTest.php @@ -356,6 +356,9 @@ public function testGetActiveRenderPath() $expectedPath = realpath('unit-tests/views/'); $this->assertEquals($expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml', realpath($view->getContent())); + +var_dump($expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml', + realpath($view->getContent())); } public function testIssue907() From d6c71048d6c6fab1e13274a5afcfd0bd9fed4fd4 Mon Sep 17 00:00:00 2001 From: WooDzu Date: Sun, 17 Nov 2013 19:07:24 +0000 Subject: [PATCH 4/5] possible solution for partial not seeing $this->view --- unit-tests/ViewTest.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/unit-tests/ViewTest.php b/unit-tests/ViewTest.php index f3a3a4a53e6..a5e660b27f9 100644 --- a/unit-tests/ViewTest.php +++ b/unit-tests/ViewTest.php @@ -337,13 +337,13 @@ public function testDisableLevels() public function testGetActiveRenderPath() { $di = new \Phalcon\Di; - $view = new View; $listener = new ViewAfterRenderListener; $eventsManager = new \Phalcon\Events\Manager; - - $di->set('view', $view); $eventsManager->attach('view', $listener); + $view = new View; + $di->set('view', $view); + $view->setDI($di); $view->setEventsManager($eventsManager); $view->setBasePath(__DIR__.'/../'); $view->setViewsDir('unit-tests/views/'); @@ -357,8 +357,9 @@ public function testGetActiveRenderPath() $this->assertEquals($expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml', realpath($view->getContent())); -var_dump($expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml', - realpath($view->getContent())); +echo 'Expected: ', $expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml'.PHP_EOL; +echo 'Actual: ', realpath($view->getContent()); + } public function testIssue907() From 61e73025be303c4ac25efe0aba8719f15bf445eb Mon Sep 17 00:00:00 2001 From: WooDzu Date: Sun, 17 Nov 2013 19:15:35 +0000 Subject: [PATCH 5/5] possible solution 2 for partial not seeing $this->view --- unit-tests/ViewTest.php | 5 ----- unit-tests/views/test15/index.phtml | 2 +- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/unit-tests/ViewTest.php b/unit-tests/ViewTest.php index a5e660b27f9..d5fe37fa9e3 100644 --- a/unit-tests/ViewTest.php +++ b/unit-tests/ViewTest.php @@ -342,7 +342,6 @@ public function testGetActiveRenderPath() $eventsManager->attach('view', $listener); $view = new View; - $di->set('view', $view); $view->setDI($di); $view->setEventsManager($eventsManager); $view->setBasePath(__DIR__.'/../'); @@ -356,10 +355,6 @@ public function testGetActiveRenderPath() $expectedPath = realpath('unit-tests/views/'); $this->assertEquals($expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml', realpath($view->getContent())); - -echo 'Expected: ', $expectedPath . DIRECTORY_SEPARATOR . 'test15' . DIRECTORY_SEPARATOR . 'index.phtml'.PHP_EOL; -echo 'Actual: ', realpath($view->getContent()); - } public function testIssue907() diff --git a/unit-tests/views/test15/index.phtml b/unit-tests/views/test15/index.phtml index ce0ac617920..38cd71e86e3 100644 --- a/unit-tests/views/test15/index.phtml +++ b/unit-tests/views/test15/index.phtml @@ -1 +1 @@ -view->getActiveRenderPath(); ?> \ No newline at end of file +getView()->getActiveRenderPath(); ?> \ No newline at end of file