<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class JobTest extends TestCase
{
    use DatabaseTransactions;

    protected $indexUrl = "/wages/jobs";
    protected $item = "job";
    protected $model = "App\Models\Wages\Job";
    protected $table = "wages_ph_jobs";

    public function testUserCanGotoManagementPage()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->see($this->indexTitle())
            ->see($item->name);
    }

    public function testUserCanCreateItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->make();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->click($this->createButton())
            ->see($this->createTitle())
            ->type($item->name, 'name')

            ->press($this->createButton())
            ->see($this->indexTitle())
            ->see($item->name);

        $this->seeInDatabase($this->table ?? $this->item, [
            'name' => $item->name
        ]);
    }

    public function testUserCanEditItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();
        $newItem = factory($this->model)->make();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->within("tr:contains('$item->name')", function() { $this->click('edit'); })
            ->see($this->editTitle(""))

            ->seeInField('name', $item->name)

            ->type($newItem->name, 'name')

            ->press($this->updateButton())
            ->see($this->indexTitle())
            ->see($newItem->name);

        $this->seeInDatabase($this->table ?? $this->item, [
            'name' => $newItem->name
        ]);
    }

    public function testUserCanDeleteItem()
    {
        $user = factory(\App\User::class)->create();
        $item = factory($this->model)->create();

        $this->actingAs($user)
            ->visit($this->indexUrl)
            ->within("tr:contains('$item->name')", function() { $this->press('delete'); })
            ->see($this->indexTitle())
            ->dontSee($item->name);

        $this->dontSeeInDatabase($this->table ?? $this->item, [
            'id' => $item->id
        ]);
    }
}
