Blame view

mobile/source/ext/phptal/tests/TriggerTest.php 2.82 KB
a1684257   Administrator   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  <?php
  /**
   * PHPTAL templating engine
   *
   * PHP Version 5
   *
   * @category HTML
   * @package  PHPTAL
   * @author   Laurent Bedubourg <lbedubourg@motion-twin.com>
   * @author   Kornel LesiƄski <kornel@aardvarkmedia.co.uk>
   * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
   * @version  SVN: $Id: TriggerTest.php 582 2009-04-26 01:24:36Z kornel $
   * @link     http://phptal.org/
   */
  
  require_once dirname(__FILE__)."/config.php";
  
  PHPTAL::setIncludePath();
  require_once 'PHPTAL/Trigger.php';
  PHPTAL::restoreIncludePath();
  
  class StupidCacheTrigger implements PHPTAL_Trigger
  {
      public $isCaching = false;
      public $cachePath = '';
  
      public function start($phptalId, $tpl)
      {
          $this->cachePath = 'trigger.' . $tpl->getContext()->someId;
  
          // if already cached, read the cache and tell PHPTAL to
          // ignore the tag content
          if (file_exists($this->cachePath)) {
              $this->isCaching = false;
              readfile($this->cachePath);
              return self::SKIPTAG;
          }
  
          // no cache, we start and output buffer and tell
          // PHPTAL to proceed (ie: execute the tag content)
          $this->isCaching = true;
          ob_start();
          return self::PROCEED;
      }
  
      public function end($phptalId, $tpl)
      {
          // end of tag, if cached file used, do nothing
          if (!$this->isCaching)
              return;
  
          // otherwise, get the content of the output buffer
          // and write it into the cache file for later usage
          $content = ob_get_contents();
          ob_end_clean();
          echo $content;
  
          $f = fopen($this->cachePath, 'w');
          fwrite($f, $content);
          fclose($f);
      }
  }
  
  class TriggerTest extends PHPTAL_TestCase
  {
      public function setUp()
      {
          parent::setUp();
          
          if (!is_writable('.')) $this->markTestSkipped();
          
          if (file_exists('trigger.10')) unlink('trigger.10');
          if (file_exists('trigger.11')) unlink('trigger.11');
      }
  
      public function tearDown()
      {
          if (file_exists('trigger.10')) unlink('trigger.10');
          if (file_exists('trigger.11')) unlink('trigger.11');
      }
  
      public function testSimple()
      {
          $trigger = new StupidCacheTrigger();
          $tpl = $this->newPHPTAL('input/trigger.01.html');
          $tpl->addTrigger('someid', $trigger);
          $exp = trim_file('output/trigger.01.html');
  
          $tpl->someId = 10;
          $res = trim_string($tpl->execute());
          $this->assertEquals($exp, $res);
          $this->assertTrue($trigger->isCaching);
          $this->assertEquals('trigger.10', $trigger->cachePath);
  
          $tpl->someId = 10;
          $res = trim_string($tpl->execute());
          $this->assertEquals($exp, $res);
          $this->assertFalse($trigger->isCaching);
          $this->assertEquals('trigger.10', $trigger->cachePath);
      }
  }