FeatureServiceTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. [
  17. "type" => "商品名称",
  18. "logic" => "包含",
  19. "describe"=>"衣服",
  20. ],[
  21. "type" => "订单类型",
  22. "logic" => "不包含",
  23. "describe"=>"创建",
  24. ],[
  25. "type" => "承运商",
  26. "logic" => "等于",
  27. "describe"=>"顺丰",
  28. ],[
  29. "type" => "店铺类型",
  30. "logic" => "不包含",
  31. "describe"=>"日用品",
  32. ],[
  33. "type" => "商品名称",
  34. "logic" => "不包含",
  35. "describe"=>"鞋子",
  36. ],[
  37. "type" => "订单类型",
  38. "logic" => "包含",
  39. "describe"=>"取消",
  40. ],[
  41. "type" => "商品名称",
  42. "logic" => "包含",
  43. "describe"=>"测试",
  44. ],[
  45. "type" => "订单类型",
  46. "logic" => "不包含",
  47. "describe"=>"实际",
  48. ],[
  49. "type" => "店铺类型",
  50. "logic" => "等于",
  51. "describe"=>"测试",
  52. ]
  53. ];
  54. }
  55. public function testGetMapArray(){
  56. factory(Feature::class,3)->create();
  57. $models = $this->service->getMapArray();
  58. $this->assertGreaterThanOrEqual(3,count($models));
  59. }
  60. public function testTranslationFeature(){
  61. $model1 = factory(Feature::class)->create($this->data[0]);
  62. $model2 = factory(Feature::class)->create($this->data[1]);
  63. $model3 = factory(Feature::class)->create($this->data[2]);
  64. $str = $model1->id."&(".$model2->id."|".$model3->id.")";
  65. $result = $this->service->translationFeature($str);
  66. $this->assertEquals(3,count($result));
  67. $this->assertEquals("订单类型",$result[1]["type"]);
  68. $this->assertEquals(false,$result[0]["strategyGroupStartSign"]);
  69. $this->assertEquals(true,$result[1]["strategyGroupStartSign"]);
  70. $this->assertEquals("并且",$result[1]["calculation"]);
  71. $this->assertEquals(true,$result[2]["strategyGroupEndSign"]);
  72. $this->assertEquals("或",$result[2]["calculation"]);
  73. }
  74. public function testAnalysisFeature(){
  75. $model1 = factory(Feature::class)->create($this->data[4]);
  76. $model2 = factory(Feature::class)->create($this->data[5]);
  77. $model3 = factory(Feature::class)->create($this->data[3]);
  78. $params = [
  79. [
  80. "strategyGroupStartSign" => true,
  81. "calculation" => "",
  82. "type"=>"商品名称",
  83. "logic"=>"不包含",
  84. "describe"=>"鞋子",
  85. "strategyGroupEndSign" => false,
  86. ],[
  87. "strategyGroupStartSign" => false,
  88. "calculation" => "并且",
  89. "type"=>"订单类型",
  90. "logic"=>"包含",
  91. "describe"=>"取消",
  92. "strategyGroupEndSign" => true,
  93. ],[
  94. "strategyGroupStartSign" => false,
  95. "calculation" => "或",
  96. "type"=>"店铺类型",
  97. "logic"=>"不包含",
  98. "describe"=>"日用品",
  99. "strategyGroupEndSign" => false,
  100. ]
  101. ];
  102. $expected = "(".$model1->id."&".$model2->id.")|".$model3->id;
  103. $result = $this->service->analysisFeature($params);
  104. $this->assertIsArray($result);
  105. $this->assertCount(2,$result);
  106. $this->assertArrayHasKey("feature",$result);
  107. $this->assertEquals($expected,$result["feature"]);
  108. }
  109. public function testFormatFeature(){
  110. $model1 = factory(Feature::class)->make($this->data[0]);
  111. $model2 = factory(Feature::class)->make($this->data[1]);
  112. $model3 = factory(Feature::class)->make($this->data[2]);
  113. $features = [1=>$model1,2=>$model2,3=>$model3];
  114. $result = $this->service->formatFeature($features,"1&(2|3)");
  115. $expected = "商品名称 包含 衣服 并且 (订单类型 不包含 创建 或 承运商 等于 顺丰)";
  116. $this->assertEquals($expected,$result);
  117. $columnMapping = ["商品名称"=>"commodity_name","订单类型"=>"order_type","承运商"=>"logistic_name"];
  118. $result = $this->service->formatFeature($features,"1&(2|3)",$columnMapping);
  119. $expected = "commodity_name like '%衣服%' and (order_type not like '%创建%' or logistic_name = '顺丰')";
  120. $this->assertEquals($expected,$result);
  121. }
  122. public function testMatchFeature(){
  123. $model1 = factory(Feature::class)->create($this->data[6]);
  124. $model2 = factory(Feature::class)->create($this->data[7]);
  125. $model3 = factory(Feature::class)->create($this->data[8]);
  126. $columnMapping = ["商品名称"=>"commodity","订单类型"=>"order","店铺类型"=>"shop"];
  127. $matchObject = ["commodity"=>"测试商品","order"=>"TEST","shop"=>"测试"];
  128. $value = $model1->id."&(".$model2->id."&".$model3->id.")";
  129. $result = $this->service->matchFeature($value,$columnMapping,$matchObject);
  130. $this->assertEquals(true,$result);
  131. $matchObject = ["commodity"=>"测试商品","order"=>"实际","shop"=>"测试"];
  132. $result = $this->service->matchFeature($value,$columnMapping,$matchObject);
  133. $this->assertEquals(false,$result);
  134. }
  135. public function testTruncate(){
  136. Feature::query()->truncate();
  137. $feature = Feature::query()->get();
  138. $this->assertCount(0,$feature);
  139. }
  140. }