<?php

require_once "ABaseTest.php";
class TopikDiskusiBansosTest extends BaseTest {
    const URL_LOGIN = 'auth/login';
    const URL_CREATE = 'topik-keluhan/create';
    const URL_UPDATE = 'topik-keluhan/update/';
    const URL_DETAIL = 'topik-keluhan/detail/';
    const URL_LIST = 'topik-keluhan';
    const URL_DELETE = 'topik-keluhan/delete/';
    const URL_RESTORE = 'topik-keluhan/restore/';

    public function testLogin()
    {
        $data = array();
        $response = $this->client->request('POST', self::URL_LOGIN,
            [
                'json' => [
                    'username' => getenv('TEST_ADMIN_USERNAME'),
                    'password' => getenv('TEST_ADMIN_PASSWORD')
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on testLogin"
        );
        self::$messages = array_merge(self::$messages, ["\nLogin Sukses User [" . $jsonResponse['data']['user_id'] . "]"]);
        $data['token'] = $jsonResponse['data']['token'];

        return $data;
    }

    /**
     * @depends testLogin
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testCreate($data) {
        $input = [
            "deskripsi" => "Unit Test - Keluhan terkait Unit Test",
            "type" => "DESA",
            "view_order" => 5
        ];
        $response = $this->client->request('POST', self::URL_CREATE,
            [
                'json' => $input,
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $data = array_merge($data, $jsonResponse['data']);
        $this->assertEquals($input['deskripsi'], $data['deskripsi']);
        $this->assertEquals($input['type'], $data['type']);
        $this->assertEquals($input['view_order'], $data['view_order']);
        self::$messages = array_merge(self::$messages, ["Create Topik Diskusi Berhasil | ID : [" . $data['id'] . "]"]);

        return $data;
    }

    /**
     * @depends testCreate
     * @param $data
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testUpdate($data) {
        $input = [
            "deskripsi" => "Unit Test - Keluhan terkait Unit Test Edited",
            "type" => "UNITTEST",
            "view_order" => 1
        ];
        $response = $this->client->request('POST', self::URL_UPDATE . $data['id'],
            [
                'json' => $input,
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $data = array_merge($data, $jsonResponse['data']);
        $this->assertEquals($input['deskripsi'], $data['deskripsi']);
        $this->assertEquals($input['type'], $data['type']);
        $this->assertEquals($input['view_order'], $data['view_order']);
        self::$messages = array_merge(self::$messages, ["Update Topik Diskusi Berhasil | ID : [" . $data['id'] . "]"]);

        return $data;
    }

    /**
     * @depends testUpdate
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testDetail($data) {
        $response = $this->client->request('GET', self::URL_DETAIL . $data['id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $detail = $jsonResponse['data'];
        $this->assertEquals($detail['id'], $data['id']);
        $this->assertEquals($detail['deskripsi'], $data['deskripsi']);
        $this->assertEquals($detail['type'], $data['type']);
        $this->assertEquals($detail['view_order'], $data['view_order']);
        self::$messages = array_merge(self::$messages, ["Detail Topik Diskusi Berhasil | ID : [" . $data['id'] . "]"]);

        return $data;
    }

    /**
     * @depends testDetail
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testDelete($data) {
        $response = $this->client->request('POST', self::URL_DELETE . $data['id'],
            [
                'json' => [],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $detail = $jsonResponse['data'];
        $this->assertEquals($detail['id'], $data['id']);
        $this->assertEquals($detail['deskripsi'], $data['deskripsi']);
        $this->assertEquals($detail['type'], $data['type']);
        $this->assertEquals($detail['view_order'], $data['view_order']);
        $this->assertNotNull($detail['deleted_at']);

        self::$messages = array_merge(self::$messages, ["Delete Topik Diskusi Berhasil | ID : [" . $data['id'] . "]"]);

        return $data;
    }

    /**
     * @depends testDelete
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testRestore($data) {
        $response = $this->client->request('POST', self::URL_RESTORE . $data['id'],
            [
                'json' => [],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $detail = $jsonResponse['data'];
        $this->assertEquals($detail['id'], $data['id']);
        $this->assertEquals($detail['deskripsi'], $data['deskripsi']);
        $this->assertEquals($detail['type'], $data['type']);
        $this->assertEquals($detail['view_order'], $data['view_order']);
        $this->assertNull($detail['deleted_at']);

        self::$messages = array_merge(self::$messages, ["Restore Topik Diskusi Berhasil | ID : [" . $data['id'] . "]"]);

        return $data;
    }

    /**
     * @depends testRestore
     * @param $data
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function testList($data) {
        $response = $this->client->request('GET', self::URL_LIST,
            [
                'query' => [
                    'type' => 'UNITTEST'
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $list = $jsonResponse['data']['result'];
        $this->assertEquals('UNITTEST', $list[0]['type']);

        self::$messages = array_merge(self::$messages, ["List Topik Diskusi Berhasil"]);

        return $data;
    }
}
