PetActiveSkillServiceTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Tests\Unit\Pet;
  3. use PHPUnit\Framework\TestCase;
  4. use App\Module\Pet\Services\PetActiveSkillService;
  5. /**
  6. * 宠物激活技能服务测试
  7. */
  8. class PetActiveSkillServiceTest extends TestCase
  9. {
  10. /**
  11. * 测试服务层方法存在性
  12. */
  13. public function test_service_methods_exist()
  14. {
  15. // 测试主要方法是否存在
  16. $this->assertTrue(method_exists(PetActiveSkillService::class, 'processActiveSkills'));
  17. $this->assertTrue(method_exists(PetActiveSkillService::class, 'processSkill'));
  18. }
  19. /**
  20. * 测试Job类重构后的简洁性
  21. */
  22. public function test_job_class_is_simplified()
  23. {
  24. $jobClass = new \ReflectionClass(\App\Module\Pet\Jobs\ProcessActiveSkillsJob::class);
  25. // 检查Job类的方法数量是否减少(应该只有run和payload方法)
  26. $methods = $jobClass->getMethods(\ReflectionMethod::IS_PUBLIC);
  27. $publicMethods = array_filter($methods, function($method) {
  28. return $method->getDeclaringClass()->getName() === \App\Module\Pet\Jobs\ProcessActiveSkillsJob::class;
  29. });
  30. // 应该只有run和payload两个公共方法
  31. $this->assertLessThanOrEqual(2, count($publicMethods), 'Job类应该保持简洁,只有必要的方法');
  32. }
  33. /**
  34. * 测试Job类是否正确使用服务层
  35. */
  36. public function test_job_uses_service_layer()
  37. {
  38. $jobClass = new \ReflectionClass(\App\Module\Pet\Jobs\ProcessActiveSkillsJob::class);
  39. $runMethod = $jobClass->getMethod('run');
  40. // 获取run方法的源代码
  41. $filename = $jobClass->getFileName();
  42. $startLine = $runMethod->getStartLine();
  43. $endLine = $runMethod->getEndLine();
  44. $lines = file($filename);
  45. $methodCode = implode('', array_slice($lines, $startLine - 1, $endLine - $startLine + 1));
  46. // 检查是否调用了服务层
  47. $this->assertStringContainsString('PetActiveSkillService::processActiveSkills', $methodCode, 'Job应该调用服务层方法');
  48. }
  49. }