NewOrderCountingRecordServiceTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 Illuminate\Foundation\Testing\RefreshDatabase;
  10. use Illuminate\Support\Arr;
  11. use Tests\TestCase;
  12. class NewOrderCountingRecordServiceTest extends TestCase
  13. {
  14. protected $newOrderCountingRecordService;
  15. protected $orders;
  16. protected $orderCountingRecords;
  17. protected $owners;
  18. protected function setUp(): void
  19. {
  20. parent::setUp(); // TODO: Change the autogenerated stub
  21. cache()->flush();
  22. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  23. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  24. }
  25. protected function tearDown(): void
  26. {
  27. cache()->flush();
  28. foreach ($this->orders as $order) {
  29. $order->delete();
  30. }
  31. foreach ($this->orderCountingRecords as $orderCountingRecord) {
  32. $orderCountingRecord->delete();
  33. }
  34. foreach ($this->owners as $owner) {
  35. $owner->delete();
  36. }
  37. parent::tearDown(); // TODO: Change the autogenerated stub
  38. }
  39. /**
  40. * @test
  41. */
  42. public function transfersToConditions()
  43. {
  44. $this->owners = factory(Owner::class)->times(2)->create();
  45. $start = Carbon::now()->subDays(2)->toDateString();
  46. $end = Carbon::now()->toDateString();
  47. $unit = '日';
  48. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  49. $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  50. $this->assertEquals([
  51. Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
  52. Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
  53. Carbon::now()->toDateString() => [0 => 1, 1 => 2],
  54. ], $dateArray);
  55. }
  56. /**
  57. * @test
  58. */
  59. public function dateUtils_week()
  60. {
  61. $this->owners = factory(Owner::class)->times(2)->create();
  62. $start = Carbon::now()->subWeeks(2)->toDateString();
  63. $end = Carbon::now()->toDateString();
  64. $unit = '周';
  65. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  66. $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  67. dd($dateArray);
  68. // $this->assertEquals([
  69. // Carbon::now()->subWeeks(2)->toDateString() => [0 => 1, 1 => 2],
  70. // Carbon::now()->subWeeks(1)->toDateString() => [0 => 1, 1 => 2],
  71. // Carbon::now()->toDateString() => [0 => 1, 1 => 2],
  72. // ], $dateArray);
  73. }
  74. /**
  75. * @test
  76. */
  77. public function orderCountingRecords_remember_day()
  78. {
  79. $start = Carbon::now()->subDays(2)->toDateString();
  80. $end = Carbon::now()->toDateString();
  81. $unit = '日';
  82. $key = 'orderCountingRecords_' . $start . '_' . $end . '_' . $unit . '_' . auth()->id();
  83. cache()->put($key, 'This is a test value');
  84. $result = $this->newOrderCountingRecordService->get($start, $end, $unit);
  85. $this->assertEquals('This is a test value', $result);
  86. }
  87. /**
  88. * @test
  89. */
  90. public function dataFromCache_all_day()
  91. {
  92. $start = Carbon::now()->subDays(2)->toDateString();
  93. $end = Carbon::now()->toDateString();
  94. $unit = '日';
  95. factory(Owner::class)->times(2)->create();
  96. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  97. $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  98. foreach ($queryCondition as $dateStr => $ownerIds) {
  99. foreach ($ownerIds as $ownerId) {
  100. $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit . '_' . auth()->id();
  101. cache()->put($key, collect()->push(factory(Owner::class)->create()));
  102. }
  103. }
  104. $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit);
  105. $this->assertCount(6, $result['dataFromCache']);
  106. }
  107. /**
  108. * @test
  109. */
  110. public function dataFromCache_empty_day()
  111. {
  112. $start = Carbon::now()->subDays(2)->toDateString();
  113. $end = Carbon::now()->toDateString();
  114. $unit = '日';
  115. factory(Owner::class)->times(2)->create();
  116. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  117. $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  118. $result = $this->newOrderCountingRecordService->getFromCache($queryCondition, $unit);
  119. $this->assertEquals([
  120. Carbon::now()->subDays(2)->toDateString() => [0 => 1, 1 => 2],
  121. Carbon::now()->subDays(1)->toDateString() => [0 => 1, 1 => 2],
  122. Carbon::now()->toDateString() => [0 => 1, 1 => 2],
  123. ], $result['unQueryCondition']);
  124. }
  125. /**
  126. * @test
  127. */
  128. public function dataFromMiddleTable_all_day()
  129. {
  130. $start = Carbon::now()->subDays(1)->toDateString();
  131. $end = Carbon::now()->toDateString();
  132. $unit = '日';
  133. $owner1 = factory(Owner::class)->create();
  134. $owner2 = factory(Owner::class)->create();
  135. factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
  136. factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner2->id]);
  137. factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner1->id]);
  138. factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->subDays(1)->toDateString(), 'owner_id' => $owner2->id]);
  139. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  140. $queryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  141. $result = $this->newOrderCountingRecordService->dataFromMiddleTable($queryCondition, $unit);
  142. $this->assertCount(8, $result['dataFromMiddleTable']);
  143. $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->toDateString()]);
  144. $this->assertCount(0, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
  145. }
  146. /**
  147. * @test
  148. */
  149. public function dataFromMiddleTable_empty_day()
  150. {
  151. $start = Carbon::now()->subDays(1)->toDateString();
  152. $end = Carbon::now()->toDateString();
  153. $unit = '日';
  154. factory(Owner::class)->create();
  155. factory(Owner::class)->create();
  156. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  157. $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  158. $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
  159. $this->assertCount(0, $result['dataFromMiddleTable']);
  160. $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->toDateString()]);
  161. $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
  162. }
  163. /**
  164. * @test
  165. */
  166. public function dataFromMiddleTable_common_day()
  167. {
  168. $start = Carbon::now()->subDays(1)->toDateString();
  169. $end = Carbon::now()->toDateString();
  170. $unit = '日';
  171. $owner1 = factory(Owner::class)->create();
  172. $owner2 = factory(Owner::class)->create();
  173. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  174. $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  175. factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
  176. $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
  177. $this->assertCount(2, $result['dataFromMiddleTable']);
  178. $this->assertCount(1, $result['unQueryCondition'][Carbon::now()->toDateString()]);
  179. $this->assertCount(2, $result['unQueryCondition'][Carbon::now()->subDays(1)->toDateString()]);
  180. }
  181. /**
  182. * @test
  183. */
  184. public function insertIntoCache_day()
  185. {
  186. $start = Carbon::now()->subDays(1)->toDateString();
  187. $end = Carbon::now()->toDateString();
  188. $unit = '日';
  189. $owner1 = factory(Owner::class)->create();
  190. $owner2 = factory(Owner::class)->create();
  191. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  192. $dateArray = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  193. $orderCountingRecord = factory(OrderCountingRecord::class)->times(2)->create(['date_target' => Carbon::now()->toDateString(), 'owner_id' => $owner1->id]);
  194. $result = $this->newOrderCountingRecordService->dataFromMiddleTable($dateArray, $unit);
  195. $this->newOrderCountingRecordService->insertIntoCache($result['dataFromMiddleTable'], $unit);
  196. $key = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit;
  197. $this->assertNotNull(cache()->get($key));
  198. $this->assertNotNull($orderCountingRecord->toArray());
  199. $this->assertEquals($orderCountingRecord->toArray(), cache()->get($key)->toArray());
  200. }
  201. /**
  202. * @test
  203. */
  204. public function dataFromOrder_day()
  205. {
  206. $start = Carbon::now()->subDays(1)->toDateString();
  207. $end = Carbon::now()->toDateString();
  208. $unit = '日';
  209. $owner1 = factory(Owner::class)->create();
  210. $owner2 = factory(Owner::class)->create();
  211. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  212. $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  213. factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
  214. factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
  215. factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
  216. factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
  217. $result = $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit);
  218. $this->assertEquals(1, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner1->id)->first()->amount);
  219. $this->assertEquals(2, $result->where('date_target', Carbon::now()->toDateString())->where('owner_id', $owner2->id)->first()->amount);
  220. $this->assertEquals(3, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner1->id)->first()->amount);
  221. $this->assertEquals(4, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->where('owner_id', $owner2->id)->first()->amount);
  222. }
  223. /**
  224. * @test
  225. */
  226. public function dataFromOrder_insertMiddleTable_day()
  227. {
  228. $start = Carbon::now()->subDays(1)->toDateString();
  229. $end = Carbon::now()->toDateString();
  230. $unit = '日';
  231. $owner1 = factory(Owner::class)->create();
  232. $owner2 = factory(Owner::class)->create();
  233. $ownerIds = $this->newOrderCountingRecordService->getCountingOwnerIds();
  234. $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $ownerIds);
  235. factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
  236. factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
  237. factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
  238. factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
  239. $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit);
  240. //当天的数据不入中间表
  241. $this->assertCount(0, OrderCountingRecord::query()->where('date_target', Carbon::now()->toDateString())->get());
  242. $this->assertCount(2, OrderCountingRecord::query()->where('date_target', Carbon::now()->subDays(1)->toDateString())->get());
  243. }
  244. /**
  245. * @test
  246. */
  247. public function dataFromOrder_insertCache_day()
  248. {
  249. $start = Carbon::now()->subDays(1)->toDateString();
  250. $end = Carbon::now()->toDateString();
  251. $unit = '日';
  252. $owner1 = factory(Owner::class)->create();
  253. $owner2 = factory(Owner::class)->create();
  254. $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $this->newOrderCountingRecordService->getCountingOwnerIds());
  255. factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
  256. factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
  257. factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
  258. factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
  259. $this->newOrderCountingRecordService->dataFromOrder($unQueryCondition, $unit);
  260. $key1 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner1->id . '_' . $unit;
  261. $key2 = 'order_counting_records_' . Carbon::now()->toDateString() . '_' . $owner2->id . '_' . $unit;
  262. $key3 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner1->id . '_' . $unit;
  263. $key4 = 'order_counting_records_' . Carbon::now()->subDays(1)->toDateString() . '_' . $owner2->id . '_' . $unit;
  264. $this->assertCount(1, cache()->get($key1));
  265. $this->assertCount(1, cache()->get($key2));
  266. $this->assertCount(1, cache()->get($key3));
  267. $this->assertCount(1, cache()->get($key4));
  268. }
  269. /**
  270. * @test
  271. */
  272. public function orderCountingRecordsDay_day()
  273. {
  274. $start = Carbon::now()->subDays(1)->toDateString();
  275. $end = Carbon::now()->toDateString();
  276. $unit = '日';
  277. $owner1 = factory(Owner::class)->create();
  278. $owner2 = factory(Owner::class)->create();
  279. factory(Order::class)->times(1)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner1->id]);
  280. factory(Order::class)->times(2)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now(), 'owner_id' => $owner2->id]);
  281. factory(Order::class)->times(3)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner1->id]);
  282. factory(Order::class)->times(4)->create(['wms_status' => '订单完成', 'created_at' => Carbon::now()->subDays(1), 'owner_id' => $owner2->id]);
  283. $unQueryCondition = $this->newOrderCountingRecordService->transfersToCondition($start, $end, $unit, $this->newOrderCountingRecordService->getCountingOwnerIds());
  284. $result = $this->newOrderCountingRecordService->getOrderCountingRecords($unQueryCondition, $unit);
  285. $this->assertEquals(3, $result->where('date_target', Carbon::now()->toDateString())->reduce(function ($carry, $item) {
  286. return $carry + $item->amount;
  287. }));
  288. $this->assertEquals(7, $result->where('date_target', Carbon::now()->subDays(1)->toDateString())->reduce(function ($carry, $item) {
  289. return $carry + $item->amount;
  290. }));
  291. }
  292. }