GetFromMiddleTableTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Database\Eloquent\Collection;
  10. use Tests\TestCase;
  11. class GetFromMiddleTableTest extends TestCase
  12. {
  13. protected $newOrderCountingRecordService;
  14. protected $queryConditionDay;
  15. protected $queryConditionWeek;
  16. protected $queryConditionMonth;
  17. protected $queryConditionYear;
  18. protected $ownerIds;
  19. protected $cache_key = 'order_counting_records_';
  20. protected $step_length = 1;
  21. protected $orderCountingRecordIds = [];
  22. protected $units = ['日', '月', '年'];
  23. protected $orderIds;
  24. protected function setUp(): void
  25. {
  26. parent::setUp(); // TODO: Change the autogenerated stub
  27. cache()->flush();
  28. $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
  29. $this->actingAs(factory(User::class)->create(['name' => 'yang']));
  30. $owners = factory(Owner::class)->times(2)->create();
  31. $this->ownerIds = array_column($owners->toArray(), 'id');
  32. $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
  33. $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
  34. $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
  35. }
  36. protected function tearDown(): void
  37. {
  38. Owner::destroy($this->ownerIds);
  39. OrderCountingRecord::destroy($this->orderCountingRecordIds);
  40. Order::destroy($this->orderIds);
  41. OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
  42. parent::tearDown();
  43. }
  44. /**
  45. * 中间表没有任何数据
  46. * @test
  47. */
  48. public function data_empty()
  49. {
  50. $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
  51. $this->assertEquals([
  52. 0 => new Collection(),
  53. ['unit' => '日',
  54. 'data' => [
  55. Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
  56. Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
  57. ]]], $result);
  58. }
  59. /**
  60. * 中间表命中全部数据
  61. * @test
  62. */
  63. public function data_all()
  64. {
  65. $orderCountingRecords = collect();
  66. foreach ($this->ownerIds as $owner_id) {
  67. for ($i = 1; $i >= 0; $i--) {
  68. $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
  69. 'date_target' => Carbon::now()->subDays($i)->toDateString(),
  70. 'owner_id' => $owner_id,
  71. ]));
  72. }
  73. }
  74. $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
  75. $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
  76. $orderCountingRecords = OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->orderByDesc('owner_id')->get();
  77. $this->assertEquals([
  78. 0 => $orderCountingRecords,
  79. ['unit' => '日','data'=>[]]], $result);
  80. }
  81. /**
  82. * 中间表命中部分数据
  83. * @test
  84. */
  85. public function data_some()
  86. {
  87. $orderCountingRecords = collect();
  88. foreach ($this->ownerIds as $owner_id) {
  89. for ($i = 0; $i >= 0; $i--) {
  90. $orderCountingRecords->push(factory(OrderCountingRecord::class)->create([
  91. 'date_target' => Carbon::now()->subDays($i)->toDateString(),
  92. 'owner_id' => $owner_id,
  93. ]));
  94. }
  95. }
  96. $this->orderCountingRecordIds = array_column($orderCountingRecords->toArray(), 'id');
  97. $result = $this->newOrderCountingRecordService->getFromMiddleTable($this->queryConditionDay);
  98. $this->assertEquals(
  99. ['unit' => '日','data' => [
  100. Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
  101. ]], $result[1]);
  102. }
  103. }