<commit_before><?php

namespace Craft;

use PHPUnit_Framework_MockObject_MockObject as Mock;

/**
 * Class Schematic_PluginsServiceTest.
 *
 * @author    Itmundi
 * @copyright Copyright (c) 2015, Itmundi
 * @license   MIT
 *
 * @link      http://www.itmundi.nl
 *
 * @coversDefaultClass Craft\Schematic_PluginsService
 * @covers ::__construct
 * @covers ::<!public>
 */
class Schematic_PluginsServiceTest extends BaseTest
{
    /**
     * @var Schematic_PluginsService
     */
    private $schematicPluginsService;

    /**
     * {@inheritdoc}
     */
    public function setUp()
    {
        $this->schematicPluginsService = new Schematic_PluginsService();
    }

    /**
     * {@inheritdoc}
     */
    public static function setUpBeforeClass()
    {
        parent::setUpBeforeClass();
        require_once __DIR__.'/../SchematicPlugin.php';
        require_once __DIR__.'/../models/Schematic_ResultModel.php';
        require_once __DIR__.'/../services/Schematic_AbstractService.php';
        require_once __DIR__.'/../services/Schematic_PluginsService.php';
    }

    /**
     * @param bool $returnPlugin
     * @param bool $installPluginResponse
     * @param bool $enablePluginResponse
     * @param bool $disablePluginResponse
     * @param bool $uninstallPluginResponse
     *
     * @return PluginsService|Mock
     */
    public function getMockPluginsService(
        $returnPlugin = true,
        $installPluginResponse = true,
        $enablePluginResponse = true,
        $disablePluginResponse = true,
        $uninstallPluginResponse = true
    ) {
        $mock = $this->getMockBuilder('Craft\PluginsService')->getMock();

        $mock->expects($this->any())->method('getPlugin')->willReturn(($returnPlugin) ? $this->getMockBasePlugin() : null);

        if ($installPluginResponse) {
            $mock->expects($this->any())->method('installPlugin')->willReturn($installPluginResponse);
        } else {
            $mock->expects($this->any())->method('installPlugin')->willThrowException(new Exception());
        }

        $mock->expects($this->any())->method('enablePlugin')->willReturn($enablePluginResponse);
        $mock->expects($this->any())->method('disablePlugin')->willReturn($disablePluginResponse);
        $mock->expects($this->any())->method('uninstallPlugin')->willReturn($uninstallPluginResponse);

        return $mock;
    }

    /**
     * @return MigrationsService|Mock
     */
    public function getMockMigrationsService()
    {
        $mock = $this->getMockBuilder('Craft\MigrationsService')->getMock();
        $mock->expects($this->any())->method('runToTop')->willReturn(true);

        return $mock;
    }

    /**
     * @return UpdatesService|Mock
     */
    public function getMockUpdatesService()
    {
        $mock = $this->getMockBuilder('Craft\UpdatesService')->getMock();
        $mock->expects($this->any())->method('setNewPluginInfo')->willReturn(true);

        return $mock;
    }

