<?php

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;
use Tests\AuthHelper;

class VectorsTest extends TestCase
{
    use DatabaseTransactions;

    public function testGetAllVectorLayers()
    {
        $this->get('/vectors');
        $this->seeStatusCode(200);
        $this->seeJsonStructure([
          [
            'id',
            'zoom_level',
            'filepath',
            'color'
          ]
        ]);
    }

    public function testGetVectorLayer()
    {
        $this->get('/vectors/1');
        $this->seeStatusCode(200);
        $this->seeJsonStructure([
          'id',
          'zoom_level',
          'filepath',
          'color'
        ]);
        $this->seeJson([
          'id' => 1
        ]);
    }

    public function testGetNotExistingVectorLayer()
    {
        $this->get('/vectors/102487');
        $this->seeStatusCode(404);
        $this->seeJsonStructure([
          'error',
        ]);
        $this->seeJson([
          'error' => 'Vector layer with provided id doesn\'t exists!'
        ]);
    }

    public function testCreateNewVectorLayerWithoutAuth()
    {
        $this->post('/vectors');
        $this->seeStatusCode(401);
        $this->seeJson([
          'error' => 'Token not provided.'
        ]);
    }

    public function testDeleteVectorLayerWithoutAuth()
    {
        $this->delete('/vectors/1');
        $this->seeStatusCode(401);
        $this->seeJson([
          'error' => 'Token not provided.'
        ]);
    }

    public function testUpdateVectorLayer()
    {
        $authHelper = new AuthHelper();
        $this->post('/vectors/1',
          [
            'color' => '#fff',
            'zoom_level' => '16'
          ],
          $authHelper->getAuthHeader()
        );
        $this->seeStatusCode(200);
        $this->seeJson([
          'success' => 'GeoJSON layer was successfully updated.'
        ]);
    }

    public function testUpdateVectorLayerWithoutAuth()
    {
        $this->post('/vectors/1');
        $this->seeStatusCode(401);
        $this->seeJson([
          'error' => 'Token not provided.'
        ]);
    }
}
