OwnerPriceLogisticServiceTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace Tests\Services\OwnerPriceLogisticService;
  3. use App\Logistic;
  4. use App\Owner;
  5. use App\OwnerPriceLogistic;
  6. use App\OwnerPriceLogisticDetail;
  7. use App\Services\OwnerPriceLogisticService;
  8. use App\Unit;
  9. use Illuminate\Support\Str;
  10. use Tests\TestCase;
  11. class OwnerPriceLogisticServiceTest extends TestCase
  12. {
  13. /** @var OwnerPriceLogisticService */
  14. public $service;
  15. public $data;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->service = app(OwnerPriceLogisticService::class);
  20. $units = factory(Unit::class,2)->create();
  21. $this->data["units"] = $units->toArray();
  22. $this->data["details"] = [];
  23. $this->data["owners"] = [];
  24. $this->data["logistics"] = [];
  25. $this->data["models"] = factory(OwnerPriceLogistic::class,3)->create([
  26. "unit_id" => $units[0]->id,
  27. "other_unit_id" => $units[1]->id,
  28. ])->toArray();
  29. }
  30. /**
  31. * @group customer
  32. */
  33. public function testPaginate()
  34. {
  35. $models = $this->service->paginate();
  36. $this->assertGreaterThanOrEqual(3,count($models));
  37. $this->assertLessThanOrEqual(50,count($models));
  38. }
  39. /**
  40. * @group customer
  41. */
  42. public function create()
  43. {
  44. $model = $this->service->create([
  45. "name" => md5(Str::random(7)),
  46. "unit_range" => "0-5,5-10,10-15,15-20",
  47. "unit_id" => $this->data["units"][0]["id"],
  48. "other_unit_range" => "0-5,5-10,10-15,15-20,30-",
  49. "other_unit_id" => $this->data["units"][1]["id"],
  50. "pick_up_price" => mt_rand(2,230) / 3,
  51. "fuel_price" => mt_rand(2,230) / 3,
  52. "service_price" => mt_rand(2,230) / 3,
  53. ]);
  54. $this->assertNotNull($model);
  55. $this->data["models"][] = $model->toArray();
  56. }
  57. /**
  58. * @group customer
  59. */
  60. public function testFind()
  61. {
  62. $model = $this->service->find($this->data["models"][0]["id"]);
  63. $this->assertNotNull($model);
  64. }
  65. /**
  66. * @group customer
  67. */
  68. public function testUpdate()
  69. {
  70. $result = $this->service->update(["id"=>$this->data["models"][0]["id"]],[
  71. "fuel_price" => 2.222,
  72. ]);
  73. $this->assertEquals(1,$result);
  74. }
  75. /**
  76. * @group customer
  77. */
  78. public function testUpdateDetail()
  79. {
  80. $province = \App\Province::query()->first();
  81. $city = \App\City::query()->where("province_id",$province->id)->first();
  82. $detail = factory(OwnerPriceLogisticDetail::class)->create([
  83. "owner_price_logistic_id" => $this->data["models"][0]["id"], //物流计费
  84. "unit_id" => 1, //单位ID
  85. "range" => "0-5", //区间
  86. "province_id" => $province->id, //省份ID
  87. "city_id" => $city->id,
  88. ]);
  89. $this->assertNotNull($detail);
  90. $this->data["details"][] = $detail->toArray();
  91. $result = $this->service->updateDetail(["id"=>$detail->id],["range"=>"30-"]);
  92. $this->assertEquals(1,$result);
  93. }
  94. /**
  95. * @group customer
  96. */
  97. public function testIsExistDetail()
  98. {
  99. $detail = factory(OwnerPriceLogisticDetail::class)->create([
  100. "owner_price_logistic_id" => $this->data["models"][0]["id"], //物流计费
  101. "unit_id" => 1, //单位ID
  102. ]);
  103. $this->data["details"][] = $detail->toArray();
  104. $result = $this->service->isExistDetail(["unit_id"=>1,"owner_price_logistic_id"=> $this->data["models"][0]["id"]]);
  105. $this->assertGreaterThan(0,$result);
  106. }
  107. /**
  108. * @group customer
  109. */
  110. public function testCreateDetail()
  111. {
  112. $detail = $this->service->createDetail([
  113. "owner_price_logistic_id" => 1,
  114. "unit_id" => 1,
  115. "range" => "0-5",
  116. "province_id" => 1,
  117. "city_id" => 1,
  118. "unit_price" => mt_rand(1,36) / 3,
  119. "delivery_fee" => mt_rand(1,136) / 3,
  120. "initial_fee" => mt_rand(1,136) / 3,
  121. "initial_amount" => mt_rand(1,136) / 3,
  122. "rate" => mt_rand(1,136) / 3,
  123. ]);
  124. $this->assertNotNull($detail);
  125. $this->data["details"][] = $detail->toArray();
  126. }
  127. /**
  128. * @group customer
  129. */
  130. public function testDestroyDetail()
  131. {
  132. $detail = factory(OwnerPriceLogisticDetail::class)->create([
  133. "owner_price_logistic_id" => $this->data["models"][0]["id"], //物流计费
  134. "unit_id" => 1, //单位ID
  135. ]);
  136. $this->data["details"][] = $detail->toArray();
  137. $result = $this->service->destroyDetail($detail->id);
  138. $this->assertEquals(1,$result);
  139. }
  140. /**
  141. * @group customer
  142. */
  143. public function testCheckRange()
  144. {
  145. $result = $this->service->checkRange("0-5,5-10,10-15,15-30,30-");
  146. $this->assertEquals(true,$result);
  147. $result = $this->service->checkRange("0-5,5-10,10-15,15-30");
  148. $this->assertEquals(false,$result);
  149. $result = $this->service->checkRange("0-5,5-10,10-15,16-30,30-");
  150. $this->assertEquals(false,$result);
  151. $result = $this->service->checkRange("0-55-10,10-15,16-30,30-");
  152. $this->assertEquals(false,$result);
  153. }
  154. /**
  155. * @group customer
  156. */
  157. public function testMatching()
  158. {
  159. /** @var OwnerPriceLogistic $model */
  160. $model = factory(OwnerPriceLogistic::class)->create([
  161. "name" => md5(date('Ymd')).Str::random(4), //名称
  162. "unit_range" => "0-5,5-10,10-15,15-20", //单价一区间
  163. "unit_id" => 1, //单位一ID
  164. "other_unit_range" => "0-5,5-10,10-15,15-20,30-", //单位二区间
  165. "other_unit_id" => 2, //单位二ID
  166. "pick_up_price" => 1.5, //提货费
  167. "fuel_price" => 2.5, //燃油附加费
  168. "service_price" => 3.5, //信息服务费
  169. ]);
  170. $this->data["models"][] = $model->toArray();
  171. $owner = factory(Owner::class)->create([
  172. "user_owner_group_id" => 1
  173. ]);
  174. $this->data["owners"][] = $owner->toArray();
  175. $logistic = factory(Logistic::class)->create();
  176. $this->data["logistics"][] = $owner->toArray();
  177. $model->owners()->sync([$owner->id]);
  178. $model->logistics()->sync([$logistic->id]);
  179. $detail = factory(OwnerPriceLogisticDetail::class)->create([
  180. "owner_price_logistic_id" => $model->id, //物流计费
  181. "unit_id" => 1, //单位ID
  182. "range" => "10-30", //区间
  183. "province_id" => 1, //省份ID
  184. "city_id" => 1, //城市ID
  185. "unit_price" => 32.6, //单价
  186. "delivery_fee" => 123, //送货费
  187. "initial_fee" => 500, //起始计费
  188. "initial_amount" => 6, //起始计数
  189. "rate" => 0, //费率
  190. ]);//7.5
  191. $this->data["details"][] = $detail->toArray();
  192. $result = $this->service->matching(20,$owner->id,$logistic->id,1,1,1);
  193. $this->assertEquals([782.5,null],$result);
  194. $result = $this->service->matching(31,$owner->id,$logistic->id,1,1,1);
  195. $this->assertEquals([null,null],$result);
  196. }
  197. public function tearDown(): void
  198. {
  199. Unit::destroy(array_column($this->data["units"],"id"));
  200. foreach ($this->data["models"] as $model){
  201. /** @var OwnerPriceLogistic $item */
  202. $item = OwnerPriceLogistic::query()->find($model["id"]);
  203. $item->owners()->sync([]);
  204. $item->logistics()->sync([]);
  205. }
  206. OwnerPriceLogistic::destroy(array_column($this->data["models"],"id"));
  207. OwnerPriceLogisticDetail::destroy(array_column($this->data["details"],"id"));
  208. Owner::destroy(array_column($this->data["owners"],"id"));
  209. Logistic::destroy(array_column($this->data["logistics"],"id"));
  210. parent::tearDown();
  211. }
  212. }