ProTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zed
  5. * Date: 17-11-13
  6. * Time: 上午10:00
  7. */
  8. use DfaFilter\SensitiveHelper;
  9. use PHPUnit\Framework\TestCase;
  10. class BaseTest extends TestCase
  11. {
  12. protected $wordPoolPath;
  13. public function setUp()
  14. {
  15. parent::setUp();
  16. // 铭感词文件路径
  17. $this->wordPoolPath = 'tests/data/words.txt';
  18. }
  19. public function testGetBadWord()
  20. {
  21. $sTime = microtime(true);
  22. $content = '这是一段测试语句,请忽略赌球网, 第二个敏感词是三级片';
  23. // 过滤,其中【赌球网】在词库中
  24. $filterContent = SensitiveHelper::init()
  25. ->setTreeByFile($this->wordPoolPath)
  26. ->getBadWord($content);
  27. // 返回规定数量的敏感词,其中【赌球网,三级片】在词库中
  28. $badWords = SensitiveHelper::init()
  29. ->setTreeByFile($this->wordPoolPath)
  30. ->getBadWord($content, 1, 2);
  31. $eTime = microtime(true);
  32. echo ($eTime - $sTime) * 1000 . 'ms' . PHP_EOL;
  33. $this->assertEquals('赌球网', $filterContent[0]);
  34. $this->assertEquals('三级片', $badWords[1]);
  35. }
  36. public function testFilterWord()
  37. {
  38. $content = '这是一段测试语句,请忽略赌球网';
  39. // 过滤,其中【赌球网】在词库中
  40. $filterContent = SensitiveHelper::init()
  41. ->setTreeByFile($this->wordPoolPath)
  42. ->replace($content,'*');
  43. $this->assertEquals('这是一段测试语句,请忽略*', $filterContent);
  44. // 过滤,其中【赌球网】在词库中
  45. $filterContent = SensitiveHelper::init()
  46. ->setTreeByFile($this->wordPoolPath)
  47. ->replace($content,'*', true);
  48. $this->assertEquals('这是一段测试语句,请忽略***', $filterContent);
  49. }
  50. public function testMarkWord()
  51. {
  52. $content = '这是一段测试语句,请忽略赌球网';
  53. // 过滤,其中【赌球网】在词库中
  54. $markedContent = SensitiveHelper::init()
  55. ->setTreeByFile($this->wordPoolPath)
  56. ->mark($content,'<mark>', '</mark>');
  57. $this->assertEquals('这是一段测试语句,请忽略<mark>赌球网</mark>', $markedContent);
  58. }
  59. }