<?php

/**
 * LICENSE: This Software is the property of Lifestyle Webconsulting GmbH (Aschaffenburg, Germany)
 * and is protected by copyright law - it is NOT Freeware.
 *
 * Any unauthorized use of this software without a valid license
 * is a violation of the license agreement and will be prosecuted by
 * civil and criminal law.
 *
 * @copyright  2016 Lifestyle Webconsulting GmbH
 * @link       http://www.life-style.de
 */

namespace Sso\RestBundle\Tests\ModelXml\Request\UserWs\Version1\UserService\PasswordConfirm;

use Sso\RestBundle\ModelXml\Common\Request\Credentials\UserWs\Credentials;
use Sso\RestBundle\ModelXml\Request\UserWs\Version1\UserService\PasswordConfirm\Request;
use Sso\RestBundle\ModelXml\Request\UserWs\Version1\UserService\PasswordConfirm\UserService;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Sso\RestBundle\Api\Manager as ApiManager;
use JMS\Serializer\SerializerInterface;
use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;

/**
 * Class RequestTest
 *
 * @copyright  2016 Lifestyle Webconsulting GmbH
 * @link       http://www.life-style.de
 * @package Sso\RestBundle\Tests\ModelRest
 */
class RequestTest extends WebTestCase
{
    /**
     * @var SerializerInterface
     */
    private $serializer;

    /**
     * @var ApiManager
     */
    private $apiManager;

    /**
     * @var FakerGenerator
     */
    private $faker;

    /**
     * @var string
     */
    private $serviceToken;

    /**
     * @var string
     */
    private $serviceName;

    /**
     * @var string
     */
    private $userIdentifier;

    /**
     * @var string
     */
    private $username;

    /**
     * @var string
     */
    private $langCode;

    /**
     * @var string
     */
    private $identifier;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $active;

    /**
     * @var string
     */
    private $email;

    /**
     * @var string
     */
    private $token;

    /**
     * @var string
     */
    private $password;

    protected function setUp()
    {
        parent::setUp();
        static::bootKernel();
        $this->apiManager = new ApiManager(static::$kernel->getContainer());
        $this->serializer = $this->apiManager->serializer();
        $this->faker = FakerFactory::create();

        // Set same random data
        $this->serviceToken = $this->faker->sha256;
        $this->serviceName = $this->faker->word;
        $this->userIdentifier = $this->faker->md5;
        $this->username = $this->faker->userName;
        $this->langCode = $this->faker->languageCode;
        $this->identifier = $this->faker->email;
        $this->name = $this->faker->name;
        $this->active = $this->faker->boolean ? 1 : 0;
        $this->email = $this->faker->email;
        $this->token = $this->faker->sha256;
        $this->password = $this->faker->password(8);
    }

    public function testSerialize()
    {
        $this->assertXmlStringEqualsXmlString($this->getRequestBody(), $this->setRequestBody());
    }

    /**
     * @return string
     */
    private function setRequestBody()
    {
        $request = new Request();
        $credentials = new Credentials();
        $userService = new UserService();

        $credentials->setCredentialsData(
            $this->serviceToken,
            $this->serviceName,
            $this->userIdentifier,
            $this->username,
            $this->langCode
        );

        $userService->setUserService(
            $this->email,
            $this->token,
            $this->password
        );

        $request->setCredentials($credentials);
        $request->setUserService($userService);

        return $this->serializer->serialize($request, 'xml');
    }

    /**
     * @return string
     */
    private function getRequestBody()
    {
        return '<SsoRequest>
            <Credentials>
                <ServiceProvider>
                    <ServiceToken><![CDATA[' . $this->serviceToken . ']]></ServiceToken>
                    <ServiceName><![CDATA[' . $this->serviceName . ']]></ServiceName>
                </ServiceProvider>
                <ServiceTrigger>
                    <UserIdentifier><![CDATA[' . $this->userIdentifier . ']]></UserIdentifier>
                    <Username><![CDATA[' . $this->username . ']]></Username>
                </ServiceTrigger>
                <ServiceCountry>
                    <Code><![CDATA[' . $this->langCode . ']]></Code>
                </ServiceCountry>
            </Credentials>
            <UserService>
                <PasswordConfirm>
                    <Key>
                        <UserType>
                            <Email><![CDATA[' . $this->email . ']]></Email>
                            <PasswordToken><![CDATA[' . $this->token . ']]></PasswordToken>
                        </UserType>
                    </Key>
                    <Value>
                        <UserType>
                            <Password><![CDATA[' . $this->password . ']]></Password>
                        </UserType>
                    </Value>
                </PasswordConfirm>
            </UserService>
        </SsoRequest>';
    }

    public function testGetterUserType()
    {
        /** @var  Request $requestModel */
        $requestModel = $this->serializer->deserialize(
            $this->setRequestBody(),
            Request::class,
            'xml'
        );

        $userService = $requestModel->getUserService()->getPasswordConfirm();
        $keyUserType = $userService->getKey()->getUserType();
        $valueUserType = $userService->getValue()->getUserType();

        $credentials = $requestModel->getCredentials();

        $this->assertEquals($this->email, $keyUserType->getEmail());
        $this->assertEquals($this->token, $keyUserType->getPasswordToken());
        $this->assertEquals($this->password, $valueUserType->getPassword());
        $this->assertEquals($this->serviceToken, $credentials->getServiceProvider()->getServiceToken());
    }
}
