<commit_before><?php
namespace Behapi\Context;

use stdClass;
use Datetime;

use Throwable;
use InvalidArgumentException;

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

use Behapi\Extension\Tools\Assert;

abstract class AbstractJson implements Context
{
    /** @var PropertyAccessor */
    private $accessor;

    public function __construct()
    {
        $this->accessor = PropertyAccess::createPropertyAccessor();
    }

    /**
     * Get the latest json response
     *
     * @return stdClass decoded json into an object
     */
    abstract protected function getJson(): stdClass;

    /**
     * Get the value for a path
     *
     * @param string $path Path to parse
     *
     * @return mixed
     * @throws AccessException path not valid
     */
    protected function getValue(string $path)
    {
        return $this->accessor->getValue($this->getJson(), $path);
    }

    /**
     * @Then the response should be a valid json response
     *
     * ---
     *
     * This method is built-on the default php's json extension. You should
     * overwrite it if you want to add supplementary checks or use something
     * else instead (such as Seldaek's JsonLint package).
     */
    public function responseIsValidjson()
    {
        $this->getJson();

        Assert::same(json_last_error(), JSON_ERROR_NONE, 'The latest json response should be a valid json response');
    }

    /** @Then :path should be accessible in the latest json response */
    public function pathShouldBeReadable(string $path)
    {
        Assert::true($this->accessor->isReadable($this->getJson(), $path), "The path $path should be a valid path");
    }

    /** @Then :path should not exist in the latest json response */
    public function pathShouldNotBeReadable(string $path)
    {
        Assert::false($this->accessor->isReadable($this->getJson(), $path), "The path $path should not be a valid path");
    }

    /** @Then in the json, :path should be equal to :expected */
    public function theJsonPathShouldBeEqualTo(string $path, $expected)
    {
        Assert::eq($this->getValue($path), $expected);
    }

    /** @Then in the json, :path should not be equal to :expected */
    public function theJsonPathShouldNotBeEqualTo(string $path, $expected)
    {
        Assert::notEq($this->getValue($path), $expected);
    }

    /** @Then in the json, :path should be: */
    public function theJsonPathShouldBePyString(string $path, PyStringNode $expected)
    {
        Assert::same($this->getValue($path)->getRaw(), $expected);
    }

    /** @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should be (?P<expected>true|false)$/ */
    public function theJsonPathShouldBe(string $path, string $expected)
    {
        Assert::same($this->getValue($path), 'true' === $expected);
    }

    /** @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should not be (?P<expected>true|false)$/ */
    public function theJsonPathShouldNotBe(string $path, string $expected)
    {
        Assert::notSame($this->getValue($path), 'true' === $expected);
    }

    /** @Then in the json, :path should be null */
    public function theJsonPathShouldBeNull(string $path)
    {
        Assert::null($this->getValue($path));
    }

    /** @Then in the json, :path should not be null */
    public function theJsonPathShouldNotBeNull(string $path)
    {<commit_msg>Fix #31: Move getRaw call on the right operand<commit_after><?php
namespace Behapi\Context;

use stdClass;
use Datetime;

use Throwable;
use InvalidArgumentException;

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessor;

use Behapi\Extension\Tools\Assert;

abstract class AbstractJson implements Context
{
    /** @var PropertyAccessor */
    private $accessor;

    public function __construct()
    {
        $this->accessor = PropertyAccess::createPropertyAccessor();
    }

    /**
     * Get the latest json response
     *
     * @return stdClass decoded json into an object
     */
    abstract protected function getJson(): stdClass;

    /**
     * Get the value for a path
     *
     * @param string $path Path to parse
     *
     * @return mixed
     * @throws AccessException path not valid
     */
    protected function getValue(string $path)
    {
        return $this->accessor->getValue($this->getJson(), $path);
    }

    /**
     * @Then the response should be a valid json response
     *
     * ---
     *
     * This method is built-on the default php's json extension. You should
     * overwrite it if you want to add supplementary checks or use something
     * else instead (such as Seldaek's JsonLint package).
     */
    public function responseIsValidjson()
    {
        $this->getJson();

        Assert::same(json_last_error(), JSON_ERROR_NONE, 'The latest json response should be a valid json response');
    }

    /** @Then :path should be accessible in the latest json response */
    public function pathShouldBeReadable(string $path)
    {
        Assert::true($this->accessor->isReadable($this->getJson(), $path), "The path $path should be a valid path");
    }

    /** @Then :path should not exist in the latest json response */
    public function pathShouldNotBeReadable(string $path)
    {
        Assert::false($this->accessor->isReadable($this->getJson(), $path), "The path $path should not be a valid path");
    }

    /** @Then in the json, :path should be equal to :expected */
    public function theJsonPathShouldBeEqualTo(string $path, $expected)
    {
        Assert::eq($this->getValue($path), $expected);
    }

    /** @Then in the json, :path should not be equal to :expected */
    public function theJsonPathShouldNotBeEqualTo(string $path, $expected)
    {
        Assert::notEq($this->getValue($path), $expected);
    }

    /** @Then in the json, :path should be: */
    public function theJsonPathShouldBePyString(string $path, PyStringNode $expected)
    {
        Assert::same($this->getValue($path), $expected->getRaw());
    }

    /** @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should be (?P<expected>true|false)$/ */
    public function theJsonPathShouldBe(string $path, string $expected)
    {
        Assert::same($this->getValue($path), 'true' === $expected);
    }

    /** @Then /^in the json, "(?P<path>(?:[^"]|\\")*)" should not be (?P<expected>true|false)$/ */
    public function theJsonPathShouldNotBe(string $path, string $expected)
    {
        Assert::notSame($this->getValue($path), 'true' === $expected);
    }

    /** @Then in the json, :path should be null */
    public function theJsonPathShouldBeNull(string $path)
    {
        Assert::null($this->getValue($path));
    }

    /** @Then in the json, :path should not be null */
    public function theJsonPathShouldNotBeNull(string $path)
    {
{'fraction_non_alphanumeric': 0.1332192269291114, 'fraction_numerical': 0.0002852660105548424, 'mean_word_length': 3.1174398120963005, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 47, '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': '95755', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1986, 'n_words': 594}