<?php

namespace Clarkeash\Shield\Test\Services;

use Clarkeash\Shield\Services\BaseService;
use Clarkeash\Shield\Test\TestCase;
use Illuminate\Http\Request;
use PHPUnit\Framework\Assert;

class BaseServiceTest extends TestCase
{
    /**
     * @var \Clarkeash\Shield\Test\Services\Stub
     */
    private $service;

    protected function setUp()
    {
        parent::setUp(); // TODO: Change the autogenerated stub

        $this->service = new Stub;
    }

    /** @test */
    public function it_can_get_a_header()
    {
        $request = $this->request();
        $request->headers->add(['X-Custom-Header' => 'sample']);

        Assert::assertEquals('sample', $this->service->header($request, 'X-Custom-Header'));
    }

    /** @test */
    public function a_default_header_can_be_provided()
    {
        Assert::assertEquals('default', $this->service->header($this->request(), 'X-Custom-Header', 'default'));
    }

    /** @test */
    public function it_will_get_the_first_element_of_an_array()
    {
        $request = $this->request();
        $request->headers->add(['X-Custom-Header' => ['a', 'b']]);

        Assert::assertEquals('a', $this->service->header($request, 'X-Custom-Header', 'sample'));
    }

    /** @test */
    public function it_will_fail_if_headers_are_not_provided()
    {
        Assert::assertFalse($this->service->checkBasic($this->request(), 'user', 'pass'));
    }

    /** @test */
    public function it_will_fail_if_invalid_credentials()
    {
        $request = $this->request();
        $request->headers->add([
            'PHP-AUTH-USER' => 'user',
            'PHP-AUTH-PW' => 'password',
        ]);

        Assert::assertFalse($this->service->checkBasic($request, 'user', 'pass'));
    }

    /** @test */
    public function it_will_pass_if_correct_credentials()
    {
        $request = $this->request();
        $request->headers->add([
            'PHP-AUTH-USER' => 'user',
            'PHP-AUTH-PW' => 'pass',
        ]);

        Assert::assertTrue($this->service->checkBasic($request, 'user', 'pass'));
    }
}

class Stub extends BaseService {

    public function verify(Request $request): bool
    {
        return true;
    }

    public function headers(): array
    {
        return [];
    }
}
