BuildLayerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: $
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. if (!defined('PHPUnit_MAIN_METHOD')) {
  27. define('PHPUnit_MAIN_METHOD', 'Zend_Dojo_BuildLayerTest::main');
  28. }
  29. require_once 'Zend/Dojo/BuildLayer.php';
  30. require_once 'Zend/Dojo.php';
  31. require_once 'Zend/View.php';
  32. require_once 'Zend/Json.php';
  33. /**
  34. * @category Zend
  35. * @package Zend_Dojo
  36. * @subpackage UnitTests
  37. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Dojo_BuildLayerTest extends PHPUnit_Framework_TestCase
  41. {
  42. public static function main()
  43. {
  44. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  45. $result = PHPUnit_TextUI_TestRunner::run($suite);
  46. }
  47. /**
  48. * Sets up the fixture, for example, open a network connection.
  49. * This method is called before a test is executed.
  50. *
  51. * @return void
  52. */
  53. public function setUp()
  54. {
  55. $this->view = new Zend_View();
  56. Zend_Dojo::enableView($this->view);
  57. }
  58. public function testViewShouldBeNullByDefault()
  59. {
  60. $build = new Zend_Dojo_BuildLayer();
  61. $this->assertNull($build->getView());
  62. }
  63. /**
  64. * @expectedException Zend_Dojo_Exception
  65. */
  66. public function testRetrievingDojoHelperShouldRaiseExceptionWhenNoViewPresent()
  67. {
  68. $build = new Zend_Dojo_BuildLayer();
  69. $build->getDojoHelper();
  70. }
  71. public function testDojoHelperShouldBeRetrievedFromViewObjectIfNotExplicitySet()
  72. {
  73. $build = new Zend_Dojo_BuildLayer(array('view' => $this->view));
  74. $helper = $build->getDojoHelper();
  75. $this->assertTrue($helper instanceof Zend_Dojo_View_Helper_Dojo_Container);
  76. }
  77. public function testLayerScriptPathIsNullByDefault()
  78. {
  79. $build = new Zend_Dojo_BuildLayer();
  80. $this->assertNull($build->getLayerScriptPath());
  81. }
  82. public function testLayerScriptPathShouldBeMutable()
  83. {
  84. $build = new Zend_Dojo_BuildLayer();
  85. $path = __FILE__;
  86. $build->setLayerScriptPath($path);
  87. $this->assertEquals($path, $build->getLayerScriptPath());
  88. }
  89. public function testShouldNotConsumeJavascriptByDefault()
  90. {
  91. $build = new Zend_Dojo_BuildLayer();
  92. $this->assertFalse($build->consumeJavascript());
  93. }
  94. public function testConsumeJavascriptFlagShouldBeMutable()
  95. {
  96. $build = new Zend_Dojo_BuildLayer();
  97. $build->setConsumeJavascript(true);
  98. $this->assertTrue($build->consumeJavascript());
  99. }
  100. public function testShouldNotConsumeOnLoadByDefault()
  101. {
  102. $build = new Zend_Dojo_BuildLayer();
  103. $this->assertFalse($build->consumeOnLoad());
  104. }
  105. public function testConsumeOnLoadFlagShouldBeMutable()
  106. {
  107. $build = new Zend_Dojo_BuildLayer();
  108. $build->setConsumeOnLoad(true);
  109. $this->assertTrue($build->consumeOnLoad());
  110. }
  111. public function testLayerNameShouldBeNullByDefault()
  112. {
  113. $build = new Zend_Dojo_BuildLayer();
  114. $this->assertNull($build->getLayerName());
  115. }
  116. public function testLayerNameShouldBeMutable()
  117. {
  118. $build = new Zend_Dojo_BuildLayer();
  119. $build->setLayerName('custom.main');
  120. $this->assertEquals('custom.main', $build->getLayerName());
  121. }
  122. /**
  123. * @expectedException Zend_Dojo_Exception
  124. */
  125. public function testSettingLayerNameToInvalidFormatShouldRaiseException()
  126. {
  127. $build = new Zend_Dojo_BuildLayer();
  128. $build->setLayerName('customFoo#bar');
  129. }
  130. public function testGeneratingLayerScriptShouldReturnValidLayerMarkup()
  131. {
  132. $this->view->dojo()->requireModule('dijit.form.Form')
  133. ->requireModule('dijit.form.TextBox')
  134. ->requireModule('dijit.form.Button');
  135. $build = new Zend_Dojo_BuildLayer(array(
  136. 'view' => $this->view,
  137. 'layerName' => 'foo.bar',
  138. ));
  139. $test = $build->generateLayerScript();
  140. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayer.js');
  141. $test = $this->stripWhitespace($test);
  142. $script = $this->stripWhitespace($script);
  143. $this->assertEquals($script, $test);
  144. }
  145. public function testGeneratingLayerScriptWithOnLoadsEnabledShouldReturnValidLayerMarkup()
  146. {
  147. $this->view->dojo()->requireModule('dijit.form.Form')
  148. ->requireModule('dijit.form.TextBox')
  149. ->requireModule('dijit.form.Button')
  150. ->addOnLoad('custom.callback');
  151. $build = new Zend_Dojo_BuildLayer(array(
  152. 'view' => $this->view,
  153. 'layerName' => 'foo.bar',
  154. 'consumeOnLoad' => true,
  155. ));
  156. $test = $build->generateLayerScript();
  157. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayerOnLoad.js');
  158. $test = $this->stripWhitespace($test);
  159. $script = $this->stripWhitespace($script);
  160. $this->assertEquals($script, $test);
  161. }
  162. public function testGeneratingLayerScriptWithOnLoadsDisabledShouldNotRenderOnLoadEvents()
  163. {
  164. $this->view->dojo()->requireModule('dijit.form.Form')
  165. ->requireModule('dijit.form.TextBox')
  166. ->requireModule('dijit.form.Button')
  167. ->addOnLoad('custom.callback');
  168. $build = new Zend_Dojo_BuildLayer(array(
  169. 'view' => $this->view,
  170. 'layerName' => 'foo.bar',
  171. ));
  172. $test = $build->generateLayerScript();
  173. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayer.js');
  174. $test = $this->stripWhitespace($test);
  175. $script = $this->stripWhitespace($script);
  176. $this->assertEquals($script, $test);
  177. }
  178. public function testGeneratingLayerScriptWithJavascriptsEnabledShouldReturnValidLayerMarkup()
  179. {
  180. $this->view->dojo()->requireModule('dijit.form.Form')
  181. ->requireModule('dijit.form.TextBox')
  182. ->requireModule('dijit.form.Button')
  183. ->addJavascript('custom.callback();');
  184. $build = new Zend_Dojo_BuildLayer(array(
  185. 'view' => $this->view,
  186. 'layerName' => 'foo.bar',
  187. 'consumeJavascript' => true,
  188. ));
  189. $test = $build->generateLayerScript();
  190. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayerJavascript.js');
  191. $test = $this->stripWhitespace($test);
  192. $script = $this->stripWhitespace($script);
  193. $this->assertEquals($script, $test);
  194. }
  195. public function testGeneratingLayerScriptWithJavascriptsDisabledShouldNotRenderJavascripts()
  196. {
  197. $this->view->dojo()->requireModule('dijit.form.Form')
  198. ->requireModule('dijit.form.TextBox')
  199. ->requireModule('dijit.form.Button')
  200. ->addJavascript('custom.callback();');
  201. $build = new Zend_Dojo_BuildLayer(array(
  202. 'view' => $this->view,
  203. 'layerName' => 'foo.bar',
  204. ));
  205. $test = $build->generateLayerScript();
  206. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayer.js');
  207. $test = $this->stripWhitespace($test);
  208. $script = $this->stripWhitespace($script);
  209. $this->assertEquals($script, $test);
  210. }
  211. public function testProfileOptionsShouldIncludeSaneDefaultsByDefault()
  212. {
  213. $build = new Zend_Dojo_BuildLayer();
  214. $expected = $this->getDefaultProfileOptions();
  215. $options = $build->getProfileOptions();
  216. $this->assertEquals($expected, $options);
  217. }
  218. public function testAddProfileOptionsShouldAddOptions()
  219. {
  220. $options = array('foo' => 'bar');
  221. $build = new Zend_Dojo_BuildLayer(array(
  222. 'profileOptions' => $options,
  223. ));
  224. $build->addProfileOptions(array('bar' => 'baz'));
  225. $expected = $this->getDefaultProfileOptions() + array('foo' => 'bar', 'bar' => 'baz');
  226. $this->assertEquals($expected, $build->getProfileOptions());
  227. }
  228. public function testAddProfileOptionShouldAddOption()
  229. {
  230. $build = new Zend_Dojo_BuildLayer();
  231. $build->addProfileOption('foo', 'bar');
  232. $this->assertTrue($build->hasProfileOption('foo'));
  233. }
  234. public function testSetProfileOptionsShouldNotOverwriteOptions()
  235. {
  236. $options = array('foo' => 'bar');
  237. $build = new Zend_Dojo_BuildLayer(array(
  238. 'profileOptions' => $options,
  239. ));
  240. $build->setProfileOptions(array('bar' => 'baz'));
  241. $this->assertNotEquals(array('bar' => 'baz'), $build->getProfileOptions());
  242. $this->assertTrue($build->hasProfileOption('bar'));
  243. }
  244. public function testProfilePrefixesAreEmptyByDefault()
  245. {
  246. $build = new Zend_Dojo_BuildLayer();
  247. $prefixes = $build->getProfilePrefixes();
  248. $this->assertTrue(empty($prefixes));
  249. }
  250. public function testProfilePrefixesIncludeLayerNamePrefix()
  251. {
  252. $build = new Zend_Dojo_BuildLayer(array('layerName' => 'foo.main'));
  253. $prefixes = $build->getProfilePrefixes();
  254. $this->assertTrue(array_key_exists('foo', $prefixes), var_export($prefixes, 1));
  255. $this->assertEquals(array('foo', '../foo'), $prefixes['foo']);
  256. }
  257. public function testProfilePrefixesShouldIncludePrefixesOfAllRequiredModules()
  258. {
  259. $this->view->dojo()->requireModule('dijit.layout.TabContainer')
  260. ->requireModule('dojox.layout.ContentPane');
  261. $build = new Zend_Dojo_BuildLayer(array('view' => $this->view));
  262. $prefixes = $build->getProfilePrefixes();
  263. $this->assertTrue(array_key_exists('dijit', $prefixes), var_export($prefixes, 1));
  264. $this->assertEquals(array('dijit', '../dijit'), $prefixes['dijit']);
  265. $this->assertTrue(array_key_exists('dojox', $prefixes), var_export($prefixes, 1));
  266. $this->assertEquals(array('dojox', '../dojox'), $prefixes['dojox']);
  267. }
  268. public function testGeneratedDojoBuildProfileWithNoExtraLayerDependencies()
  269. {
  270. $build = new Zend_Dojo_BuildLayer(array(
  271. 'view' => $this->view,
  272. 'layerName' => 'zend.main',
  273. ));
  274. $profile = $build->generateBuildProfile();
  275. $expected = file_get_contents(dirname(__FILE__) . '/_files/BuildProfile.js');
  276. $decodedProfile = $this->decodeProfileJson($profile);
  277. $decodedExpected = $this->decodeProfileJson($expected);
  278. $this->assertEquals($decodedExpected, $decodedProfile, 'Expected: ' . $expected . "\nReceived: " . $profile . "\n");
  279. }
  280. public function testGeneratedDojoBuildProfileWithLayerDependencies()
  281. {
  282. $this->view->dojo()->requireModule('dijit.layout.BorderContainer')
  283. ->requireModule('dojox.layout.ContentPane');
  284. $build = new Zend_Dojo_BuildLayer(array(
  285. 'view' => $this->view,
  286. 'layerName' => 'zend.main',
  287. ));
  288. $profile = $build->generateBuildProfile();
  289. $expected = file_get_contents(dirname(__FILE__) . '/_files/BuildProfileWithDependencies.js');
  290. $decodedProfile = $this->decodeProfileJson($profile);
  291. $decodedExpected = $this->decodeProfileJson($expected);
  292. $this->assertEquals($decodedExpected, $decodedProfile, 'Expected: ' . $expected . "\nReceived: " . $profile . "\n");
  293. }
  294. protected function stripWhitespace($string)
  295. {
  296. $string = preg_replace('/^[ ]+/m', '', $string);
  297. $string = preg_replace('/([ ]{2,})/s', ' ', $string);
  298. $string = preg_replace('/(\r|\r\n|\n){2, }/s', "\n", $string);
  299. $string = preg_replace('/(\r|\r\n|\n)$/', '', $string);
  300. return $string;
  301. }
  302. protected function getDefaultProfileOptions()
  303. {
  304. return array(
  305. 'action' => 'release',
  306. 'optimize' => 'shrinksafe',
  307. 'layerOptimize' => 'shrinksafe',
  308. 'copyTests' => false,
  309. 'loader' => 'default',
  310. 'cssOptimize' => 'comments',
  311. );
  312. }
  313. protected function decodeProfileJson($profile)
  314. {
  315. $profile = preg_replace('/^dependencies = (.*?);$/s', '$1', $profile);
  316. return Zend_Json::decode($profile);
  317. }
  318. }
  319. if (PHPUnit_MAIN_METHOD == 'Zend_Dojo_BuildLayerTest::main') {
  320. Zend_Dojo_BuildLayerTest::main();
  321. }