<commit_before><?php

/**
 * @author      <NAME> <<EMAIL>>
 * @copyright   Copyright (C) 2016.
 * @license     MIT
 */
namespace SerendipityHQ\Library\PHPUnit_Helper;

use Symfony\Component\PropertyAccess\PropertyAccess;

/**
 * A PHPUnit helper to better manage tested resources, mocked objects and test values.
 */
trait PHPUnitHelper
{
    /** @var  array The expected values */
    private $areExpected = [];

    /** @var array Contains all the mocked objects */
    private $helpMocks = [];

    /** @var array Contains all the collections of mocked objects */
    private $helpMocksCollections = [];

    /** @var array Contains the resources used by the test */
    private $helpResources = [];

    /** @var mixed The result of the test */
    private $helpResult;

    /** @var object The tested resource */
    private $objectToTest;

    /** @var array Contains the help values */
    private $helpValues = [];

    private $useReflection = false;
    private $memoryAfterTearDown;
    private $memoryBeforeTearDown;

    /**
     * Add an expected value.
     *
     * @param $name
     * @param $value
     *
     * @return $this
     */
    private function addExpectedValue($name, $value)
    {
        $this->areExpected[$name] = $value;

        return $this;
    }

    /**
     * Add a mock object.
     *
     * Use "Help" for consistency with getHelpMock.
     *
     * @param $id
     * @param \PHPUnit_Framework_MockObject_MockObject $class
     * @param bool                                     $addToExpected Define if the mock has to be added to the expected values
     *
     * @return $this
     */
    protected function addHelpMock($id, \PHPUnit_Framework_MockObject_MockObject $class, $addToExpected = false)
    {
        $this->helpMocks[$id] = $class;

        if ($addToExpected) {
            $this->addExpectedValue($id, $this->getHelpMock($id));
        }

        return $this;
    }

    /**
     * @param $id
     * @param array $collection
     * @param bool  $addToExpected
     * @param bool   $overwrite Defines if a resource can be overwritten or not
     *
     * @return $this
     */
    protected function addHelpMocksCollection($id, array $collection, $addToExpected = false, $overwrite = false)
    {
        if (isset($this->helpMocksCollections[$id]) && false === $overwrite)
            throw new \LogicException('The Mocks Collection you are trying to add is already set. Set the fourth parameter to "true" to overwrite it.');

        $this->helpMocksCollections[$id] = $collection;

        if ($addToExpected) {
            $this->addExpectedValue($id, $this->getHelpMocksCollection($id));
        }

        return $this;
    }

    /**
     * Add a resource to help during the test of the class.
     *
     * @param string $id        The name of the resource
     * @param mixed  $resource  The resource
     * @param bool   $overwrite Defines if a resource can be overwritten or not
     *
     * @return $this
     */
    protected function addHelpResource($id, $resource, $overwrite = false)
    {
        if (isset($this->helpResources[$id]) && false === $overwrite) {
            throw new \LogicException('The resource you are trying to add is already set. Set the third parameter to "true" to overwrite it.');
        }

        if (false === is_object(($resource))) {
            throw new \InvalidArgumentException(sprintf('The resource "%s" you are trying to add is not an object. addResource() accepts only objects. Use addHelpValue() to store other kind of values.', $id));
        }

        $this->helpResources[$id] = $resource;

        return $this;
    }

    /**
     * Add a value used in tests.
     *
     * @param string $id
     * @param mixed $value
     * @param bool $addToExpected Define if the mock has to be added to the expected values
     * @param $overwrite bool If false, the result isn't overwritten
     *
     * @return $this
     */
    protected function addHelpValue($id, $value, $addToExpected = false, $overwrite = false)
    {
        if ($value instanceof \PHPUnit_Framework_MockObject_MockObject) {
            throw new \LogicException(sprintf('The HelpValue with ID "%s" you are trying to add is a mock object. Use $this->addHelpMock() instead.', $id));
        }

        if (isset($this->helpValues[$id]) && false === $overwrite)
            throw new \LogicException('The HelpValue you are trying to add is already set. Set the fourth parameter to "true" to overwrite it.');

        if (isset($this->helpValues[$id])) {
            throw new \LogicException('Another HelpValue with ID "%s" is already set.', $id);
        }

        $this->helpValues[$id] = $value;

        if ($addToExpected) {
            $this->addExpectedValue($id, $this->getHelpValue($id));
        }

        return $this;
    }

