<?php

namespace Test\Routes\Owner\{{ module }};

use App\DBRealTestSupport\TableCreation;
use App\DBRealTestSupport\TransactionSupported\TS{{ module }}Mother;
use App\DBRealTestSupport\TransactionSupported\Session\TSAdminSessionGenerator;
use App\DBRealTestSupport\TransactionSupported\Session\TSHistoryTableMother;
use App\Http\History\HistoryTables;
use App\Modules\{{ module }}\Domain\{{ module }};
use App\Modules\{{ module }}\Domain\{{ module }}Repository;
use App\Modules\{{ module }}\Domain\{{ module }}Status;
use App\Modules\{{ module }}\Domain\Id{{ module }};
use App\Modules\{{ module }}\Domain\Index{{ module }};
use Test\TestCase;
use Test\TransactionTrait;
use Test\TransactionSpyTrait;

class {{ module }}UpdateControllerTest extends TestCase
{
    use TransactionTrait;
    use TransactionSpyTrait;

    private static $session;
    private static $token;
    private static $admin;
    private {{ module }}Repository $repository;

    public function init()
    {
        $this->clear();
        $this->repository = $this->app->make({{ module }}Repository::class);
        list(self::$session, self::$token, self::$admin) = TSAdminSessionGenerator::createAll();
        TSHistoryTableMother::insert(HistoryTables::{{ const }});
    }

    public function clear()
    {
        TSHistoryTableMother::clear();
        TableCreation::clearTables(
{{ clear }}
        );
    }

    public function testUpdate()
    {
        $this->init();
        $model = TS{{ module }}Mother::pushRandom();
        $newStatus = $model->status()->value() === {{ module }}Status::ACTIVE
            ? {{ module }}Status::INACTIVE
            : {{ module }}Status::ACTIVE;

        $this->put('owner/{{ route }}', [
            'Id_{{ module }}'     => $model->id()->value(),
            '{{ module }}_Status' => $newStatus,
        ], self::$token);

        /** @var {{ module }} $found */
        $found = $this->repository->find(new Index{{ module }}($model->id()));
        $this->assertEquals($newStatus, $found->status()->value());
    }

    public function testUpdateIsTransactional()
    {
        $this->init();
        $model = TS{{ module }}Mother::pushRandom();
        $this->bindTransactionSpy();

        $this->put('owner/{{ route }}', [
            'Id_{{ module }}'     => $model->id()->value(),
            '{{ module }}_Status' => {{ module }}Status::INACTIVE,
        ], self::$token);

        $this->assertTransactionalSuccess();
    }
}
