<?php

namespace AuthStack\Driver\tests\units\Model;

use mageekguy\atoum;
use AuthStack\Driver\Model\User as AuthStackUser;
use AuthStack\Driver\Errors\InvalidUserAttributesError;

class User extends atoum
{
    protected $capsule;

    public function testSomething()
    {
        $this->integer(1)->isEqualTo(1);
    }

    public function testRetrieveNonexistentUserPasses()
    {
        $this
            ->given($attributes = ['conn_id' => 2, 'username' => 'uid=test@buckhill.co.uk,ou=users,dc=buckhill,dc=co,dc=uk'])
            ->then
                ->object($user = AuthStackUser::retrieve($attributes))->isInstanceOf(AuthStackUser::class)
                //->boolean($user->delete())->isTrue()
            ;
    }

    public function testUpdateUserConfig()
    {
        $this
            ->given($attributes = ['conn_id' => 2, 'username' => 'uid=test@buckhill.co.uk,ou=users,dc=buckhill,dc=co,dc=uk'])
            ->then
                ->object($user = AuthStackUser::retrieve($attributes))->isInstanceOf(AuthStackUser::class)
                ->object($user->updateUserConfig(['timezone' => 'Europe/London', 'date_format' => 'DD.MM.YYYY']))->isInstanceOf(AuthStackUser::class)
                ->boolean($user->save())->isTrue()
            ;
    }

    public function testRetrieveNonexistentUserFails()
    {
        $this
            ->given($attributes = ['username' => 'uid=test@buckhill.co.uk,ou=users,dc=buckhill,dc=co,dc=uk'])
            ->then
                ->exception(function () use ($attributes) {
                    $user = AuthStackUser::retrieve($attributes);
                })
                ->isInstanceOf(InvalidUserAttributesError::class)
        ;
    }
}
