<commit_before><?php

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
use Sanpi\Behatch\Context\BehatchContext;
use Contentacle\Services\Yaml;

/**
 * Features context.
 */
class FeatureContext extends MinkContext
{
    public function __construct(array $parameters)
    {
        $this->useContext('behatch', new BehatchContext($parameters));
    }

    /**
     * @BeforeScenario
     */
    public function copyTestRepo()
    {
        $to = realpath(dirname(__FILE__).'/../../repos').'/';
        exec("rm -rf $to*");
        $from = realpath(dirname(__FILE__).'/../repos/peej');
        exec("cp -r $from $to 2> /dev/null");
        $from = realpath(dirname(__FILE__).'/../repos/empty');
        exec("cp -r $from $to 2> /dev/null");
    }

    /**
     * @BeforeScenario
     */
    public function setHeaders()
    {
        $this->getSession()->setRequestHeader('Accept', '*/*');
    }

    /**
     * Set HTTP header for next request
     *
     * @When /^I set the "(?P<header>[^"]*)" header to "(?P<value>[^"]*)"$/
     */
    public function iSetRequestHeaderAs($header, $value)
    {
        $this->getSession()->setRequestHeader($header, $value);
    }

    /**
     * @Then /^the "([^"]*)" response header should be "([^"]*)"$/
     */
    public function theResponseHeaderShouldBe($header, $value)
    {
        $headers = $this->getSession()->getResponseHeaders();
        if (!isset($headers[$header]) || $headers[$header] != array($value)) {
            throw new Exception;
        }
    }

    /**
     * @Then /^the response should have the property "([^"]*)" with the value "([^"]*)"$/
     * @Then /^response property "([^"]*)" should be "([^"]*)"$/
     */
    public function theResponseShouldHavePropertyWithValue($name, $value)
    {
        $session = $this->getSession();
        $response = $session->getPage()->getContent();
        $data = json_decode($response, true);
        if ($data == false) {
            $yaml = new Yaml;
            $data = $yaml->decode($response);
        }
        $parts = explode('->', $name);
        foreach ($parts as $part) {
            if (!isset($data[$part])) {
                throw new Exception;
            }
            $data = $data[$part];
        }
        if ($data != $value) {
            throw new Exception;
        }
    }

    /**
     * @Given /^response property "([^"]*)" should contain "([^"]*)"$/
     */
    public function responsePropertyShouldContain($name, $value)
    {
        $session = $this->getSession();
        $response = $session->getPage()->getContent();
        $data = json_decode($response, true);
        if ($data == false) {
            $yaml = new Yaml;
            $data = $yaml->decode($response);
        }
        $parts = explode('->', $name);
        foreach ($parts as $part) {
            if (!isset($data[$part])) {
                throw new Exception;
            }
            $data = $data[$part];
        }
        if (!is_array($data) || !in_array($value, $data)) {
            throw new Exception;
        }<commit_msg>Add additional context info to thrown exceptions<commit_after><?php

use Behat\Behat\Context\ClosuredContextInterface,
    Behat\Behat\Context\TranslatedContextInterface,
    Behat\Behat\Context\BehatContext,
    Behat\Behat\Exception\PendingException;
use Behat\Gherkin\Node\PyStringNode,
    Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
use Sanpi\Behatch\Context\BehatchContext;
use Contentacle\Services\Yaml;

/**
 * Features context.
 */
class FeatureContext extends MinkContext
{
    public function __construct(array $parameters)
    {
        $this->useContext('behatch', new BehatchContext($parameters));
    }

    /**
     * @BeforeScenario
     */
    public function copyTestRepo()
    {
        $to = realpath(dirname(__FILE__).'/../../repos').'/';
        exec("rm -rf $to*");
        $from = realpath(dirname(__FILE__).'/../repos/peej');
        exec("cp -r $from $to 2> /dev/null");
        $from = realpath(dirname(__FILE__).'/../repos/empty');
        exec("cp -r $from $to 2> /dev/null");
    }

    /**
     * @BeforeScenario
     */
    public function setHeaders()
    {
        $this->getSession()->setRequestHeader('Accept', '*/*');
    }

    /**
     * Set HTTP header for next request
     *
     * @When /^I set the "(?P<header>[^"]*)" header to "(?P<value>[^"]*)"$/
     */
    public function iSetRequestHeaderAs($header, $value)
    {
        $this->getSession()->setRequestHeader($header, $value);
    }

    /**
     * @Then /^the "([^"]*)" response header should be "([^"]*)"$/
     */
    public function theResponseHeaderShouldBe($header, $value)
    {
        $headers = $this->getSession()->getResponseHeaders();
        if (!isset($headers[$header]) || $headers[$header] != array($value)) {
            throw new Exception;
        }
    }

    /**
     * @Then /^the response should have the property "([^"]*)" with the value "([^"]*)"$/
     * @Then /^response property "([^"]*)" should be "([^"]*)"$/
     */
    public function theResponseShouldHavePropertyWithValue($name, $value)
    {
        $session = $this->getSession();
        $response = $session->getPage()->getContent();
        $data = json_decode($response, true);
        if ($data == false) {
            $yaml = new Yaml;
            $data = $yaml->decode($response);
        }
        $parts = explode('->', $name);
        foreach ($parts as $part) {
            if (!isset($data[$part])) {
                throw new Exception('Could not find '.$part);
            }
            $data = $data[$part];
        }
        if ($data != $value) {
            throw new Exception($name.' is "'.$data.'" not "'.$value.'"');
        }
    }

    /**
     * @Given /^response property "([^"]*)" should contain "([^"]*)"$/
     */
    public function responsePropertyShouldContain($name, $value)
    {
        $session = $this->getSession();
        $response = $session->getPage()->getContent();
        $data = json_decode($response, true);
        if ($data == false) {
            $yaml = new Yaml;
            $data = $yaml->decode($response);
        }
        $parts = explode('->', $name);
        foreach ($parts as $part) {
            if (!isset($data[$part])) {
                throw new Exception;
            }
            $data = $data[$part];
        }
        if (!is_array($data) || !in_array($value, $data)) {
            throw new Exception;
        }
{'fraction_non_alphanumeric': 0.18257756563245822, 'fraction_numerical': 0.0005966587112171838, 'mean_word_length': 2.2204610951008648, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 45, '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': '5509735', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1993, 'n_words': 405}