TinySrcTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. // Call Zend_View_Helper_TinySrcTest::main() if this source file is executed directly.
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_View_Helper_TinySrcTest::main');
  24. }
  25. /**
  26. * @see Zend_View_Helper_TinySrc
  27. */
  28. require_once 'Zend/View/Helper/TinySrc.php';
  29. /**
  30. * @see Zend_View
  31. */
  32. require_once 'Zend/View.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_View
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. * @group Zend_View
  40. * @group Zend_View_Helper
  41. */
  42. class Zend_View_Helper_TinySrcTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var Zend_View_Helper_TinySrc
  46. */
  47. public $helper;
  48. /**
  49. * @var Zend_View
  50. */
  51. public $view;
  52. /**
  53. * Main
  54. */
  55. public static function main()
  56. {
  57. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  58. $result = PHPUnit_TextUI_TestRunner::run($suite);
  59. }
  60. /**
  61. * Prepares the environment before running a test.
  62. */
  63. protected function setUp()
  64. {
  65. $this->helper = new Zend_View_Helper_TinySrc();
  66. $this->view = new Zend_View();
  67. $this->view->doctype()->setDoctype(strtoupper("XHTML1_STRICT"));
  68. $this->helper->setView($this->view);
  69. }
  70. public function testCallingHelperMethodWithNoArgumentsReturnsHelperInstance()
  71. {
  72. $test = $this->helper->tinySrc();
  73. $this->assertSame($this->helper, $test);
  74. }
  75. public function testHelperUsesServerAndBaseUrlFromHelpersByDefault()
  76. {
  77. $base = $this->view->getHelper('baseUrl');
  78. $base->setBaseUrl('/foo/bar');
  79. $server = $this->view->getHelper('serverUrl');
  80. $server->setScheme('https')
  81. ->setHost('example.com:8080');
  82. $test = $this->helper->getBaseUrl();
  83. $this->assertEquals('https://example.com:8080/foo/bar/', $test);
  84. }
  85. public function testAllowsSettingDefaultFormat()
  86. {
  87. $this->helper->setDefaultFormat('png')
  88. ->setBaseUrl('http://example.com');
  89. $image = $this->helper->tinySrc('foo.jpg');
  90. $this->assertContains('/png/', $image);
  91. }
  92. public function testSettingInvalidDefaultFormatRaisesException()
  93. {
  94. $this->setExpectedException('Zend_View_Exception', 'Invalid format');
  95. $this->helper->setDefaultFormat('gif');
  96. }
  97. public function testPassingNullValueToDefaultFormatClearsSetFormat()
  98. {
  99. $this->helper->setDefaultFormat('png')
  100. ->setBaseUrl('http://example.com');
  101. $this->helper->setDefaultFormat(null);
  102. $image = $this->helper->tinySrc('foo.jpg');
  103. $this->assertNotContains('/png/', $image);
  104. }
  105. public function testAllowsPassingDefaultWidth()
  106. {
  107. $this->helper->setBaseUrl('http://example.com')
  108. ->setDefaultDimensions('-5');
  109. $image = $this->helper->tinySrc('foo.jpg');
  110. $this->assertContains('/-5/', $image);
  111. }
  112. /**
  113. * @dataProvider invalidDimensions
  114. */
  115. public function testRaisesExceptionOnInvalidDefaultWidthValue($dim)
  116. {
  117. $this->setExpectedException('Zend_View_Exception', 'Invalid dimension');
  118. $this->helper->setDefaultDimensions($dim);
  119. }
  120. public function testAllowsPassingDefaultWidthAndHeight()
  121. {
  122. $this->helper->setBaseUrl('http://example.com')
  123. ->setDefaultDimensions('5', 'x20');
  124. $image = $this->helper->tinySrc('foo.jpg');
  125. $this->assertContains('/5/x20/', $image);
  126. }
  127. /**
  128. * @dataProvider invalidDimensions
  129. */
  130. public function testRaisesExceptionOnInvalidDefaultHeightValue($dim)
  131. {
  132. $this->setExpectedException('Zend_View_Exception', 'Invalid dimension');
  133. $this->helper->setDefaultDimensions('10', $dim);
  134. }
  135. public function testPassingNullAsDefaultWidthValueClearsBothWidthAndHeight()
  136. {
  137. $this->helper->setBaseUrl('http://example.com')
  138. ->setDefaultDimensions('5', 'x20');
  139. $this->helper->setDefaultDimensions(null, 'x20');
  140. $image = $this->helper->tinySrc('foo.jpg');
  141. $this->assertNotContains('/5/x20/', $image);
  142. $this->assertNotContains('/5/', $image);
  143. $this->assertNotContains('/x20/', $image);
  144. }
  145. public function testPassingNullAsDefaultHeightValueClearsHeight()
  146. {
  147. $this->helper->setBaseUrl('http://example.com')
  148. ->setDefaultDimensions('5', 'x20');
  149. $this->helper->setDefaultDimensions('5');
  150. $image = $this->helper->tinySrc('foo.jpg');
  151. $this->assertNotContains('/5/x20/', $image);
  152. $this->assertContains('/5/', $image);
  153. }
  154. public function testCreatesImageTagByDefault()
  155. {
  156. $this->helper->setBaseUrl('http://example.com');
  157. $image = $this->helper->tinySrc('foo.jpg');
  158. $this->assertContains('<img src="', $image);
  159. }
  160. public function testImageTagObeysDoctype()
  161. {
  162. $this->view->doctype('XHTML1_STRICT');
  163. $this->helper->setBaseUrl('http://example.com');
  164. $image = $this->helper->tinySrc('foo.jpg');
  165. $this->assertContains('/>', $image);
  166. }
  167. public function testAllowsSpecifyingTagCreation()
  168. {
  169. $this->helper->setCreateTag(false);
  170. $this->helper->setBaseUrl('http://example.com');
  171. $image = $this->helper->tinySrc('foo.jpg');
  172. $this->assertNotContains('<img src="', $image);
  173. }
  174. public function testPassingOptionsToHelperMethodOverridesDefaults()
  175. {
  176. $this->helper->setBaseUrl('http://example.com')
  177. ->setCreateTag(false)
  178. ->setDefaultDimensions(320, 480);
  179. $image = $this->helper->tinySrc('foo.jpg', array(
  180. 'base_url' => 'https://example.org:8080/public',
  181. 'format' => 'png',
  182. 'width' => 160,
  183. 'height' => null,
  184. 'create_tag' => true,
  185. ));
  186. $this->assertContains('<img width="160" src="http://i.tinysrc.mobi/png/160/https://example.org:8080/public/foo.jpg"', $image);
  187. }
  188. public function testUnknownOptionsPassedToHelperMethodAreTreatedAsImageAttributes()
  189. {
  190. $this->helper->setBaseUrl('http://example.com');
  191. $image = $this->helper->tinySrc('foo.jpg', array(
  192. 'alt' => 'Alt text for image',
  193. ));
  194. $this->assertContains('alt="Alt text for image"', $image);
  195. }
  196. public function invalidDimensions()
  197. {
  198. return array(
  199. array('foo'),
  200. array(true),
  201. array(array()),
  202. array(new stdClass),
  203. );
  204. }
  205. }
  206. // Call Zend_View_Helper_TinySrcTest::main() if this source file is executed directly.
  207. if (PHPUnit_MAIN_METHOD == 'Zend_View_Helper_TinySrcTest::main') {
  208. Zend_View_Helper_TinySrcTest::main();
  209. }