DependencyAnalyzerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. /**
  3. * @author Thomas Müller
  4. * @author Lukas Reschke
  5. * @copyright 2014 Thomas Müller deepdiver@owncloud.com
  6. * @copyright 2016 Lukas Reschke <lukas@statuscode.ch>
  7. *
  8. * See the COPYING-README file.
  9. */
  10. namespace Test\App;
  11. use OC\App\DependencyAnalyzer;
  12. use OC\App\Platform;
  13. use OCP\IL10N;
  14. use Test\TestCase;
  15. class DependencyAnalyzerTest extends TestCase {
  16. /** @var Platform|\PHPUnit\Framework\MockObject\MockObject */
  17. private $platformMock;
  18. /** @var IL10N */
  19. private $l10nMock;
  20. /** @var DependencyAnalyzer */
  21. private $analyser;
  22. protected function setUp(): void {
  23. $this->platformMock = $this->getMockBuilder(Platform::class)
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $this->platformMock->expects($this->any())
  27. ->method('getPhpVersion')
  28. ->willReturn('5.4.3');
  29. $this->platformMock->expects($this->any())
  30. ->method('getIntSize')
  31. ->willReturn(4);
  32. $this->platformMock->expects($this->any())
  33. ->method('getDatabase')
  34. ->willReturn('mysql');
  35. $this->platformMock->expects($this->any())
  36. ->method('getOS')
  37. ->willReturn('Linux');
  38. $this->platformMock->expects($this->any())
  39. ->method('isCommandKnown')
  40. ->willReturnCallback(function ($command) {
  41. return ($command === 'grep');
  42. });
  43. $this->platformMock->expects($this->any())
  44. ->method('getLibraryVersion')
  45. ->willReturnCallback(function ($lib) {
  46. if ($lib === 'curl') {
  47. return "2.3.4";
  48. }
  49. return null;
  50. });
  51. $this->platformMock->expects($this->any())
  52. ->method('getOcVersion')
  53. ->willReturn('8.0.2');
  54. $this->l10nMock = $this->getMockBuilder(IL10N::class)
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->l10nMock->expects($this->any())
  58. ->method('t')
  59. ->willReturnCallback(function ($text, $parameters = []) {
  60. return vsprintf($text, $parameters);
  61. });
  62. $this->analyser = new DependencyAnalyzer($this->platformMock, $this->l10nMock);
  63. }
  64. /**
  65. * @dataProvider providesPhpVersion
  66. *
  67. * @param string $expectedMissing
  68. * @param string $minVersion
  69. * @param string $maxVersion
  70. * @param string $intSize
  71. */
  72. public function testPhpVersion($expectedMissing, $minVersion, $maxVersion, $intSize) {
  73. $app = [
  74. 'dependencies' => [
  75. 'php' => []
  76. ]
  77. ];
  78. if (!is_null($minVersion)) {
  79. $app['dependencies']['php']['@attributes']['min-version'] = $minVersion;
  80. }
  81. if (!is_null($maxVersion)) {
  82. $app['dependencies']['php']['@attributes']['max-version'] = $maxVersion;
  83. }
  84. if (!is_null($intSize)) {
  85. $app['dependencies']['php']['@attributes']['min-int-size'] = $intSize;
  86. }
  87. $missing = $this->analyser->analyze($app);
  88. $this->assertTrue(is_array($missing));
  89. $this->assertEquals($expectedMissing, $missing);
  90. }
  91. /**
  92. * @dataProvider providesDatabases
  93. * @param $expectedMissing
  94. * @param $databases
  95. */
  96. public function testDatabases($expectedMissing, $databases) {
  97. $app = [
  98. 'dependencies' => [
  99. ]
  100. ];
  101. if (!is_null($databases)) {
  102. $app['dependencies']['database'] = $databases;
  103. }
  104. $missing = $this->analyser->analyze($app);
  105. $this->assertTrue(is_array($missing));
  106. $this->assertEquals($expectedMissing, $missing);
  107. }
  108. /**
  109. * @dataProvider providesCommands
  110. *
  111. * @param string $expectedMissing
  112. * @param string|null $commands
  113. */
  114. public function testCommand($expectedMissing, $commands) {
  115. $app = [
  116. 'dependencies' => [
  117. ]
  118. ];
  119. if (!is_null($commands)) {
  120. $app['dependencies']['command'] = $commands;
  121. }
  122. $missing = $this->analyser->analyze($app);
  123. $this->assertTrue(is_array($missing));
  124. $this->assertEquals($expectedMissing, $missing);
  125. }
  126. /**
  127. * @dataProvider providesLibs
  128. * @param $expectedMissing
  129. * @param $libs
  130. */
  131. public function testLibs($expectedMissing, $libs) {
  132. $app = [
  133. 'dependencies' => [
  134. ]
  135. ];
  136. if (!is_null($libs)) {
  137. $app['dependencies']['lib'] = $libs;
  138. }
  139. $missing = $this->analyser->analyze($app);
  140. $this->assertTrue(is_array($missing));
  141. $this->assertEquals($expectedMissing, $missing);
  142. }
  143. /**
  144. * @dataProvider providesOS
  145. * @param $expectedMissing
  146. * @param $oss
  147. */
  148. public function testOS($expectedMissing, $oss) {
  149. $app = [
  150. 'dependencies' => []
  151. ];
  152. if (!is_null($oss)) {
  153. $app['dependencies']['os'] = $oss;
  154. }
  155. $missing = $this->analyser->analyze($app);
  156. $this->assertTrue(is_array($missing));
  157. $this->assertEquals($expectedMissing, $missing);
  158. }
  159. /**
  160. * @dataProvider providesOC
  161. * @param $expectedMissing
  162. * @param $oc
  163. */
  164. public function testOC($expectedMissing, $oc) {
  165. $app = [
  166. 'dependencies' => []
  167. ];
  168. if (!is_null($oc)) {
  169. $app['dependencies'] = $oc;
  170. }
  171. $missing = $this->analyser->analyze($app);
  172. $this->assertTrue(is_array($missing));
  173. $this->assertEquals($expectedMissing, $missing);
  174. }
  175. /**
  176. * @return array
  177. */
  178. public function providesOC() {
  179. return [
  180. // no version -> no missing dependency
  181. [
  182. [],
  183. null,
  184. ],
  185. [
  186. [],
  187. [
  188. 'nextcloud' => [
  189. '@attributes' => [
  190. 'min-version' => '8',
  191. 'max-version' => '8',
  192. ],
  193. ],
  194. ],
  195. ],
  196. [
  197. [],
  198. [
  199. 'nextcloud' => [
  200. '@attributes' => [
  201. 'min-version' => '8.0',
  202. 'max-version' => '8.0',
  203. ],
  204. ],
  205. ],
  206. ],
  207. [
  208. [],
  209. [
  210. 'nextcloud' => [
  211. '@attributes' => [
  212. 'min-version' => '8.0.2',
  213. 'max-version' => '8.0.2'
  214. ],
  215. ],
  216. ],
  217. ],
  218. [
  219. [
  220. 'Server version 8.0.3 or higher is required.',
  221. ],
  222. [
  223. 'nextcloud' => [
  224. '@attributes' => [
  225. 'min-version' => '8.0.3'
  226. ],
  227. ],
  228. ],
  229. ],
  230. [
  231. [
  232. 'Server version 9 or higher is required.',
  233. ],
  234. [
  235. 'nextcloud' => [
  236. '@attributes' => [
  237. 'min-version' => '9'
  238. ],
  239. ],
  240. ],
  241. ],
  242. [
  243. [
  244. 'Server version 10 or higher is required.',
  245. ],
  246. [
  247. 'nextcloud' => [
  248. '@attributes' => [
  249. 'min-version' => '10'
  250. ],
  251. ],
  252. 'owncloud' => [
  253. '@attributes' => [
  254. 'min-version' => '9'
  255. ],
  256. ],
  257. ],
  258. ],
  259. [
  260. [
  261. 'Server version 10 or higher is required.',
  262. ],
  263. [
  264. 'nextcloud' => [
  265. '@attributes' => [
  266. 'min-version' => '9.1',
  267. ],
  268. ],
  269. ],
  270. ],
  271. [
  272. [
  273. 'Server version 9.2 or higher is required.',
  274. ],
  275. [
  276. 'nextcloud' => [
  277. '@attributes' => [
  278. 'min-version' => '9.2',
  279. ],
  280. ],
  281. ],
  282. ],
  283. [
  284. [
  285. 'Server version 11 or higher is required.',
  286. ],
  287. [
  288. 'nextcloud' => [
  289. '@attributes' => [
  290. 'min-version' => '11',
  291. ],
  292. ],
  293. ],
  294. ],
  295. [
  296. [
  297. 'Server version 8.0.1 or lower is required.',
  298. ],
  299. [
  300. 'nextcloud' => [
  301. '@attributes' => [
  302. 'max-version' => '8.0.1',
  303. ],
  304. ],
  305. ],
  306. ],
  307. [
  308. [],
  309. [
  310. 'owncloud' => [
  311. '@attributes' => [
  312. 'min-version' => '8',
  313. 'max-version' => '8',
  314. ],
  315. ],
  316. ],
  317. ],
  318. [
  319. [],
  320. [
  321. 'owncloud' => [
  322. '@attributes' => [
  323. 'min-version' => '8.0',
  324. 'max-version' => '8.0',
  325. ],
  326. ],
  327. ],
  328. ],
  329. [
  330. [],
  331. [
  332. 'owncloud' => [
  333. '@attributes' => [
  334. 'min-version' => '8.0.2',
  335. 'max-version' => '8.0.2'
  336. ],
  337. ],
  338. ],
  339. ],
  340. [
  341. [
  342. 'Server version 8.0.3 or higher is required.',
  343. ],
  344. [
  345. 'owncloud' => [
  346. '@attributes' => [
  347. 'min-version' => '8.0.3'
  348. ],
  349. ],
  350. ],
  351. ],
  352. [
  353. [
  354. 'Server version 9 or higher is required.',
  355. ],
  356. [
  357. 'owncloud' => [
  358. '@attributes' => [
  359. 'min-version' => '9'
  360. ],
  361. ],
  362. ],
  363. ],
  364. [
  365. [
  366. 'Server version 10 or higher is required.',
  367. ],
  368. [
  369. 'owncloud' => [
  370. '@attributes' => [
  371. 'min-version' => '9.1',
  372. ],
  373. ],
  374. ],
  375. ],
  376. [
  377. [
  378. 'Server version 9.2 or higher is required.',
  379. ],
  380. [
  381. 'owncloud' => [
  382. '@attributes' => [
  383. 'min-version' => '9.2',
  384. ],
  385. ],
  386. ],
  387. ],
  388. [
  389. [
  390. 'Server version 8.0.1 or lower is required.',
  391. ],
  392. [
  393. 'owncloud' => [
  394. '@attributes' => [
  395. 'max-version' => '8.0.1',
  396. ],
  397. ],
  398. ],
  399. ],
  400. ];
  401. }
  402. /**
  403. * @return array
  404. */
  405. public function providesOS() {
  406. return [
  407. [[], null],
  408. [[], []],
  409. [['The following platforms are supported: ANDROID'], 'ANDROID'],
  410. [['The following platforms are supported: WINNT'], ['WINNT']]
  411. ];
  412. }
  413. /**
  414. * @return array
  415. */
  416. public function providesLibs() {
  417. return [
  418. // we expect curl to exist
  419. [[], 'curl'],
  420. // we expect abcde to exist
  421. [['The library abcde is not available.'], ['abcde']],
  422. // curl in version 100.0 does not exist
  423. [['Library curl with a version higher than 100.0 is required - available version 2.3.4.'],
  424. [['@attributes' => ['min-version' => '100.0'], '@value' => 'curl']]],
  425. // curl in version 100.0 does not exist
  426. [['Library curl with a version lower than 1.0.0 is required - available version 2.3.4.'],
  427. [['@attributes' => ['max-version' => '1.0.0'], '@value' => 'curl']]],
  428. [['Library curl with a version lower than 2.3.3 is required - available version 2.3.4.'],
  429. [['@attributes' => ['max-version' => '2.3.3'], '@value' => 'curl']]],
  430. [['Library curl with a version higher than 2.3.5 is required - available version 2.3.4.'],
  431. [['@attributes' => ['min-version' => '2.3.5'], '@value' => 'curl']]],
  432. [[],
  433. [['@attributes' => ['min-version' => '2.3.4', 'max-version' => '2.3.4'], '@value' => 'curl']]],
  434. [[],
  435. [['@attributes' => ['min-version' => '2.3', 'max-version' => '2.3'], '@value' => 'curl']]],
  436. [[],
  437. [['@attributes' => ['min-version' => '2', 'max-version' => '2'], '@value' => 'curl']]],
  438. [[],
  439. ['@attributes' => ['min-version' => '2', 'max-version' => '2'], '@value' => 'curl']],
  440. ];
  441. }
  442. /**
  443. * @return array
  444. */
  445. public function providesCommands() {
  446. return [
  447. [[], null],
  448. // grep is known on linux
  449. [[], [['@attributes' => ['os' => 'Linux'], '@value' => 'grep']]],
  450. // grepp is not known on linux
  451. [['The command line tool grepp could not be found'], [['@attributes' => ['os' => 'Linux'], '@value' => 'grepp']]],
  452. // we don't care about tools on Windows - we are on Linux
  453. [[], [['@attributes' => ['os' => 'Windows'], '@value' => 'grepp']]],
  454. // grep is known on all systems
  455. [[], 'grep'],
  456. [[], ['@attributes' => ['os' => 'Linux'], '@value' => 'grep']],
  457. ];
  458. }
  459. /**
  460. * @return array
  461. */
  462. public function providesDatabases() {
  463. return [
  464. // non BC - in case on databases are defined -> all are supported
  465. [[], null],
  466. [[], []],
  467. [['The following databases are supported: mongodb'], 'mongodb'],
  468. [['The following databases are supported: sqlite, postgres'], ['sqlite', ['@value' => 'postgres']]],
  469. ];
  470. }
  471. /**
  472. * @return array
  473. */
  474. public function providesPhpVersion() {
  475. return [
  476. [[], null, null, null],
  477. [[], '5.4', null, null],
  478. [[], null, '5.5', null],
  479. [[], '5.4', '5.5', null],
  480. [['PHP 5.4.4 or higher is required.'], '5.4.4', null, null],
  481. [['PHP with a version lower than 5.4.2 is required.'], null, '5.4.2', null],
  482. [['64bit or higher PHP required.'], null, null, 64],
  483. [[], '5.4', '5.4', null],
  484. ];
  485. }
  486. }