| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- <?php
- namespace NewOrderCountingRecordService;
- use App\Order;
- use App\OrderCountingRecord;
- use App\Owner;
- use App\Services\NewOrderCountingRecordService;
- use App\User;
- use Carbon\Carbon;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Collection;
- use Tests\TestCase;
- use Tightenco\Collect\Support\Arr;
- class GetOrderCountingRecordsTest extends TestCase
- {
- use RefreshDatabase;
- protected $newOrderCountingRecordService;
- protected $queryConditionDay;
- protected $queryConditionWeek;
- protected $queryConditionMonth;
- protected $queryConditionYear;
- protected $ownerIds;
- protected $cache_key = 'order_counting_records_';
- protected $step_length = 1;
- protected $orderCountingRecordIds = [];
- protected $units = ['日', '周', '月', '年'];
- protected $orderIds;
- protected function setUp(): void
- {
- parent::setUp(); // TODO: Change the autogenerated stub
- cache()->flush();
- $this->newOrderCountingRecordService = new NewOrderCountingRecordService();
- $this->actingAs(factory(User::class)->create(['name' => 'yang']));
- $owners = factory(Owner::class)->times(2)->create();
- $this->ownerIds = array_column($owners->toArray(), 'id');
- $this->queryConditionDay = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subDays($this->step_length)->toDateString(), Carbon::now()->toDateString(), '日', $this->ownerIds);
- $this->queryConditionWeek = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subWeeks($this->step_length)->toDateString(), Carbon::now()->toDateString(), '周', $this->ownerIds);
- $this->queryConditionMonth = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subMonths($this->step_length)->toDateString(), Carbon::now()->toDateString(), '月', $this->ownerIds);
- $this->queryConditionYear = $this->newOrderCountingRecordService->transfersToCondition(Carbon::now()->subYears($this->step_length)->toDateString(), Carbon::now()->toDateString(), '年', $this->ownerIds);
- }
- protected function tearDown(): void
- {
- cache()->flush();
- Owner::destroy($this->ownerIds);
- orderCountingRecord::destroy($this->orderCountingRecordIds);
- parent::tearDown();
- }
- /**
- * @test
- */
- public function get_all_from_cache()
- {
- for ($i = 0; $i <= $this->step_length; $i++) {
- foreach ($this->ownerIds as $ownerId) {
- foreach ($this->units as $unit) {
- switch ($unit) {
- case "日":
- $dateStr = Carbon::now()->subDays($i)->toDateString();
- break;
- case "周":
- $dateStr = Carbon::now()->subWeeks($i)->toDateString();
- break;
- case "月":
- $dateStr = Carbon::now()->subMonths($i)->toDateString();
- break;
- default:
- break;
- }
- if ($dateStr) {
- $orderCountingRecord = factory(OrderCountingRecord::class)->create([
- 'date_target' => $dateStr,
- 'owner_id' => $ownerId,
- 'counting_unit' => $unit,
- ]);
- $key = 'order_counting_records_' . $dateStr . '_' . $ownerId . '_' . $unit;
- cache()->put($key, collect()->push($orderCountingRecord));
- if (empty($this->orderCountingRecordIds[$unit])) $this->orderCountingRecordIds[$unit] = [];
- $this->orderCountingRecordIds[$unit][] = $orderCountingRecord->id;
- $dateStr = null;
- }
- }
- }
- }
- $resultDay = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
- $resultWeek = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek);
- // $resultMonth = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
- foreach ($this->units as $unit) {
- if (!empty($this->orderCountingRecordIds[$unit])) {
- switch ($unit) {
- case "日":
- $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultDay->sortBy('id')->toArray(), 'id'));
- break;
- case "周":
- $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultWeek->sortBy('id')->toArray(), 'id'));
- break;
- case "月":
- // $this->assertEquals($this->orderCountingRecordIds[$unit], array_column($resultMonth->sortBy('id')->toArray(), 'id'));
- break;
- }
- }
- }
- }
- /**
- * @test
- */
- public
- function get_all_from_middle_day()
- {
- for ($i = 0; $i <= $this->step_length; $i++) {
- foreach ($this->ownerIds as $ownerId) {
- $unit = '日';
- $dateStr = Carbon::now()->subDays($i)->toDateString();
- $orderCountingRecord = factory(OrderCountingRecord::class)->create([
- 'date_target' => $dateStr,
- 'owner_id' => $ownerId,
- 'counting_unit' => $unit,
- ]);
- $this->orderCountingRecordIds[] = $orderCountingRecord->id;
- }
- }
- $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
- $this->assertEquals($this->orderCountingRecordIds, array_column($result->sortBy('id')->toArray(), 'id'));
- }
- /**
- * @test
- */
- public function get_all_from_orders_day()
- {
- for ($i = 0; $i <= $this->step_length; $i++) {
- foreach ($this->ownerIds as $ownerId) {
- $dateStr = Carbon::now()->subDays($i)->toDateTimeString();
- $order = factory(Order::class)->create([
- 'created_at' => $dateStr,
- 'owner_id' => $ownerId,
- 'wms_status' => '订单完成'
- ]);
- $this->orderIds = $order->id;
- }
- }
- $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionDay);
- $this->assertNotEmpty($result);
- }
- /**
- * @test
- */
- public function get_all_from_orders_week()
- {
- for ($i = 0; $i <= $this->step_length; $i++) {
- foreach ($this->ownerIds as $ownerId) {
- $dateStr = Carbon::now()->subWeeks($i)->toDateTimeString();
- $order = factory(Order::class)->create([
- 'created_at' => $dateStr,
- 'owner_id' => $ownerId,
- 'wms_status' => '订单完成'
- ]);
- $this->orderIds = $order->id;
- }
- }
- $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionWeek);
- dump($result->toArray());
- $this->assertEquals([1,1,1,1],array_column($result->toArray(),'amount'));
- }
- /**
- * @test
- */
- public function get_all_from_orders_Month()
- {
- for ($i = 0; $i <= $this->step_length; $i++) {
- foreach ($this->ownerIds as $ownerId) {
- $dateStr = Carbon::now()->subMonths($i)->toDateTimeString();
- $order = factory(Order::class)->create([
- 'created_at' => $dateStr,
- 'owner_id' => $ownerId,
- 'wms_status' => '订单完成'
- ]);
- $this->orderIds = $order->id;
- }
- }
- $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
- dd($result->toArray());
- $this->assertEquals([1,1,1,1],array_column($result->toArray(),'amount'));
- }
- }
|