<commit_before><?php

namespace Draw\Bundle\DrawTestHelperBundle\Helper;

class JsonHelper extends BaseRequestHelper
{
    /**
     * The body to json_encode and send as the request
     *
     * @var mixed
     */
    private $body;

    /**
     * Use assoc on json decode
     *
     * The default value is true since it's easier to validate with assertSame when using array
     *
     * @see RequestHelper::executeAndJsonDecode
     *
     * @var boolean
     */
    private $jsonDecodeAssoc = true;

    /**
     * @var PropertyHelper[]
     */
    private $propertyHelpers = array();

    /**
     * @return boolean
     */
    public function getJsonDecodeAssoc()
    {
        return $this->jsonDecodeAssoc;
    }

    /**
     * @param boolean $jsonDecodeAssoc
     *
     * @return $this
     */
    public function setJsonDecodeAssoc($jsonDecodeAssoc)
    {
        $this->jsonDecodeAssoc = $jsonDecodeAssoc;

        return $this;
    }

    protected function initialize()
    {
        $this->requestHelper->setServerParameter('HTTP_ACCEPT', 'application/json');
        $this->requestHelper->setServerParameter('CONTENT_TYPE', 'application/json');
        $this->requestHelper->expectContentType('application/json');
        $this->requestHelper->asserting(array($this, 'assert'));

        $this->requestHelper->addListener(
            RequestHelper::EVENT_PRE_REQUEST,
            function (RequestHelperEvent $event) {
                if (!is_null($this->body)) {
                    $event->setBody(json_encode($this->body));
                }
            }
        );
    }

    public function assert(RequestHelper $requestHelper)
    {
        $data = $this->jsonDecode($requestHelper, false);
        foreach ($this->propertyHelpers as $propertyHelper) {
            $propertyHelper->assert($data);
        }
    }

    private function jsonDecode(RequestHelper $requestHelper, $assoc)
    {
        return json_decode($requestHelper->getClient()->getResponse()->getContent(), $assoc);
    }

    public function executeAndJsonDecode()
    {
        return $this->jsonDecode($this->requestHelper->execute(), $this->jsonDecodeAssoc);
    }

    /**
     * @return PropertyHelper
     */
    public function propertyHelper()
    {
        $this->propertyHelpers[] = $propertyHelper = PropertyHelper::instantiate($this->requestHelper);

        return $propertyHelper;
    }

    /**
     * @param $body
     * @return $this
     */
    public function withBody($body)
    {<commit_msg>now use json property helper and supprot for assertContentEqualsJsonString<commit_after><?php

namespace Draw\Bundle\DrawTestHelperBundle\Helper;

class JsonHelper extends BaseRequestHelper
{
    /**
     * The body to json_encode and send as the request
     *
     * @var mixed
     */
    private $body;

    /**
     * Use assoc on json decode
     *
     * The default value is true since it's easier to validate with assertSame when using array
     *
     * @see RequestHelper::executeAndJsonDecode
     *
     * @var boolean
     */
    private $jsonDecodeAssoc = true;

    /**
     * @var PropertyHelper[]
     */
    private $propertyHelpers = array();

    /**
     * @var null|string
     */
    private $expectedJsonString = null;

    /**
     * @return boolean
     */
    public function getJsonDecodeAssoc()
    {
        return $this->jsonDecodeAssoc;
    }

    /**
     * @param boolean $jsonDecodeAssoc
     *
     * @return $this
     */
    public function setJsonDecodeAssoc($jsonDecodeAssoc)
    {
        $this->jsonDecodeAssoc = $jsonDecodeAssoc;

        return $this;
    }

    protected function initialize()
    {
        $this->requestHelper->setServerParameter('HTTP_ACCEPT', 'application/json');
        $this->requestHelper->setServerParameter('CONTENT_TYPE', 'application/json');
        $this->requestHelper->expectContentType('application/json');
        $this->requestHelper->asserting(array($this, 'assert'));

        $this->requestHelper->addListener(
            RequestHelper::EVENT_PRE_REQUEST,
            function (RequestHelperEvent $event) {
                if (!is_null($this->body)) {
                    $event->setBody(json_encode($this->body));
                }
            }
        );
    }

    public function assert(RequestHelper $requestHelper)
    {
        if($this->expectedJsonString) {
            $requestHelper->getTestCase()
                ->assertJsonStringEqualsJsonString(
                    $this->expectedJsonString,
                    $requestHelper->getClient()->getResponse()->getContent()
                );
        }

        $data = $this->jsonDecode($requestHelper, false);
        foreach ($this->propertyHelpers as $propertyHelper) {
            $propertyHelper->assert($data);
        }
    }

    private function jsonDecode(RequestHelper $requestHelper, $assoc)
    {
        return json_decode($requestHelper->getClient()->getResponse()->getContent(), $assoc);
    }

    public function executeAndJsonDecode()
    {
        return $this->jsonDecode($this->requestHelper->execute(), $this->jsonDecodeAssoc);
    }

    public function assertContentEqualsJsonString($expectedJsonString)
    {
        $this->expectedJsonString = $expectedJsonString;

        return $this;
    }

    /**
     * @return JsonRequestPropertyHelper
     */
    public function propertyHelper($path = null)
    {
        $this->propertyHelpers[] = $propertyHelper = JsonRequestPropertyHelper::instantiate($this->requestHelper);

        if(!is_null($path)) {
            $propertyHelper->setPath($path);
        }
        return $propertyHelper;
    }

    /**
     * @param $body
     * @return $this
     */
    public function withBody($body)
    {
{'fraction_non_alphanumeric': 0.12096351893873276, 'fraction_numerical': 0.0, 'mean_word_length': 2.352837916910474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, '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': '2602215', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1518, 'n_words': 292}