Skip to content

Commit

Permalink
Update failing tests caused by changes in view events.
Browse files Browse the repository at this point in the history
Update failing view & helper tests. Account for the various API changes
in the Event system and simplify tests by changing which objects are
mocked.
  • Loading branch information
markstory committed Jul 3, 2013
1 parent 89bc7b5 commit dd085fb
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 43 deletions.
1 change: 0 additions & 1 deletion lib/Cake/Test/TestCase/Utility/DebuggerTest.php
Expand Up @@ -365,7 +365,6 @@ public function testExportVar() {
[protected] _currentType => ''
[protected] _stack => array()
[protected] _eventManager => object(Cake\Event\EventManager) {}
[protected] _eventManagerConfigured => false
}
TEXT;

Expand Down
25 changes: 9 additions & 16 deletions lib/Cake/Test/TestCase/View/Helper/CacheHelperTest.php
Expand Up @@ -120,16 +120,16 @@ public function testLayoutCacheParsingNoTagsInView() {

$View = new View($this->Controller);
$result = $View->render('index');
$this->assertNotRegExp('/cake:nocache/', $result);
$this->assertNotRegExp('/php echo/', $result);
$this->assertNotContains('cake:nocache', $result);
$this->assertNotContains('<?php echo', $result);

$filename = CACHE . 'views/cachetest_cache_parsing.php';
$this->assertTrue(file_exists($filename));

$contents = file_get_contents($filename);
$this->assertRegExp('/php echo \$variable/', $contents);
$this->assertRegExp('/php echo microtime()/', $contents);
$this->assertRegExp('/clark kent/', $result);
$this->assertContains('<?php echo $variable', $contents);
$this->assertContains('<?php echo microtime()', $contents);
$this->assertContains('clark kent', $result);

unlink($filename);
}
Expand Down Expand Up @@ -547,14 +547,7 @@ public function testAfterRenderConditions() {
->with('posts/index', 'content')
->will($this->returnValue(''));

$Cache->afterRenderFile('posts/index', 'content');

Configure::write('Cache.check', false);
$Cache->afterRender('posts/index');

Configure::write('Cache.check', true);
$View->cacheAction = false;
$Cache->afterRender('posts/index');
$Cache->afterRenderFile(null, 'posts/index', 'content');
}

/**
Expand All @@ -574,14 +567,14 @@ public function testAfterLayoutConditions() {
->with('posts/index', $View->fetch('content'))
->will($this->returnValue(''));

$Cache->afterLayout('posts/index');
$Cache->afterLayout(null, 'posts/index');

Configure::write('Cache.check', false);
$Cache->afterLayout('posts/index');
$Cache->afterLayout(null, 'posts/index');

Configure::write('Cache.check', true);
$View->cacheAction = false;
$Cache->afterLayout('posts/index');
$Cache->afterLayout(null, 'posts/index');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -741,7 +741,7 @@ public function testPassedArgsMergingWithUrlOptions() {

$this->Paginator->request->params['pass'] = array(2);
$this->Paginator->request->query = array('foo' => 'bar', 'x' => 'y');
$this->Paginator->beforeRender('posts/index');
$this->Paginator->beforeRender(null, 'posts/index');

$result = $this->Paginator->sort('title');
$expected = array(
Expand Down
10 changes: 6 additions & 4 deletions lib/Cake/Test/TestCase/View/HelperTest.php
Expand Up @@ -984,12 +984,14 @@ public function testThatHelperHelpersAreNotAttached() {
), App::RESET);
Plugin::loadAll();

$events = $this->getMock('\Cake\Event\EventManager');
$this->View->setEventManager($events);

$events->expects($this->never())
->method('attach');

$Helper = new TestHelper($this->View);
$Helper->OtherHelper;

$result = $this->View->Helpers->enabled();
$expected = array();
$this->assertEquals($expected, $result, 'Helper helpers were attached to the collection.');
}

/**
Expand Down
36 changes: 21 additions & 15 deletions lib/Cake/Test/TestCase/View/ViewTest.php
Expand Up @@ -699,13 +699,16 @@ public function testElementInexistent3() {
*
*/
public function testElementCallbacks() {
$mock = $this->getMock('Cake\View\Helper', [], [$this->View]);
$mock->expects($this->at(0))->method('beforeRender');
$mock->expects($this->at(1))->method('afterRender');
$this->View->Helpers->set('Test', $mock);
$this->View->Helpers->enable('Test');
$count = 0;
$callback = function ($event, $file) use (&$count) {
$count++;
};
$events = $this->View->getEventManager();
$events->attach($callback, 'View.beforeRender');
$events->attach($callback, 'View.afterRender');

$this->View->element('test_element', array(), array('callbacks' => true));
$this->assertEquals(2, $count);
}

