OwnerPriceExpressServiceTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Tests\Services\OwnerPriceExpressService;
  3. use App\Logistic;
  4. use App\Owner;
  5. use App\OwnerPriceExpress;
  6. use App\OwnerPriceExpressProvince;
  7. use App\Province;
  8. use App\Services\OwnerPriceExpressService;
  9. use Illuminate\Support\Str;
  10. use Tests\TestCase;
  11. class OwnerPriceExpressServiceTest extends TestCase
  12. {
  13. /** @var OwnerPriceExpressService */
  14. public $service;
  15. public $data;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->service = app(OwnerPriceExpressService::class);
  20. $this->data["models"] = factory(OwnerPriceExpress::class,2)->create()->toArray();
  21. }
  22. /**
  23. * @group customer
  24. */
  25. public function testPaginate()
  26. {
  27. $models = $this->service->paginate();
  28. $this->assertGreaterThanOrEqual(2,count($models));
  29. $models = $this->service->paginate($this->data["models"][0]["id"]);
  30. $this->assertCount(1,$models);
  31. }
  32. /**
  33. * @group customer
  34. */
  35. public function testFind()
  36. {
  37. $model = $this->service->find($this->data["models"][0]["id"]);
  38. $this->assertNotNull($model);
  39. }
  40. /**
  41. * @group customer
  42. */
  43. public function testUpdateDetail()
  44. {
  45. $detail = factory(OwnerPriceExpressProvince::class)->create([
  46. "owner_price_express_id" => $this->data["models"][0]["id"], //快递价格ID
  47. ]);
  48. $result = $this->service->updateDetail(["id"=>$detail->id],["initial_weight_price"=>5]);
  49. $this->assertEquals(1,$result);
  50. OwnerPriceExpressProvince::destroy($detail->id);
  51. }
  52. /**
  53. * @group customer
  54. */
  55. public function testCreate()
  56. {
  57. $model = $this->service->create([
  58. "name" => md5(Str::random(8)),
  59. "initial_weight" => mt_rand(6,260) / 5,
  60. "additional_weight" => mt_rand(6,260) / 5,
  61. ]);
  62. $this->assertNotNull($model);
  63. $this->data["models"][] = $model->toArray();
  64. }
  65. /**
  66. * @group customer
  67. */
  68. public function testCreateDetail()
  69. {
  70. $model = $this->service->createDetail([
  71. "owner_price_express_id" => $this->data["models"][0]["id"],
  72. "province_id" => 1,
  73. "initial_weight_price" => mt_rand(6,260) / 5,
  74. "additional_weight_price"=>mt_rand(6,260) / 5,
  75. ]);
  76. $this->assertNotNull($model);
  77. $this->assertNotNull($model->id);
  78. OwnerPriceExpressProvince::destroy($model->id);
  79. }
  80. /**
  81. * @group customer
  82. */
  83. public function testIsExistDetail()
  84. {
  85. $province = Province::query()->first();
  86. $detail = factory(OwnerPriceExpressProvince::class)->create([
  87. "owner_price_express_id" => $this->data["models"][0]["id"],
  88. "province_id" => $province->id
  89. ]);
  90. $result = $this->service->isExistDetail([
  91. "owner_price_express_id"=>$this->data["models"][0]["id"],
  92. "province_id" => $province->id
  93. ]);
  94. $this->assertGreaterThan(0,$result);
  95. OwnerPriceExpressProvince::destroy($detail->id);
  96. }
  97. /**
  98. * @group customer
  99. */
  100. public function testDestroyDetail()
  101. {
  102. $detail = factory(OwnerPriceExpressProvince::class)->create([
  103. "owner_price_express_id" => $this->data["models"][0]["id"],
  104. ]);
  105. $result = $this->service->destroyDetail($detail->id);
  106. $this->assertEquals(1,$result);
  107. }
  108. /**
  109. * @group customer
  110. */
  111. public function testGetExistOwnerName()
  112. {
  113. $owner = factory(Owner::class)->create([
  114. "user_owner_group_id" => 1,
  115. ]);
  116. $logistic = factory(Logistic::class)->create();
  117. /** @var OwnerPriceExpress $model */
  118. $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
  119. $model->owners()->sync([$owner->id]);
  120. $model->logistics()->sync([$logistic->id]);
  121. $result = $this->service->getExistOwnerName([$owner->id],$logistic->id,$this->data["models"][0]["id"]);
  122. $this->assertCount(0,$result);
  123. $result = $this->service->getExistOwnerName([$owner->id],$logistic->id);
  124. $this->assertCount(1,$result);
  125. $this->assertStringContainsString($owner->name,$result[0]);
  126. Owner::destroy($owner->id);
  127. $model->owners()->sync([]);
  128. $model->logistics()->sync([]);
  129. }
  130. /**
  131. * @group customer
  132. */
  133. public function testGetExistLogisticName()
  134. {
  135. $logistic = factory(Logistic::class)->create();
  136. /** @var OwnerPriceExpress $model */
  137. $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
  138. $model->logistics()->sync([$logistic->id]);
  139. $result = $this->service->getExistLogisticName([$logistic->id],$this->data["models"][0]["id"]);
  140. $this->assertCount(0,$result);
  141. $result = $this->service->getExistLogisticName([$logistic->id]);
  142. $this->assertCount(1,$result);
  143. $this->assertEquals($logistic->name,$result[0]);
  144. Logistic::destroy($logistic->id);
  145. $model->logistics()->sync([]);
  146. }
  147. /**
  148. * @group customer
  149. */
  150. public function testDestroy()
  151. {
  152. $result = $this->service->destroy($this->data["models"][0]["id"]);
  153. $this->assertEquals(1,$result);
  154. unset($this->data["models"][0]);
  155. }
  156. /**
  157. * @group customer
  158. */
  159. public function testUpdate()
  160. {
  161. $result = $this->service->update(["id"=>$this->data["models"][0]["id"]],["initial_weight"=>3]);
  162. $this->assertEquals(1,$result);
  163. }
  164. /**
  165. * @group customer
  166. */
  167. public function testMatching()
  168. {
  169. /** @var OwnerPriceExpress $model */
  170. $model = factory(OwnerPriceExpress::class)->create([
  171. "initial_weight" => 2.4,
  172. "additional_weight" => 3.1,//16.2
  173. ]);
  174. $this->data["models"][] = $model->toArray();
  175. $detail = factory(OwnerPriceExpressProvince::class)->create([
  176. "owner_price_express_id" => $model->id,
  177. "province_id" => 1,
  178. "initial_weight_price" => 2, //初始单价 4.8
  179. "additional_weight_price"=>2.2, //续重单价
  180. ]);
  181. $owner = factory(Owner::class)->create([
  182. "user_owner_group_id" => 1
  183. ]);
  184. $logistic = factory(Logistic::class)->create();
  185. $model->owners()->sync([$owner->id]);
  186. $model->logistics()->sync([$logistic->id]);
  187. $result = $this->service->matching(18.6,$owner->id,$logistic->id,1);
  188. $this->assertEquals([0,null],$result);
  189. $model->owners()->sync([]);
  190. $model->logistics()->sync([]);
  191. OwnerPriceExpressProvince::destroy($detail->id);
  192. Owner::destroy($owner->id);
  193. Logistic::destroy($logistic->id);
  194. }
  195. public function tearDown(): void
  196. {
  197. OwnerPriceExpress::destroy(array_column($this->data["models"],"id"));
  198. parent::tearDown();
  199. }
  200. }