<?php

namespace AuthStack\UserDirectory\Service\test\units;

use mageekguy\atoum;
use AuthStack\UserDirectory\Exceptions\GroupException;
use AuthStack\UserDirectory\Model\Group as GroupModel;

class Group extends atoum\test
{
    public function testGroupFlow()
    {
        $this->given($group = (new GroupModel())
                ->setBaseUrl($this->getBaseUrl())
                ->setBasicAuthorization($this->getAuthToken())
                ->setTitle('A test group')
                ->setDescription('This group has been created for test purposes')
                ->setAdditional(['additional' => 'data']))
            ->then
                ->object($group)->isInstanceOf(GroupModel::class)
                ->object($group = $group->create())->isInstanceOf(GroupModel::class)

                ->string($group->getTitle())->isEqualTo('A test group')

                // Read a group - we'll read the one we created
                ->object($load = $group->read($group->getID()))->isInstanceOf(GroupModel::class)

                // Create a new group, then create a parent-child relation
                ->object($parent = (new GroupModel($group->getModel()))->setService($group->getService())->create())->isInstanceOf(GroupModel::class)

                // Set a parent now
                ->boolean($group->setParentGroup($parent->getID()))->isTrue()

                // Update
                ->boolean($group->update(['title' => 'Group title has been changed']))->isTrue()
                ->string($group->getTitle())->isEqualTo('Group title has been changed')

                // List members - @todo - not implemented on server
                //->array($group->getMembers())->hasKeys(['data'])

                // list groups
                ->array($group->listing())->hasKeys(['data'])

                // Delete group
                ->boolean($group->delete())->isTrue()

                // Delete parent group
                ->boolean($parent->delete())->isTrue()

                ;
    }

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

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