<commit_before><?php

namespace atoum\AtoumBundle\Test\Units;

use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * WebTestCase
 *
 * @uses Test
 * @author <NAME> <<EMAIL>>
 */
abstract class WebTestCase extends Test
{
    protected static $class;
    protected static $kernel;

    /**
     * Creates a Client.
     *
     * @param array $options An array of options to pass to the createKernel class
     * @param array $server  An array of server parameters
     *
     * @return Client A Client instance
     */
    protected static function createClient(array $options = array(), array $server = array())
    {
        if (null !== static::$kernel) {
            static::$kernel->shutdown();
        }

        static::$kernel = static::createKernel($options);
        static::$kernel->boot();

        $client = static::$kernel->getContainer()->get('test.client');
        $client->setServerParameters($server);

        return $client;
    }

    /**
     * Creates a Kernel.
     *
     * Available options:
     *
     *  * environment
     *  * debug
     *
     * @param array $options An array of options
     *
     * @return HttpKernelInterface A HttpKernelInterface instance
     */
    protected static function createKernel(array $options = array())
    {
        if (null === static::$class) {
            static::$class = static::getKernelClass();
        }

        return new static::$class(
            isset($options['environment']) ? $options['environment'] : 'test',
            isset($options['debug']) ? $options['debug'] : true
        );
    }

    /**
     * Attempts to guess the kernel location.
     *
     * When the Kernel is located, the file is required.
     *
     * @return string The Kernel class name
     */
    protected static function getKernelClass()
    {
        $dir = self::getKernelDirectory();

        $finder = new Finder();
        $finder->name('*Kernel.php')->depth(0)->in($dir);
        $results = iterator_to_array($finder);
        if (!count($results)) {
            throw new \RuntimeException('Impossible to find a Kernel file, override the WebTestCase::getKernelDirectory() method or WebTestCase::createKernel() method.');
        }

        $file = current($results);
        $class = $file->getBasename('.php');

        require_once $file;

        return $class;
    }

    /**
     * Override this method if needed
     *
     * @return string
     */
    protected static function getKernelDirectory()
    {
        $dir = getcwd().'/app';
        if (!is_dir($dir)) {
            $dir = dirname($dir);
        }

        return $dir;
    }<commit_msg>Wires asserters to WebTestCase<commit_after><?php

namespace atoum\AtoumBundle\Test\Units;

use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use atoum\AtoumBundle\Test\Asserters;

/**
 * WebTestCase
 *
 * @uses Test
 * @author <NAME> <<EMAIL>>
 */
abstract class WebTestCase extends Test
{
    /** @var $string */
    protected $class;

    /** @var \Symfony\Component\HttpFoundation\HttpKernelInterface */
    protected $kernel;

    public function __construct(\mageekguy\atoum\factory $factory = null)
    {
        parent::__construct($factory);

        $generator = $this->getAsserterGenerator();
        $test = $this;
        $crawler = null;
        $client = null;

        $this->getAssertionManager()
            ->setHandler(
                'request',
                function(array $options = array(), array $server = array()) use(& $client, $test, $generator) {
                    $client = $test->createClient($options, $server);

                    return $test;
                }
            )
            ->setHandler('get', $get = $this->getSendRequestHandler($client, $crawler, 'GET'))
            ->setHandler('GET', $get)
            ->setHandler('head', $head = $this->getSendRequestHandler($client, $crawler, 'HEAD'))
            ->setHandler('HEAD', $head)
            ->setHandler('post', $post = $this->getSendRequestHandler($client, $crawler, 'POST'))
            ->setHandler('POST', $post)
            ->setHandler('put', $put = $this->getSendRequestHandler($client, $crawler, 'PUT'))
            ->setHandler('PUT', $put)
            ->setHandler('delete', $delete = $this->getSendRequestHandler($client, $crawler, 'DELETE'))
            ->setHandler('DELETE', $delete)
            ->setHandler('options', $options = $this->getSendRequestHandler($client, $crawler, 'OPTIONS'))
            ->setHandler('OPTIONS', $options)
            ->setHandler(
                'crawler',
                function() use(& $crawler, $generator) {
                    $asserter = new Asserters\Crawler($generator);

                    return $asserter->setWith($crawler);
                }
            )
        ;

    }

    /**
     * @param \Symfony\Bundle\FrameworkBundle\Client $client
     * @param \Symfony\Component\DomCrawler\Crawler  $crawler
     * @param string                                 $method
     *
     * @return callable
     */
    protected function getSendRequestHandler(& $client, & $crawler, $method)
    {
        $generator = $this->getAsserterGenerator();

        return function($path, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true) use(& $client, & $crawler, $method, $generator)
        {
            /** @var $client \Symfony\Bundle\FrameworkBundle\Client */
            $crawler = $client->request($method, $path, $parameters, $files, $server, $content, $changeHistory);
            $asserter = new Asserters\Response($generator);

            return $asserter->setWith($client->getResponse());
        };
    }

    /**
     * Creates a Client.
     *
     * @param array $options An array of options to pass to the createKernel class
     * @param array $server  An array of server parameters
     *
     * @return Client A Client instance
     */
    public function createClient(array $options = array(), array $server = array())
    {
        if (null !== $this->kernel) {
            $this->kernel->shutdown();
        }

        $this->kernel = $this->createKernel($options);
        $this->kernel->boot();

        $client = $this->kernel->getContainer()->get('test.client');
        $client->setServerParameters($server);

        return $client;
    }

    /**
     * Creates a Kernel.
     *
     * Available options:
     *
     *  * environment
     *  * debug
     *
     * @param array $options An array of options
     *
     * @return HttpKernelInterface A HttpKernelInterface instance
     */
    protected function createKernel(array $options = array())
    {
        if (null === $this->class) {
            $this->class = $this->getKernelClass();
        }

        return new $this->class(
            isset($options['environment']) ? $options['environment'] : 'test',
            isset($options['debug']) ? $options['debug'] : true
        );
    }

    /**
     * Attempts to guess the kernel location.
     *
     * When the Kernel is located, the file is required.
     *
     * @return string The Kernel class name
     */
    protected function getKernelClass()
    {
        $dir = self::getKernelDirectory();

        $finder = new Finder();
        $finder->name('*Kernel.php')->depth(0)->in($dir);
        $results = iterator_to_array($finder);
        if (!count($results)) {
            throw new \RuntimeException('Impossible to find a Kernel file, override the WebTestCase::getKernelDirectory() method or WebTestCase::createKernel() method.');
        }

        $file = current($results);
        $class = $file->getBasename('.php');

        require_once $file;

        return $class;
    }

    /**
     * Override this method if needed
     *
     * @return string
     */
    protected function getKernelDirectory()
    {
        $dir = getcwd().'/app';
        if (!is_dir($dir)) {
            $dir = dirname($dir);
        }

        return $dir;
    }
{'fraction_non_alphanumeric': 0.14181952480610613, 'fraction_numerical': 0.0002462144527883787, 'mean_word_length': 2.231503579952267, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5598909', 'n_tokens_mistral': 2264, 'n_tokens_neox': 2224, 'n_words': 527}