123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace Test;
- use OC\AppScriptDependency;
- use OC\AppScriptSort;
- use Psr\Log\LoggerInterface;
- class AppScriptSortTest extends \Test\TestCase {
- private $logger;
- protected function setUp(): void {
- $this->logger = $this->getMockBuilder(LoggerInterface::class)
- ->disableOriginalConstructor()
- ->getMock();
- parent::setUp();
- }
- public function testSort(): void {
- $scripts = [
- 'first' => ['myFirstJSFile'],
- 'core' => [
- 'core/js/myFancyJSFile1',
- 'core/js/myFancyJSFile4',
- 'core/js/myFancyJSFile5',
- 'core/js/myFancyJSFile1',
- ],
- 'files' => ['files/js/myFancyJSFile2'],
- 'myApp5' => ['myApp5/js/myApp5JSFile'],
- 'myApp' => ['myApp/js/myFancyJSFile3'],
- 'myApp4' => ['myApp4/js/myApp4JSFile'],
- 'myApp3' => ['myApp3/js/myApp3JSFile'],
- 'myApp2' => ['myApp2/js/myApp2JSFile'],
- ];
- $scriptDeps = [
- 'first' => new AppScriptDependency('first', ['core']),
- 'core' => new AppScriptDependency('core', ['core']),
- 'files' => new AppScriptDependency('files', ['core']),
- 'myApp5' => new AppScriptDependency('myApp5', ['myApp2']),
- 'myApp' => new AppScriptDependency('myApp', ['core']),
- 'myApp4' => new AppScriptDependency('myApp4', ['myApp3']),
- 'myApp3' => new AppScriptDependency('myApp3', ['myApp2']),
- 'myApp2' => new AppScriptDependency('myApp2', ['myApp']),
- ];
-
- $this->logger->expects(self::never())->method('error');
- $scriptSort = new AppScriptSort($this->logger);
- $sortedScripts = $scriptSort->sort($scripts, $scriptDeps);
- $sortedScriptKeys = array_keys($sortedScripts);
-
- $this->assertEquals(
- 0,
- array_search('core', $sortedScriptKeys, true)
- );
-
- $this->assertLessThan(
- array_search('files', $sortedScriptKeys, true),
- array_search('core', $sortedScriptKeys, true)
- );
- $this->assertLessThan(
- array_search('myApp2', $sortedScriptKeys, true),
- array_search('myApp', $sortedScriptKeys, true)
- );
- $this->assertLessThan(
- array_search('myApp3', $sortedScriptKeys, true),
- array_search('myApp2', $sortedScriptKeys, true)
- );
- $this->assertLessThan(
- array_search('myApp4', $sortedScriptKeys, true),
- array_search('myApp3', $sortedScriptKeys, true)
- );
- $this->assertLessThan(
- array_search('myApp5', $sortedScriptKeys, true),
- array_search('myApp2', $sortedScriptKeys, true)
- );
-
- foreach ($scripts as $app => $_) {
- $this->assertContains($app, $sortedScriptKeys);
- }
- }
- public function testSortCircularDependency(): void {
- $scripts = [
- 'circular' => ['circular/js/file1'],
- 'dependency' => ['dependency/js/file2'],
- ];
- $scriptDeps = [
- 'circular' => new AppScriptDependency('circular', ['dependency']),
- 'dependency' => new AppScriptDependency('dependency', ['circular']),
- ];
-
- $this->logger->expects(self::once())->method('error');
- $scriptSort = new AppScriptSort($this->logger);
- $sortedScripts = $scriptSort->sort($scripts, $scriptDeps);
- $sortedScriptKeys = array_keys($sortedScripts);
-
- foreach ($scripts as $app => $_) {
- $this->assertContains($app, $sortedScriptKeys);
- }
- }
- }
|