AppScriptSortTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use OC\AppScriptDependency;
  10. use OC\AppScriptSort;
  11. use Psr\Log\LoggerInterface;
  12. /**
  13. * Class AppScriptSortTest
  14. *
  15. * @package Test
  16. * @group DB
  17. */
  18. class AppScriptSortTest extends \Test\TestCase {
  19. private $logger;
  20. protected function setUp(): void {
  21. $this->logger = $this->getMockBuilder(LoggerInterface::class)
  22. ->disableOriginalConstructor()
  23. ->getMock();
  24. parent::setUp();
  25. }
  26. public function testSort(): void {
  27. $scripts = [
  28. 'first' => ['myFirstJSFile'],
  29. 'core' => [
  30. 'core/js/myFancyJSFile1',
  31. 'core/js/myFancyJSFile4',
  32. 'core/js/myFancyJSFile5',
  33. 'core/js/myFancyJSFile1',
  34. ],
  35. 'files' => ['files/js/myFancyJSFile2'],
  36. 'myApp5' => ['myApp5/js/myApp5JSFile'],
  37. 'myApp' => ['myApp/js/myFancyJSFile3'],
  38. 'myApp4' => ['myApp4/js/myApp4JSFile'],
  39. 'myApp3' => ['myApp3/js/myApp3JSFile'],
  40. 'myApp2' => ['myApp2/js/myApp2JSFile'],
  41. ];
  42. $scriptDeps = [
  43. 'first' => new AppScriptDependency('first', ['core']),
  44. 'core' => new AppScriptDependency('core', ['core']),
  45. 'files' => new AppScriptDependency('files', ['core']),
  46. 'myApp5' => new AppScriptDependency('myApp5', ['myApp2']),
  47. 'myApp' => new AppScriptDependency('myApp', ['core']),
  48. 'myApp4' => new AppScriptDependency('myApp4', ['myApp3']),
  49. 'myApp3' => new AppScriptDependency('myApp3', ['myApp2']),
  50. 'myApp2' => new AppScriptDependency('myApp2', ['myApp']),
  51. ];
  52. // No circular dependency is detected and logged as an error
  53. $this->logger->expects(self::never())->method('error');
  54. $scriptSort = new AppScriptSort($this->logger);
  55. $sortedScripts = $scriptSort->sort($scripts, $scriptDeps);
  56. $sortedScriptKeys = array_keys($sortedScripts);
  57. // Core should appear first
  58. $this->assertEquals(
  59. 0,
  60. array_search('core', $sortedScriptKeys, true)
  61. );
  62. // Dependencies should appear before their children
  63. $this->assertLessThan(
  64. array_search('files', $sortedScriptKeys, true),
  65. array_search('core', $sortedScriptKeys, true)
  66. );
  67. $this->assertLessThan(
  68. array_search('myApp2', $sortedScriptKeys, true),
  69. array_search('myApp', $sortedScriptKeys, true)
  70. );
  71. $this->assertLessThan(
  72. array_search('myApp3', $sortedScriptKeys, true),
  73. array_search('myApp2', $sortedScriptKeys, true)
  74. );
  75. $this->assertLessThan(
  76. array_search('myApp4', $sortedScriptKeys, true),
  77. array_search('myApp3', $sortedScriptKeys, true)
  78. );
  79. $this->assertLessThan(
  80. array_search('myApp5', $sortedScriptKeys, true),
  81. array_search('myApp2', $sortedScriptKeys, true)
  82. );
  83. // All apps still there
  84. foreach ($scripts as $app => $_) {
  85. $this->assertContains($app, $sortedScriptKeys);
  86. }
  87. }
  88. public function testSortCircularDependency(): void {
  89. $scripts = [
  90. 'circular' => ['circular/js/file1'],
  91. 'dependency' => ['dependency/js/file2'],
  92. ];
  93. $scriptDeps = [
  94. 'circular' => new AppScriptDependency('circular', ['dependency']),
  95. 'dependency' => new AppScriptDependency('dependency', ['circular']),
  96. ];
  97. // A circular dependency is detected and logged as an error
  98. $this->logger->expects(self::once())->method('error');
  99. $scriptSort = new AppScriptSort($this->logger);
  100. $sortedScripts = $scriptSort->sort($scripts, $scriptDeps);
  101. $sortedScriptKeys = array_keys($sortedScripts);
  102. // All apps still there
  103. foreach ($scripts as $app => $_) {
  104. $this->assertContains($app, $sortedScriptKeys);
  105. }
  106. }
  107. }