<?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 }}Repository;
use App\Modules\{{ module }}\Domain\Index{{ module }};
use Test\TestCase;
use Test\TransactionTrait;
use Test\TransactionSpyTrait;

class {{ module }}DeleteControllerTest 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 testDelete()
    {
        $this->init();
        $model = TS{{ module }}Mother::pushRandom();

        $this->delete('owner/{{ route }}/' . $model->id()->value(), [], self::$token);

        try {
            $this->repository->find(new Index{{ module }}($model->id()));
            throw new \Exception('Expected {{ module }}NotFound');
        } catch (\Throwable $th) {
            $this->assertNotEmpty($th->getMessage());
        }
    }

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

        $this->delete('owner/{{ route }}/' . $model->id()->value(), [], self::$token);

        $this->assertTransactionalSuccess();
    }
}
