<?php

namespace AuthStack\AuthProfile\tests\units;


use AuthStack\Driver\Model\User;
use mageekguy\atoum;
use Illuminate\Support\Collection;
use AuthStack\AuthProfile\Model\AuthProfile;
use AuthStack\AuthProfile\ProfileManager as AuthProfileManager;

class ProfileManager extends atoum
{
    public function testCreateProfileManager()
    {
        $this
            ->given($profiles = AuthProfileManager::fromArray($this->getProfileArray(), 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'))
            ->then
                ->object($profiles->getCollection())->isInstanceOf(Collection::class)
                ->object($profiles->getActiveProfile())->isInstanceOf(AuthProfile::class)
                ->array($profiles->toArray())->hasKey('active')
                ->string($profiles->toArray()['active'])->isEqualTo('a94a8fe5ccb19ba61c4c0873d391e987982fbbd3')
                ->string($profiles->getActiveProfileId())->isEqualTo('a94a8fe5ccb19ba61c4c0873d391e987982fbbd3')
            ;
    }

    public function testAuthProfileToArray()
    {
        $this
            ->given($profiles = AuthProfileManager::fromArray($this->getProfileArray(), 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'))
            ->then
                ->object($active = $profiles->getActiveProfile())->isInstanceOf(AuthProfile::class)
                ->array($active->toArray(function() {
                    return [
                        'app_title' => 'Test app'
                    ];
                }))->hasKey('app_title')

            ;
    }

    public function testIsAuthenticated()
    {
        $this
            ->given($profile = AuthProfileManager::fromArray($this->getProfileArray(), 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'))
            ->and($profile_empty = AuthProfileManager::fromArray([], ''))
            ->then
                ->boolean($profile->isAuthenticated())->isTrue()
                ->boolean($profile_empty->isAuthenticated())->isFalse()
            ;
    }

    public function testAddProfile()
    {
        $this
            ->given($manager = new AuthProfileManager())
            ->and($profile = AuthProfile::fromArray(['user_id' => 1]))
            ->then
                ->object($first = $manager->add($profile))->isInstanceOf(AuthProfile::class)
                ->integer($manager->getCollection()->count())->isEqualTo(1)

                // It's important we repeat the process twice to ensure that only one unique instance of profile is in the collection
                ->object($second = $manager->add($profile))->isInstanceOf(AuthProfile::class)
                ->integer($manager->getCollection()->count())->isEqualTo(1)

                ->boolean($first->getSessionId() === $second->getSessionId())->isTrue()
            ;
    }

    public function testGetProvisionedUser()
    {
        $this
            ->given($profiles = AuthProfileManager::fromArray($this->getProfileArray(), 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3'))
            ->and($active = $profiles->getActiveProfile())
            ->then
                ->object($active->getProvisionedUser())->isInstanceOf(User::class)
            ;
    }

    protected function getProfileArray()
    {
        return [

            [
                'protocol'          => 'SAML',
                'session_id'        => 'a94a8fe5ccb19ba61c4c0873d391e987982fbbd3',
                'conn_id'           => 2,
                'application_id'    => 1,
                'user_id'           => 9,
                'browser'           => '',
                'ip'                => '10.0.0.60',
                'created_at'        => '2016-08-30 22:22:22'
            ]
        ];
    }
}