SchemaPersistorTest.php 6.71 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Framework\Setup\Test\Unit;

use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Setup\SchemaListener;
use Magento\Framework\Setup\XmlPersistor;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

/**
 * Unit test for schema persistor.
 *
 * @package Magento\Framework\Setup\Test\Unit
 */
class SchemaPersistorTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var \Magento\Framework\Setup\SchemaPersistor
     */
    private $model;

    /**
     * @var ObjectManagerHelper
     */
    private $objectManagerHelper;

    /**
     * @var ComponentRegistrar|\PHPUnit_Framework_MockObject_MockObject
     */
    private $componentRegistrarMock;

    /**
     * @var XmlPersistor|\PHPUnit_Framework_MockObject_MockObject
     */
    private $xmlPersistor;

    protected function setUp() : void
    {
        $this->componentRegistrarMock = $this->getMockBuilder(ComponentRegistrar::class)
            ->disableOriginalConstructor()
            ->getMock();
        $this->xmlPersistor = $this->getMockBuilder(XmlPersistor::class)
            ->getMock();
        $this->objectManagerHelper = new ObjectManagerHelper($this);
        $this->model = $this->objectManagerHelper->getObject(
            \Magento\Framework\Setup\SchemaPersistor::class,
            [
                'componentRegistrar' => $this->componentRegistrarMock,
                'xmlPersistor' => $this->xmlPersistor
            ]
        );
    }

    /**
     * @dataProvider schemaListenerTablesDataProvider
     * @param array $tables
     * @param string $expectedXML
     */
    public function testPersist(array $tables, $expectedXML) : void
    {
        $moduleName = 'First_Module';
        /** @var SchemaListener|\PHPUnit_Framework_MockObject_MockObject $schemaListenerMock */
        $schemaListenerMock = $this->getMockBuilder(SchemaListener::class)
            ->disableOriginalConstructor()
            ->getMock();
        $schemaListenerMock->expects(self::once())
            ->method('getTables')
            ->willReturn($tables);
        $this->componentRegistrarMock->expects(self::once())
            ->method('getPath')
            ->with('module', $moduleName)
            ->willReturn('some-non-existing-path');
        $simpleXmlElement = new \SimpleXMLElement($expectedXML);
        $this->xmlPersistor
            ->expects(self::once())
            ->method('persist')
            ->with($simpleXmlElement, 'some-non-existing-path/etc/db_schema.xml');

        $this->model->persist($schemaListenerMock);
    }

    /**
     * Provide listened schema.
     *
     * @return array
     */
    public function schemaListenerTablesDataProvider() : array
    {
        return [
            [
                'schemaListenerTables' => [
                    'First_Module' => [
                        'first_table' => [
                            'disabled' => false,
                            'name' => 'first_table',
                            'resource' => 'default',
                            'engine' => 'innodb',
                            'columns' => [
                                'first_column' => [
                                    'name' => 'first_column',
                                    'xsi:type' => 'integer',
                                    'nullable' => 1,
                                    'unsigned' => '0',
                                ],
                                'second_column' => [
                                    'name' => 'second_column',
                                    'xsi:type' => 'date',
                                    'nullable' => 0,
                                ]
                            ],
                            'indexes' => [
                                'TEST_INDEX' => [
                                    'name' => 'TEST_INDEX',
                                    'indexType' => 'btree',
                                    'columns' => [
                                        'first_column'
                                    ]
                                ]
                            ],
                            'constraints' => [
                                'foreign' => [
                                    'some_foreign_constraint' => [
                                        'referenceTable' => 'table',
                                        'referenceColumn' => 'column',
                                        'table' => 'first_table',
                                        'column' => 'first_column'
                                    ]
                                ],
                                'primary' => [
                                    'PRIMARY' => [
                                        'xsi:type' => 'primary',
                                        'name' => 'PRIMARY',
                                        'columns' => [
                                            'second_column'
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
                // @codingStandardsIgnoreStart
                'XMLResult' => '<?xml version="1.0"?>
                        <schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                            xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
                            <table name="first_table" resource="default" engine="innodb">
                                <column xmlns:xsi="xsi" xsi:type="integer" name="first_column" nullable="1" 
                                    unsigned="0"/>
                                <column xmlns:xsi="xsi" xsi:type="date" name="second_column" nullable="0"/>
                                <constraint xmlns:xsi="xsi" xsi:type="foreign" referenceId="some_foreign_constraint" 
                                    referenceTable="table" referenceColumn="column" 
                                    table="first_table" column="first_column"/>
                                <constraint xmlns:xsi="xsi" xsi:type="primary" referenceId="PRIMARY">
                                    <column name="second_column"/>
                                </constraint>
                                <index referenceId="TEST_INDEX" indexType="btree">
                                    <column name="first_column"/>
                                </index>
                            </table>
                        </schema>'
                // @codingStandardsIgnoreEnd
            ]
        ];
    }
}