<?php

namespace Tests\Repository;

use \App;
use Tests\ApiTestTrait;
use Tests\TestCase;
use \Tests\Traits\MakeQaLeadTrait;
use App\Models\QaLead;
use App\Repositories\Admin\QaLeadRepository;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class QaLeadRepositoryTest extends TestCase
{
    use MakeQaLeadTrait, ApiTestTrait, DatabaseTransactions;

    /**
     * @var QaLeadRepository
     */
    protected $qaLeadRepo;

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

    /**
     * @test create
     */
    public function testCreateQaLead()
    {
        $qaLead = $this->fakeQaLeadData();
        $createdQaLead = $this->qaLeadRepo->create($qaLead);
        $createdQaLead = $createdQaLead->toArray();
        $this->assertArrayHasKey('id', $createdQaLead);
        $this->assertNotNull($createdQaLead['id'], 'Created QaLead must have id specified');
        $this->assertNotNull(QaLead::find($createdQaLead['id']), 'QaLead with given id must be in DB');
        $this->assertModelData($qaLead, $createdQaLead);
    }

    /**
     * @test read
     */
    public function testReadQaLead()
    {
        $qaLead = $this->makeQaLead();
        $dbQaLead = $this->qaLeadRepo->find($qaLead->id);
        $dbQaLead = $dbQaLead->toArray();
        $this->assertModelData($qaLead->toArray(), $dbQaLead);
    }

    /**
     * @test update
     */
    public function testUpdateQaLead()
    {
        $qaLead = $this->makeQaLead();
        $fakeQaLead = $this->fakeQaLeadData();
        $updatedQaLead = $this->qaLeadRepo->update($fakeQaLead, $qaLead->id);
        $this->assertModelData($fakeQaLead, $updatedQaLead->toArray());
        $dbQaLead = $this->qaLeadRepo->find($qaLead->id);
        $this->assertModelData($fakeQaLead, $dbQaLead->toArray());
    }

    /**
     * @test delete
     */
    public function testDeleteQaLead()
    {
        $qaLead = $this->makeQaLead();
        $resp = $this->qaLeadRepo->delete($qaLead->id);
        $this->assertTrue($resp);
        $this->assertNull(QaLead::find($qaLead->id), 'QaLead should not exist in DB');
    }
}