    /**
     * @return Mock|BasePlugin
     */
    public function getMockBasePlugin()
    {
        $mock = $this->getMockBuilder('Craft\BasePlugin')->getMock();

        return $mock;
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithInstalledPlugins()
    {
        $data = $this->getPluginsData();
        $mockPluginsService = $this->getMockPluginsService();
        $this->setComponent(craft(), 'plugins', $mockPluginsService);
        $mockMigrationsService = $this->getMockMigrationsService();
        $this->setComponent(craft(), 'migrations', $mockMigrationsService);
        $mockUpdatesService = $this->getMockUpdatesService();
        $this->setComponent(craft(), 'updates', $mockUpdatesService);

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertFalse($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithInstalledDisabledPlugins()
    {
        $data = $this->getPluginsData();
        $data['itmundiplugin']['isEnabled'] = false;
        $mockPluginsService = $this->getMockPluginsService();
        $this->setComponent(craft(), 'plugins', $mockPluginsService);
        $mockMigrationsService = $this->getMockMigrationsService();
        $this->setComponent(craft(), 'migrations', $mockMigrationsService);
        $mockUpdatesService = $this->getMockUpdatesService();
        $this->setComponent(craft(), 'updates', $mockUpdatesService);

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertFalse($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithMissingPlugin()
    {
        $data = $this->getPluginsData();

        $mockPluginsService = $this->getMockPluginsService(false);
        $this->setComponent(craft(), 'plugins', $mockPluginsService);

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertTrue($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithInstallException()
    {
        $data = $this->getPluginsData();

        $mockPluginsService = $this->getMockPluginsService(true, false);
        $this->setComponent(craft(), 'plugins', $mockPluginsService);
        $mockMigrationsService = $this->getMockMigrationsService();
        $this->setComponent(craft(), 'migrations', $mockMigrationsService);
        $mockUpdatesService = $this->getMockUpdatesService();
        $this->setComponent(craft(), 'updates', $mockUpdatesService);

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertTrue($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */<commit_msg>Prevent code duplication in unit test<commit_after><?php

namespace Craft;

use PHPUnit_Framework_MockObject_MockObject as Mock;

/**
 * Class Schematic_PluginsServiceTest.
 *
 * @author    Itmundi
 * @copyright Copyright (c) 2015, Itmundi
 * @license   MIT
 *
 * @link      http://www.itmundi.nl
 *
 * @coversDefaultClass Craft\Schematic_PluginsService
 * @covers ::__construct
 * @covers ::<!public>
 */
class Schematic_PluginsServiceTest extends BaseTest
{
    /**
     * @var Schematic_PluginsService
     */
    private $schematicPluginsService;

    /**
     * {@inheritdoc}
     */
    public function setUp()
    {
        $this->schematicPluginsService = new Schematic_PluginsService();
    }

    /**
     * {@inheritdoc}
     */
    public static function setUpBeforeClass()
    {
        parent::setUpBeforeClass();
        require_once __DIR__.'/../SchematicPlugin.php';
        require_once __DIR__.'/../models/Schematic_ResultModel.php';
        require_once __DIR__.'/../services/Schematic_AbstractService.php';
        require_once __DIR__.'/../services/Schematic_PluginsService.php';
    }

    /**
     * Prevent code duplication by mocking multiple services.
     */
    public function mockMultipleServices()
    {
        $mockPluginsService = $this->getMockPluginsService();
        $this->setComponent(craft(), 'plugins', $mockPluginsService);
        $mockMigrationsService = $this->getMockMigrationsService();
        $this->setComponent(craft(), 'migrations', $mockMigrationsService);
        $mockUpdatesService = $this->getMockUpdatesService();
        $this->setComponent(craft(), 'updates', $mockUpdatesService);
    }

    /**
     * @param bool $returnPlugin
     * @param bool $installPluginResponse
     * @param bool $enablePluginResponse
     * @param bool $disablePluginResponse
     * @param bool $uninstallPluginResponse
     *
     * @return PluginsService|Mock
     */
    public function getMockPluginsService(
        $returnPlugin = true,
        $installPluginResponse = true,
        $enablePluginResponse = true,
        $disablePluginResponse = true,
        $uninstallPluginResponse = true
    ) {
        $mock = $this->getMockBuilder('Craft\PluginsService')->getMock();

        $mock->expects($this->any())->method('getPlugin')->willReturn(($returnPlugin) ? $this->getMockBasePlugin() : null);

        if ($installPluginResponse) {
            $mock->expects($this->any())->method('installPlugin')->willReturn($installPluginResponse);
        } else {
            $mock->expects($this->any())->method('installPlugin')->willThrowException(new Exception());
        }

        $mock->expects($this->any())->method('enablePlugin')->willReturn($enablePluginResponse);
        $mock->expects($this->any())->method('disablePlugin')->willReturn($disablePluginResponse);
        $mock->expects($this->any())->method('uninstallPlugin')->willReturn($uninstallPluginResponse);

        return $mock;
    }

    /**
     * @return MigrationsService|Mock
     */
    public function getMockMigrationsService()
    {
        $mock = $this->getMockBuilder('Craft\MigrationsService')->getMock();
        $mock->expects($this->any())->method('runToTop')->willReturn(true);

        return $mock;
    }

    /**
     * @return UpdatesService|Mock
     */
    public function getMockUpdatesService()
    {
        $mock = $this->getMockBuilder('Craft\UpdatesService')->getMock();
        $mock->expects($this->any())->method('setNewPluginInfo')->willReturn(true);

        return $mock;
    }

    /**
     * @return Mock|BasePlugin
     */
    public function getMockBasePlugin()
    {
        $mock = $this->getMockBuilder('Craft\BasePlugin')->getMock();

        return $mock;
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithInstalledPlugins()
    {
        $data = $this->getPluginsData();

        $this->mockMultipleServices();

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertFalse($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithInstalledDisabledPlugins()
    {
        $data = $this->getPluginsData();
        $data['itmundiplugin']['isEnabled'] = false;

        $this->mockMultipleServices();

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertFalse($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithMissingPlugin()
    {
        $data = $this->getPluginsData();

        $mockPluginsService = $this->getMockPluginsService(false);
        $this->setComponent(craft(), 'plugins', $mockPluginsService);

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertTrue($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
    public function testImportWithInstallException()
    {
        $data = $this->getPluginsData();

        $this->mockMultipleServices();

        $import = $this->schematicPluginsService->import($data);

        $this->assertTrue($import instanceof Schematic_ResultModel);
        $this->assertTrue($import->hasErrors());
    }

    /**
     * Test default import functionality.
     *
     * @covers ::import
     */
{'fraction_non_alphanumeric': 0.14644845621731062, 'fraction_numerical': 0.0006748776784207862, 'mean_word_length': 3.0323129251700682, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 168, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3817509', 'n_tokens_mistral': 3354, 'n_tokens_neox': 3292, 'n_words': 501}