PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/Breeze/View/Driver/DriverTest.php

http://github.com/whatthejeff/breeze
PHP | 296 lines | 157 code | 24 blank | 115 comment | 0 complexity | c9ba86444507b7b699424c20ca9bcaae MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Breeze Framework - View Driver test case.
  4. *
  5. * This file contains the {@link Breeze\View\Driver\Tests\DriverTest} class.
  6. *
  7. * LICENSE
  8. *
  9. * This file is part of the Breeze Framework package and is subject to the new
  10. * BSD license. For full copyright and license information, please see the
  11. * LICENSE file that is distributed with this package.
  12. *
  13. * @package Breeze
  14. * @subpackage Tests
  15. * @author Jeff Welch <whatthejeff@gmail.com>
  16. * @copyright 2010-2011 Jeff Welch <whatthejeff@gmail.com>
  17. * @license https://github.com/whatthejeff/breeze/blob/master/LICENSE New BSD License
  18. * @link http://breezephp.com/
  19. */
  20. namespace Breeze\View\Driver\Tests;
  21. /**
  22. * @see Breeze\View\Driver\Driver
  23. */
  24. use Breeze\View\Driver\Driver;
  25. /**
  26. * The test case for the {@link Breeze\View\Driver\Driver} class.
  27. *
  28. * @package Breeze
  29. * @subpackage Tests
  30. * @author Jeff Welch <whatthejeff@gmail.com>
  31. * @copyright 2010-2011 Jeff Welch <whatthejeff@gmail.com>
  32. * @license https://github.com/whatthejeff/breeze/blob/master/LICENSE New BSD License
  33. * @link http://breezephp.com/
  34. */
  35. class DriverTest extends \PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * The driver object for testing.
  39. *
  40. * @param Breeze\View\Driver\Driver
  41. */
  42. protected $_driver;
  43. /**
  44. * The application stub for testing {@link Breeze\View\Driver\Driver}.
  45. *
  46. * @param Breeze\Application
  47. */
  48. protected $_application;
  49. /**
  50. * Options to use for testing the setting of options with
  51. * {@link Breeze\View\Driver\Driver}.
  52. *
  53. * @param Breeze\Application
  54. */
  55. protected static $_options = array(
  56. 'option1'=>'value1',
  57. 'option2'=>'value2'
  58. );
  59. /**
  60. * Sets up the test case for {@link Breeze\View\Driver\Driver}.
  61. *
  62. * @return void
  63. */
  64. public function setUp()
  65. {
  66. $this->_application = $this->getMock(
  67. 'Breeze\\Application', array(), array(), '', FALSE
  68. );
  69. $this->_driver = $this->getMockForAbstractClass(
  70. 'Breeze\\View\\Driver\\Driver', array($this->_application)
  71. );
  72. }
  73. /**
  74. * Tests {@link Breeze\View\Driver\Driver::setPath()} with an invalid path.
  75. */
  76. public function testSetPathWithInvalidPath()
  77. {
  78. $this->setExpectedException(
  79. '\\InvalidArgumentException', 'is not a valid template path.'
  80. );
  81. $this->_driver->setPath('DOES NOT EXIST');
  82. }
  83. /**
  84. * Tests {@link Breeze\View\Driver\Driver::setPath()} with a valid path.
  85. */
  86. public function testSetPathWithValidPath()
  87. {
  88. $this->_driver->setPath(\Breeze\Tests\FIXTURES_PATH);
  89. $this->assertSame(
  90. \Breeze\Tests\FIXTURES_PATH,
  91. $this->_driver->getPath()
  92. );
  93. }
  94. /**
  95. * Tests {@link Breeze\View\Driver\Driver::__construct()} with an invalid
  96. * path.
  97. */
  98. public function testSetPathWithInvalidPathWithConstructor()
  99. {
  100. $this->setExpectedException(
  101. '\\InvalidArgumentException', 'is not a valid template path.'
  102. );
  103. $this->_driver = $this->getMockForAbstractClass(
  104. 'Breeze\\View\\Driver\\Driver',
  105. array($this->_application, 'DOES NOT EXIST')
  106. );
  107. }
  108. /**
  109. * Tests {@link Breeze\View\Driver\Driver::__construct()} with a valid
  110. * path.
  111. */
  112. public function testSetPathWithValidPathWithConstructor()
  113. {
  114. $this->_driver = $this->getMockForAbstractClass(
  115. 'Breeze\\View\\Driver\\Driver',
  116. array($this->_application, \Breeze\Tests\FIXTURES_PATH)
  117. );
  118. $this->assertSame(
  119. \Breeze\Tests\FIXTURES_PATH,
  120. $this->_driver->getPath()
  121. );
  122. }
  123. /**
  124. * Tests {@link Breeze\View\Driver\Driver::templateExists()} with an
  125. * invalid path.
  126. */
  127. public function testTemplateExistsWithInvalidTemplate()
  128. {
  129. $this->_driver->setPath(\Breeze\Tests\FIXTURES_PATH);
  130. $this->assertFalse($this->_driver->templateExists('DOES NOT EXIST'));
  131. }
  132. /**
  133. * Tests {@link Breeze\View\Driver\Driver::templateExists()} with a valid
  134. * path.
  135. */
  136. public function testTemplateExistsWithValidTemplate()
  137. {
  138. $this->_driver->setPath(\Breeze\Tests\FIXTURES_PATH);
  139. $this->assertTrue($this->_driver->templateExists('template.php'));
  140. }
  141. /**
  142. * Tests {@link Breeze\View\Driver\Driver::getTemplatePath()} to get the
  143. * path to a template.
  144. */
  145. public function testGetTemplatePath()
  146. {
  147. $this->_driver->setPath(\Breeze\Tests\FIXTURES_PATH);
  148. $this->assertSame(
  149. \Breeze\Tests\FIXTURES_PATH . "/template.php",
  150. $this->_driver->getTemplatePath('template.php')
  151. );
  152. }
  153. /**
  154. * Tests {@link Breeze\View\Driver\Driver::setOptions()} to set options for
  155. * the current driver.
  156. */
  157. public function testSetOptions()
  158. {
  159. $this->_driver->setOptions(self::$_options);
  160. foreach (self::$_options as $option => $value) {
  161. $this->assertSame($value, $this->_driver->getOption($option));
  162. }
  163. }
  164. /**
  165. * Tests {@link Breeze\View\Driver\Driver::__construct()} to set options
  166. * for the current driver.
  167. */
  168. public function testSetOptionsWithConstructor()
  169. {
  170. $this->_driver = $this->getMockForAbstractClass(
  171. 'Breeze\\View\\Driver\\Driver',
  172. array($this->_application, null, self::$_options)
  173. );
  174. foreach (self::$_options as $option => $value) {
  175. $this->assertSame($value, $this->_driver->getOption($option));
  176. }
  177. }
  178. /**
  179. * Tests {@link Breeze\View\Driver\Driver::getOption()} to get the
  180. * specified default option for the current driver.
  181. */
  182. public function testGetOptionWithDefault()
  183. {
  184. $this->assertSame(
  185. 'default', $this->_driver->getOption('DOES NOT EXIST', 'default')
  186. );
  187. }
  188. /**
  189. * Tests {@link Breeze\View\Driver\Driver::getOption()} to get an unset
  190. * option with no default specified.
  191. */
  192. public function testGetUnsetOption()
  193. {
  194. $this->assertNull($this->_driver->getOption('DOES NOT EXIST'));
  195. }
  196. /**
  197. * Tests {@link Breeze\View\Driver\Driver::fetch()} with an invalid
  198. * template path.
  199. */
  200. public function testFetchWithInvalidTemplate()
  201. {
  202. $this->setExpectedException(
  203. '\\InvalidArgumentException', 'is not a valid template.'
  204. );
  205. $this->_driver->setPath(\Breeze\Tests\FIXTURES_PATH);
  206. $this->_driver->fetch('DOES NOT EXIST');
  207. }
  208. /**
  209. * Tests {@link Breeze\View\Driver\Driver::fetch()} with a valid template.
  210. */
  211. public function testFetchWithValidTemplate()
  212. {
  213. $this->_driver->expects($this->once())
  214. ->method('_fetchTemplate')
  215. ->will($this->returnValue('contents'));
  216. $this->_driver->setPath(\Breeze\Tests\FIXTURES_PATH);
  217. $this->assertSame('contents', $this->_driver->fetch('template.php'));
  218. }
  219. /**
  220. * Tests {@link Breeze\View\Driver\Driver::__construct()} calls
  221. * {@link Breeze\View\Driver\Driver::_config()}.
  222. */
  223. public function testConstructorCallsConfig()
  224. {
  225. $this->_driver->expects($this->once())
  226. ->method('_config');
  227. $this->_driver->__construct($this->_application);
  228. }
  229. /**
  230. * Tests {@link Breeze\View\Driver\Driver::updateConfig()} with options
  231. * that have changed will call
  232. * {@link Breeze\View\Driver\Driver::_config()}.
  233. */
  234. public function testConfigWithDirtyOptions()
  235. {
  236. $this->_application->expects($this->at(0))
  237. ->method('config')
  238. ->with($this->equalTo('template_directory'))
  239. ->will(
  240. $this->returnValue(\Breeze\Tests\FIXTURES_PATH)
  241. );
  242. $this->_application->expects($this->at(1))
  243. ->method('config')
  244. ->with($this->equalTo('template_options'))
  245. ->will($this->returnValue(array()));
  246. $this->_driver->expects($this->once())
  247. ->method('_config');
  248. $this->_driver->updateConfig();
  249. }
  250. /**
  251. * Tests {@link Breeze\View\Driver\Driver::updateConfig()} with options
  252. * that have not changed will not call
  253. * {@link Breeze\View\Driver\Driver::_config()}.
  254. */
  255. public function testConfigWithCleanOptions()
  256. {
  257. $this->_application->expects($this->at(0))
  258. ->method('config')
  259. ->with($this->equalTo('template_directory'))
  260. ->will(
  261. $this->returnValue(\Breeze\Tests\FIXTURES_PATH)
  262. );
  263. $this->_application->expects($this->at(1))
  264. ->method('config')
  265. ->with($this->equalTo('template_options'))
  266. ->will($this->returnValue(array()));
  267. $this->_driver->expects($this->never())
  268. ->method('_config');
  269. $this->_driver->setPath(\Breeze\Tests\FIXTURES_PATH);
  270. $this->_driver->updateConfig();
  271. }
  272. }