<commit_before><?php
namespace Czim\CmsCore\Test\Modules;

use Czim\CmsCore\Contracts\Auth\AclRepositoryInterface;
use Czim\CmsCore\Contracts\Core\CoreInterface;
use Czim\CmsCore\Contracts\Modules\ModuleInterface;
use Czim\CmsCore\Modules\Manager\Manager;
use Czim\CmsCore\Test\Helpers\Modules\SimpleAssociatedTestModule;
use Czim\CmsCore\Test\Helpers\Modules\SimpleTestModule;
use Czim\CmsCore\Test\Helpers\Modules\SimpleTestModuleWithSameServiceProvider;
use Czim\CmsCore\Test\Helpers\Modules\SimpleTestModuleWithServiceProviders;
use Czim\CmsCore\Test\Helpers\Modules\TestModuleWithRoutes;
use Czim\CmsCore\Test\TestCase;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;

class ManagerTest extends TestCase
{

    /**
     * @test
     */
    function it_initializes_succesfully()
    {
        $manager = $this->makeManager();

        $manager->initialize();
    }

    /**
     * @test
     */
    function it_loads_configured_modules_on_initialization()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $modules = $manager->getModules();

        $this->assertInstanceOf(Collection::class, $modules, 'getModules() did not return Collection');
        $this->assertCount(2, $modules, "Two modules should be loaded by default");
        $this->assertTrue($modules->has('test-module'), 'Test Module should be loaded');
        $this->assertTrue($modules->has('associated-test-module'), 'Associated Test Module should be loaded');
    }

    /**
     * @test
     */
    function it_initializes_with_injected_module_array_without_loading_configured_modules()
    {
        $manager = $this->makeManager();

        $manager->initialize([ SimpleAssociatedTestModule::class ]);

        $modules = $manager->getModules();

        $this->assertCount(1, $modules, 'There should be only one module loaded');
        $this->assertEquals('associated-test-module', $modules->first()->getKey(), 'The wrong module was loaded');
    }

    /**
     * @test
     */
    function it_returns_loaded_modules_by_key()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertInstanceOf(ModuleInterface::class, $manager->get('associated-test-module'));
    }

    /**
     * @test
     */
    function it_returns_loaded_modules_by_associated_class()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertInstanceOf(ModuleInterface::class, $manager->getByAssociatedClass(Manager::class));
    }

    /**
     * @test
     */
    function it_returns_false_when_getting_unloaded_module_by_key()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertSame(false, $manager->get('does-not-exist'));
    }

    /**
     * @test
     */
    function it_returns_false_when_getting_unloaded_module_by_associated_class()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertSame(false, $manager->getByAssociatedClass('Some\\Unassociated\\Class'));
    }

    /**
     * @test
     */
    function it_returns_whether_a_module_is_loaded_by_key()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertTrue($manager->has('test-module'), 'Should return true for loaded module');
        $this->assertFalse($manager->has('not-loaded'), 'Should return false for unloaded module');
    }

    /**
     * @test
     * @expectedException \Exception
     */
    function it_throws_an_exception_if_the_same_module_key_is_encountered_more_than_once()
    {
        $manager = $this->makeManager();

        $manager->initialize([
            SimpleTestModule::class,
            SimpleTestModule::class,
        ]);
    }
    
    /**
     * @test
     */
    function it_returns_service_providers_defined_by_modules()
    {
        $manager = $this->makeManager();

        $manager->initialize([ SimpleTestModuleWithServiceProviders::class ]);

        $exampleModule = new SimpleTestModuleWithServiceProviders();

        $this->assertEquals(
            $exampleModule->getServiceProviders(),
            $manager->getServiceProviders(),
            'Manager should return the same service providers as the module'
        );
    }

    /**
     * @test
     */
    function it_does_not_return_the_same_service_provider_more_than_once()
    {
        $manager = $this->makeManager();

        $manager->initialize([
            SimpleTestModuleWithServiceProviders::class,
            SimpleTestModuleWithSameServiceProvider::class,
        ]);

        $exampleModule = new SimpleTestModuleWithServiceProviders();

        $this->assertEquals(
            $exampleModule->getServiceProviders(),
            $manager->getServiceProviders(),
            'Manager should not return more than two service providers'
        );
    }

    /**
     * @test
     */
    function it_builds_web_routes_for_loaded_modules_on_a_given_router()
    {
        $manager = $this->makeManager();

        $manager->initialize([ TestModuleWithRoutes::class ]);

        $router = $this->getMockRouter();
        $router->expects($this->once())->method('get')->with('test', 'SomeController@index');
        $router->expects($this->once())->method('post')->with('test', 'SomeController@store');

        $manager->mapWebRoutes($router);
    }

    /**
     * @test
     */
    function it_builds_api_routes_for_loaded_modules_on_a_given_router()
    {
        $manager = $this->makeManager();

        $manager->initialize([ TestModuleWithRoutes::class ]);

        $router = $this->getMockRouter();
        $router->expects($this->once())->method('get')->with('test', 'SomeApiController@index');
        $router->expects($this->once())->method('post')->with('test', 'SomeApiController@store');

        $manager->mapApiRoutes($router);
    }

    /**
     * @test
     */
    function it_sorts_the_service_providers()
    {
        // todo.. how should it actually sort them?
        $this->markTestIncomplete();
    }

    /**
     * @test<commit_msg>updated test for module manager sorting<commit_after><?php
namespace Czim\CmsCore\Test\Modules;

use Czim\CmsCore\Contracts\Auth\AclRepositoryInterface;
use Czim\CmsCore\Contracts\Core\CoreInterface;
use Czim\CmsCore\Contracts\Modules\ModuleInterface;
use Czim\CmsCore\Modules\Manager\Manager;
use Czim\CmsCore\Test\Helpers\Modules\SimpleAssociatedTestModule;
use Czim\CmsCore\Test\Helpers\Modules\SimpleTestModule;
use Czim\CmsCore\Test\Helpers\Modules\SimpleTestModuleWithSameServiceProvider;
use Czim\CmsCore\Test\Helpers\Modules\SimpleTestModuleWithServiceProviders;
use Czim\CmsCore\Test\Helpers\Modules\TestModuleWithRoutes;
use Czim\CmsCore\Test\TestCase;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;

class ManagerTest extends TestCase
{

    /**
     * @test
     */
    function it_initializes_succesfully()
    {
        $manager = $this->makeManager();

        $manager->initialize();
    }

    /**
     * @test
     */
    function it_loads_configured_modules_on_initialization()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $modules = $manager->getModules();

        $this->assertInstanceOf(Collection::class, $modules, 'getModules() did not return Collection');
        $this->assertCount(2, $modules, "Two modules should be loaded by default");
        $this->assertTrue($modules->has('test-module'), 'Test Module should be loaded');
        $this->assertTrue($modules->has('associated-test-module'), 'Associated Test Module should be loaded');
    }

    /**
     * @test
     */
    function it_initializes_with_injected_module_array_without_loading_configured_modules()
    {
        $manager = $this->makeManager();

        $manager->initialize([ SimpleAssociatedTestModule::class ]);

        $modules = $manager->getModules();

        $this->assertCount(1, $modules, 'There should be only one module loaded');
        $this->assertEquals('associated-test-module', $modules->first()->getKey(), 'The wrong module was loaded');
    }

    /**
     * @test
     */
    function it_returns_loaded_modules_by_key()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertInstanceOf(ModuleInterface::class, $manager->get('associated-test-module'));
    }

    /**
     * @test
     */
    function it_returns_loaded_modules_by_associated_class()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertInstanceOf(ModuleInterface::class, $manager->getByAssociatedClass(Manager::class));
    }

    /**
     * @test
     */
    function it_returns_false_when_getting_unloaded_module_by_key()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertSame(false, $manager->get('does-not-exist'));
    }

    /**
     * @test
     */
    function it_returns_false_when_getting_unloaded_module_by_associated_class()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertSame(false, $manager->getByAssociatedClass('Some\\Unassociated\\Class'));
    }

    /**
     * @test
     */
    function it_returns_whether_a_module_is_loaded_by_key()
    {
        $manager = $this->makeManager();

        $manager->initialize();

        $this->assertTrue($manager->has('test-module'), 'Should return true for loaded module');
        $this->assertFalse($manager->has('not-loaded'), 'Should return false for unloaded module');
    }

    /**
     * @test
     * @expectedException \Exception
     */
    function it_throws_an_exception_if_the_same_module_key_is_encountered_more_than_once()
    {
        $manager = $this->makeManager();

        $manager->initialize([
            SimpleTestModule::class,
            SimpleTestModule::class,
        ]);
    }
    
    /**
     * @test
     */
    function it_returns_service_providers_defined_by_modules()
    {
        $manager = $this->makeManager();

        $manager->initialize([ SimpleTestModuleWithServiceProviders::class ]);

        $exampleModule = new SimpleTestModuleWithServiceProviders();

        $this->assertEquals(
            $exampleModule->getServiceProviders(),
            $manager->getServiceProviders(),
            'Manager should return the same service providers as the module'
        );
    }

    /**
     * @test
     */
    function it_does_not_return_the_same_service_provider_more_than_once()
    {
        $manager = $this->makeManager();

        $manager->initialize([
            SimpleTestModuleWithServiceProviders::class,
            SimpleTestModuleWithSameServiceProvider::class,
        ]);

        $exampleModule = new SimpleTestModuleWithServiceProviders();

        $this->assertEquals(
            $exampleModule->getServiceProviders(),
            $manager->getServiceProviders(),
            'Manager should not return more than two service providers'
        );
    }

    /**
     * @test
     */
    function it_builds_web_routes_for_loaded_modules_on_a_given_router()
    {
        $manager = $this->makeManager();

        $manager->initialize([ TestModuleWithRoutes::class ]);

        $router = $this->getMockRouter();
        $router->expects($this->once())->method('get')->with('test', 'SomeController@index');
        $router->expects($this->once())->method('post')->with('test', 'SomeController@store');

        $manager->mapWebRoutes($router);
    }

    /**
     * @test
     */
    function it_builds_api_routes_for_loaded_modules_on_a_given_router()
    {
        $manager = $this->makeManager();

        $manager->initialize([ TestModuleWithRoutes::class ]);

        $router = $this->getMockRouter();
        $router->expects($this->once())->method('get')->with('test', 'SomeApiController@index');
        $router->expects($this->once())->method('post')->with('test', 'SomeApiController@store');

        $manager->mapApiRoutes($router);
    }

    /**
     * @test
     */
    function it_sorts_the_modules_by_name()
    {
        $manager = $this->makeManager();

        $manager->initialize([
            SimpleTestModuleWithServiceProviders::class,
            SimpleTestModule::class,
        ]);

        $modules = $manager->getModules();

        $this->assertCount(2, $modules);
        $this->assertEquals('test-module', $modules->first()->getKey());
        $this->assertEquals('test-module-with-service-providers', $modules->last()->getKey());
    }

    /**
     * @test
{'fraction_non_alphanumeric': 0.14206083771301162, 'fraction_numerical': 0.00039815257206561556, 'mean_word_length': 2.966835123183828, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 166, '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': '7294677', 'n_tokens_mistral': 3649, 'n_tokens_neox': 3615, 'n_words': 503}