<commit_before><?php

/*
 * This file is part of the Ivory Http Adapter bundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please read the LICENSE
 * file that was distributed with this source code.
 */

namespace Ivory\HttpAdapterBundle\Tests\DependencyInjection;

use Ivory\HttpAdapter\Event\Events;
use Ivory\HttpAdapterBundle\DependencyInjection\Compiler\RegisterListenerCompilerPass;
use Ivory\HttpAdapterBundle\DependencyInjection\IvoryHttpAdapterExtension;
use Ivory\HttpAdapterBundle\Tests\AbstractTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Abstract Ivory http adapter extension test.
 *
 * @author GeLo <<EMAIL>>
 */
abstract class AbstractIvoryHttpAdapterExtensionTest extends AbstractTestCase
{
    /** @var \Symfony\Component\DependencyInjection\ContainerBuilder */
    private $container;

    /**
     * {@inheritdoc}
     */
    protected function setUp()
    {
        $this->container = new ContainerBuilder();
        $this->container->setParameter('kernel.debug', false);
        $this->container->addCompilerPass(new RegisterListenerCompilerPass());
        $this->container->registerExtension($httpAdapter = new IvoryHttpAdapterExtension());
        $this->container->loadFromExtension($httpAdapter->getAlias());
    }

    /**
     * {@inheritdoc}
     */
    protected function tearDown()
    {
        unset($this->container);
    }

    /**
     * Loads a configuration.
     *
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container     The container.
     * @param string                                                  $configuration The configuration.
     */
    abstract protected function loadConfiguration(ContainerBuilder $container, $configuration);

    public function testDefaultHttpAdapter()
    {
        $this->container->compile();

        $this->assertInstanceOf(
            'Ivory\HttpAdapter\SocketHttpAdapter',
            $httpAdapter = $this->container->get('ivory.http_adapter')
        );

        $this->assertSame($httpAdapter, $this->container->get('ivory.http_adapter.default'));
        $this->assertSame($httpAdapter, $this->container->get('ivory.http_adapter.default.adapter'));

        $this->assertFalse($this->container->has('ivory.http_adapter.default.wrapper.stopwatch'));
        $this->assertFalse($this->container->has('ivory.http_adapter.default.wrapper.event_dispatcher'));
    }

    public function testDebugHttpAdapter()
    {
        $this->container->setParameter('kernel.debug', true);
        $this->container->set('debug.stopwatch', $this->createMock('Symfony\Component\Stopwatch\Stopwatch'));
        $this->container->compile();

        $this->assertInstanceOf(
            'Ivory\HttpAdapter\StopwatchHttpAdapter',
            $stopwatchHttpAdapter = $this->container->get('ivory.http_adapter')
        );

        $this->assertSame($stopwatchHttpAdapter, $this->container->get('ivory.http_adapter.default'));
        $this->assertSame($stopwatchHttpAdapter, $this->container->get('ivory.http_adapter.default.wrapper.stopwatch'));

        $this->assertInstanceOf(
            'Ivory\HttpAdapter\EventDispatcherHttpAdapter',
            $eventDispatcherHttpAdapter = $this->container->get('ivory.http_adapter.default.wrapper.event_dispatcher')
        );

        $stopwatchListener = $this->assertListener(
            $eventDispatcher = $this->container->get('ivory.http_adapter.default.event_dispatcher'),
            Events::REQUEST_CREATED,
            'Ivory\HttpAdapter\Event\Subscriber\StopwatchSubscriber',
            false
        );

        $this->assertSame($stopwatchListener->getStopwatch(), $this->container->get('debug.stopwatch'));

        $dataCollector = $this->assertListener(
            $eventDispatcher,
            Events::REQUEST_CREATED,
            'Ivory\HttpAdapterBundle\DataCollector\IvoryHttpAdapterDataCollector',
            false
        );

        $this->assertSame($dataCollector, $this->container->get('ivory.http_adapter.data_collector'));
    }

    /**
     * @dataProvider httpAdapterProvider
     */
    public function testHttpAdapter($configuration, $service, $class)
    {
        if (($configuration === 'guzzle4' && !class_exists('GuzzleHttp\Adapter\Curl\CurlAdapter'))
            || ($configuration === 'guzzle5' && !class_exists('GuzzleHttp\Ring\Client\CurlHandler'))
            || ($configuration === 'guzzle6') && !class_exists('GuzzleHttp\Handler\CurlHandler')
            || ($configuration === 'pecl_http') && !extension_loaded('pecl_http')) {
            $this->markTestSkipped();
        }

        $this->loadConfiguration($this->container, $configuration);
        $this->container->compile();

        $httpAdapter = $this->container->get($service);

        $this->assertInstanceOf($class, $httpAdapter);
        $this->assertSame($httpAdapter, $this->container->get('ivory.http_adapter'));
    }

