<commit_before><?php

// Making abstract so I can have a child that does exactly this, and one
// that does repeat these tests with a modifier, to confirm everything is
// still working with that modifier
// (PHPUnit doesn't like it if I do this without the base class being
//  abstract)

/**
 * @runTestsInSeparateProcesses
 */
abstract class GoodServiceBaseTest extends \PHPUnit\Framework\TestCase
{
    private $files = array(); // so we know what to clean up
    protected $inputDir = '';
    private $outputDir = '';

    private function assertNoExceptions()
    {
        // Make assertion so test isn't marked as risky
        $this->assertTrue(true);
    }

    public function setUp(): void
    {
        $this->inputDir = dirname(__FILE__) . '/../testInputFiles/';
        $this->outputDir =  dirname(__FILE__) . '/../generated/';
    }

    protected function compile($types, $modifiers = null, $inputFiles = null)
    {
        if ($modifiers == null)
        {
            $modifiers = [];
        }

        $rolemodel = new \Good\Rolemodel\Rolemodel();

        if ($inputFiles == null)
        {
            $inputFiles = [];

            foreach ($types as $type)
            {
                $inputFiles[] = $this->inputDir . $type . '.datatype';
            }
        }

        $schema = $rolemodel->createSchema($inputFiles);

        $service = new \Good\Service\Service();

        $service->compile($modifiers, $schema, $this->outputDir);

        $this->files = array_merge($this->files, $inputFiles);

        $file = $this->outputDir . 'GeneratedBaseClass.php';
        require $file;
        $this->files[] = $file;

        foreach ($types as $type)
        {
            $file = $this->outputDir . $type . '.datatype.php';
            require $file;
            $this->files[] = $file;

            $file = $this->outputDir . $type . 'Resolver.php';
            require $file;
            $this->files[] = $file;
        }
    }

    public function tearDown(): void
    {
        foreach($this->files as $file)
        {
            unlink($file);
        }

        // not strictly necessary, but I'd rather not rely ont he fact
        // that each test runs in a different instance fo this class
        $this->files = array();
    }

    public function testIntTypeModifierMinValueAboveMaxValue()
    {
        file_put_contents($this->inputDir . 'MyType.datatype',
                            "datatype MyType{ int(minValue=42, maxValue=10) myInt;}");

        $this->expectException("Good\Rolemodel\InvalidTypeModifierException");

        $this->compile(array('MyType'));
    }

    public function testIntTypeModifierNonNegativeAndMinValueNegative()
    {
        file_put_contents($this->inputDir . 'MyType.datatype',
                            "datatype MyType{ int(minValue=-1, nonNegative) myInt;}");

        $this->expectException("Good\Rolemodel\InvalidTypeModifierException");<commit_msg>[#129] Fix failing test<commit_after><?php

// Making abstract so I can have a child that does exactly this, and one
// that does repeat these tests with a modifier, to confirm everything is
// still working with that modifier
// (PHPUnit doesn't like it if I do this without the base class being
//  abstract)

/**
 * @runTestsInSeparateProcesses
 */
abstract class GoodServiceBaseTest extends \PHPUnit\Framework\TestCase
{
    private $files = array(); // so we know what to clean up
    protected $inputDir = '';
    private $outputDir = '';

    private function assertNoExceptions()
    {
        // Make assertion so test isn't marked as risky
        $this->assertTrue(true);
    }

    public function setUp(): void
    {
        $this->inputDir = dirname(__FILE__) . '/../testInputFiles/';
        $this->outputDir =  dirname(__FILE__) . '/../generated/';
    }

    protected function compile($types, $modifiers = null, $inputFiles = null)
    {
        if ($modifiers == null)
        {
            $modifiers = [];
        }

        $rolemodel = new \Good\Rolemodel\Rolemodel();

        if ($inputFiles == null)
        {
            $inputFiles = [];

            foreach ($types as $type)
            {
                $inputFiles[] = $this->inputDir . $type . '.datatype';
            }
        }

        $schema = $rolemodel->createSchema($inputFiles);

        $service = new \Good\Service\Service();

        $service->compile($modifiers, $schema, $this->outputDir);

        $this->files = array_merge($this->files, $inputFiles);

        $file = $this->outputDir . 'GeneratedBaseClass.php';
        require $file;
        $this->files[] = $file;

        foreach ($types as $type)
        {
            $file = $this->outputDir . $type . '.datatype.php';
            require $file;
            $this->files[] = $file;

            $file = $this->outputDir . $type . 'Resolver.php';

            if (file_exists($file))
            {
                $this->files[] = $file;
            }
        }
    }

    public function tearDown(): void
    {
        foreach($this->files as $file)
        {
            unlink($file);
        }

        // not strictly necessary, but I'd rather not rely ont he fact
        // that each test runs in a different instance fo this class
        $this->files = array();
    }

    public function testIntTypeModifierMinValueAboveMaxValue()
    {
        file_put_contents($this->inputDir . 'MyType.datatype',
                            "datatype MyType{ int(minValue=42, maxValue=10) myInt;}");

        $this->expectException("Good\Rolemodel\InvalidTypeModifierException");

        $this->compile(array('MyType'));
    }

    public function testIntTypeModifierNonNegativeAndMinValueNegative()
    {
        file_put_contents($this->inputDir . 'MyType.datatype',
                            "datatype MyType{ int(minValue=-1, nonNegative) myInt;}");

        $this->expectException("Good\Rolemodel\InvalidTypeModifierException");
{'fraction_non_alphanumeric': 0.12984822934232715, 'fraction_numerical': 0.0021922428330522765, 'mean_word_length': 2.097127937336815, 'pattern_counts': {'":': 0, '<': 5, '<?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': '806598', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1601, 'n_words': 416}