OwnerPriceExpressServiceTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /** @var OwnerPriceExpress $model */
  117. $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
  118. $model->owners()->sync([$owner->id]);
  119. $result = $this->service->getExistOwnerName([$owner->id],$this->data["models"][0]["id"]);
  120. $this->assertCount(0,$result);
  121. $result = $this->service->getExistOwnerName([$owner->id]);
  122. $this->assertCount(1,$result);
  123. $this->assertEquals($owner->name,$result[0]);
  124. Owner::destroy($owner->id);
  125. $model->owners()->sync([]);
  126. }
  127. /**
  128. * @group customer
  129. */
  130. public function testGetExistLogisticName()
  131. {
  132. $logistic = factory(Logistic::class)->create();
  133. /** @var OwnerPriceExpress $model */
  134. $model = OwnerPriceExpress::query()->find($this->data["models"][0]["id"]);
  135. $model->logistics()->sync([$logistic->id]);
  136. $result = $this->service->getExistLogisticName([$logistic->id],$this->data["models"][0]["id"]);
  137. $this->assertCount(0,$result);
  138. $result = $this->service->getExistLogisticName([$logistic->id]);
  139. $this->assertCount(1,$result);
  140. $this->assertEquals($logistic->name,$result[0]);
  141. Logistic::destroy($logistic->id);
  142. $model->logistics()->sync([]);
  143. }
  144. /**
  145. * @group customer
  146. */
  147. public function testDestroy()
  148. {
  149. $result = $this->service->destroy($this->data["models"][0]["id"]);
  150. $this->assertEquals(1,$result);
  151. unset($this->data["models"][0]);
  152. }
  153. /**
  154. * @group customer
  155. */
  156. public function testUpdate()
  157. {
  158. $result = $this->service->update(["id"=>$this->data["models"][0]["id"]],["initial_weight"=>3]);
  159. $this->assertEquals(1,$result);
  160. }
  161. /**
  162. * @group customer
  163. */
  164. public function testMatching()
  165. {
  166. /** @var OwnerPriceExpress $model */
  167. $model = factory(OwnerPriceExpress::class)->create([
  168. "initial_weight" => 2.4,
  169. "additional_weight" => 3.1,//16.2
  170. ]);
  171. $this->data["models"][] = $model->toArray();
  172. $detail = factory(OwnerPriceExpressProvince::class)->create([
  173. "owner_price_express_id" => $model->id,
  174. "province_id" => 1,
  175. "initial_weight_price" => 2, //初始单价 4.8
  176. "additional_weight_price"=>2.2, //续重单价
  177. ]);
  178. $model->owners()->sync([1]);
  179. $model->logistics()->sync([1]);
  180. $result = $this->service->matching(18.6,1,1,1);
  181. $this->assertEquals(18,$result);
  182. $model->owners()->sync([]);
  183. $model->logistics()->sync([]);
  184. OwnerPriceExpressProvince::destroy($detail->id);
  185. }
  186. public function tearDown(): void
  187. {
  188. OwnerPriceExpress::destroy(array_column($this->data["models"],"id"));
  189. parent::tearDown();
  190. }
  191. }