<commit_before><?php

namespace Chadicus;

/**
 * Trait for adding asserts for arrays
 */
trait ArrayAssertsTrait
{
    /**
     * Asserts the given $actual array is the same as the $expected array disregarding index order
     *
     * @param array       $expected The expected array.
     * @param mixed       $actual   The actual array.
     * @param string|null $prefix   Prefix to use with error messages. Useful for nested arrays.
     *
     * @return void
     */
    public function assertSameArray(array $expected, $actual, $prefix = null)
    {
        //assert that the actual value is an array
        $this->assertInternalType('array', $actual, '$actual was not an array');

        $expectedKeys = array_keys($expected);
        $actualKeys = array_keys($actual);

        //find any keys in the expected array that are not present in the actual array
        $missingExpectedKeys = array_diff($expectedKeys, $actualKeys);
        $this->assertCount(
            0,
            $missingExpectedKeys,
            sprintf(
                '$actual array is missing %d keys: %s',
                count($missingExpectedKeys),
                implode(', ', $missingExpectedKeys)
            )
        );

        //find any keys in the actual array that are not expected in the expected array
        $unexpectedKeys = array_diff($actualKeys, $expectedKeys);
        $this->assertCount(
            0,
            $unexpectedKeys,
            sprintf(
                '$actual array contains %d unexpected keys: %s',
                count($unexpectedKeys),
                implode(', ', $unexpectedKeys)
            )
        );

        //Assert all values are the same value and type.
        //Recursively call assertSameArray on array values
        foreach ($expected as $key => $value) {
            if (is_array($value)) {
                $this->assertSameArray($value, $actual[$key], "{$prefix}{$key}.");
                continue;
            }

            $this->assertSame(
                $value,
                $actual[$key],
                sprintf(
                    "{$prefix}{$key} value is not correct expected %s\nfound %s",
                    var_export($value, 1),
                    var_export($actual[$key], 1)
                )
            );
        }
    }

    /**
     * Asserts the number of elements of an array, Countable or Traversable.
     *
     * Ensures this method must be provided by classes using this trait.
     *
     * @param integer $expectedCount The expected number of items in $haystack.
     * @param mixed   $haystack      The array, countable or traversable object containing items.
     * @param string  $message       Optional error message to give upon failure.
     *
     * @return void
     */
    abstract public function assertCount($expectedCount, $haystack, $message = '');

    /**
     * Asserts that a variable is of a given type.
     *
     * Ensures this method must be provided by classes using this trait.
     *
     * @param string $expected The expected internal type.
     * @param mixed  $actual   The variable to verify.
     * @param string $message  Optional error message to give upon failure.
     *
     * @return void
     */
    abstract public function assertInternalType($expected, $actual, $message = '');
}
<commit_msg>Add abstract assertSame<commit_after><?php

namespace Chadicus;

/**
 * Trait for adding asserts for arrays
 */
trait ArrayAssertsTrait
{
    /**
     * Asserts the given $actual array is the same as the $expected array disregarding index order
     *
     * @param array       $expected The expected array.
     * @param mixed       $actual   The actual array.
     * @param string|null $prefix   Prefix to use with error messages. Useful for nested arrays.
     *
     * @return void
     */
    public function assertSameArray(array $expected, $actual, $prefix = null)
    {
        //assert that the actual value is an array
        $this->assertInternalType('array', $actual, '$actual was not an array');

        $expectedKeys = array_keys($expected);
        $actualKeys = array_keys($actual);

        //find any keys in the expected array that are not present in the actual array
        $missingExpectedKeys = array_diff($expectedKeys, $actualKeys);
        $this->assertCount(
            0,
            $missingExpectedKeys,
            sprintf(
                '$actual array is missing %d keys: %s',
                count($missingExpectedKeys),
                implode(', ', $missingExpectedKeys)
            )
        );

        //find any keys in the actual array that are not expected in the expected array
        $unexpectedKeys = array_diff($actualKeys, $expectedKeys);
        $this->assertCount(
            0,
            $unexpectedKeys,
            sprintf(
                '$actual array contains %d unexpected keys: %s',
                count($unexpectedKeys),
                implode(', ', $unexpectedKeys)
            )
        );

        //Assert all values are the same value and type.
        //Recursively call assertSameArray on array values
        foreach ($expected as $key => $value) {
            if (is_array($value)) {
                $this->assertSameArray($value, $actual[$key], "{$prefix}{$key}.");
                continue;
            }

            $this->assertSame(
                $value,
                $actual[$key],
                sprintf(
                    "{$prefix}{$key} value is not correct expected %s\nfound %s",
                    var_export($value, 1),
                    var_export($actual[$key], 1)
                )
            );
        }
    }

    /**
     * Asserts the number of elements of an array, Countable or Traversable.
     *
     * Ensures this method must be provided by classes using this trait.
     *
     * @param integer $expectedCount The expected number of items in $haystack.
     * @param mixed   $haystack      The array, countable or traversable object containing items.
     * @param string  $message       Optional error message to give upon failure.
     *
     * @return void
     */
    abstract public function assertCount($expectedCount, $haystack, $message = '');

    /**
     * Asserts that a variable is of a given type.
     *
     * Ensures this method must be provided by classes using this trait.
     *
     * @param string $expected The expected internal type.
     * @param mixed  $actual   The variable to verify.
     * @param string $message  Optional error message to give upon failure.
     *
     * @return void
     */
    abstract public function assertInternalType($expected, $actual, $message = '');

    /**
     * Asserts that two variables have the same type and value. Used on objects, it asserts that two variables reference
     * the same object.
     *
     * Ensures this method must be provided by classes using this trait.
     *
     * @param string $expected The expected value.
     * @param mixed  $actual   The actual value.
     * @param string $message  Optional error message to give upon failure.
     *
     * @return void
     */
    abstract public function assertSame($expected, $actual, $message = '');
}

{'fraction_non_alphanumeric': 0.09361939258846476, 'fraction_numerical': 0.0011145165784341043, 'mean_word_length': 1.9494658997534922, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '631655', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1787, 'n_words': 661}