/**
Expand Down Expand Up @@ -854,18 +857,20 @@ public function testLazyLoadHelpers() {
*/
public function testHelperCallbackTriggering() {
$View = new View($this->PostsController);
$View->helpers = array();
$View->Helpers = $this->getMock('Cake\View\HelperCollection', array('trigger'), array($View));

$View->Helpers->expects($this->at(0))->method('trigger')
$manager = $this->getMock('Cake\Event\EventManager');
$View->setEventManager($manager);

$manager->expects($this->at(0))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
$this->attributeEqualTo('_name', 'View.beforeRender'),
$this->attributeEqualTo('_subject', $View)
)
);
$View->Helpers->expects($this->at(1))->method('trigger')

$manager->expects($this->at(1))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
Expand All @@ -874,15 +879,16 @@ public function testHelperCallbackTriggering() {
)
);

$View->Helpers->expects($this->at(2))->method('trigger')
$manager->expects($this->at(2))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
$this->attributeEqualTo('_name', 'View.afterRenderFile'),
$this->attributeEqualTo('_subject', $View)
)
);
$View->Helpers->expects($this->at(3))->method('trigger')

$manager->expects($this->at(3))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
Expand All @@ -891,7 +897,7 @@ public function testHelperCallbackTriggering() {
)
);

$View->Helpers->expects($this->at(4))->method('trigger')
$manager->expects($this->at(4))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
Expand All @@ -900,7 +906,7 @@ public function testHelperCallbackTriggering() {
)
);

$View->Helpers->expects($this->at(5))->method('trigger')
$manager->expects($this->at(5))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
Expand All @@ -909,7 +915,7 @@ public function testHelperCallbackTriggering() {
)
);

$View->Helpers->expects($this->at(6))->method('trigger')
$manager->expects($this->at(6))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
Expand All @@ -918,7 +924,7 @@ public function testHelperCallbackTriggering() {
)
);

$View->Helpers->expects($this->at(7))->method('trigger')
$manager->expects($this->at(7))->method('dispatch')
->with(
$this->logicalAnd(
$this->isInstanceOf('Cake\Event\Event'),
Expand Down
1 change: 1 addition & 0 deletions lib/Cake/View/Helper.php
Expand Up @@ -46,6 +46,7 @@
* - `afterLayout(Event $event, $layoutFile)` - afterLayout is called after the layout has rendered.
* - `beforeRenderFile(Event $event, $viewFile)` - Called before any view fragment is rendered.
* - `afterRenderFile(Event $event, $viewFile, $content)` - Called after any view fragment is rendered.
* If a listener returns a non-null value, the output of the rendered file will be set to that.
*
* @package Cake.View
*/
Expand Down
6 changes: 4 additions & 2 deletions lib/Cake/View/Helper/CacheHelper.php
Expand Up @@ -64,10 +64,11 @@ protected function _enabled() {
/**
* Parses the view file and stores content for cache file building.
*
* @param Cake\Event\Event $event The event instance.
* @param string $viewFile
* @return void
*/
public function afterRenderFile($viewFile, $output) {
public function afterRenderFile($event, $viewFile, $output) {
if ($this->_enabled()) {
return $this->_parseContent($viewFile, $output);
}
Expand All @@ -76,10 +77,11 @@ public function afterRenderFile($viewFile, $output) {
/**
* Parses the layout file and stores content for cache file building.
*
* @param Cake\Event\Event $event The event instance.
* @param string $layoutFile
* @return void
*/
public function afterLayout($layoutFile) {
public function afterLayout($event, $layoutFile) {
if ($this->_enabled()) {
$this->_View->assign(
'content',
Expand Down
4 changes: 2 additions & 2 deletions lib/Cake/View/Helper/PaginatorHelper.php
Expand Up @@ -64,15 +64,15 @@ class PaginatorHelper extends Helper {
/**
* Before render callback. Overridden to merge passed args with url options.
*
* @param Cake\Event\Event $event The event instance.
* @param string $viewFile
* @return void
*/
public function beforeRender($viewFile) {
public function beforeRender($event, $viewFile) {
$this->options['url'] = array_merge($this->request->params['pass']);
if (!empty($this->request->query)) {
$this->options['url']['?'] = $this->request->query;
}
parent::beforeRender($viewFile);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions lib/Cake/View/View.php
Expand Up @@ -362,6 +362,18 @@ public function getEventManager() {
return $this->_eventManager;
}

/**
* Set the Eventmanager used by View.
*
* Primarily useful for testing.
*
* @param Cake\Event\EventManager $eventManager.
* @return void
*/
public function setEventManager(EventManager $eventManager) {
$this->_eventManager = $eventManager;
}

/**
* Renders a piece of PHP with provided parameters and returns HTML, XML, or any other string.
*
Expand Down Expand Up @@ -854,9 +866,10 @@ protected function _render($viewFile, $data = array()) {
$content = $this->_evaluate($viewFile, $data);

$afterEvent = new Event('View.afterRenderFile', $this, array($viewFile, $content));
$afterEvent->modParams = 1;
$eventManager->dispatch($afterEvent);
$content = $afterEvent->data[1];
if (isset($afterEvent->result)) {
$content = $afterEvent->result;
}

if (isset($this->_parents[$viewFile])) {
$this->_stack[] = $this->fetch('content');
Expand Down

0 comments on commit dd085fb

Please sign in to comment.