GetFromLowerUnitTest.php 8.0 KB

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