Browse Source

Add a test that makes sure cell action is called only the first time when the cell cache is enabled

Yves P 10 years ago
parent
commit
5ab243eee4
2 changed files with 21 additions and 25 deletions
  1. 13 25
      tests/TestCase/View/CellTest.php
  2. 8 0
      tests/test_app/TestApp/View/Cell/ArticlesCell.php

+ 13 - 25
tests/TestCase/View/CellTest.php

@@ -19,6 +19,7 @@ use Cake\Controller\Controller;
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
 use Cake\Event\EventManager;
+use Cake\ORM\TableRegistry;
 use Cake\TestSuite\TestCase;
 use Cake\View\Cell;
 use Cake\View\CellTrait;
@@ -144,12 +145,6 @@ class CellTest extends TestCase
         $this->assertContains('This is the alternate template', "{$appCell}");
         $this->assertEquals('alternate_teaser_list', $appCell->template);
         $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
-
-        $appCell = $this->View->cell('Articles::customTemplate');
-
-        $this->assertContains('This is the alternate template', $appCell->render());
-        $this->assertEquals('alternate_teaser_list', $appCell->template);
-        $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
     }
 
     /**
@@ -163,11 +158,6 @@ class CellTest extends TestCase
 
         $this->assertContains('This is the alternate template', "{$appCell}");
         $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
-
-        $appCell = $this->View->cell('Articles::customTemplateViewBuilder');
-
-        $this->assertContains('This is the alternate template', $appCell->render());
-        $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->template());
     }
 
     /**
@@ -401,7 +391,7 @@ class CellTest extends TestCase
     }
 
     /**
-     * Test cached render.
+     * Test cached render when using an action changing the template used
      *
      * @return void
      */
@@ -425,26 +415,24 @@ class CellTest extends TestCase
     }
 
     /**
-     * Test cached render.
+     * Test that when the cell cache is enabled, the cell action is only invoke the first
+     * time the cell is rendered
      *
      * @return void
      */
     public function testCachedRenderSimpleCustomTemplateViewBuilder()
     {
-        $mock = $this->getMock('Cake\Cache\CacheEngine');
-        $mock->method('init')
-            ->will($this->returnValue(true));
-        $mock->method('read')
-            ->will($this->returnValue(false));
-        $mock->expects($this->once())
-            ->method('write')
-            ->with('cell_test_app_view_cell_articles_cell_customTemplateViewBuilder', "<h1>This is the alternate template</h1>\n");
-        Cache::config('default', $mock);
-
-        $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => true]);
+        Cache::config('default', [
+            'className' => 'File',
+            'path' => CACHE,
+        ]);
+        $cell = $this->View->cell('Articles::customTemplateViewBuilder', [], ['cache' => ['key' => 'celltest']]);
         $result = $cell->render();
+        $this->assertEquals(1, $cell->counter);
+        $cell->render();
+        $this->assertEquals(1, $cell->counter);
         $this->assertContains('This is the alternate template', $result);
-
+        Cache::delete('celltest');
         Cache::drop('default');
     }
 }

+ 8 - 0
tests/test_app/TestApp/View/Cell/ArticlesCell.php

@@ -28,6 +28,13 @@ class ArticlesCell extends \Cake\View\Cell
     protected $_validCellOptions = ['limit', 'page'];
 
     /**
+     * Counter used to test the cache cell feature
+     *
+     * @return void
+     */
+    public $counter = 0;
+
+    /**
      * Default cell action.
      *
      * @return void
@@ -71,6 +78,7 @@ class ArticlesCell extends \Cake\View\Cell
     public function customTemplateViewBuilder()
     {
         $this->template = 'derp';
+        $this->counter++;
         $this->viewBuilder()->template('alternate_teaser_list');
     }