<?php

namespace Tests\Repository;

use \App;
use Tests\ApiTestTrait;
use Tests\TestCase;
use \Tests\Traits\MakeLeadStatusTrait;
use App\Models\LeadStatus;
use App\Repositories\Admin\LeadStatusRepository;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class LeadStatusRepositoryTest extends TestCase
{
    use MakeLeadStatusTrait, ApiTestTrait, DatabaseTransactions;

    /**
     * @var LeadStatusRepository
     */
    protected $leadStatusRepo;

    public function setUp()
    {
        parent::setUp();
        $this->leadStatusRepo = App::make(LeadStatusRepository::class);
    }

    /**
     * @test create
     */
    public function testCreateLeadStatus()
    {
        $leadStatus = $this->fakeLeadStatusData();
        $createdLeadStatus = $this->leadStatusRepo->create($leadStatus);
        $createdLeadStatus = $createdLeadStatus->toArray();
        $this->assertArrayHasKey('id', $createdLeadStatus);
        $this->assertNotNull($createdLeadStatus['id'], 'Created LeadStatus must have id specified');
        $this->assertNotNull(LeadStatus::find($createdLeadStatus['id']), 'LeadStatus with given id must be in DB');
        $this->assertModelData($leadStatus, $createdLeadStatus);
    }

    /**
     * @test read
     */
    public function testReadLeadStatus()
    {
        $leadStatus = $this->makeLeadStatus();
        $dbLeadStatus = $this->leadStatusRepo->find($leadStatus->id);
        $dbLeadStatus = $dbLeadStatus->toArray();
        $this->assertModelData($leadStatus->toArray(), $dbLeadStatus);
    }

    /**
     * @test update
     */
    public function testUpdateLeadStatus()
    {
        $leadStatus = $this->makeLeadStatus();
        $fakeLeadStatus = $this->fakeLeadStatusData();
        $updatedLeadStatus = $this->leadStatusRepo->update($fakeLeadStatus, $leadStatus->id);
        $this->assertModelData($fakeLeadStatus, $updatedLeadStatus->toArray());
        $dbLeadStatus = $this->leadStatusRepo->find($leadStatus->id);
        $this->assertModelData($fakeLeadStatus, $dbLeadStatus->toArray());
    }

    /**
     * @test delete
     */
    public function testDeleteLeadStatus()
    {
        $leadStatus = $this->makeLeadStatus();
        $resp = $this->leadStatusRepo->delete($leadStatus->id);
        $this->assertTrue($resp);
        $this->assertNull(LeadStatus::find($leadStatus->id), 'LeadStatus should not exist in DB');
    }
}
