GetFromLowerUnitTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Tests\Services\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. //
  64. // $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
  65. // $this->assertEquals([1, 1, 1, 1],$result->pluck('amount')->toArray());
  66. // }
  67. /**
  68. * unit为年,中间表日为空,查询orders
  69. * @test
  70. */
  71. public function unit_year_from_order()
  72. {
  73. $orders = collect();
  74. foreach ($this->ownerIds as $ownerId) {
  75. for ($i = 1; $i >= 0; $i--) {
  76. $orders->push(factory(Order::class)->create([
  77. 'created_at' => Carbon::now()->subYears($i)->toDateString(),
  78. 'owner_id' => $ownerId,
  79. 'wms_status' => '订单完成',
  80. ]));
  81. }
  82. }
  83. $this->orderIds = array_column($orders->toArray(), 'id');
  84. $result = $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
  85. $this->assertEquals([2, 0, 2, 0],$result->pluck('amount')->toArray());
  86. }
  87. /**
  88. * 插入中间表测试 月
  89. * @test
  90. */
  91. public function unit_month_from_order_insert()
  92. {
  93. $orders = collect();
  94. foreach ($this->ownerIds as $ownerId) {
  95. for ($i = 1; $i >= 0; $i--) {
  96. $orders->push(factory(Order::class)->create([
  97. 'created_at' => Carbon::now()->subMonths($i)->toDateString(),
  98. 'owner_id' => $ownerId,
  99. 'wms_status' => '订单完成',
  100. ]));
  101. }
  102. }
  103. $this->orderIds = array_column($orders->toArray(), 'id');
  104. $this->assertDatabaseMissing('order_counting_records', [
  105. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  106. 'counting_unit' => '月',
  107. 'owner_id' => $this->ownerIds[0],
  108. ]);
  109. $this->assertDatabaseMissing('order_counting_records', [
  110. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  111. 'counting_unit' => '月',
  112. 'owner_id' => $this->ownerIds[1],
  113. ]);
  114. $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionMonth);
  115. $this->assertDatabaseHas('order_counting_records', [
  116. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  117. // 'counting_unit' => '月',
  118. 'owner_id' => $this->ownerIds[0],
  119. ]);
  120. $this->assertDatabaseHas('order_counting_records', [
  121. 'date_target' =>Carbon::now()->subMonths(1)->startOfMonth()->toDateString(),
  122. // 'counting_unit' => '月',
  123. 'owner_id' => $this->ownerIds[1],
  124. ]);
  125. }
  126. /**
  127. * 插入中间表测试 年
  128. * @test
  129. */
  130. public function unit_year_from_order_insert()
  131. {
  132. $orders = collect();
  133. foreach ($this->ownerIds as $ownerId) {
  134. for ($i = 1; $i >= 0; $i--) {
  135. $orders->push(factory(Order::class)->create([
  136. 'created_at' => Carbon::now()->subYears($i)->toDateString(),
  137. 'owner_id' => $ownerId,
  138. 'wms_status' => '订单完成',
  139. ]));
  140. }
  141. }
  142. $this->orderIds = array_column($orders->toArray(), 'id');
  143. $this->assertDatabaseMissing('order_counting_records', [
  144. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  145. 'counting_unit' => '年',
  146. 'owner_id' => $this->ownerIds[0],
  147. ]);
  148. $this->assertDatabaseMissing('order_counting_records', [
  149. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  150. 'counting_unit' => '年',
  151. 'owner_id' => $this->ownerIds[1],
  152. ]);
  153. $this->newOrderCountingRecordService->getFromLowerUnit($this->queryConditionYear);
  154. $this->assertDatabaseHas('order_counting_records', [
  155. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  156. 'counting_unit' => '年',
  157. 'owner_id' => $this->ownerIds[0],
  158. ]);
  159. $this->assertDatabaseHas('order_counting_records', [
  160. 'date_target' =>Carbon::now()->subYears(1)->startOfYear()->toDateString(),
  161. 'counting_unit' => '年',
  162. 'owner_id' => $this->ownerIds[1],
  163. ]);
  164. }
  165. /**
  166. * @test
  167. */
  168. public function currentDateTest()
  169. {
  170. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'日');
  171. $this->assertFalse($result);
  172. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'月');
  173. $this->assertFalse($result);
  174. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->toDateString(),'年');
  175. $this->assertFalse($result);
  176. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subDay()->toDateString(),'日');
  177. $this->assertTrue($result);
  178. // $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subMonth() ->toDateString(),'月');
  179. // $this->assertTrue($result);
  180. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYear() ->toDateString(),'年');
  181. $this->assertTrue($result);
  182. $result = $this->newOrderCountingRecordService->isNotCurrentDate(Carbon::now()->subYears()->toDateString(),'月');
  183. $this->assertTrue($result);
  184. }
  185. }