DiffOldSchemaTest.php 5.13 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Setup;

use Magento\Framework\Setup\Declaration\Schema\Diff\DiffFactory;
use Magento\Framework\Setup\Declaration\Schema\Diff\SchemaDiff;
use Magento\Framework\Setup\Declaration\Schema\SchemaConfigInterface;
use Magento\TestFramework\Deploy\CliCommand;
use Magento\TestFramework\Deploy\TestModuleManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\SetupTestCase;

/**
 * The purpose of this test is verifying initial InstallSchema, InstallData scripts.
 */
class DiffOldSchemaTest extends SetupTestCase
{
    /**
     * @var TestModuleManager
     */
    private $moduleManager;

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

    /**
     * @var SchemaDiff
     */
    private $schemaDiff;

    /**
     * @var DiffFactory
     */
    private $changeRegistryFactory;

    /**
     * @var SchemaConfigInterface
     */
    private $schemaConfig;

    public function setUp()
    {
        $objectManager = Bootstrap::getObjectManager();
        $this->moduleManager = $objectManager->get(TestModuleManager::class);
        $this->cliCommad = $objectManager->get(CliCommand::class);
        $this->schemaConfig = $objectManager->get(SchemaConfigInterface::class);
        $this->schemaDiff = $objectManager->get(SchemaDiff::class);
        $this->changeRegistryFactory = $objectManager->get(DiffFactory::class);
    }

    /**
     * @moduleName Magento_TestSetupDeclarationModule1
     */
    public function testOldDiff()
    {
        //Move db_schema.xml
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule1',
            'old_diff_before',
            'db_schema.xml',
            'etc'
        );
        //Move InstallSchema file and tried to install
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule1',
            'old_diff',
            'InstallSchema.php',
            'Setup'
        );
        $this->cliCommad->install(['Magento_TestSetupDeclarationModule1']);
        //Move db_schema.xml
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule1',
            'old_diff',
            'db_schema.xml',
            'etc'
        );
        $declarativeSchema = $this->schemaConfig->getDeclarationConfig();
        $generatedSchema = $this->schemaConfig->getDbConfig();
        $diff = $this->schemaDiff->diff($declarativeSchema, $generatedSchema);
        $allChanges = $diff->getAll();
        self::assertCount(1, $allChanges);
        self::assertEquals(
            $this->getBigIntKeyXmlSensitiveData(),
            reset($allChanges)['modify_column'][0]->getNew()->getDiffSensitiveParams()
        );
        self::assertEquals(
            $this->getBigIntKeyDbSensitiveData(),
            reset($allChanges)['modify_column'][0]->getOld()->getDiffSensitiveParams()
        );
    }

    /**
     * @moduleName Magento_TestSetupDeclarationModule1
     * @param string $dbPrefix
     * @throws \Exception
     * @dataProvider oldSchemaUpgradeDataProvider
     */
    public function testOldSchemaUpgrade(string $dbPrefix)
    {
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule1',
            'old_diff_before',
            'db_schema.xml',
            'etc'
        );
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule1',
            'base_update',
            'InstallSchema.php',
            'Setup'
        );
        $this->cliCommad->install(
            ['Magento_TestSetupDeclarationModule1'],
            ['db-prefix' => $dbPrefix]
        );
        //Move db_schema.xml
        $this->moduleManager->updateRevision(
            'Magento_TestSetupDeclarationModule1',
            'base_update',
            'db_schema.xml',
            'etc'
        );
        $declarativeSchema = $this->schemaConfig->getDeclarationConfig();
        $generatedSchema = $this->schemaConfig->getDbConfig();
        $diff = $this->schemaDiff->diff($declarativeSchema, $generatedSchema);
        self::assertEmpty($diff->getAll());
    }

    /**
     * @return array
     */
    public function oldSchemaUpgradeDataProvider(): array
    {
        return [
            'Without db prefix' => [
                'dbPrefix' => '',
            ],
            'With db prefix' => [
                'dbPrefix' => 'spec_',
            ],
        ];
    }

    /**
     * @return array
     */
    private function getBigIntKeyDbSensitiveData()
    {
        return [
            'type' => 'bigint',
            'nullable' => true,
            'padding' => 20,
            'unsigned' => false,
            'identity' => false,
            'default' => 0,
            'comment' => 'Bigint'
        ];
    }

    /**
     * @return array
     */
    private function getBigIntKeyXmlSensitiveData()
    {
        return [
            'type' => 'bigint',
            'nullable' => true,
            'padding' => 20,
            'unsigned' => false,
            'identity' => false,
            'default' => 1,
            'comment' => 'Bigint',
        ];
    }
}