    /**
     * Automatically set the properties of the Resource with expected values.
     *
     * @return $this<commit_msg>Make `addHelpValue()` accept all except `object`s.<commit_after><?php

/**
 * @author      <NAME> <<EMAIL>>
 * @copyright   Copyright (C) 2016.
 * @license     MIT
 */
namespace SerendipityHQ\Library\PHPUnit_Helper;

use Symfony\Component\PropertyAccess\PropertyAccess;

/**
 * A PHPUnit helper to better manage tested resources, mocked objects and test values.
 */
trait PHPUnitHelper
{
    /** @var  array The expected values */
    private $areExpected = [];

    /** @var array Contains all the mocked objects */
    private $helpMocks = [];

    /** @var array Contains all the collections of mocked objects */
    private $helpMocksCollections = [];

    /** @var array Contains the resources used by the test */
    private $helpResources = [];

    /** @var mixed The result of the test */
    private $helpResult;

    /** @var object The tested resource */
    private $objectToTest;

    /** @var array Contains the help values */
    private $helpValues = [];

    private $useReflection = false;
    private $memoryAfterTearDown;
    private $memoryBeforeTearDown;

    /**
     * Add an expected value.
     *
     * @param $name
     * @param $value
     *
     * @return $this
     */
    private function addExpectedValue($name, $value)
    {
        $this->areExpected[$name] = $value;

        return $this;
    }

    /**
     * Add a mock object.
     *
     * Use "Help" for consistency with getHelpMock.
     *
     * @param $id
     * @param \PHPUnit_Framework_MockObject_MockObject $class
     * @param bool                                     $addToExpected Define if the mock has to be added to the expected values
     *
     * @return $this
     */
    protected function addHelpMock($id, \PHPUnit_Framework_MockObject_MockObject $class, $addToExpected = false)
    {
        $this->helpMocks[$id] = $class;

        if ($addToExpected) {
            $this->addExpectedValue($id, $this->getHelpMock($id));
        }

        return $this;
    }

    /**
     * @param $id
     * @param array $collection
     * @param bool  $addToExpected
     * @param bool   $overwrite Defines if a resource can be overwritten or not
     *
     * @return $this
     */
    protected function addHelpMocksCollection($id, array $collection, $addToExpected = false, $overwrite = false)
    {
        if (isset($this->helpMocksCollections[$id]) && false === $overwrite)
            throw new \LogicException('The Mocks Collection you are trying to add is already set. Set the fourth parameter to "true" to overwrite it.');

        $this->helpMocksCollections[$id] = $collection;

        if ($addToExpected) {
            $this->addExpectedValue($id, $this->getHelpMocksCollection($id));
        }

        return $this;
    }

    /**
     * Add a resource to help during the test of the class.
     *
     * @param string $id        The name of the resource
     * @param mixed  $resource  The resource
     * @param bool   $overwrite Defines if a resource can be overwritten or not
     *
     * @return $this
     */
    protected function addHelpResource($id, $resource, $overwrite = false)
    {
        if (isset($this->helpResources[$id]) && false === $overwrite) {
            throw new \LogicException('The resource you are trying to add is already set. Set the third parameter to "true" to overwrite it.');
        }

        if (false === is_object(($resource))) {
            throw new \InvalidArgumentException(sprintf('The resource "%s" you are trying to add is not an object. addResource() accepts only objects. Use addHelpValue() to store other kind of values.', $id));
        }

        $this->helpResources[$id] = $resource;

        return $this;
    }

    /**
     * Add a value used in tests.
     *
     * @param string $id
     * @param mixed $value
     * @param bool $addToExpected Define if the mock has to be added to the expected values
     * @param $overwrite bool If false, the result isn't overwritten
     *
     * @return $this
     */
    protected function addHelpValue($id, $value, $addToExpected = false, $overwrite = false)
    {
        if (is_object($value)) {
            throw new \LogicException(sprintf('The HelpValue with ID "%s" you are trying to add is an object. Use $this->addHelpMock() instead.', $id));
        }

        if (isset($this->helpValues[$id]) && false === $overwrite)
            throw new \LogicException('The HelpValue you are trying to add is already set. Set the fourth parameter to "true" to overwrite it.');

        if (isset($this->helpValues[$id])) {
            throw new \LogicException('Another HelpValue with ID "%s" is already set.', $id);
        }

        $this->helpValues[$id] = $value;

        if ($addToExpected) {
            $this->addExpectedValue($id, $this->getHelpValue($id));
        }

        return $this;
    }

    /**
     * Automatically set the properties of the Resource with expected values.
     *
     * @return $this
{'fraction_non_alphanumeric': 0.11594496155402671, 'fraction_numerical': 0.0008093889113719142, 'mean_word_length': 2.407445708376422, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, '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': '1641171', 'n_tokens_mistral': 2791, 'n_tokens_neox': 2725, 'n_words': 919}