OwnerOutStorageRuleServiceTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Tests\Services;
  3. use App\OwnerOutStorageRule;
  4. use App\OwnerPriceOperation;
  5. use App\Services\OwnerOutStorageRuleService;
  6. use App\Unit;
  7. use Tests\TestCase;
  8. class OwnerOutStorageRuleServiceTest extends TestCase
  9. {
  10. /** @var OwnerOutStorageRuleService */
  11. public $service;
  12. public $data;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->service = app(OwnerOutStorageRuleService::class);
  17. $this->data["units"] = [];
  18. $unit1 = Unit::query()->where("name","件")->first();
  19. if (!$unit1){
  20. $unit1 = factory(Unit::class)->create([
  21. "name" => "件"
  22. ]);
  23. $this->data["units"][] = $unit1->toArray();
  24. }
  25. $unit2 = Unit::query()->where("name","箱")->first();
  26. if (!$unit2){
  27. $unit2 = factory(Unit::class)->create([
  28. "name" => "箱"
  29. ]);
  30. $this->data["units"][] = $unit2->toArray();
  31. }
  32. $ownerPriceOperation = factory(OwnerPriceOperation::class)->create();
  33. $this->data["ownerPriceOperations"] = [$ownerPriceOperation->toArray()];
  34. $this->data["models"] = [];
  35. $this->data["models"][] = factory(OwnerOutStorageRule::class)->create([
  36. "owner_price_operation_id" => $ownerPriceOperation->id,
  37. "unit_id" => $unit1->id,
  38. ])->toArray();
  39. $this->data["models"][] = factory(OwnerOutStorageRule::class)->create([
  40. "owner_price_operation_id" => $ownerPriceOperation->id,
  41. "unit_id" => $unit2->id,
  42. ])->toArray();
  43. }
  44. public function testGet()
  45. {
  46. $models = $this->service->get(["owner_price_operation_id"=>$this->data["ownerPriceOperations"][0]["id"]]);
  47. $this->assertCount(2,$models);
  48. }
  49. public function testUpdate()
  50. {
  51. $model = $this->data["models"][0];
  52. $result = $this->service->update(["id"=>$model["id"]],[
  53. "unit_price" => 1.33,
  54. "priority" => 99,
  55. "amount" => 50
  56. ]);
  57. $this->assertEquals(1,$result);
  58. $expected = OwnerOutStorageRule::query()->find($model["id"]);
  59. $this->assertEquals(99,$expected->priority);
  60. $this->assertEquals(1.33,$expected->unit_price);
  61. $this->assertEquals(50,$expected->amount);
  62. }
  63. public function testCreate()
  64. {
  65. $model = $this->service->create([
  66. "owner_price_operation_id" => 1, //作业计费ID
  67. "strategy" => "特征", //出库策略
  68. "amount" => 20, //起步数
  69. "unit_id" => 2, //单位ID
  70. "unit_price" => 2, //单价
  71. "feature" => "1&3", //特征
  72. "priority" => 6, //优先级 值越大越高
  73. ]);
  74. $this->assertNotNull($model);
  75. $this->data["models"][] = $model->toArray();
  76. }
  77. public function testFindUpdate()
  78. {
  79. $model = OwnerOutStorageRule::query()->find($this->data["models"][0]["id"]);
  80. $result = $this->service->findUpdate($model,[
  81. "unit_price" => 0.566,
  82. ]);
  83. $this->assertEquals(1,$result);
  84. }
  85. public function testFind()
  86. {
  87. $models = $this->service->find(array_column($this->data["models"],"id"));
  88. $this->assertCount(2,$models);
  89. }
  90. public function testIsExist()
  91. {
  92. $result = $this->service->isExist([
  93. "id" => $this->data["models"][0]["id"],
  94. "owner_price_operation_id" => $this->data["models"][0]["owner_price_operation_id"],
  95. "strategy" => $this->data["models"][0]["strategy"],
  96. ]);
  97. $this->assertGreaterThan(0,$result);
  98. }
  99. public function tearDown(): void
  100. {
  101. Unit::destroy(array_column($this->data["units"],"id"));
  102. OwnerPriceOperation::destroy(array_column($this->data["ownerPriceOperations"],"id"));
  103. OwnerOutStorageRule::destroy(array_column($this->data["models"],"id"));
  104. parent::tearDown();
  105. }
  106. }