    public function testGlobalProtocolVersion()
    {
        $this->loadConfiguration($this->container, 'global_protocol_version');
        $this->container->compile();

        $this->assertSame(<commit_msg>[Travis] Fix tests<commit_after><?php

/*
 * This file is part of the Ivory Http Adapter bundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please read the LICENSE
 * file that was distributed with this source code.
 */

namespace Ivory\HttpAdapterBundle\Tests\DependencyInjection;

use Ivory\HttpAdapter\Event\Events;
use Ivory\HttpAdapterBundle\DependencyInjection\Compiler\RegisterListenerCompilerPass;
use Ivory\HttpAdapterBundle\DependencyInjection\IvoryHttpAdapterExtension;
use Ivory\HttpAdapterBundle\Tests\AbstractTestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Abstract Ivory http adapter extension test.
 *
 * @author GeLo <<EMAIL>>
 */
abstract class AbstractIvoryHttpAdapterExtensionTest extends AbstractTestCase
{
    /** @var \Symfony\Component\DependencyInjection\ContainerBuilder */
    private $container;

    /**
     * {@inheritdoc}
     */
    protected function setUp()
    {
        $this->container = new ContainerBuilder();
        $this->container->setParameter('kernel.debug', false);
        $this->container->addCompilerPass(new RegisterListenerCompilerPass());
        $this->container->registerExtension($httpAdapter = new IvoryHttpAdapterExtension());
        $this->container->loadFromExtension($httpAdapter->getAlias());
    }

    /**
     * {@inheritdoc}
     */
    protected function tearDown()
    {
        unset($this->container);
    }

    /**
     * Loads a configuration.
     *
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container     The container.
     * @param string                                                  $configuration The configuration.
     */
    abstract protected function loadConfiguration(ContainerBuilder $container, $configuration);

    public function testDefaultHttpAdapter()
    {
        $this->container->compile();

        $this->assertInstanceOf(
            'Ivory\HttpAdapter\SocketHttpAdapter',
            $httpAdapter = $this->container->get('ivory.http_adapter')
        );

        $this->assertSame($httpAdapter, $this->container->get('ivory.http_adapter.default'));
        $this->assertSame($httpAdapter, $this->container->get('ivory.http_adapter.default.adapter'));

        $this->assertFalse($this->container->has('ivory.http_adapter.default.wrapper.stopwatch'));
        $this->assertFalse($this->container->has('ivory.http_adapter.default.wrapper.event_dispatcher'));
    }

    public function testDebugHttpAdapter()
    {
        $this->container->setParameter('kernel.debug', true);
        $this->container->set('debug.stopwatch', $this->createMock('Symfony\Component\Stopwatch\Stopwatch'));
        $this->container->compile();

        $this->assertInstanceOf(
            'Ivory\HttpAdapter\StopwatchHttpAdapter',
            $stopwatchHttpAdapter = $this->container->get('ivory.http_adapter')
        );

        $this->assertSame($stopwatchHttpAdapter, $this->container->get('ivory.http_adapter.default'));
        $this->assertSame($stopwatchHttpAdapter, $this->container->get('ivory.http_adapter.default.wrapper.stopwatch'));

        $this->assertInstanceOf(
            'Ivory\HttpAdapter\EventDispatcherHttpAdapter',
            $eventDispatcherHttpAdapter = $this->container->get('ivory.http_adapter.default.wrapper.event_dispatcher')
        );

        $stopwatchListener = $this->assertListener(
            $eventDispatcher = $this->container->get('ivory.http_adapter.default.event_dispatcher'),
            Events::REQUEST_CREATED,
            'Ivory\HttpAdapter\Event\Subscriber\StopwatchSubscriber',
            false
        );

        $this->assertSame($stopwatchListener->getStopwatch(), $this->container->get('debug.stopwatch'));

        $dataCollector = $this->assertListener(
            $eventDispatcher,
            Events::REQUEST_CREATED,
            'Ivory\HttpAdapterBundle\DataCollector\IvoryHttpAdapterDataCollector',
            false
        );

        $this->assertSame($dataCollector, $this->container->get('ivory.http_adapter.data_collector'));
    }

    /**
     * @dataProvider httpAdapterProvider
     */
    public function testHttpAdapter($configuration, $service, $class)
    {
        if (($configuration === 'guzzle3' && !class_exists('Guzzle\Common\Version'))
            || ($configuration === 'guzzle4' && !class_exists('GuzzleHttp\Adapter\Curl\CurlAdapter'))
            || ($configuration === 'guzzle5' && !class_exists('GuzzleHttp\Ring\Client\CurlHandler'))
            || ($configuration === 'guzzle6') && !class_exists('GuzzleHttp\Handler\CurlHandler')
            || ($configuration === 'pecl_http') && !extension_loaded('pecl_http')) {
            $this->markTestSkipped();
        }

        $this->loadConfiguration($this->container, $configuration);
        $this->container->compile();

        $httpAdapter = $this->container->get($service);

        $this->assertInstanceOf($class, $httpAdapter);
        $this->assertSame($httpAdapter, $this->container->get('ivory.http_adapter'));
    }

    public function testGlobalProtocolVersion()
    {
        $this->loadConfiguration($this->container, 'global_protocol_version');
        $this->container->compile();

        $this->assertSame(
{'fraction_non_alphanumeric': 0.1390728476821192, 'fraction_numerical': 0.0006718495057107208, 'mean_word_length': 3.3040066088393227, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 161, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3750934', 'n_tokens_mistral': 2792, 'n_tokens_neox': 2730, 'n_words': 386}