aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppScriptSortTest.php
blob: 2db3e2282d99c96a23737f7f50258a01c1ee90c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
 * Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test;

use OC\AppScriptDependency;
use OC\AppScriptSort;
use Psr\Log\LoggerInterface;

/**
 * Class AppScriptSortTest
 *
 * @package Test
 * @group DB
 */
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']),
		];

		// No circular dependency is detected and logged as an error
		$this->logger->expects(self::never())->method('error');

		$scriptSort = new AppScriptSort($this->logger);
		$sortedScripts = $scriptSort->sort($scripts, $scriptDeps);

		$sortedScriptKeys = array_keys($sortedScripts);

		// Core should appear first
		$this->assertEquals(
			0,
			array_search('core', $sortedScriptKeys, true)
		);

		// Dependencies should appear before their children
		$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)
		);

		// All apps still there
		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']),
		];

		// A circular dependency is detected and logged as an error
		$this->logger->expects(self::once())->method('error');

		$scriptSort = new AppScriptSort($this->logger);
		$sortedScripts = $scriptSort->sort($scripts, $scriptDeps);

		$sortedScriptKeys = array_keys($sortedScripts);

		// All apps still there
		foreach ($scripts as $app => $_) {
			$this->assertContains($app, $sortedScriptKeys);
		}
	}
}