<?php

namespace AuthStack\UserDirectory\Service\test\units;

use mageekguy\atoum;
use AuthStack\UserDirectory\Exceptions\PolicyException;
use AuthStack\UserDirectory\Model\Policy as PolicyModel;

class Policy extends atoum\test
{
    public function testPolicyFlow()
    {
        $policy = new PolicyModel();

        $policy
            ->setTitle('Testing policy title')
            ->setMinLength(9)
            ->setMinStrength(4)
            ->setLockout(5)
            ->setLockoutDuration(3600)
            ->setHistory(5)
        ;

        $this->given($policy = $policy
                ->setBaseUrl($this->getBaseUrl())
                ->setBasicAuthorization($this->getAuthToken()))
            ->then
                ->object($policy = $policy->create())->isInstanceOf(PolicyModel::class)
                ->object($newpolicy = $policy->read($policy->getID()))->isInstanceOf(PolicyModel::class)
                ->string($newpolicy->getTitle())->isEqualTo('Testing policy title')
                ->boolean($newpolicy->setHistory(1)->update(['title' => 'Test']))->isTrue()
                ->integer($newpolicy->getHistory())->isEqualTo(1)
                ->array($newpolicy->listing())->hasKeys(['data'])
                ->boolean($policy->delete())->isTrue()
        ;

    }

    protected function getBaseUrl($default = 'http://aud.app')
    {
        return function_exists('env') ? env('AUD_URL', $default) : $default;
    }

    protected function getAuthToken($default = 's2fNbDoMKF1hzd0OKrFiphUoLwH29Sgx')
    {
        return base64_encode(function_exists('env') ? env('AUD_TOKEN', $default) : $default);
    }
}
