<?php

namespace Tests\Repository;

use \App;
use Tests\ApiTestTrait;
use Tests\TestCase;
use \Tests\Traits\MakeProductionTeamTrait;
use App\Models\ProductionTeam;
use App\Repositories\Admin\ProductionTeamRepository;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ProductionTeamRepositoryTest extends TestCase
{
    use MakeProductionTeamTrait, ApiTestTrait, DatabaseTransactions;

    /**
     * @var ProductionTeamRepository
     */
    protected $productionTeamRepo;

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

    /**
     * @test create
     */
    public function testCreateProductionTeam()
    {
        $productionTeam = $this->fakeProductionTeamData();
        $createdProductionTeam = $this->productionTeamRepo->create($productionTeam);
        $createdProductionTeam = $createdProductionTeam->toArray();
        $this->assertArrayHasKey('id', $createdProductionTeam);
        $this->assertNotNull($createdProductionTeam['id'], 'Created ProductionTeam must have id specified');
        $this->assertNotNull(ProductionTeam::find($createdProductionTeam['id']), 'ProductionTeam with given id must be in DB');
        $this->assertModelData($productionTeam, $createdProductionTeam);
    }

    /**
     * @test read
     */
    public function testReadProductionTeam()
    {
        $productionTeam = $this->makeProductionTeam();
        $dbProductionTeam = $this->productionTeamRepo->find($productionTeam->id);
        $dbProductionTeam = $dbProductionTeam->toArray();
        $this->assertModelData($productionTeam->toArray(), $dbProductionTeam);
    }

    /**
     * @test update
     */
    public function testUpdateProductionTeam()
    {
        $productionTeam = $this->makeProductionTeam();
        $fakeProductionTeam = $this->fakeProductionTeamData();
        $updatedProductionTeam = $this->productionTeamRepo->update($fakeProductionTeam, $productionTeam->id);
        $this->assertModelData($fakeProductionTeam, $updatedProductionTeam->toArray());
        $dbProductionTeam = $this->productionTeamRepo->find($productionTeam->id);
        $this->assertModelData($fakeProductionTeam, $dbProductionTeam->toArray());
    }

    /**
     * @test delete
     */
    public function testDeleteProductionTeam()
    {
        $productionTeam = $this->makeProductionTeam();
        $resp = $this->productionTeamRepo->delete($productionTeam->id);
        $this->assertTrue($resp);
        $this->assertNull(ProductionTeam::find($productionTeam->id), 'ProductionTeam should not exist in DB');
    }
}
