1
0

AppScriptSortTest.php 3.5 KB

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