<commit_before><?php

class CM_Provision_LoaderTest extends CMTest_TestCase {

    public function testGetScriptList() {
        $script1 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script2 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script2->mockMethod('getRunLevel')->set(10);
        $script3 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script3->mockMethod('getRunLevel')->set(1);

        $loader = new CM_Provision_Loader();
        $loader->registerScript($script1);
        $loader->registerScript($script2);
        $loader->registerScript($script3);

        $scriptList = CMTest_TH::callProtectedMethod($loader, '_getScriptList');
        $expected = [$script3, $script1, $script2];
        $this->assertSame($expected, $scriptList);
    }

    public function testLoad() {
        $outputStream = new CM_OutputStream_Null();

        $script = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script->mockMethod('shouldBeLoaded')->set(true);
        $loadMethod = $script->mockMethod('load')->set(function ($output) use ($outputStream) {
            $this->assertSame($outputStream, $output);
        });
        /** @var CM_Provision_Script_Abstract $script */

        $loader = new CM_Provision_Loader($outputStream);
        $loader->registerScript($script);
        $loader->load();
        $this->assertSame(1, $loadMethod->getCallCount());
    }

    public function testGetScriptListUnloadable() {
        $classScriptUnloadable = $this->mockClass('CM_Provision_Script_Abstract', ['CM_Provision_Script_UnloadableInterface']);

        $script1 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script2 = $classScriptUnloadable->newInstanceWithoutConstructor();
        $script3 = $classScriptUnloadable->newInstanceWithoutConstructor();

        $loader = $this->mockObject('CM_Provision_Loader');
        $loader->mockMethod('_getScriptList')->set([$script1, $script2, $script3]);
        $this->assertSame([$script3, $script2], CMTest_TH::callProtectedMethod($loader, '_getScriptListUnloadable'));
    }

    public function testUnload() {
        $outputStream = new CM_OutputStream_Null();

        $script = $this->mockClass('CM_Provision_Script_Abstract', ['CM_Provision_Script_UnloadableInterface'])->newInstanceWithoutConstructor();
        $script->mockMethod('shouldBeUnloaded')->set(true);
        $unloadMethod = $script->mockMethod('unload')->set(function ($output) use ($outputStream) {
            $this->assertSame($outputStream, $output);
        });
        /** @var CM_Provision_Script_Abstract $script */

        $loader = new CM_Provision_Loader($outputStream);
        $loader->registerScript($script);
        $loader->unload();
        $this->assertSame(1, $unloadMethod->getCallCount());
    }
}<commit_msg>Test unloading multiple scripts, skip those which should not be unloaded<commit_after><?php

class CM_Provision_LoaderTest extends CMTest_TestCase {

    public function testGetScriptList() {
        $script1 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script2 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script2->mockMethod('getRunLevel')->set(10);
        $script3 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script3->mockMethod('getRunLevel')->set(1);

        $loader = new CM_Provision_Loader();
        $loader->registerScript($script1);
        $loader->registerScript($script2);
        $loader->registerScript($script3);

        $scriptList = CMTest_TH::callProtectedMethod($loader, '_getScriptList');
        $expected = [$script3, $script1, $script2];
        $this->assertSame($expected, $scriptList);
    }

    public function testLoad() {
        $outputStream = new CM_OutputStream_Null();

        $script = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script->mockMethod('shouldBeLoaded')->set(true);
        $loadMethod = $script->mockMethod('load')->set(function ($output) use ($outputStream) {
            $this->assertSame($outputStream, $output);
        });
        /** @var CM_Provision_Script_Abstract $script */

        $loader = new CM_Provision_Loader($outputStream);
        $loader->registerScript($script);
        $loader->load();
        $this->assertSame(1, $loadMethod->getCallCount());
    }

    public function testGetScriptListUnloadable() {
        $classScriptUnloadable = $this->mockClass('CM_Provision_Script_Abstract', ['CM_Provision_Script_UnloadableInterface']);

        $script1 = $this->mockClass('CM_Provision_Script_Abstract')->newInstanceWithoutConstructor();
        $script2 = $classScriptUnloadable->newInstanceWithoutConstructor();
        $script3 = $classScriptUnloadable->newInstanceWithoutConstructor();

        $loader = $this->mockObject('CM_Provision_Loader');
        $loader->mockMethod('_getScriptList')->set([$script1, $script2, $script3]);
        $this->assertSame([$script3, $script2], CMTest_TH::callProtectedMethod($loader, '_getScriptListUnloadable'));
    }

    public function testUnload() {
        $outputStream = new CM_OutputStream_Null();

        $script = $this->mockClass('CM_Provision_Script_Abstract', ['CM_Provision_Script_UnloadableInterface'])->newInstanceWithoutConstructor();
        $script->mockMethod('shouldBeUnloaded')
            ->at(0, true)
            ->at(1, false)
            ->at(2, true);
        $unloadMethod = $script->mockMethod('unload')->set(function ($output) use ($outputStream) {
            $this->assertSame($outputStream, $output);
        });
        /** @var CM_Provision_Script_Abstract $script */

        $loader = new CM_Provision_Loader($outputStream);
        $loader->registerScript($script);
        $loader->registerScript($script);
        $loader->registerScript($script);
        $loader->unload();
        $this->assertSame(2, $unloadMethod->getCallCount());
    }
}
{'fraction_non_alphanumeric': 0.14768137017288738, 'fraction_numerical': 0.008240426563257392, 'mean_word_length': 4.086277732128184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 97, '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': '1869722', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1693, 'n_words': 228}