<?php

require_once 'ABaseTest.php';

class LitbangSatgasTest extends BaseTest {
    protected $token;
    protected $userId;

    public function setUp()
    {
        parent::setUp();
        $loginInfo = $this->doLogin();
        $this->token = $loginInfo['token'];
        $this->userId = $loginInfo['user_id'];
    }

    public function doLogin()
    {
        $login = $this->login(getenv("TEST_ADMIN_USERNAME"), getenv("TEST_ADMIN_PASSWORD"));
        $this->assertContains(
            [
                'success' => true
            ],
            $login
        );
        return $login['data'];
    }

    #region PRIVATE
    private function doGetList() {
        $response = $this->client->request(
            'GET',
            'litbang/satgas',
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["indexSatgas ...passed"]
        );

        return $jsonResponse;
    }
    #endregion

    #region TEST
    public function testCreate()
    {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Create Litbang Satgas ---"]
        );

        $list = $this->doGetList();

        $response = $this->client->request(
            'POST',
            'litbang/satgas/create',
            [
                'json' => [
                    "nama" => "Satgas " . ((int)$list['data']['total_record'] + 1)

                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["createSatgas ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Create Litbang Satgas ---"]
        );

        return $jsonResponse['data'];
    }

    /**
     * @depends testCreate
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testUpdate($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Update Litbang Satgas ---"]
        );

        $response = $this->client->request(
            'POST',
            'litbang/satgas/update/' . $data['id'],
            [
                'json' => [
                    "nama" => $data['nama'] . " Edited"

                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["updateSatgas ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Update Litbang Satgas ---"]
        );

        return $jsonResponse['data'];
    }

    /**
     * @depends testUpdate
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testDelete($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Delete Litbang Satgas ---"]
        );

        $response = $this->client->request(
            'POST',
            'litbang/satgas/delete/' . $data['id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token,
                    'Content-Type' => 'application/json'
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["deleteSatgas ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Delete Litbang Satgas ---"]
        );

        return $jsonResponse['data'];
    }

    /**
     * @depends testDelete
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testRestore($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Restore Litbang Satgas ---"]
        );

        $response = $this->client->request(
            'POST',
            'litbang/satgas/restore/' . $data['id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token,
                    'Content-Type' => 'application/json'
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse
        );

        self::$messages = array_merge(
            self::$messages,
            ["restoreSatgas ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Restore Litbang Satgas ---"]
        );

        return $jsonResponse['data'];
    }

    /**
     * @depends testRestore
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testDetail($data) {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Detail Litbang Satgas ---"]
        );

        $response = $this->client->request(
            'GET',
            'litbang/satgas/detail/' . $data['id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->token
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertEquals($data['id'], $jsonResponse['data']['id']);

        self::$messages = array_merge(
            self::$messages,
            ["detailSatgas ...passed"]
        );

        self::$messages = array_merge(
            self::$messages,
            ["--- End Detail Litbang Satgas ---"]
        );

        return $data;
    }

    public function testIndex() {
        self::$messages = array_merge(
            self::$messages,
            ["\n--- Start Index Litbang Satgas ---"]
        );

        $this->doGetList();

        self::$messages = array_merge(
            self::$messages,
            ["--- End Index Litbang Satgas ---"]
        );
    }
    #endregion
}
