helpers.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Create test user
  5. */
  6. function createTestUser(array $attributes = []): \App\Models\User
  7. {
  8. $faker = \Faker\Factory::create();
  9. $defaults = [
  10. 'email' => $faker->unique()->safeEmail,
  11. 'username' => $faker->userName,
  12. 'password' => password_hash('password', PASSWORD_DEFAULT),
  13. 'port' => rand(10000, 60000),
  14. 'passwd' => bin2hex(random_bytes(16)),
  15. 'uuid' => \Ramsey\Uuid\Uuid::uuid4()->toString(),
  16. 'method' => 'aes-256-gcm',
  17. 'transfer_enable' => 1099511627776, // 1TB
  18. 'u' => 0,
  19. 'd' => 0,
  20. 'money' => 0,
  21. 'class' => 0,
  22. 'node_group' => 0,
  23. 'is_admin' => 0,
  24. 'is_banned' => 0,
  25. 'reg_date' => date('Y-m-d H:i:s'),
  26. ];
  27. $user = new \App\Models\User();
  28. foreach (array_merge($defaults, $attributes) as $key => $value) {
  29. $user->$key = $value;
  30. }
  31. return $user;
  32. }
  33. /**
  34. * Create test node
  35. */
  36. function createTestNode(array $attributes = []): \App\Models\Node
  37. {
  38. static $nodeId = 1;
  39. $defaults = [
  40. 'id' => $nodeId++,
  41. 'name' => 'Test Node ' . $nodeId,
  42. 'type' => 'shadowsocks',
  43. 'server' => 'node' . $nodeId . '.example.com',
  44. 'sort' => 0,
  45. 'info' => '1',
  46. 'status' => 'online',
  47. 'node_class' => 0,
  48. 'node_group' => 0,
  49. 'custom_config' => '{}',
  50. ];
  51. $node = new \App\Models\Node();
  52. foreach (array_merge($defaults, $attributes) as $key => $value) {
  53. $node->$key = $value;
  54. }
  55. return $node;
  56. }
  57. /**
  58. * Assert response is JSON format
  59. */
  60. function assertJsonResponse($response): array
  61. {
  62. $contentType = $response->getHeaderLine('Content-Type');
  63. PHPUnit\Framework\Assert::assertStringContainsString('application/json', $contentType);
  64. $body = (string) $response->getBody();
  65. $data = json_decode($body, true);
  66. PHPUnit\Framework\Assert::assertIsArray($data);
  67. return $data;
  68. }
  69. /**
  70. * Assert successful response
  71. */
  72. function assertSuccessResponse($response): void
  73. {
  74. PHPUnit\Framework\Assert::assertEquals(200, $response->getStatusCode());
  75. $data = assertJsonResponse($response);
  76. PHPUnit\Framework\Assert::assertEquals(1, $data['ret'] ?? $data['success'] ?? 0);
  77. }
  78. /**
  79. * Create test config
  80. */
  81. function createTestConfig(array $items = []): void
  82. {
  83. $defaults = [
  84. 'appName' => 'SSPanel-Test',
  85. 'version' => '2024.1',
  86. 'db_version' => 2023020100,
  87. 'baseUrl' => 'http://localhost',
  88. 'muKey' => bin2hex(random_bytes(32)),
  89. 'min_port' => 10000,
  90. 'max_port' => 65535,
  91. ];
  92. foreach (array_merge($defaults, $items) as $key => $value) {
  93. \App\Models\Config::set($key, $value);
  94. }
  95. }
  96. /**
  97. * Clean test database
  98. */
  99. function cleanTestDatabase(): void
  100. {
  101. $db = \App\Services\DB::getPdo();
  102. $db->exec('SET FOREIGN_KEY_CHECKS = 0');
  103. $tables = $db->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN);
  104. foreach ($tables as $table) {
  105. if ($table !== 'config' && $table !== 'migrations') {
  106. $db->exec("TRUNCATE TABLE `{$table}`");
  107. }
  108. }
  109. $db->exec('SET FOREIGN_KEY_CHECKS = 1');
  110. }
  111. /**
  112. * Run SQL file
  113. */
  114. function runSqlFile(string $file): void
  115. {
  116. $sql = file_get_contents($file);
  117. $statements = array_filter(
  118. array_map('trim', explode(';', $sql)),
  119. fn($s) => !empty($s)
  120. );
  121. $db = \App\Services\DB::getPdo();
  122. foreach ($statements as $statement) {
  123. $db->exec($statement);
  124. }
  125. }