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