<commit_before><?php
/*
 * This file is part of the FreshSinchBundle
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Fresh\SinchBundle\Tests\Service;

use Fresh\SinchBundle\Service\SinchExceptionResolver;
use use GuzzleHttp\Exception\ClientException;

/**
 * FreshSinchExtensionTest
 *
 * @author <NAME> <<EMAIL>>
 */
class SinchExceptionResolverTest extends \PHPUnit_Framework_TestCase
{
//    /**
//     * @var FreshSinchExtension $extension FreshSinchExtension
//     */
//    private $sinchExceptionResolver;
//
//    /**
//     * {@inheritdoc}
//     */
//    protected function setUp()
//    {
//        $this->extension = new FreshSinchExtension();
//        $this->container = new ContainerBuilder();
//        $this->container->registerExtension($this->extension);
//    }

    /**
     * Test load extension
     *
     * @expectedException SinchParameterValidationException
     */
    public function testLoadExtension()
    {
        $yaml = <<<EOF
fresh_sinch:
    key: some_dummy_key
    secret: some_dummy_secret
EOF;
        $parser = new Parser();
        $config = $parser->parse($yaml);

        $this->extension->load($config, $this->container);
        $this->container->loadFromExtension($this->extension->getAlias(), $config['fresh_sinch']);
        $this->container->compile();

        // Check loaded resources
        $resources = $this->container->getResources();
        $resourceList = [];
        foreach ($resources as $resource) {
            if ($resource instanceof FileResource) {
                $path = $resource->getResource();
                $resourceList[] = substr($path, strrpos($path, '/') + 1);
            }
        }
        $this->assertContains('services.yml', $resourceList);

        // Check auto generated parameters
        $this->assertTrue($this->container->hasParameter('sinch.host'));
        $this->assertTrue($this->container->hasParameter('sinch.key'));
        $this->assertTrue($this->container->hasParameter('sinch.secret'));

        // Check that service has been loaded
        $this->assertTrue($this->container->has('sinch'));
    }
}<commit_msg>Improve SinchExceptionResolverTest<commit_after><?php
/*
 * This file is part of the FreshSinchBundle
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Fresh\SinchBundle\Tests\Service;

use Fresh\SinchBundle\Helper\SinchErrorCode;
use Fresh\SinchBundle\Service\SinchExceptionResolver;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\HttpFoundation\Response;

/**
 * FreshSinchExtensionTest
 *
 * @author <NAME> <<EMAIL>>
 */
class SinchExceptionResolverTest extends \PHPUnit_Framework_TestCase
{
    // region Bad Request exceptions

    /**
     * Test SinchParameterValidationException
     */
    public function testSinchParameterValidationException()
    {
        $e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::PARAMETER_VALIDATION);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\BadRequest\SinchParameterValidationException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    /**
     * Test SinchInvalidRequestException
     */
    public function testSinchInvalidRequestException()
    {
        $e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::INVALID_REQUEST);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\BadRequest\SinchInvalidRequestException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    /**
     * Test SinchMissingParameterException
     */
    public function testSinchMissingParameterException()
    {
        $e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::MISSING_PARAMETER);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\BadRequest\SinchMissingParameterException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    // endregion

    // region Unauthorized exceptions

    /**
     * Test SinchIllegalAuthorizationHeaderException
     */
    public function testSinchIllegalAuthorizationHeaderException()
    {
        $e = $this->getClientException(Response::HTTP_UNAUTHORIZED, SinchErrorCode::ILLEGAL_AUTHORIZATION_HEADER);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\Unauthorized\SinchIllegalAuthorizationHeaderException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    // endregion

    // region Payment Required exceptions

    /**
     * Test SinchPaymentRequiredException
     */
    public function testSinchPaymentRequiredException()
    {
        $e = $this->getClientException(Response::HTTP_PAYMENT_REQUIRED, SinchErrorCode::THERE_IS_NOT_ENOUGH_FUNDS_TO_SEND_THE_MESSAGE);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\PaymentRequired\SinchPaymentRequiredException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    // endregion

    // region Forbidden exceptions

    /**
     * Test SinchForbiddenRequestException
     */
    public function testSinchForbiddenRequestException()
    {
        $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::FORBIDDEN_REQUEST);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\Forbidden\SinchForbiddenRequestException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    /**
     * Test SinchInvalidAuthorizationSchemeException
     */
    public function testSinchInvalidAuthorizationSchemeException()
    {
        $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::INVALID_AUTHORIZATION_SCHEME_FOR_CALLING_THE_METHOD);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\Forbidden\SinchInvalidAuthorizationSchemeException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    /**
     * Test SinchNoVerifiedPhoneNumberException
     */
    public function testSinchNoVerifiedPhoneNumberException()
    {
        $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::NO_VERIFIED_PHONE_NUMBER_ON_YOUR_SINCH_ACCOUNT);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\Forbidden\SinchNoVerifiedPhoneNumberException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );

        $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::SANDBOX_SMS_ONLY_ALLOWED_TO_BE_SENT_TO_VERIFIED_NUMBERS);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\Forbidden\SinchNoVerifiedPhoneNumberException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    // endregion

    // region Internal Server Error exceptions

    /**
     * Test SinchInternalErrorException
     */
    public function testSinchInternalErrorException()
    {
        $e = $this->getClientException(Response::HTTP_INTERNAL_SERVER_ERROR, SinchErrorCode::INTERNAL_ERROR);

        $this->assertInstanceOf(
            '\Fresh\SinchBundle\Exception\InternalServerError\SinchInternalErrorException',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    // endregion

    // region Standard exceptions

    /**
     * Test standard exception
     */
    public function testStandardException()
    {
        $e = $this->getClientException(Response::HTTP_GATEWAY_TIMEOUT, 0);

        $this->assertInstanceOf(
            '\Exception',
            SinchExceptionResolver::createAppropriateSinchException($e)
        );
    }

    // endregion

    /**
     * Get client exception
     *
     * @param int $statusCode Status code
     * @param int $errorCode  Error code
     *
     * @return ClientException
     */
    private function getClientException($statusCode, $errorCode)
    {
        $request  = $this->getMockBuilder('Psr\Http\Message\RequestInterface')->disableOriginalConstructor()->getMock();
        $response = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->disableOriginalConstructor()->getMock();
        $body     = $this->getMockBuilder('Psr\Http\Message\StreamInterface')->disableOriginalConstructor()->getMock();

        $response->expects($this->once())->method('getStatusCode')->willReturn($statusCode);
        $response->expects($this->once())->method('getBody')->will($this->returnValue($body));

        $body->expects($this->once())->method('getContents')->will($this->returnValue(<<<JSON
{
    "errorCode": $errorCode,
    "message": "Some message"
}
JSON
        ));


        return new ClientException(null, $request, $response);
    }
}
{'fraction_non_alphanumeric': 0.11663162553944893, 'fraction_numerical': 0.00022131238242779683, 'mean_word_length': 3.2431924882629106, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1680460', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2441, 'n_words': 357}