<?php

namespace Tests\Services\StationRuleBatchService;

use App\Batch;
use App\Owner;
use App\Services\BatchService;
use App\Services\StationRuleBatchService;
use App\StationRuleBatch;
use Tests\TestCase;

class GetBatches_shouldProcessTest extends TestCase
{
    /** @var StationRuleBatchService $service */
    private $service;
    /** @var BatchService $batchService */
    private $batchService;
    private $data = [];
    private $amountOfInRule = 3;

    public function setUp(): void
    {
        parent::setUp();
        $this->service = app('StationRuleBatchService');
        $this->batchService = app('BatchService');
        $this->data['owner_target'] = factory(Owner::class)->create();
        $this->data['owner_none_target'] = factory(Owner::class)->create();
        $this->data['stationRuleBatch'] = factory(StationRuleBatch::class)
            ->create([
                'owner_id' => $this->data['owner_target']['id'],
            ]);
        $this->data['batches'] = factory(Batch::class, $this->amountOfInRule)->create([
            'owner_id' => $this->data['owner_target']['id'],
        ]);
        $this->data['batches'][]=factory(Batch::class)->create([
            'owner_id' => $this->data['owner_none_target']['id'],
        ]);
    }

    public function testFilteredOut()
    {
        $batches=$this->service->getBatches_shouldProcess($this->data['batches']);
        $this->assertEquals($this->amountOfInRule,$batches->count());
    }

    public function tearDown(): void
    {
        cache()->flush();
        Owner::query()->whereIn('id',[
            $this->data['owner_target']['id'],
            $this->data['owner_none_target']['id'],
        ])->delete();
        StationRuleBatch::query()->where('id',$this->data['stationRuleBatch']['id'])->delete();
        Batch::query()->whereIn('id',data_get($this->data['batches'],'*.id'))->delete();
        parent::tearDown(); // TODO: Change the autogenerated stub
    }
}
