<?php

$dotenv = new Dotenv\Dotenv(__DIR__ . '/../');
$dotenv->load();

class PAKSekolahTest extends \PHPUnit\Framework\TestCase
{
    private $username = 'user50@mail.com';
    private $password = 'password12345';

    protected $client;
    protected static $messages = [];
    public function setUp()
    {
        $this->client = new GuzzleHttp\Client(['base_uri' => getenv('TEST_BASE_URL')]);
    }

    public static function tearDownAfterClass()
    {
        echo implode("\n", self::$messages);
    }

    public function testLoginPAK()
    {
        self::$messages = array_merge(
        	self::$messages, 
        	["\n\n\t\t---Test Sekolah PAK---"]
        );
        
        $response = $this->client->request(
        	'POST', 
        	'auth/login',
            [
                'json' => [
                    'username' => $this->username,
                    'password' => $this->password
                ]
            ]
        );

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

        self::$messages = array_merge(
            self::$messages, 
            ["\t\t\tLogin PAK Sukses User [" . $jsonResponse['data']['user_id']. "]"]
        );
        $data['token'] = $jsonResponse['data']['token'];

        return $data;
    }

    /**
     *  @depends testLoginPAK
     */
    public function test_index_PAKSekolah($data)
    {
        $response = $this->client->request(
            'GET', 
            'pak/sekolah/index',
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on sekolah/index"
        );
        
        $data['initialTotalRecordPAKSekolah'] = $jsonResponse['data']['total_record'];
        
        self::$messages = array_merge(
            self::$messages, 
            ["\t\t\tpak/sekolah/index total_record: " . $data['initialTotalRecordPAKSekolah'] . " ...passed"]
        );

        return $data;
    }

    protected function getTotalRecordsPAKSekolah($data)
    {
        $response = $this->client->request(
            'GET', 
            'pak/sekolah/index',
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

        $jsonResponse = json_decode($response->getBody(), TRUE);

        return $jsonResponse['data']['total_record'];
    }

    /**
     * @depends test_index_PAKSekolah
     */
    public function test_create_PAKSekolah($data)
    {
        $response = $this->client->request(
        	'POST', 
        	'pak/sekolah/create',
            [
                'json' => [
                    "sekolah_id" => "20616049"
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $newTotalRecord = $this->getTotalRecordsPAKSekolah($data);
        $this->assertGreaterThan(
            $data['initialTotalRecordPAKSekolah'], 
            $newTotalRecord
        );
               
        self::$messages = array_merge(
        	self::$messages, 
        	["\t\t\tpak/sekolah/create #" . $jsonResponse['data']['result'][0]['id']. " ...passed"]
        );

        $data['sekolah_id'] = $jsonResponse['data']['result'][0]['id'];

        return $data;
    }

    /**
     * @depends test_create_PAKSekolah
     */
    public function test_view_PAKSekolah($data)
    {
        $response = $this->client->request(
        	'GET', 
        	'pak/sekolah/view/' . $data['sekolah_id'],
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on sekolah/view"
        );
        
        self::$messages = array_merge(
        	self::$messages, 
        	["\t\t\tpak/sekolah/view/" . $data['sekolah_id'] . " ...passed"]
        );

        return $data;
    }

    /**
     * @depends test_create_PAKSekolah
     */
    public function test_update_PAKSekolah($data)
    {
        $response = $this->client->request(
        	'POST', 
        	'pak/sekolah/update/' . $data['sekolah_id'],
            [
                'json' => [
                    "sekolah_id" => "20616049"
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on sekolah/update"
        );
        
        self::$messages = array_merge(
        	self::$messages, 
        	["\t\t\tpak/sekolah/update/" . $data['sekolah_id'] . " ...passed"]
        );

        return $data;
    }

    /**
     * @depends test_create_PAKSekolah
     */
    public function test_delete_PAKSekolah($data)
    {
        $response = $this->client->request(
        	'POST', 
        	'pak/sekolah/delete/' . $data['sekolah_id'],
            [
                'json' => [
                    'id' => $data['sekolah_id']
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $newTotalRecord = $this->getTotalRecordsPAKSekolah($data);
        $this->assertEquals(
            $data['initialTotalRecordPAKSekolah'], 
            $newTotalRecord
        );

        self::$messages = array_merge(
        	self::$messages, 
        	["\t\t\tpak/sekolah/delete/" . $data['sekolah_id'] . " ...passed"]
        );

        return $data;
    }

    /**
     * @depends test_create_PAKSekolah
     */
    public function test_all_PAKSekolah_should_give_initial_total_records_plus_one($data)
    {
        $response = $this->client->request(
        	'GET', 
        	'pak/sekolah/all',
            [
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        
        $this->assertGreaterThan(
            $data['initialTotalRecordPAKSekolah'], 
            $jsonResponse['data']['total_record']
        );

        self::$messages = array_merge(
        	self::$messages, 
        	["\t\t\tpak/sekolah/all ...passed"]
        );

        return $data;
    }

    /**
     * @depends test_create_PAKSekolah
     */
    public function test_restore_PAKSekolah($data)
    {
        $response = $this->client->request(
        	'POST', 
        	'pak/sekolah/restore/' . $data['sekolah_id'],
            [
                'json' => [
                    'id' => $data['sekolah_id']
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on sekolah/restore"
        );
        
        $newTotalRecord = $this->getTotalRecordsPAKSekolah($data);
        $this->assertGreaterThan(
            $data['initialTotalRecordPAKSekolah'], 
            $newTotalRecord
        );

        self::$messages = array_merge(
        	self::$messages, 
        	["\t\t\tpak/sekolah/restore/". $data['sekolah_id'] . " ...passed"]
        );

        return $data;
    }

	/**
     * @depends test_restore_PAKSekolah
     */
    public function test_destroy_PAKSekolah($data)
    {
        $response = $this->client->request(
        	'POST', 
        	'pak/sekolah/destroy/' . $data['sekolah_id'],
            [
                'json' => [
                    'id' => $data['sekolah_id']
                ],
                'headers' => [
                    'Authorization' => 'Bearer ' . $data['token']
                ]
            ]
        );

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

        $newTotalRecord = $this->getTotalRecordsPAKSekolah($data);
        $this->assertEquals(
            $data['initialTotalRecordPAKSekolah'], 
            $newTotalRecord
        );
        
        self::$messages = array_merge(
        	self::$messages, 
        	["\t\t\tpak/sekolah/destroy/". $data['sekolah_id'] . " ...passed"]
        );
        self::$messages = array_merge(
            self::$messages, 
            ["\t\t------------------------------------------------"]
        );

        return $data;
    }    

}

?>