FileTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\template\view\adapter;
  9. use lithium\core\Libraries;
  10. use lithium\template\view\adapter\File;
  11. class FileTest extends \lithium\test\Unit {
  12. protected $_path;
  13. public function setUp() {
  14. $this->_path = Libraries::get(true, 'resources') . '/tmp/tests';
  15. $template1 = '<' . '?php echo $foo; ?' . '>';
  16. $template2 = '<' . '?php echo $this["foo"]; ?' . '>';
  17. file_put_contents("{$this->_path}/template1.html.php", $template1);
  18. file_put_contents("{$this->_path}/template2.html.php", $template2);
  19. }
  20. public function tearDown() {
  21. unlink("{$this->_path}/template1.html.php");
  22. unlink("{$this->_path}/template2.html.php");
  23. }
  24. public function testRenderingWithExtraction() {
  25. $file = new File();
  26. $content = $file->render("{$this->_path}/template1.html.php", array('foo' => 'bar'));
  27. $this->assertEqual('bar', $content);
  28. $content = $file->render("{$this->_path}/template2.html.php", array('foo' => 'bar'));
  29. $this->assertEqual('bar', $content);
  30. }
  31. public function testRenderingWithNoExtraction() {
  32. $file = new File(array('extract' => false));
  33. $this->expectException('Undefined variable: foo');
  34. $content = $file->render("{$this->_path}/template1.html.php", array('foo' => 'bar'));
  35. $this->assertFalse($content);
  36. $content = $file->render("{$this->_path}/template2.html.php", array('foo' => 'bar'));
  37. $this->assertEqual('bar', $content);
  38. }
  39. public function testContextOffsetManipulation() {
  40. $file = new File();
  41. $this->assertFalse(isset($file['title']));
  42. $file['title'] = 'Document Title';
  43. $this->assertEqual('Document Title', $file['title']);
  44. $this->assertTrue(isset($file['title']));
  45. unset($file['title']);
  46. $this->assertFalse(isset($file['title']));
  47. }
  48. /**
  49. * @todo Rewrite this test to generate a temporary template in the resources
  50. * directory.
  51. */
  52. public function testTemplateLocating() {
  53. $path = Libraries::get(true, 'path') . '/views/pages/home.html.php';
  54. $this->skipIf(!file_exists($path), 'No default app template.');
  55. $file = new File(array('paths' => array(
  56. 'template' => '{:library}/views/{:controller}/{:template}.{:type}.php'
  57. )));
  58. $template = $file->template('template', array(
  59. 'controller' => 'pages', 'template' => 'home', 'type' => 'html'
  60. ));
  61. $this->assertPattern('/template_views_pages_home\.html_[0-9]+/', $template);
  62. $file = new File(array('compile' => false, 'paths' => array(
  63. 'template' => '{:library}/views/{:controller}/{:template}.{:type}.php'
  64. )));
  65. $template = $file->template('template', array(
  66. 'controller' => 'pages', 'template' => 'home', 'type' => 'html'
  67. ));
  68. $this->assertPattern('/\/views\/pages\/home\.html\.php$/', $template);
  69. $this->expectException('/Template not found/');
  70. $file->template('template', array(
  71. 'controller' => 'pages', 'template' => 'foo', 'type' => 'html'
  72. ));
  73. }
  74. public function testInvalidTemplateType() {
  75. $file = new File(array('compile' => false, 'paths' => array(
  76. 'template' => '{:library}/views/{:controller}/{:template}.{:type}.php'
  77. )));
  78. $this->expectException("Invalid template type 'invalid'.");
  79. $template = $file->template('invalid', array('template' => 'foo'));
  80. }
  81. }
  82. ?>