BaseTest.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 $wordData;
  13. public function setUp()
  14. {
  15. parent::setUp();
  16. // 获取铭感词库
  17. $wordPool = file_get_contents('tests/data/keyWord.txt');
  18. $this->wordData = explode(',', $wordPool);
  19. }
  20. public function testGetBadWord()
  21. {
  22. $content = '这是一段测试语句,请忽略赌球网, 第二个敏感词是三级片';
  23. // 过滤,其中【赌球网】在词库中
  24. $filterContent = SensitiveHelper::init()
  25. ->setTree($this->wordData)
  26. ->getBadWord($content);
  27. // 返回规定数量的敏感词,其中【赌球网,三级片】在词库中
  28. $badWords = SensitiveHelper::init()
  29. ->setTree($this->wordData)
  30. ->getBadWord($content, 1, 2);
  31. $this->assertEquals('赌球网', $filterContent[0]);
  32. $this->assertEquals('三级片', $badWords[1]);
  33. }
  34. public function testFilterWord()
  35. {
  36. $content = '这是一段测试语句,请忽略赌球网';
  37. // 过滤,其中【赌球网】在词库中
  38. $filterContent = SensitiveHelper::init()
  39. ->setTree($this->wordData)
  40. ->replace($content,'*');
  41. $this->assertEquals('这是一段测试语句,请忽略*',$filterContent);
  42. }
  43. }