<?php
require_once 'ABaseTest.php';
class BosTest extends BaseTest
{
    public function testListProvinsiShouldGiveSuccessResponse() {
        $response = $this->client->request('GET', 'bos/list_provinsi',
            [
                'query' => [
                    'tahun' => 2017
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/list_provinsi"
        );
        $this->assertGreaterThan(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\n\t\t\tbos/list_provinsi with tahun=2017 Passed"]);
    }

    public function testListProvinsiShouldGiveNotFoundResponse() {
        $response = $this->client->request('GET', 'bos/list_provinsi',
            [
                'query' => [
                    'tahun' => 2019
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/list_provinsi"
        );
        $this->assertEquals(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_provinsi with tahun=2018 Passed"]);
    }

    public function testListProvinsiShouldGiveMustBeNumericResponse() {
        try{
            $response = $this->client->request('GET', 'bos/list_provinsi',
                [
                    'query' => [
                        'tahun' => 'abc'
                    ]
                ]
            );
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun must be numeric"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/list_provinsi"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_provinsi with tahun=abc Passed"]);
        }
    }

    public function testListProvinsiShouldGiveRequiredResponse() {
        try{
            $response = $this->client->request('GET', 'bos/list_provinsi');
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun is required"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/list_provinsi"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_provinsi with no tahun Passed"]);
        }
    }

    public function testListKotaKabShouldGiveSuccessResponse() {
        $response = $this->client->request('GET', 'bos/list_kota_kabupaten',
            [
                'query' => [
                    'tahun' => 2017,
                    "provinsi" => "Prov. Bali"
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/list_kota_kabupaten"
        );
        $this->assertGreaterThan(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\n\t\t\tbos/list_kota_kabupaten with tahun=2017,provinsi=Prov. Bali Passed"]);
    }

    public function testListKotaKabShouldGiveNotFoundResponse() {
        $response = $this->client->request('GET', 'bos/list_kota_kabupaten',
            [
                'query' => [
                    'tahun' => 2017,
                    "provinsi" => "Prov. D.I. Yogyakarta1"
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/list_kota_kabupaten"
        );
        $this->assertEquals(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_kota_kabupaten with tahun=2017,provinsi=Prov. D.I. Yogyakarta1 Passed"]);
    }

    public function testListKotaKabShouldGiveMustBeNumericResponse() {
        try{
            $response = $this->client->request('GET', 'bos/list_kota_kabupaten',
                [
                    'query' => [
                        'tahun' => 'abc',
                        "provinsi" => "Prov. D.I. Yogyakarta"
                    ]
                ]
            );
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun must be numeric"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/list_kota_kabupaten"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_kota_kabupaten with tahun=abc,provinsi=Prov. D.I. Yogyakarta Passed"]);
        }
    }

    public function testListKotaKabShouldGiveRequiredResponse() {
        try{
            $response = $this->client->request('GET', 'bos/list_kota_kabupaten');
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun is required"
                        ],
                        [
                            "field" => "provinsi",
                            "message" => "Field provinsi is required"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/list_kota_kabupaten"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_kota_kabupaten with no tahun,provinsi Passed"]);
        }
    }

    public function testSummaryKecamatanShouldGiveSuccessResponse() {
        $response = $this->client->request('GET', 'bos/summary_kecamatan',
            [
                'query' => [
                    'tahun' => 2017,
                    "kecamatan" => "Kec. Manggis"
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/summary_kecamatan"
        );
        $this->assertGreaterThan(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\n\t\t\tbos/summary_kecamatan with tahun=2017,kecamatan=Kec. Manggis Passed"]);
    }

    public function testSummaryKecamatanShouldGiveNotFoundResponse() {
        $response = $this->client->request('GET', 'bos/summary_kecamatan',
        [
            'query' => [
                'tahun' => 2017,
                "kecamatan" => "Kec. Jetis1"
            ]
        ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/summary_kecamatan"
        );
        $this->assertEquals(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\t\t\tbos/summary_kecamatan with tahun=2017,kecamatan=Kec. Jetis1 Passed"]);
    }

    public function testSummaryKecamatanShouldGiveMustBeNumericResponse() {
        try{
            $response = $this->client->request('GET', 'bos/summary_kecamatan',
                [
                    'query' => [
                        'tahun' => 'abc',
                        "kecamatan" => "Kec. Jetis"
                    ]
                ]
            );
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun must be numeric"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/summary_kecamatan"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/summary_kecamatan with tahun=abc,kecamatan=Kec. Jetis Passed"]);
        }
    }

    public function testSummaryKecamatanShouldGiveRequiredResponse() {
        try{
            $response = $this->client->request('GET', 'bos/summary_kecamatan');
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun is required"
                        ],
                        [
                            "field" => "kecamatan",
                            "message" => "Field kecamatan is required"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/summary_kecamatan"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/summary_kecamatan with no tahun,kecamatan Passed"]);
        }
    }

	public function testListSekolahShouldGiveSuccessResponse() {
        $response = $this->client->request('GET', 'bos/list_sekolah',
            [
                'query' => [
                    'tahun' => 2017,
                    "kecamatan" => "Kec. Manggis"
                ]
            ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/list_sekolah"
        );
        $this->assertGreaterThan(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\n\t\t\tbos/list_sekolah with tahun=2017,kecamatan=Kec. Manggis Passed"]);
    }

    public function testListSekolahShouldGiveNotFoundResponse() {
        $response = $this->client->request('GET', 'bos/list_sekolah',
        [
            'query' => [
                'tahun' => 2017,
                "kecamatan" => "Kec. Jetis1"
            ]
        ]
        );

        $statusCode = $response->getStatusCode();
        $jsonResponse = json_decode($response->getBody(), TRUE);
        $this->assertEquals(200, $statusCode);
        $this->assertContains(
            [
                'success' => true
            ],
            $jsonResponse,
            "Error on bos/list_sekolah"
        );
        $this->assertEquals(0, $jsonResponse['data']['total_record']);
        self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_sekolah with tahun=2017,kecamatan=Kec. Jetis1 Passed"]);
    }

    public function testListSekolahShouldGiveMustBeNumericResponse() {
        try{
            $response = $this->client->request('GET', 'bos/list_sekolah',
                [
                    'query' => [
                        'tahun' => 'abc',
                        "kecamatan" => "Kec. Jetis"
                    ]
                ]
            );
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun must be numeric"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/list_sekolah"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_sekolah with tahun=abc,kecamatan=Kec. Jetis Passed"]);
        }
    }

    public function testListSekolahShouldGiveRequiredResponse() {
        try{
            $response = $this->client->request('GET', 'bos/list_sekolah');
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun is required"
                        ],
                        [
                            "field" => "kecamatan",
                            "message" => "Field kecamatan is required"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/list_sekolah"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/list_sekolah with no tahun,kecamatan Passed"]);
        }
    }

	// public function testDetailShouldGiveSuccessResponse() {
    //     $response = $this->client->request('GET', 'bos/detail',
    //         [
    //             'query' => [
    //                 'tahun' => 2017,
    //                 "npsn" => "20400521"
    //             ]
    //         ]
    //     );

    //     $statusCode = $response->getStatusCode();
    //     $jsonResponse = json_decode($response->getBody(), TRUE);
    //     $this->assertEquals(200, $statusCode);
    //     $this->assertContains(
    //         [
    //             'success' => true
    //         ],
    //         $jsonResponse,
    //         "Error on bos/detail"
    //     );
    //     $this->assertGreaterThan(0, $jsonResponse['data']['total_record']);
    //     self::$messages = array_merge(self::$messages, ["\n\t\t\tbos/detail with tahun=2017,npsn=20400521 Passed"]);
    // }

    // public function testDetailShouldGiveNotFoundResponse() {
    //     $response = $this->client->request('GET', 'bos/detail',
    //     [
    //         'query' => [
    //             'tahun' => 2017,
    //             "npsn" => "2040052110"
    //         ]
    //     ]
    //     );

    //     $statusCode = $response->getStatusCode();
    //     $jsonResponse = json_decode($response->getBody(), TRUE);
    //     $this->assertEquals(200, $statusCode);
    //     $this->assertContains(
    //         [
    //             'success' => true
    //         ],
    //         $jsonResponse,
    //         "Error on bos/detail"
    //     );
    //     $this->assertEquals(4, $jsonResponse['data']['total_record']);
    //     self::$messages = array_merge(self::$messages, ["\t\t\tbos/detail with tahun=2017,npsn=2040052110 Passed"]);
    // }

    public function testDetailShouldGiveMustBeNumericResponse() {
        try{
            $response = $this->client->request('GET', 'bos/detail',
                [
                    'query' => [
                        'tahun' => 'abc',
                        "npsn" => "20400521"
                    ]
                ]
            );
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun must be numeric"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/detail"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/detail with tahun=abc,npsn=20400521 Passed"]);
        }
    }

    public function testDetailShouldGiveRequiredResponse() {
        try{
            $response = $this->client->request('GET', 'bos/detail');
        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            $statusCode = $response->getStatusCode();
            $jsonResponse = json_decode($response->getBody(), TRUE);
            $this->assertEquals(460, $statusCode);
            $this->assertArraySubset(
                [
                    'success' => false,
                    "errors" => [
                        [
                            "field" => "tahun",
                            "message" => "Field tahun is required"
                        ],
                        [
                            "field" => "npsn",
                            "message" => "Field npsn is required"
                        ]
                    ]
                ],
                $jsonResponse,
                "Error on bos/detail"
            );
            self::$messages = array_merge(self::$messages, ["\t\t\tbos/detail with no tahun,npsn Passed"]);
        }
    }

    public function testSummaryNasionalPerJenjangSuccess() {
        try {
            $this->doAPIShouldGiveSuccessResponse(
                'GET',
                'bos/summary_nasional_per_jenjang?tahun=2017',
                [],
                200,
                "testSummaryNasionalPerJenjangSuccess Passed"
            );
        }
        catch (Exception $e) {
            self::$messages = array_merge(self::$messages, ["testSummaryNasionalPerJenjangSuccess Failed", $e->getMessage()]);
        }
    }

    public function testSummaryNasionalPerKomponenSuccess() {
        try {
            $this->doAPIShouldGiveSuccessResponse(
                'GET',
                'bos/summary_nasional_per_komponen?tahun=2018',
                [],
                200,
                "testSummaryNasionalPerKomponenSuccess Passed"
            );
        }
        catch (Exception $e) {
            self::$messages = array_merge(self::$messages, ["testSummaryNasionalPerKomponenSuccess Failed", $e->getMessage()]);
        }
    }

    public function testYearsSuccess() {
        try {
            $this->doAPIShouldGiveSubsetResponse(
                'GET',
                'bos/years',
                200,
                [],
                "testYearsSuccess Passed"
            );
        }
        catch (Exception $e) {
            self::$messages = array_merge(self::$messages, ["testYearsSuccess Failed", $e->getMessage()]);
        }
    }
}
?>