GetFromCacheTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 GetFromCacheTest 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. * 缓存没有任何数据
  48. * @test
  49. */
  50. public function cache_data_empty()
  51. {
  52. $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
  53. $this->assertEquals([
  54. 0 => collect(),
  55. ['unit' => '日',
  56. 'data' => [
  57. Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
  58. Carbon::now()->subDays(0)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],
  59. ]]], $result);
  60. }
  61. /**
  62. * 缓存命中全部数据
  63. * @test
  64. */
  65. public function cache_data_all()
  66. {
  67. foreach ($this->ownerIds as $owner_id) {
  68. for ( $i = 1; $i >= 0; $i--) {
  69. $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
  70. cache()->put($key, collect(['aaa']));
  71. }
  72. }
  73. $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
  74. $this->assertEquals([
  75. 0 => collect(['aaa','aaa','aaa','aaa']),
  76. ['unit' => '日']], $result);
  77. }
  78. /**
  79. * 缓存命中部分数据
  80. * @test
  81. */
  82. public function cache_data_some()
  83. {
  84. foreach ($this->ownerIds as $owner_id) {
  85. for ( $i = 0; $i >= 0; $i--) {
  86. $key = 'order_counting_records_' . Carbon::now()->subDays($i)->toDateString() . '_' . $owner_id . '_' . '日';
  87. cache()->put($key, collect(['aaa']));//缓存中放入当天的数据
  88. }
  89. }
  90. $result = $this->newOrderCountingRecordService->getFromCache($this->queryConditionDay);
  91. $this->assertEquals([
  92. 0 => collect(['aaa','aaa']),
  93. ['unit' => '日',
  94. 'data' => [
  95. Carbon::now()->subDays(1)->toDateString() => [0 => $this->ownerIds[0], 1 => $this->ownerIds[1]],//昨天的数据没放在缓存,应该在data中返回
  96. ]]], $result);
  97. }
  98. }