FeatureServiceTest.php 5.4 KB

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