<?php

/**
 * Lifestyle Webconsulting GmbH
 *
 * LICENSE: This Software is the property of Lifestyle Webconsulting GmbH (Aschaffenburg, Germany)
 * and is private 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\WebserviceBundle\Tests\Services\UserApplication\ShowDetails;

use Sso\WebserviceBundle\Api\ApiManagerInterface;
use Sso\WebserviceBundle\Database\Webservice\User as UserRepository;
use Sso\WebserviceBundle\ErrorHandler\ErrorHandlerInterface;
use Sso\WebserviceBundle\Services\ApiVersionInflectorInterface;
use Sso\WebserviceBundle\Services\UserApplication\ShowDetails\Factory;
use Sso\WebserviceBundle\Services\UserApplication\ShowDetails\Handler;
use Sso\WebserviceBundle\Services\RequestInterface;
use Sso\WebserviceBundle\Services\ResponseBuilderInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
 * Class HandlerTest
 *
 * @copyright  2016 Lifestyle Webconsulting GmbH
 * @link       http://www.life-style.de
 * @package Sso\WebserviceBundle\Tests\Services\User\Show
 */
class HandlerTest extends WebTestCase
{
    /**
     * @var UserRepository
     */
    private $database;

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

    public function testHandleSuccess()
    {
        $faker = \Faker\Factory::create();
        $email = $faker->email;

        $apiVersion = 2;

        $apiVersionInflector = $this->createMock(ApiVersionInflectorInterface::class);
        $apiVersionInflector
            ->method('inflect')
            ->will(self::returnCallback(function($namespace, $classname) use ($apiVersion) {
                return $namespace . '\\Version' . $apiVersion . '\\' . $classname;
            }));
        $factory = new Factory($this->apiManager, $apiVersionInflector);

        $errorHandler = $this->createMock(ErrorHandlerInterface::class);
        $errorHandler
            ->method('hasErrors')
            ->will(self::returnValue(false));

        $request = $this->createMock(RequestInterface::class);
        $request
            ->expects(self::once())
            ->method('getRequestBody')
            ->will(self::returnValue($this->getRequestBody($email)));

        $handler = new Handler($factory, $this->database, $errorHandler);

        $responseBuilder = $handler->handle($request);

        self::assertInstanceOf(ResponseBuilderInterface::class, $responseBuilder);
    }

    /**
     * @param string $identifier
     * @return string
     */
    private function getRequestBody($identifier)
    {
        return '<?xml version="1.0"?>
                <SsoRequest>
                    <User>
                        <ShowDetails>
                            <Key>
                                <UserType>
                                    <Identifier>' . $identifier . '</Identifier>
                                </UserType>
                            </Key>
                        </ShowDetails>
                    </User>
                </SsoRequest>';
    }

    protected function setUp()
    {
        parent::setUp();
        static::bootKernel();
        $this->apiManager = static::$kernel->getContainer()->get('service_api_manager');
        $entityManager = $this->createMock(EntityManagerInterface::class);
        $validator = $this->createMock(ValidatorInterface::class);
        $readonly = false;
        $this->database = $this->createMock(UserRepository::class, array(), array($entityManager, $validator, $readonly));
    }

}
