<?php

namespace AuthStack\Driver\Connection\tests\units;

use mageekguy\atoum;
use Illuminate\Database\QueryException;
use AuthStack\Driver\Driver\AuthDriverInterface;
use AuthStack\Driver\System\Driver as SystemDriver;
use AuthStack\Driver\Connection\Model\Driver as DriverModel;
use AuthStack\Driver\Connection\Model\Connection as ConnectionModel;
use AuthStack\Driver\Connection\Manager as ConnectionManager;

class Manager extends atoum
{
    public function testConnecting()
    {
        $this->given($connection = ConnectionManager::initialize(2))
            ->then
                ->object($connection)->isInstanceOf(AuthDriverInterface::class)
            ;
    }

    public function testInstallDriver()
    {
        $this
            ->object($driver = ConnectionManager::install(SystemDriver::install()))->isInstanceOf(DriverModel::class)
            ->exception(function() use ($driver)
            {
                $driver->addDriverConnection([]);
            })->isInstanceOf(QueryException::class)

            ->object($connection = $driver->addDriverConnection([
                'title' => 'Altered LDAP Connection (ou = memberof)',
                'configuration' => [
                    'dc' => '',
                    'ou' => 'ou=users',
                    'uid' => 'uid',

                    /**
                     * These are the attributes that are returned by the group filter
                     */
                    'attributes' => [
                        "memberof",
                        "cn",
                        "dn",
                        "mail",
                        "uid",
                        "givenname",
                        "sn",
                        "telephonenumber",
                        "postaladdress",
                        "o",
                        "l",
                        "postalcode",
                        "st"
                    ],

                    "admin_group" => "administrators",
                    "primary_key" => "dn",
                    "group_search" => [
                        "filter" => "(&(objectClass=*)(member=%s))",
                        "base_dn" => "cn=%s,ou=groups,dc=buckhill,dc=co,dc=uk"
                    ],


                    'connection' => [
                        'TLS' => false,
                        'host' => '10.0.0.188',
                        'port' => 389,
                        'base_dn' => 'dc=buckhill,dc=co,dc=uk',
                        'options' => [
                            [
                                'name' => LDAP_OPT_PROTOCOL_VERSION,
                                'value' => 3
                            ]
                        ]
                    ],

                    /**
                     * This is an example on how to create an attribute list which is used by AuthStack UI to
                     * expose attribute mapping functionality
                     */
                    'attribute_list' => [
                        [
                            'label' => 'Member of',
                            'value' => 'memberof',
                        ],

                        [
                            'label' => 'Common Name',
                            'value' => 'cn',
                        ],

                        [
                            'label' => 'Distinguished Name',
                            'value' => 'dn'
                        ],

                        [
                            'label' => 'Mail',
                            'value' => 'mail'
                        ],

                        [
                            'label' => 'Unique Identifier',
                            'value' => 'uid'
                        ],

                        [
                            'label' => 'Given Name',
                            'value' => 'givenname'
                        ],

                        [
                            'label' => 'Surname',
                            'value' => 'sn'
                        ],

                        [
                            'label' => 'Telephone Number',
                            'value' => 'telephonenumber'
                        ],

                        [
                            'label' => 'Postal Address',
                            'value' => 'postaladdress'
                        ],

                        [
                            'label' => 'Organization',
                            'value' => 'o'
                        ],

                        [
                            'label' => 'Locality',
                            'value' => 'city'
                        ],

                        [
                            'label' => 'Postal Code',
                            'value' => 'postalcode'
                        ],

                        [
                            'label' => 'Street Name',
                            'value' => 'st'
                        ]
                    ]
                ]
                ]))->isInstanceOf(ConnectionModel::class)

                ->array($connection->getAttributeList())->isNotEmpty()->hasSize(13)

                ->boolean($connection->delete())->isTrue()


            ;

    }
}