| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?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
- {
- 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
- $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->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
- {
- Owner::destroy($this->ownerIds);
- OrderCountingRecord::destroy($this->orderCountingRecordIds);
- Order::destroy($this->orderIds);
- OrderCountingRecord::query()->whereIn('owner_id', $this->ownerIds)->delete();
- parent::tearDown();
- }
- /**
- * @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->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);
- $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
- }
- /**
- * @test
- */
- public function get_all_from_orders_Year()
- {
- for ($i = 0; $i <= $this->step_length; $i++) {
- foreach ($this->ownerIds as $ownerId) {
- $dateStr = Carbon::now()->subYears($i)->toDateTimeString();
- $order = factory(Order::class)->create([
- 'created_at' => $dateStr,
- 'owner_id' => $ownerId,
- 'wms_status' => '订单完成'
- ]);
- $this->orderIds = $order->id;
- }
- }
- $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionYear);
- $this->assertEquals([1, 1, 1, 1], array_column($result->toArray(), 'amount'));
- }
- /**
- * 按照月份查询插入中间表的测试
- * @test
- */
- public function insert_monthly_order_counting_records()
- {
- //前一个月的订单2个
- $carbon = Carbon::now()->subMonths(1);
- $orders1 = factory(Order::class)->times(2)->create(['created_at' => $carbon, 'owner_id' => $this->ownerIds[0], 'wms_status' => '订单完成']);
- //本月本日的订单2个
- $orders2 = factory(Order::class)->times(2)->create(['created_at' => Carbon::now(), 'owner_id' => $this->ownerIds[0], 'wms_status' => '订单完成']);
- $orders = $orders1->merge($orders2);
- $this->orderIds = array_column($orders->toArray(), 'id');
- //判断中间表中没有上一个月,'counting_unit' => '月' 'owner_id' => $this->ownerIds[0], 的数据
- $this->assertDatabaseMissing('order_counting_records', [
- 'date_target' => $carbon->startOfMonth()->toDateString(),
- 'owner_id' => $this->ownerIds[0],
- 'counting_unit' => '月',
- 'amount' => '2',
- ]);
- $result = $this->newOrderCountingRecordService->getOrderCountingRecords($this->queryConditionMonth);
- $this->assertDatabaseHas('order_counting_records', [
- 'date_target' => $carbon->startOfMonth()->toDateString(),
- 'owner_id' => $this->ownerIds[0],
- 'counting_unit' => '月',
- 'amount' => '2',
- ]);
- dump($result->toArray());
- }
- }
|