{#
/**
 * Test Case bake template
 *
 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
 * @link          https://cakephp.org CakePHP(tm) Project
 * @since         2.0.0
 * @license       https://www.opensource.org/licenses/mit-license.php MIT License
 */
#}
{% set isController = type|lower == 'controller' %}
{% set isPlugin = 'app'|lower == 'app' %}
{% set isTable = type|lower == 'table' or type|lower == 'model/table' %}
{% set isCommand = type|lower == 'command' %}
{% if isController %}
    {%- set traitName = 'IntegrationTestTrait' %}
    {%- set uses = uses|merge(['Cake\\TestSuite\\IntegrationTestTrait', 'PHPUnit\\Exception']) %}
{% elseif isCommand %}
    {%- set traitName = 'ConsoleIntegrationTestTrait' %}
    {%- set uses = uses|merge(['Cake\\Console\\TestSuite\\ConsoleIntegrationTestTrait']) %}
{% endif %}
{%- set uses = uses|merge(["Cake\\TestSuite\\TestCase"]) %}

{{- element('Bake.file_header', {
    namespace: "#{baseNamespace}\\Test\\TestCase\\#{subNamespace}",
    classImports: uses,
}) }}

/**
 * {{ fullClassName }} Test Case
{% if isController or isCommand %}
 *
 * @uses \{{ fullClassName }}
{% endif %}
 */
class {{ className }}Test extends TestCase
{
{% if traitName is defined %}
    use {{ traitName }};
{% if properties or fixtures or construction or methods %}

{% endif %}
{% endif %}
{% if properties %}
{% for propertyInfo in properties %}
{% if loop.index > 1 %}

{% endif %}
    /**
     * {{ propertyInfo.description }}
     *
     * @var {{ propertyInfo.type }}
     */
    protected ${{ propertyInfo.name }}{% if propertyInfo.value is defined and propertyInfo.value %} = {{ propertyInfo.value }}{% endif %};
{% if loop.last and (fixtures or construction or methods) %}

{% endif %}
{% endfor %}
{% endif %}

{%- if fixtures %}
    /**
     * Fixtures
     *
     * @var array<string>
     */
    protected array $fixtures = {{ Bake.exportVar(fixtures|values, 1)|raw }};
{% if construction or methods %}

{% endif %}
{% endif %}

{%- if construction or isController %}
    /**
     * setUp method
     *
     * @return void
     */
    protected function setUp(): void
    {
        parent::setUp();
{% if preConstruct %}
        {{ preConstruct|raw }}
{% endif %}
{% if isCommand %}
        {{ construction|raw }}
{% elseif isController %}
        $this->{{ subject }} = $this->getTableLocator()->get('{{ subject }}');
{% else %}
        $this->{{ (subject ~ ' = ' ~ construction)|raw }}
{% endif %}
{% if postConstruct %}
        {{ postConstruct|raw }}
{% endif %}
    }
{% if not isCommand %}

    /**
     * tearDown method
     *
     * @return void
     */
    protected function tearDown(): void
    {
        unset($this->{{ subject }});

        parent::tearDown();
    }
{% if methods %}

{% endif %}
{% endif %}
{% endif %}

{%- if isTable %}
    /**
    * TestInitialize method
    *
    * @return void
    * @uses \{{ fullClassName }}::initialize()
    */
    public function testInitialize(): void
    {
        // verify all associations loaded
        $expectedAssociations = [];
        $associations = $this->{{ subject}}->associations();

        $this->assertCount(count($expectedAssociations), $associations);
        foreach ($expectedAssociations as $expectedAssociation) {
            $this->assertTrue($this->{{ subject }}->hasAssociation($expectedAssociation));
        }

        // verify all behaviors loaded
        $expectedBehaviors = [];
        $behaviors = $this->{{ subject }}->behaviors();

        $this->assertCount(count($expectedBehaviors), $behaviors);
        foreach ($expectedBehaviors as $expectedBehavior) {
            $this->assertTrue($this->{{ subject }}->hasBehavior($expectedBehavior));
        }
    }

{% endif %}
{%- for method in methods %}
{% if loop.index > 1 %}

{% endif %}
{%- if isController and method|lower == 'index' %}
    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with an unauthenticated user (not logged in)
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetUnauthenticated(): void
    {
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
        ];
        $this->get($url);
        $this->assertResponseCode(302);
        $this->assertRedirectContains('login');
    }

    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetLoggedIn(): void
    {
        // $this->loginUserByRole('admin');
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
        ];
        $this->get($url);
        $this->assertResponseCode(200);
    }
{% endif %}
{%- if isController and method|lower == 'view' %}
    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with an unauthenticated user (not logged in)
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetUnauthenticated(): void
    {
        $id = 1;
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            $id,
        ];
        $this->get($url);
        $this->assertResponseCode(302);
        $this->assertRedirectContains('login');
    }

    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetLoggedIn(): void
    {
        $id = 1;
        // $this->loginUserByRole('admin');
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            $id,
        ];
        $this->get($url);
        $this->assertResponseCode(200);
    }
{% endif %}
{%- if isController and method|lower == 'add' %}
    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with an unauthenticated user (not logged in)
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetUnauthenticated(): void
    {
        $cntBefore = $this->{{ subject }}->find()->count();
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
        ];
        $this->get($url);
        $this->assertResponseCode(302);
        $this->assertRedirectContains('login');

        $cntAfter = $this->{{ subject }}->find()->count();
        $this->assertEquals($cntBefore, $cntAfter);
    }

    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetLoggedIn(): void
    {
        $cntBefore = $this->{{ subject }}->find()->count();

        // $this->loginUserByRole('admin');
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
        ];
        $this->get($url);
        $this->assertResponseCode(200);

        $cntAfter = $this->{{ subject }}->find()->count();
        $this->assertEquals($cntBefore, $cntAfter);
    }

    /**
    * Test {{ method }} method
    *
    * Tests a POST request to the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}PostLoggedInSuccess(): void
    {
        $cntBefore = $this->{{ subject }}->find()->count();

        // $this->loginUserByRole('admin');
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
        ];
        $data = [];
        $this->post($url, $data);
        $this->assertResponseCode(302);
        $this->assertRedirectContains('{{ subject|lower }}/view');

        $cntAfter = $this->{{ subject }}->find()->count();
        $this->assertEquals($cntBefore + 1, $cntAfter);
    }

    /**
    * Test {{ method }} method
    *
    * Tests a POST request to the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}PostLoggedInFailure(): void
    {
        $cntBefore = $this->{{ subject }}->find()->count();

        // $this->loginUserByRole('admin');
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
        ];
        $data = [];
        $this->post($url, $data);
        $this->assertResponseCode(200);

        $cntAfter = $this->{{ subject }}->find()->count();
        $this->assertEquals($cntBefore, $cntAfter);
    }
{% endif %}
{%- if isController and method|lower == 'edit' %}
    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with an unauthenticated user (not logged in)
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetUnauthenticated(): void
    {
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            1,
        ];
        $this->get($url);
        $this->assertResponseCode(302);
        $this->assertRedirectContains('login');
    }

    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}GetLoggedIn(): void
    {
        // $this->loginUserByRole('admin');
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            1,
        ];
        $this->get($url);
        $this->assertResponseCode(200);
    }

    /**
    * Test {{ method }} method
    *
    * Tests a PUT request to the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}PutLoggedInSuccess(): void
    {
        // $this->loginUserByRole('admin');
        $id = 1;
        $before = $this->{{ subject }}->get($id);
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            $id,
        ];
        $data = [
            // test new data here
        ];
        $this->put($url, $data);

        $this->assertResponseCode(302);
        $this->assertRedirectContains('{{ subject|lower }}/view');

        $after = $this->{{ subject }}->get($id);
        // assert saved properly below
    }

    /**
    * Test {{ method }} method
    *
    * Tests a PUT request to the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}PutLoggedInFailure(): void
    {
        // $this->loginUserByRole('admin');
        $id = 1;
        $before = $this->{{ subject }}->get($id);
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            $id,
        ];
        $data = [];
        $this->put($url, $data);
        $this->assertResponseCode(200);
        $after = $this->{{ subject }}->get($id);

        // assert save failed below
    }
{% endif %}
{%- if isController and method|lower == 'delete' %}
    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with an unauthenticated user (not logged in)
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}Unauthenticated(): void
    {
        $cntBefore = $this->{{ subject }}->find()->count();

        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            1,
        ];
        $this->delete($url);
        $this->assertResponseCode(302);
        $this->assertRedirectContains('login');

        $cntAfter = $this->{{ subject }}->find()->count();
        $this->assertEquals($cntBefore, $cntAfter);
    }

    /**
    * Test {{ method }} method
    *
    * Tests the {{ method }} action with a logged in user
    *
    * @uses \{{ fullClassName }}::{{ method }}()
    * @throws Exception
    *
    * @return void
    */
    public function test{{ method|camelize }}LoggedIn(): void
    {
        $cntBefore = $this->{{ subject }}->find()->count();

        // $this->loginUserByRole('admin');
        $url = [
{% if baseNamespace != 'App' %}
            'plugin' => '{{ baseNamespace }}',
{% endif %}
            'controller' => '{{ subject }}',
            'action' => '{{ method|lower }}',
            1,
        ];
        $this->delete($url);
        $this->assertResponseCode(302);
        $this->assertRedirectContains('{{ subject|lower }}');

        $cntAfter = $this->{{ subject }}->find()->count();
        $this->assertEquals($cntBefore - 1, $cntAfter);
    }
{% endif %}
{%- if isController and (method|lower == 'index' or method|lower == 'view' or method|lower == 'add' or method|lower == 'edit' or method|lower == 'delete') %}
{% else %}
    /**
    * Test {{ method }} method
    *
    * @return void
    * @uses \{{ fullClassName }}::{{ method }}()
    */
    public function test{{ method|camelize }}(): void
    {
        $this->markTestIncomplete('Not implemented yet.');
    }
{% endif %}
{% endfor %}
}