2
0

BuildLayerTest.php 13 KB

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