DependencyUtilTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. namespace SPC\Tests\util;
  4. use PHPUnit\Framework\TestCase;
  5. use SPC\exception\WrongUsageException;
  6. use SPC\store\Config;
  7. use SPC\util\DependencyUtil;
  8. /**
  9. * @internal
  10. */
  11. final class DependencyUtilTest extends TestCase
  12. {
  13. public function testGetExtLibsByDeps(): void
  14. {
  15. // example
  16. Config::$source = [
  17. 'test1' => [
  18. 'type' => 'url',
  19. 'url' => 'https://pecl.php.net/get/APCu',
  20. 'filename' => 'apcu.tgz',
  21. 'license' => [
  22. 'type' => 'file',
  23. 'path' => 'LICENSE',
  24. ],
  25. ],
  26. ];
  27. Config::$lib = [
  28. 'libaaa' => [
  29. 'source' => 'test1',
  30. 'static-libs' => ['libaaa.a'],
  31. 'lib-depends' => ['libbbb', 'libccc'],
  32. 'lib-suggests' => ['libeee'],
  33. ],
  34. 'libbbb' => [
  35. 'source' => 'test1',
  36. 'static-libs' => ['libbbb.a'],
  37. 'lib-suggests' => ['libccc'],
  38. ],
  39. 'libccc' => [
  40. 'source' => 'test1',
  41. 'static-libs' => ['libccc.a'],
  42. ],
  43. 'libeee' => [
  44. 'source' => 'test1',
  45. 'static-libs' => ['libeee.a'],
  46. 'lib-suggests' => ['libfff'],
  47. ],
  48. 'libfff' => [
  49. 'source' => 'test1',
  50. 'static-libs' => ['libfff.a'],
  51. ],
  52. ];
  53. Config::$ext = [
  54. 'ext-a' => [
  55. 'type' => 'builtin',
  56. 'lib-depends' => ['libaaa'],
  57. 'ext-suggests' => ['ext-b'],
  58. ],
  59. 'ext-b' => [
  60. 'type' => 'builtin',
  61. 'lib-depends' => ['libeee'],
  62. ],
  63. ];
  64. // test getExtLibsByDeps (notmal test with ext-depends and lib-depends)
  65. [$exts, $libs, $not_included] = DependencyUtil::getExtsAndLibs(['ext-a'], include_suggested_exts: true);
  66. $this->assertContains('libbbb', $libs);
  67. $this->assertContains('libccc', $libs);
  68. $this->assertContains('ext-b', $exts);
  69. $this->assertContains('ext-b', $not_included);
  70. // test dep order
  71. $this->assertIsInt($b = array_search('libbbb', $libs));
  72. $this->assertIsInt($c = array_search('libccc', $libs));
  73. $this->assertIsInt($a = array_search('libaaa', $libs));
  74. // libbbb, libaaa
  75. $this->assertTrue($b < $a);
  76. $this->assertTrue($c < $a);
  77. $this->assertTrue($c < $b);
  78. }
  79. public function testNotExistExtException(): void
  80. {
  81. $this->expectException(WrongUsageException::class);
  82. DependencyUtil::getExtsAndLibs(['sdsd']);
  83. }
  84. }