TinySrcTest.php 7.5 KB

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