BuildLayerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 testProfilePathIsNullByDefault()
  90. {
  91. $build = new Zend_Dojo_BuildLayer();
  92. $this->assertNull($build->getProfilePath());
  93. }
  94. public function testProfilePathShouldBeMutable()
  95. {
  96. $build = new Zend_Dojo_BuildLayer();
  97. $path = __FILE__;
  98. $build->setProfilePath($path);
  99. $this->assertEquals($path, $build->getProfilePath());
  100. }
  101. public function testShouldNotConsumeJavascriptByDefault()
  102. {
  103. $build = new Zend_Dojo_BuildLayer();
  104. $this->assertFalse($build->consumeJavascript());
  105. }
  106. public function testConsumeJavascriptFlagShouldBeMutable()
  107. {
  108. $build = new Zend_Dojo_BuildLayer();
  109. $build->setConsumeJavascript(true);
  110. $this->assertTrue($build->consumeJavascript());
  111. }
  112. public function testShouldNotConsumeOnLoadByDefault()
  113. {
  114. $build = new Zend_Dojo_BuildLayer();
  115. $this->assertFalse($build->consumeOnLoad());
  116. }
  117. public function testConsumeOnLoadFlagShouldBeMutable()
  118. {
  119. $build = new Zend_Dojo_BuildLayer();
  120. $build->setConsumeOnLoad(true);
  121. $this->assertTrue($build->consumeOnLoad());
  122. }
  123. public function testLayerNameShouldBeNullByDefault()
  124. {
  125. $build = new Zend_Dojo_BuildLayer();
  126. $this->assertNull($build->getLayerName());
  127. }
  128. public function testLayerNameShouldBeMutable()
  129. {
  130. $build = new Zend_Dojo_BuildLayer();
  131. $build->setLayerName('custom.main');
  132. $this->assertEquals('custom.main', $build->getLayerName());
  133. }
  134. /**
  135. * @expectedException Zend_Dojo_Exception
  136. */
  137. public function testSettingLayerNameToInvalidFormatShouldRaiseException()
  138. {
  139. $build = new Zend_Dojo_BuildLayer();
  140. $build->setLayerName('customFoo#bar');
  141. }
  142. public function testGeneratingLayerScriptShouldReturnValidLayerMarkup()
  143. {
  144. $this->view->dojo()->requireModule('dijit.form.Form')
  145. ->requireModule('dijit.form.TextBox')
  146. ->requireModule('dijit.form.Button');
  147. $build = new Zend_Dojo_BuildLayer(array(
  148. 'view' => $this->view,
  149. 'layerName' => 'foo.bar',
  150. ));
  151. $test = $build->generateLayerScript();
  152. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayer.js');
  153. $test = $this->stripWhitespace($test);
  154. $script = $this->stripWhitespace($script);
  155. $this->assertEquals($script, $test);
  156. }
  157. public function testGeneratingLayerScriptWithOnLoadsEnabledShouldReturnValidLayerMarkup()
  158. {
  159. $this->view->dojo()->requireModule('dijit.form.Form')
  160. ->requireModule('dijit.form.TextBox')
  161. ->requireModule('dijit.form.Button')
  162. ->addOnLoad('custom.callback');
  163. $build = new Zend_Dojo_BuildLayer(array(
  164. 'view' => $this->view,
  165. 'layerName' => 'foo.bar',
  166. 'consumeOnLoad' => true,
  167. ));
  168. $test = $build->generateLayerScript();
  169. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayerOnLoad.js');
  170. $test = $this->stripWhitespace($test);
  171. $script = $this->stripWhitespace($script);
  172. $this->assertEquals($script, $test);
  173. }
  174. public function testGeneratingLayerScriptWithOnLoadsDisabledShouldNotRenderOnLoadEvents()
  175. {
  176. $this->view->dojo()->requireModule('dijit.form.Form')
  177. ->requireModule('dijit.form.TextBox')
  178. ->requireModule('dijit.form.Button')
  179. ->addOnLoad('custom.callback');
  180. $build = new Zend_Dojo_BuildLayer(array(
  181. 'view' => $this->view,
  182. 'layerName' => 'foo.bar',
  183. ));
  184. $test = $build->generateLayerScript();
  185. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayer.js');
  186. $test = $this->stripWhitespace($test);
  187. $script = $this->stripWhitespace($script);
  188. $this->assertEquals($script, $test);
  189. }
  190. public function testGeneratingLayerScriptWithJavascriptsEnabledShouldReturnValidLayerMarkup()
  191. {
  192. $this->view->dojo()->requireModule('dijit.form.Form')
  193. ->requireModule('dijit.form.TextBox')
  194. ->requireModule('dijit.form.Button')
  195. ->addJavascript('custom.callback();');
  196. $build = new Zend_Dojo_BuildLayer(array(
  197. 'view' => $this->view,
  198. 'layerName' => 'foo.bar',
  199. 'consumeJavascript' => true,
  200. ));
  201. $test = $build->generateLayerScript();
  202. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayerJavascript.js');
  203. $test = $this->stripWhitespace($test);
  204. $script = $this->stripWhitespace($script);
  205. $this->assertEquals($script, $test);
  206. }
  207. public function testGeneratingLayerScriptWithJavascriptsDisabledShouldNotRenderJavascripts()
  208. {
  209. $this->view->dojo()->requireModule('dijit.form.Form')
  210. ->requireModule('dijit.form.TextBox')
  211. ->requireModule('dijit.form.Button')
  212. ->addJavascript('custom.callback();');
  213. $build = new Zend_Dojo_BuildLayer(array(
  214. 'view' => $this->view,
  215. 'layerName' => 'foo.bar',
  216. ));
  217. $test = $build->generateLayerScript();
  218. $script = file_get_contents(dirname(__FILE__) . '/_files/BuildLayer.js');
  219. $test = $this->stripWhitespace($test);
  220. $script = $this->stripWhitespace($script);
  221. $this->assertEquals($script, $test);
  222. }
  223. public function testProfileOptionsShouldIncludeSaneDefaultsByDefault()
  224. {
  225. $build = new Zend_Dojo_BuildLayer();
  226. $expected = $this->getDefaultProfileOptions();
  227. $options = $build->getProfileOptions();
  228. $this->assertEquals($expected, $options);
  229. }
  230. public function testAddProfileOptionsShouldAddOptions()
  231. {
  232. $options = array('foo' => 'bar');
  233. $build = new Zend_Dojo_BuildLayer(array(
  234. 'profileOptions' => $options,
  235. ));
  236. $build->addProfileOptions(array('bar' => 'baz'));
  237. $expected = $this->getDefaultProfileOptions() + array('foo' => 'bar', 'bar' => 'baz');
  238. $this->assertEquals($expected, $build->getProfileOptions());
  239. }
  240. public function testAddProfileOptionShouldAddOption()
  241. {
  242. $build = new Zend_Dojo_BuildLayer();
  243. $build->addProfileOption('foo', 'bar');
  244. $this->assertTrue($build->hasProfileOption('foo'));
  245. }
  246. public function testSetProfileOptionsShouldNotOverwriteOptions()
  247. {
  248. $options = array('foo' => 'bar');
  249. $build = new Zend_Dojo_BuildLayer(array(
  250. 'profileOptions' => $options,
  251. ));
  252. $build->setProfileOptions(array('bar' => 'baz'));
  253. $this->assertNotEquals(array('bar' => 'baz'), $build->getProfileOptions());
  254. $this->assertTrue($build->hasProfileOption('bar'));
  255. }
  256. public function testProfilePrefixesAreEmptyByDefault()
  257. {
  258. $build = new Zend_Dojo_BuildLayer();
  259. $prefixes = $build->getProfilePrefixes();
  260. $this->assertTrue(empty($prefixes));
  261. }
  262. public function testProfilePrefixesIncludeLayerNamePrefix()
  263. {
  264. $build = new Zend_Dojo_BuildLayer(array('layerName' => 'foo.main'));
  265. $prefixes = $build->getProfilePrefixes();
  266. $this->assertTrue(array_key_exists('foo', $prefixes), var_export($prefixes, 1));
  267. $this->assertEquals(array('foo', '../foo'), $prefixes['foo']);
  268. }
  269. public function testProfilePrefixesShouldIncludePrefixesOfAllRequiredModules()
  270. {
  271. $this->view->dojo()->requireModule('dijit.layout.TabContainer')
  272. ->requireModule('dojox.layout.ContentPane');
  273. $build = new Zend_Dojo_BuildLayer(array('view' => $this->view));
  274. $prefixes = $build->getProfilePrefixes();
  275. $this->assertTrue(array_key_exists('dijit', $prefixes), var_export($prefixes, 1));
  276. $this->assertEquals(array('dijit', '../dijit'), $prefixes['dijit']);
  277. $this->assertTrue(array_key_exists('dojox', $prefixes), var_export($prefixes, 1));
  278. $this->assertEquals(array('dojox', '../dojox'), $prefixes['dojox']);
  279. }
  280. public function testGeneratedDojoBuildProfileWithNoExtraLayerDependencies()
  281. {
  282. $build = new Zend_Dojo_BuildLayer(array(
  283. 'view' => $this->view,
  284. 'layerName' => 'zend.main',
  285. ));
  286. $profile = $build->generateBuildProfile();
  287. $expected = file_get_contents(dirname(__FILE__) . '/_files/BuildProfile.js');
  288. $decodedProfile = $this->decodeProfileJson($profile);
  289. $decodedExpected = $this->decodeProfileJson($expected);
  290. $this->assertEquals($decodedExpected, $decodedProfile, 'Expected: ' . $expected . "\nReceived: " . $profile . "\n");
  291. }
  292. public function testGeneratedDojoBuildProfileWithLayerDependencies()
  293. {
  294. $this->view->dojo()->requireModule('dijit.layout.BorderContainer')
  295. ->requireModule('dojox.layout.ContentPane');
  296. $build = new Zend_Dojo_BuildLayer(array(
  297. 'view' => $this->view,
  298. 'layerName' => 'zend.main',
  299. ));
  300. $profile = $build->generateBuildProfile();
  301. $expected = file_get_contents(dirname(__FILE__) . '/_files/BuildProfileWithDependencies.js');
  302. $decodedProfile = $this->decodeProfileJson($profile);
  303. $decodedExpected = $this->decodeProfileJson($expected);
  304. $this->assertEquals($decodedExpected, $decodedProfile, 'Expected: ' . $expected . "\nReceived: " . $profile . "\n");
  305. }
  306. protected function stripWhitespace($string)
  307. {
  308. $string = preg_replace('/^[ ]+/m', '', $string);
  309. $string = preg_replace('/([ ]{2,})/s', ' ', $string);
  310. $string = preg_replace('/(\r|\r\n|\n){2, }/s', "\n", $string);
  311. $string = preg_replace('/(\r|\r\n|\n)$/', '', $string);
  312. return $string;
  313. }
  314. protected function getDefaultProfileOptions()
  315. {
  316. return array(
  317. 'action' => 'release',
  318. 'optimize' => 'shrinksafe',
  319. 'layerOptimize' => 'shrinksafe',
  320. 'copyTests' => false,
  321. 'loader' => 'default',
  322. 'cssOptimize' => 'comments',
  323. );
  324. }
  325. protected function decodeProfileJson($profile)
  326. {
  327. $profile = preg_replace('/^dependencies = (.*?);$/s', '$1', $profile);
  328. return Zend_Json::decode($profile);
  329. }
  330. }
  331. if (PHPUnit_MAIN_METHOD == 'Zend_Dojo_BuildLayerTest::main') {
  332. Zend_Dojo_BuildLayerTest::main();
  333. }