FeatureServiceTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Tests\Unit\Customer\FeatureService;
  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. /**
  22. * @group customer
  23. */
  24. public function testGetMapArray(){
  25. $models = $this->service->getMapArray();
  26. $this->assertGreaterThanOrEqual(3,count($models));
  27. }
  28. /**
  29. * @group customer
  30. */
  31. public function testTranslationFeature(){
  32. $str = $this->data["models"][0]["id"]."&(".$this->data["models"][1]["id"]."|".$this->data["models"][2]["id"].")";
  33. $result = $this->service->translationFeature($str);
  34. $this->assertEquals(3,count($result));
  35. // $this->assertEquals($this->data["models"][1]["type"],$result[1]["type"]);
  36. $this->assertEquals(false,$result[0]["strategyGroupStartSign"]);
  37. $this->assertEquals(true,$result[1]["strategyGroupStartSign"]);
  38. $this->assertEquals("并且",$result[1]["calculation"]);
  39. $this->assertEquals(true,$result[2]["strategyGroupEndSign"]);
  40. $this->assertEquals("或",$result[2]["calculation"]);
  41. }
  42. /**
  43. * @group customer
  44. */
  45. public function testAnalysisFeature(){
  46. $models = $this->data["models"];
  47. $params = [
  48. [
  49. "strategyGroupStartSign" => true,
  50. "calculation" => "",
  51. "type"=>$models[0]["type"],
  52. "logic"=>$models[0]["logic"],
  53. "describe"=>$models[0]["describe"],
  54. "strategyGroupEndSign" => false,
  55. ],[
  56. "strategyGroupStartSign" => false,
  57. "calculation" => "并且",
  58. "type"=>$models[1]["type"],
  59. "logic"=>$models[1]["logic"],
  60. "describe"=>$models[1]["describe"],
  61. "strategyGroupEndSign" => true,
  62. ],[
  63. "strategyGroupStartSign" => false,
  64. "calculation" => "或",
  65. "type"=>$models[2]["type"],
  66. "logic"=>$models[2]["logic"],
  67. "describe"=>$models[2]["describe"],
  68. "strategyGroupEndSign" => false,
  69. ]
  70. ];
  71. $expected = "(".$models[0]["id"]."&".$models[1]["id"].")|".$models[2]["id"];
  72. $result = $this->service->analysisFeature($params);
  73. $this->assertIsArray($result);
  74. $this->assertCount(2,$result);
  75. $this->assertArrayHasKey("feature",$result);
  76. $this->assertEquals($expected,$result["feature"]);
  77. }
  78. /**
  79. * @group customer
  80. */
  81. public function testFormatFeature(){
  82. $model1 = factory(Feature::class)->make([
  83. "id" => 1,
  84. "type"=>"商品名称",
  85. "logic"=>"包含",
  86. "describe"=>"衣服"
  87. ]);
  88. $model2 = factory(Feature::class)->make([
  89. "id" => 2,
  90. "type"=>"订单类型",
  91. "logic"=>"不包含",
  92. "describe"=>"创建"
  93. ]);
  94. $model3 = factory(Feature::class)->make([
  95. "id"=>3,
  96. "type"=>"承运商",
  97. "logic"=>"等于",
  98. "describe"=>"顺丰"
  99. ]);
  100. $features = [1=>$model1,2=>$model2,3=>$model3];
  101. $result = $this->service->formatFeature($features,"1&(2|3)");
  102. $expected = "商品名称 包含 衣服 并且 (订单类型 不包含 创建 或 承运商 等于 顺丰)";
  103. $this->assertEquals($expected,$result);
  104. $columnMapping = ["商品名称"=>"commodity_name","订单类型"=>"order_type","承运商"=>"logistic_name"];
  105. $result = $this->service->formatFeature($features,"1&(2|3)",$columnMapping);
  106. $expected = "commodity_name like '%衣服%' and (order_type not like '%创建%' or logistic_name = '顺丰')";
  107. $this->assertEquals($expected,$result);
  108. }
  109. /**
  110. * @group customer
  111. */
  112. public function testMatchFeature(){
  113. $model1 = factory(Feature::class)->create([
  114. "type" => "商品名称",
  115. "logic"=>"包含",
  116. "describe"=>Uuid::uuid1(),
  117. ]);
  118. $model2 = factory(Feature::class)->create([
  119. "type" => "订单类型",
  120. "logic"=>"不包含",
  121. "describe"=>Uuid::uuid1(),
  122. ]);
  123. $model3 = factory(Feature::class)->create([
  124. "type" => "店铺类型",
  125. "logic"=>"等于",
  126. "describe"=>Uuid::uuid1(),
  127. ]);
  128. $this->data["models"][] = $model1->toArray();
  129. $this->data["models"][] = $model2->toArray();
  130. $this->data["models"][] = $model3->toArray();
  131. $columnMapping = ["商品名称"=>"commodity","订单类型"=>"order","店铺类型"=>"shop"];
  132. $matchObject = ["commodity"=>Str::random(2).$model1->describe,"order"=>$model1->describe,"shop"=>$model3->describe];
  133. $value = $model1->id."&(".$model2->id."&".$model3->id.")";
  134. $result = $this->service->matchFeature($value,$columnMapping,$matchObject);
  135. $this->assertEquals(true,$result);
  136. $columnMapping = ["商品名称"=>"commodity","订单类型"=>"order","店铺类型"=>"shop","packages"=>"ps"];
  137. $matchObject = ["ps"=>[["a"=>1,"commodity"=>Str::random(2).$model1->describe]],"order"=>$model1->describe,"shop"=>$model3->describe];
  138. $value = $model1->id."&(".$model2->id."&".$model3->id.")";
  139. $result = $this->service->matchFeature($value,$columnMapping,$matchObject,true);
  140. $this->assertEquals(true,$result);
  141. $matchObject = ["commodity"=>$model1->describe.Str::random(2),"order"=>$model2->describe.Str::random(2),"shop"=>"1"];
  142. $result = $this->service->matchFeature($value,$columnMapping,$matchObject);
  143. $this->assertEquals(false,$result);
  144. }
  145. public function tearDown(): void
  146. {
  147. Feature::destroy(array_column($this->data["models"],"id"));
  148. parent::tearDown();
  149. }
  150. }