ConfigValidatorTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. declare(strict_types=1);
  3. namespace SPC\Tests\util;
  4. use PHPUnit\Framework\TestCase;
  5. use SPC\exception\ValidationException;
  6. use SPC\util\ConfigValidator;
  7. /**
  8. * @internal
  9. */
  10. class ConfigValidatorTest extends TestCase
  11. {
  12. public function testValidateSourceGood(): void
  13. {
  14. $good_source = [
  15. 'source1' => [
  16. 'type' => 'filelist',
  17. 'url' => 'https://example.com',
  18. 'regex' => '.*',
  19. ],
  20. 'source2' => [
  21. 'type' => 'git',
  22. 'url' => 'https://example.com',
  23. 'rev' => 'master',
  24. ],
  25. 'source3' => [
  26. 'type' => 'ghtagtar',
  27. 'repo' => 'aaaa/bbbb',
  28. ],
  29. 'source4' => [
  30. 'type' => 'ghtar',
  31. 'repo' => 'aaa/bbb',
  32. 'path' => 'path/to/dir',
  33. ],
  34. 'source5' => [
  35. 'type' => 'ghrel',
  36. 'repo' => 'aaa/bbb',
  37. 'match' => '.*',
  38. ],
  39. 'source6' => [
  40. 'type' => 'url',
  41. 'url' => 'https://example.com',
  42. ],
  43. ];
  44. try {
  45. ConfigValidator::validateSource($good_source);
  46. $this->assertTrue(true);
  47. } catch (ValidationException $e) {
  48. $this->fail($e->getMessage());
  49. }
  50. }
  51. public function testValidateSourceBad(): void
  52. {
  53. $bad_source = [
  54. 'source1' => [
  55. 'type' => 'filelist',
  56. 'url' => 'https://example.com',
  57. // no regex
  58. ],
  59. 'source2' => [
  60. 'type' => 'git',
  61. 'url' => true, // not string
  62. 'rev' => 'master',
  63. ],
  64. 'source3' => [
  65. 'type' => 'ghtagtar',
  66. 'url' => 'aaaa/bbbb', // not repo
  67. ],
  68. 'source4' => [
  69. 'type' => 'ghtar',
  70. 'repo' => 'aaa/bbb',
  71. 'path' => true, // not string
  72. ],
  73. 'source5' => [
  74. 'type' => 'ghrel',
  75. 'repo' => 'aaa/bbb',
  76. 'match' => 1, // not string
  77. ],
  78. 'source6' => [
  79. 'type' => 'url', // no url
  80. ],
  81. ];
  82. foreach ($bad_source as $name => $src) {
  83. try {
  84. ConfigValidator::validateSource([$name => $src]);
  85. $this->fail("should throw ValidationException for source {$name}");
  86. } catch (ValidationException) {
  87. $this->assertTrue(true);
  88. }
  89. }
  90. }
  91. public function testValidateLibsGood(): void
  92. {
  93. $good_libs = [
  94. 'lib1' => [
  95. 'source' => 'source1',
  96. ],
  97. 'lib2' => [
  98. 'source' => 'source2',
  99. 'lib-depends' => [
  100. 'lib1',
  101. ],
  102. ],
  103. 'lib3' => [
  104. 'source' => 'source3',
  105. 'lib-suggests' => [
  106. 'lib1',
  107. ],
  108. ],
  109. ];
  110. try {
  111. ConfigValidator::validateLibs($good_libs, ['source1' => [], 'source2' => [], 'source3' => []]);
  112. $this->assertTrue(true);
  113. } catch (ValidationException $e) {
  114. $this->fail($e->getMessage());
  115. }
  116. }
  117. public function testValidateLibsBad(): void
  118. {
  119. // lib.json is broken
  120. try {
  121. ConfigValidator::validateLibs('not array');
  122. $this->fail('should throw ValidationException');
  123. } catch (ValidationException) {
  124. $this->assertTrue(true);
  125. }
  126. // lib source not exists
  127. try {
  128. ConfigValidator::validateLibs(['lib1' => ['source' => 'source3']], ['source1' => [], 'source2' => []]);
  129. $this->fail('should throw ValidationException');
  130. } catch (ValidationException) {
  131. $this->assertTrue(true);
  132. }
  133. // source must be string
  134. try {
  135. ConfigValidator::validateLibs(['lib1' => ['source' => true]], ['source1' => [], 'source2' => []]);
  136. $this->fail('should throw ValidationException');
  137. } catch (ValidationException) {
  138. $this->assertTrue(true);
  139. }
  140. // lib-depends must be list
  141. try {
  142. ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'lib-depends' => ['a' => 'not list']]], ['source1' => [], 'source2' => []]);
  143. $this->fail('should throw ValidationException');
  144. } catch (ValidationException) {
  145. $this->assertTrue(true);
  146. }
  147. // lib-suggests must be list
  148. try {
  149. ConfigValidator::validateLibs(['lib1' => ['source' => 'source1', 'lib-suggests' => ['a' => 'not list']]], ['source1' => [], 'source2' => []]);
  150. $this->fail('should throw ValidationException');
  151. } catch (ValidationException) {
  152. $this->assertTrue(true);
  153. }
  154. }
  155. /**
  156. * @throws ValidationException
  157. */
  158. public function testValidateExts(): void
  159. {
  160. ConfigValidator::validateExts([]);
  161. $this->expectException(ValidationException::class);
  162. ConfigValidator::validateExts(null);
  163. }
  164. }