FeatureServiceTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Tests\Unit\Customer;
  3. use App\Feature;
  4. use App\Services\FeatureService;
  5. use Tests\TestCase;
  6. class FeatureServiceTest extends TestCase
  7. {
  8. /** @var FeatureService */
  9. public $service;
  10. public $data;
  11. protected function setUp(): void
  12. {
  13. parent::setUp();
  14. $this->service = app(FeatureService::class);
  15. $this->data = [
  16. "models" => factory(Feature::class,3)->create()->toArray(),
  17. ];
  18. }
  19. public function testGetMapArray(){
  20. $models = $this->service->getMapArray();
  21. $this->assertGreaterThanOrEqual(3,count($models));
  22. }
  23. public function testTranslationFeature(){
  24. $str = $this->data["models"][0]["id"]."&(".$this->data["models"][1]["id"]."|".$this->data["models"][2]["id"].")";
  25. $result = $this->service->translationFeature($str);
  26. $this->assertEquals(3,count($result));
  27. $this->assertEquals($this->data["models"][1]["type"],$result[1]["type"]);
  28. $this->assertEquals(false,$result[0]["strategyGroupStartSign"]);
  29. $this->assertEquals(true,$result[1]["strategyGroupStartSign"]);
  30. $this->assertEquals("并且",$result[1]["calculation"]);
  31. $this->assertEquals(true,$result[2]["strategyGroupEndSign"]);
  32. $this->assertEquals("或",$result[2]["calculation"]);
  33. }
  34. //TODO 标记
  35. public function testAnalysisFeature(){
  36. $model1 = factory(Feature::class)->create($this->data[4]);
  37. $model2 = factory(Feature::class)->create($this->data[5]);
  38. $model3 = factory(Feature::class)->create($this->data[3]);
  39. $params = [
  40. [
  41. "strategyGroupStartSign" => true,
  42. "calculation" => "",
  43. "type"=>"商品名称",
  44. "logic"=>"不包含",
  45. "describe"=>"鞋子",
  46. "strategyGroupEndSign" => false,
  47. ],[
  48. "strategyGroupStartSign" => false,
  49. "calculation" => "并且",
  50. "type"=>"订单类型",
  51. "logic"=>"包含",
  52. "describe"=>"取消",
  53. "strategyGroupEndSign" => true,
  54. ],[
  55. "strategyGroupStartSign" => false,
  56. "calculation" => "或",
  57. "type"=>"店铺类型",
  58. "logic"=>"不包含",
  59. "describe"=>"日用品",
  60. "strategyGroupEndSign" => false,
  61. ]
  62. ];
  63. $expected = "(".$model1->id."&".$model2->id.")|".$model3->id;
  64. $result = $this->service->analysisFeature($params);
  65. $this->assertIsArray($result);
  66. $this->assertCount(2,$result);
  67. $this->assertArrayHasKey("feature",$result);
  68. $this->assertEquals($expected,$result["feature"]);
  69. }
  70. public function testFormatFeature(){
  71. $model1 = factory(Feature::class)->make($this->data[0]);
  72. $model2 = factory(Feature::class)->make($this->data[1]);
  73. $model3 = factory(Feature::class)->make($this->data[2]);
  74. $features = [1=>$model1,2=>$model2,3=>$model3];
  75. $result = $this->service->formatFeature($features,"1&(2|3)");
  76. $expected = "商品名称 包含 衣服 并且 (订单类型 不包含 创建 或 承运商 等于 顺丰)";
  77. $this->assertEquals($expected,$result);
  78. $columnMapping = ["商品名称"=>"commodity_name","订单类型"=>"order_type","承运商"=>"logistic_name"];
  79. $result = $this->service->formatFeature($features,"1&(2|3)",$columnMapping);
  80. $expected = "commodity_name like '%衣服%' and (order_type not like '%创建%' or logistic_name = '顺丰')";
  81. $this->assertEquals($expected,$result);
  82. }
  83. public function testMatchFeature(){
  84. $model1 = factory(Feature::class)->create($this->data[6]);
  85. $model2 = factory(Feature::class)->create($this->data[7]);
  86. $model3 = factory(Feature::class)->create($this->data[8]);
  87. $columnMapping = ["商品名称"=>"commodity","订单类型"=>"order","店铺类型"=>"shop"];
  88. $matchObject = ["commodity"=>"测试商品","order"=>"TEST","shop"=>"测试"];
  89. $value = $model1->id."&(".$model2->id."&".$model3->id.")";
  90. $result = $this->service->matchFeature($value,$columnMapping,$matchObject);
  91. $this->assertEquals(true,$result);
  92. $matchObject = ["commodity"=>"测试商品","order"=>"实际","shop"=>"测试"];
  93. $result = $this->service->matchFeature($value,$columnMapping,$matchObject);
  94. $this->assertEquals(false,$result);
  95. }
  96. public function testTruncate(){
  97. Feature::query()->truncate();
  98. $feature = Feature::query()->get();
  99. $this->assertCount(0,$feature);
  100. }
  101. public function tearDown(): void
  102. {
  103. Feature::destroy(array_column($this->data["models"],"id"));
  104. parent::tearDown();
  105. }
  106. }