ConfigTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace Elgg\Amd;
  3. class ConfigTest extends \PHPUnit_Framework_TestCase {
  4. public function testCanConfigureBaseUrl() {
  5. $hooks = new \Elgg\PluginHooksService();
  6. $amdConfig = new \Elgg\Amd\Config($hooks);
  7. $amdConfig->setBaseUrl('http://foobar.com');
  8. $configArray = $amdConfig->getConfig();
  9. $this->assertEquals('http://foobar.com', $configArray['baseUrl']);
  10. }
  11. public function testCanConfigureModulePaths() {
  12. $hooks = new \Elgg\PluginHooksService();
  13. $amdConfig = new \Elgg\Amd\Config($hooks);
  14. $amdConfig->addPath('jquery', '/some/path.js');
  15. $this->assertTrue($amdConfig->hasModule('jquery'));
  16. $configArray = $amdConfig->getConfig();
  17. $this->assertEquals(array('/some/path'), $configArray['paths']['jquery']);
  18. $amdConfig->removePath('jquery', '/some/path.js');
  19. $this->assertFalse($amdConfig->hasModule('jquery'));
  20. }
  21. public function testCanConfigureModuleShims() {
  22. $hooks = new \Elgg\PluginHooksService();
  23. $amdConfig = new \Elgg\Amd\Config($hooks);
  24. $amdConfig->addShim('jquery', array(
  25. 'deps' => array('dep'),
  26. 'exports' => 'jQuery',
  27. 'random' => 'stuff',
  28. ));
  29. $this->assertTrue($amdConfig->hasShim('jquery'));
  30. $this->assertTrue($amdConfig->hasModule('jquery'));
  31. $configArray = $amdConfig->getConfig();
  32. $this->assertEquals(array('dep'), $configArray['shim']['jquery']['deps']);
  33. $this->assertEquals('jQuery', $configArray['shim']['jquery']['exports']);
  34. $this->assertFalse(isset($configArray['shim']['jquery']['random']));
  35. $amdConfig->removeShim('jquery');
  36. $this->assertFalse($amdConfig->hasShim('jquery'));
  37. $this->assertFalse($amdConfig->hasModule('jquery'));
  38. }
  39. public function testCanRequireUnregisteredAmdModules() {
  40. $hooks = new \Elgg\PluginHooksService();
  41. $amdConfig = new \Elgg\Amd\Config($hooks);
  42. $amdConfig->addDependency('jquery');
  43. $configArray = $amdConfig->getConfig();
  44. $this->assertEquals(array('jquery'), $configArray['deps']);
  45. $this->assertTrue($amdConfig->hasDependency('jquery'));
  46. $this->assertTrue($amdConfig->hasModule('jquery'));
  47. $amdConfig->removeDependency('jquery');
  48. $this->assertFalse($amdConfig->hasDependency('jquery'));
  49. $this->assertFalse($amdConfig->hasModule('jquery'));
  50. }
  51. /**
  52. * @expectedException \InvalidParameterException
  53. */
  54. public function testThrowsOnBadShim() {
  55. $hooks = new \Elgg\PluginHooksService();
  56. $amdConfig = new \Elgg\Amd\Config($hooks);
  57. $amdConfig->addShim('bad_shim', array('invalid' => 'config'));
  58. $configArray = $amdConfig->getConfig();
  59. $this->assertEquals(array('jquery'), $configArray['deps']);
  60. }
  61. public function testCanAddModuleAsAmd() {
  62. $hooks = new \Elgg\PluginHooksService();
  63. $amdConfig = new \Elgg\Amd\Config($hooks);
  64. $amdConfig->addModule('jquery');
  65. $configArray = $amdConfig->getConfig();
  66. $this->assertEquals(array('jquery'), $configArray['deps']);
  67. }
  68. public function testCanAddModuleAsShim() {
  69. $hooks = new \Elgg\PluginHooksService();
  70. $amdConfig = new \Elgg\Amd\Config($hooks);
  71. $amdConfig->addModule('jquery.form', array(
  72. 'url' => 'http://foobar.com',
  73. 'exports' => 'jquery.fn.ajaxform',
  74. 'deps' => array('jquery')
  75. ));
  76. $configArray = $amdConfig->getConfig();
  77. $this->assertArrayHasKey('jquery.form', $configArray['shim']);
  78. $this->assertEquals(array(
  79. 'exports' => 'jquery.fn.ajaxform',
  80. 'deps' => array('jquery')
  81. ), $configArray['shim']['jquery.form']);
  82. $this->assertArrayHasKey('jquery.form', $configArray['paths']);
  83. $this->assertEquals(array('http://foobar.com'), $configArray['paths']['jquery.form']);
  84. $this->assertTrue($amdConfig->hasModule('jquery.form'));
  85. $this->assertTrue($amdConfig->hasShim('jquery.form'));
  86. $amdConfig->removeModule('jquery.form');
  87. $this->assertFalse($amdConfig->hasModule('jquery.form'));
  88. $this->assertFalse($amdConfig->hasShim('jquery.form'));
  89. }
  90. public function testGetConfigTriggersTheConfigAmdPluginHook() {
  91. $hooks = new \Elgg\PluginHooksService();
  92. $amdConfig = new \Elgg\Amd\Config($hooks);
  93. $test_input = ['test' => 'test_' . time()];
  94. $hooks->registerHandler('config', 'amd', function() use ($test_input) {
  95. return $test_input;
  96. });
  97. $config = $amdConfig->getConfig();
  98. $this->assertEquals($test_input, $config);
  99. }
  100. }