<commit_before><?php

class ContextCheckerTest extends \PHPUnit_Framework_TestCase
{
    const TEST_FILE_EXTENSION = '.test';

    /**
     * @dataProvider messageProvider
     */
    public function testMessages($name, $code, $expectedMessages)
    {
        $containerBuilder = new \Sstalle\php7cc\Infrastructure\ContainerBuilder();
        $container = $containerBuilder->buildContainer(
            new Symfony\Component\Console\Output\NullOutput(),
            \Sstalle\php7cc\NodeVisitor\BitwiseShiftVisitor::MIN_INT_SIZE
        );
        $contextChecker = $container['contextChecker'];
        /** @var \PhpParser\NodeTraverserInterface $traverser */
        $traverser = $container['traverser'];
        /** @var \Sstalle\php7cc\NodeVisitor\ResolverInterface $resolver */
        $resolver = $container['nodeVisitorResolver'];
        $resolver->setLevel(\Sstalle\php7cc\CompatibilityViolation\Message::LEVEL_INFO);
        foreach ($resolver->resolve() as $visitor) {
            $traverser->addVisitor($visitor);
        }

        $context = new \Sstalle\php7cc\CompatibilityViolation\StringContext($code, 'test');

        $contextChecker->checkContext($context);
        $expectedMessageCount = count($expectedMessages);
        $actualMessages = array_merge($context->getMessages(), $context->getErrors());
        $actualMessageCount = count($actualMessages);
        $this->assertEquals($expectedMessageCount, $actualMessageCount, $name);
        if ($expectedMessageCount == $actualMessageCount) {
            /** @var \Sstalle\php7cc\AbstractBaseMessage $message */
            foreach ($actualMessages as $i => $message) {
                $this->assertEquals(
                    $this->canonicalize($expectedMessages[$i]),
                    $this->canonicalize($message->getRawText()),
                    $name
                );
            }
        }
    }

    /**
     * Copypasted from PhpParser\CodeTestAbstract.
     *
     * @return array
     */
    public function messageProvider()
    {
        $it = new \RecursiveDirectoryIterator(__DIR__ . '/../resource');
        $it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
        $it = new \RegexIterator($it, '(\.' . preg_quote('test') . '$)');
        $tests = array();<commit_msg>Add messages produced by checks to test output<commit_after><?php

class ContextCheckerTest extends \PHPUnit_Framework_TestCase
{
    const TEST_FILE_EXTENSION = '.test';

    /**
     * @dataProvider messageProvider
     */
    public function testMessages($name, $code, $expectedMessages)
    {
        $containerBuilder = new \Sstalle\php7cc\Infrastructure\ContainerBuilder();
        $container = $containerBuilder->buildContainer(
            new Symfony\Component\Console\Output\NullOutput(),
            \Sstalle\php7cc\NodeVisitor\BitwiseShiftVisitor::MIN_INT_SIZE
        );
        $contextChecker = $container['contextChecker'];
        /** @var \PhpParser\NodeTraverserInterface $traverser */
        $traverser = $container['traverser'];
        /** @var \Sstalle\php7cc\NodeVisitor\ResolverInterface $resolver */
        $resolver = $container['nodeVisitorResolver'];
        $resolver->setLevel(\Sstalle\php7cc\CompatibilityViolation\Message::LEVEL_INFO);
        foreach ($resolver->resolve() as $visitor) {
            $traverser->addVisitor($visitor);
        }

        $context = new \Sstalle\php7cc\CompatibilityViolation\StringContext($code, 'test');
        $contextChecker->checkContext($context);

        $expectedMessageCount = count($expectedMessages);
        $actualMessages = array_map(function (\Sstalle\php7cc\AbstractBaseMessage $message) {
            return $message->getRawText();
        }, array_merge($context->getMessages(), $context->getErrors()));
        $actualMessageCount = count($actualMessages);
        $combinedActualMessages = implode(' | ', $actualMessages);

        $this->assertEquals(
            $expectedMessageCount,
            $actualMessageCount,
            sprintf('Test: %s. Actual messages: %s', $name, $combinedActualMessages)
        );
        if ($expectedMessageCount == $actualMessageCount) {
            /** @var \Sstalle\php7cc\AbstractBaseMessage $message */
            foreach ($actualMessages as $i => $message) {
                $this->assertEquals(
                    $this->canonicalize($expectedMessages[$i]),
                    $this->canonicalize($actualMessages[$i]),
                    $name
                );
            }
        }
    }

    /**
     * Copypasted from PhpParser\CodeTestAbstract.
     *
     * @return array
     */
    public function messageProvider()
    {
        $it = new \RecursiveDirectoryIterator(__DIR__ . '/../resource');
        $it = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::LEAVES_ONLY);
        $it = new \RegexIterator($it, '(\.' . preg_quote('test') . '$)');
        $tests = array();
{'fraction_non_alphanumeric': 0.13371150729335493, 'fraction_numerical': 0.002633711507293355, 'mean_word_length': 2.657037037037037, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '5915817', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1326, 'n_words': 207}