GetFromLowerUnitTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace NewOrderCountingRecordService;
  3. use App\Order;
  4. use App\OrderCountingRecord;
  5. use App\Owner;
  6. use App\Services\NewOrderCountingRecordService;
  7. use App\User;
  8. use Carbon\Carbon;
  9. use Tests\TestCase;
  10. class GetFromLowerUnitTest extends TestCase
  11. {
  12. protected $newOrderCountingRecordService;
  13. protected $queryConditionDay;
  14. protected $queryConditionWeek;
  15. protected $queryConditionMonth;
  16. protected $queryConditionYear;
  17. protected $ownerIds;
  18. protected $cache_key = 'order_counting_records_';
  19. protected $step_length = 1;
  20. protected $orderCountingRecordIds = [];
  21. protected $units = ['日', '月', '年'];
  22. protected $orderIds;
  23. protected function setUp(): void
  24. {
  25. parent::setUp(); // TODO: Change the autogenerated stub
  26. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  27. // $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  28. $user = new User([
  29. 'name'=>'yang'
  30. ]);
  31. $this->be($user);
  32. $owners = factory(Owner::class)->times(2)->create();
  33. $this->ownerIds = array_column($owners->toArray(), 'id');
  34. $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
  35. $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
  36. $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
  37. }
  38. protected function tearDown(): void
  39. {
  40. Owner::destroy($this->ownerIds);
  41. OrderCountingRecord::destroy($this->orderCountingRecordIds);
  42. Order::destroy($this->orderIds);
  43. OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
  44. parent::tearDown();
  45. }
  46. /**
  47. * unit为月,中间表日为空,查询orders
  48. * @test
  49. */
  50. public function unit_month_from_order()
  51. {
  52. $orders = collect();
  53. foreach ($this->ownerIds as $ownerId) {
  54. for ($i = 1; $i >= 0; $i--) {
  55. $orders->push(factory(Order::class)->create([
  56. 'created_at' => Carbon::now()->subMonths($i)->toDateString(),
  57. 'owner_id' => $ownerId,
  58. 'wms_status' => '订单完成',
  59. ]));
  60. }
  61. }
  62. $this->orderIds = array_column($orders->toArray(), 'id');
  63. $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
  64. $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
  65. }
  66. /**
  67. * unit为年,中间表日为空,查询orders
  68. * @test
  69. */
  70. public function unit_year_from_order()
  71. {
  72. $orders = collect();
  73. foreach ($this->ownerIds as $ownerId) {
  74. for ($i = 1; $i >= 0; $i--) {
  75. $orders->push(factory(Order::class)->create([
  76. 'created_at' => Carbon::now()->subYears($i)->toDateString(),
  77. 'owner_id' => $ownerId,
  78. 'wms_status' => '订单完成',
  79. ]));
  80. }
  81. }
  82. $this->orderIds = array_column($orders->toArray(), 'id');
  83. $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
  84. $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
  85. }
  86. /**
  87. * 插入中间表测试 月
  88. * @test
  89. */
  90. public function unit_month_from_order_insert()
  91. {
  92. $orders = collect();
  93. foreach ($this->ownerIds as $ownerId) {
  94. for ($i = 1; $i >= 0; $i--) {
  95. $orders->push(factory(Order::class)->create([
  96. 'created_at' => Carbon::now()->subMonths($i)->toDateString(),
  97. 'owner_id' => $ownerId,
  98. 'wms_status' => '订单完成',
  99. ]));
  100. }
  101. }
  102. $this->orderIds = array_column($orders->toArray(), 'id');
  103. $this->assertDatabaseMissing('order_counting_records', [
  104. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  105. 'counting_unit' => '月',
  106. 'owner_id' => $this->ownerIds[0],
  107. ]);
  108. $this->assertDatabaseMissing('order_counting_records', [
  109. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  110. 'counting_unit' => '月',
  111. 'owner_id' => $this->ownerIds[1],
  112. ]);
  113. $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
  114. $this->assertDatabaseHas('order_counting_records', [
  115. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  116. 'counting_unit' => '月',
  117. 'owner_id' => $this->ownerIds[0],
  118. ]);
  119. $this->assertDatabaseHas('order_counting_records', [
  120. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  121. 'counting_unit' => '月',
  122. 'owner_id' => $this->ownerIds[1],
  123. ]);
  124. }
  125. /**
  126. * 插入中间表测试 年
  127. * @test
  128. */
  129. public function unit_year_from_order_insert()
  130. {
  131. $orders = collect();
  132. foreach ($this->ownerIds as $ownerId) {
  133. for ($i = 1; $i >= 0; $i--) {
  134. $orders->push(factory(Order::class)->create([
  135. 'created_at' => Carbon::now()->subYears($i)->toDateString(),
  136. 'owner_id' => $ownerId,
  137. 'wms_status' => '订单完成',
  138. ]));
  139. }
  140. }
  141. $this->orderIds = array_column($orders->toArray(), 'id');
  142. $this->assertDatabaseMissing('order_counting_records', [
  143. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  144. 'counting_unit' => '年',
  145. 'owner_id' => $this->ownerIds[0],
  146. ]);
  147. $this->assertDatabaseMissing('order_counting_records', [
  148. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  149. 'counting_unit' => '年',
  150. 'owner_id' => $this->ownerIds[1],
  151. ]);
  152. $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
  153. $this->assertDatabaseHas('order_counting_records', [
  154. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  155. 'counting_unit' => '年',
  156. 'owner_id' => $this->ownerIds[0],
  157. ]);
  158. $this->assertDatabaseHas('order_counting_records', [
  159. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  160. 'counting_unit' => '年',
  161. 'owner_id' => $this->ownerIds[1],
  162. ]);
  163. }
  164. /**
  165. * @test
  166. */
  167. public function currentDateTest()
  168. {
  169. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'日');
  170. $this->assertFalse($result);
  171. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'月');
  172. $this->assertFalse($result);
  173. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'年');
  174. $this->assertFalse($result);
  175. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->toDateString(),'日');
  176. $this->assertTrue($result);
  177. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth() ->toDateString(),'月');
  178. $this->assertTrue($result);
  179. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYear() ->toDateString(),'年');
  180. $this->assertTrue($result);
  181. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYears()->toDateString(),'月');
  182. $this->assertTrue($result);
  183. }
  184. }