<?php

namespace Tests\Unit\Customer\CustomerService;

use App\Customer;
use App\Services\CustomerService;
use Illuminate\Support\Str;
use Tests\TestCase;

class CustomerTest extends TestCase
{
    /** @var CustomerService */
    public $service;
    public $data;

    protected function setUp(): void
    {
        parent::setUp();
        $this->service = app(CustomerService::class);
        $this->data = [
            "models"=>factory(Customer::class,3)->create()->toArray()
        ];
    }

    /**
     * @group customer
     */
    public function testCreate(){
        $model = $this->service->create([
            "code" => date('Ymd').Str::random(4),
            "name" => date('Ymd').Str::random(5),
            "company_name" => date('Ymd').Str::random(6),
        ]);
        $this->assertNotNull($model);
        $this->data["models"][] = $model->toArray();
    }
    /**
     * @group customer
     */
    public function testGetSelection(){
        $models = $this->service->getSelection(["id","name","company_name"]);
        $this->assertGreaterThanOrEqual(1,count($models));
        $this->assertArrayHasKey("company_name",$models[0]);
    }
    /**
     * @group customer
     */
    public function testPaginate(){
        $models = $this->service->paginate(["paginate"=>5]);
        $this->assertGreaterThanOrEqual(1,count($models));
        $this->assertLessThanOrEqual(5,count($models));
    }
    /**
     * @group customer
     */
    public function testFind(){
        $model = $this->service->find($this->data["models"][0]["id"]);
        $this->assertNotNull($model);
    }
    /**
     * @group customer
     */
    public function testUpdate()
    {
        $value = date('YmdH').Str::random(4);
        $row = $this->service->update(["id"=>$this->data["models"][0]["id"]],[
            "name" => $value,
        ]);
        $this->data["models"][0]["name"] = $value;
        $this->assertEquals($row,1);
    }
    /**
     * @group customer
     */
    public function testDestroy()
    {
        $row = $this->service->destroy($this->data["models"][0]["id"]);
        $this->assertEquals($row,1);
        $this->assertDeleted("customers", $this->data["models"][0]);
    }

    public function tearDown(): void
    {
        Customer::destroy(array_column($this->data["models"],'id'));
        parent::tearDown();
    }
}
