You need to sign in or sign up before continuing.
DataPatchInstallationTest.php 6.96 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Setup;

use Magento\Framework\Module\ModuleResource;
use Magento\Framework\Setup\Patch\PatchHistory;
use Magento\TestFramework\Deploy\CliCommand;
use Magento\TestFramework\Deploy\TableData;
use Magento\TestFramework\Deploy\TestModuleManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\SetupTestCase;
use Magento\TestSetupDeclarationModule3\Setup\Patch\Data\IncrementalSomeIntegerPatch;
use Magento\TestSetupDeclarationModule3\Setup\Patch\Data\ReferenceIncrementalSomeIntegerPatch;
use Magento\TestSetupDeclarationModule3\Setup\Patch\Data\ZFirstPatch;

/**
 * The purpose of this test is validating schema reader operations.
 */
class DataPatchInstallationTest extends SetupTestCase
{
    /**
     * @var TestModuleManager
     */
    private $moduleManager;

    /**
     * @var CliCommand
     */
    private $cliCommad;

    /**
     * @var ModuleResource
     */
    private $moduleResource;

    /**
     * @var PatchHistory
     */
    private $patchList;

    /**
     * @var TableData
     */
    private $tableData;

    public function setUp()
    {
        $objectManager = Bootstrap::getObjectManager();
        $this->moduleManager = $objectManager->get(TestModuleManager::class);
        $this->cliCommad = $objectManager->get(CliCommand::class);
        $this->moduleResource = $objectManager->get(ModuleResource::class);
        $this->patchList = $objectManager->get(PatchHistory::class);
        $this->tableData = $objectManager->get(TableData::class);
    }

    /**
     * @moduleName Magento_TestSetupDeclarationModule3
     */
    public function testDataPatchesInstallation()
    {
        $this->cliCommad->install(
            ['Magento_TestSetupDeclarationModule3']
        );

        self::assertEquals(
            '0.0.1',
            $this->moduleResource->getDataVersion('Magento_TestSetupDeclarationModule3')
        );

        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule3',
            'first_patch_revision',
            'module.xml',
            'etc'
        );
        $this->movePatches();
        ModuleResource::flush();
        $this->cliCommad->upgrade();
        self::assertEquals(
            '0.0.3',
            $this->moduleResource->getDataVersion('Magento_TestSetupDeclarationModule3')
        );
        self::assertTrue($this->patchList->isApplied(IncrementalSomeIntegerPatch::class));
        self::assertTrue($this->patchList->isApplied(ReferenceIncrementalSomeIntegerPatch::class));
        self::assertTrue($this->patchList->isApplied(ZFirstPatch::class));
        $tableData = $this->tableData->describeTableData('test_table');
        self::assertEquals($this->getTestTableData(), $tableData);
    }

    /**
     * @moduleName Magento_TestSetupDeclarationModule3
     * @expectedException \Magento\Framework\Exception\LocalizedException
     */
    public function testCyclomaticDependency()
    {
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule3',
            'cyclomatic_and_bic_revision',
            'module.xml',
            'etc'
        );

        $this->movePatches();
        /**
         * Test whether installation give the same result as upgrade
         */
        $this->cliCommad->install(
            ['Magento_TestSetupDeclarationModule3']
        );
        $tableData = $this->tableData->describeTableData('test_table');
        self::assertEquals($this->getTestTableData(), $tableData);
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule3',
            'cyclomatic_and_bic_revision',
            'BicPatch.php',
            'Setup/Patch/Data'
        );
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule3',
            'cyclomatic_and_bic_revision',
            'RefBicPatch.php',
            'Setup/Patch/Data'
        );

        $this->cliCommad->upgrade();
    }

    /**
     * Move patches
     */
    private function movePatches()
    {
        //Install with patches
        $this->moduleManager->addRevision(
            'Magento_TestSetupDeclarationModule3',
            'patches_revision',
            'Setup/Patch/Data'
        );
        //Upgrade with UpgradeData
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule3',
            'first_patch_revision',
            'UpgradeData.php',
            'Setup'
        );

        //Upgrade with UpgradeData
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule3',
            'first_patch_revision',
            'module.xml',
            'etc'
        );
    }

    /**
     * @moduleName Magento_TestSetupDeclarationModule3
     */
    public function testPatchesRevert()
    {
        $this->movePatches();
        $this->cliCommad->install(['Magento_TestSetupDeclarationModule3']);
        $this->cliCommad->uninstallModule('Magento_TestSetupDeclarationModule3');
        $testTableData = $this->tableData->describeTableData('test_table');
        $patchListTableData = $this->tableData->describeTableData('patch_list');
        self::assertEmpty($patchListTableData);
        self::assertEmpty($testTableData);
        $refTableData = $this->tableData->describeTableData('reference_table');
        self::assertEquals($this->getRefTableData(), $refTableData);
    }

    /**
     * @return array
     */
    private function getTestTableData()
    {
        return [
            [
                'smallint' => '1',
                'tinyint' => null,
                'varchar' => 'Ololo123',
                'varbinary' => '33288',
            ],
            [
                'smallint' => '2',
                'tinyint' => null,
                'varchar' => 'Ololo123_ref',
                'varbinary' => '33288',
            ],
            [
                'smallint' => '3',
                'tinyint' => null,
                'varchar' => 'changed__very_secret_string',
                'varbinary' => '0',
            ],
        ];
    }

    /**
     * Retrieve reference table data
     *
     * @return array
     */
    private function getRefTableData()
    {
        return [
            [
                'tinyint_ref' => '2',
                'some_integer' => '2',
                'for_patch_testing' => null,
            ],
            [
                'tinyint_ref' => '3',
                'some_integer' => '3',
                'for_patch_testing' => null,
            ],
            [
                'tinyint_ref' => '4',
                'some_integer' => '5',
                'for_patch_testing' => null,
            ],
            [
                'tinyint_ref' => '5',
                'some_integer' => '6',
                'for_patch_testing' => null,
            ],
            [
                'tinyint_ref' => '6',
                'some_integer' => '12',
                'for_patch_testing' => null,
            ],
        ];
    }
}