GetFromCacheTest.php 4.0 KB

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