XsdTest.php 8.73 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\MessageQueue\Test\Unit\Publisher;

class XsdTest extends \PHPUnit\Framework\TestCase
{
    /**
     * @var string
     */
    protected $_schemaFile;

    protected function setUp()
    {
        if (!function_exists('libxml_set_external_entity_loader')) {
            $this->markTestSkipped('Skipped on HHVM. Will be fixed in MAGETWO-45033');
        }
        $urnResolver = new \Magento\Framework\Config\Dom\UrnResolver();
        $this->_schemaFile = $urnResolver->getRealPath('urn:magento:framework-message-queue:etc/publisher.xsd');
    }

    /**
     * @param string $fixtureXml
     * @param array $expectedErrors
     * @dataProvider exemplarXmlDataProvider
     */
    public function testExemplarXml($fixtureXml, array $expectedErrors)
    {
        $validationState = $this->createMock(\Magento\Framework\Config\ValidationStateInterface::class);
        $validationState->expects($this->any())
            ->method('isValidationRequired')
            ->willReturn(true);
        $messageFormat = '%message%';
        $dom = new \Magento\Framework\Config\Dom($fixtureXml, $validationState, [], null, null, $messageFormat);
        $actualErrors = [];
        $actualResult = $dom->validate($this->_schemaFile, $actualErrors);
        $this->assertEquals(empty($expectedErrors), $actualResult, "Validation result is invalid.");
        $this->assertEquals($expectedErrors, $actualErrors, "Validation errors does not match.");
    }

    /**
     * @return array
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function exemplarXmlDataProvider()
    {
        // @codingStandardsIgnoreStart
        return [
            /** Valid configurations */
            'valid' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.01">
                        <connection name="amqp" exchange="magento2" />
                        <connection name="db" exchange="magento2" disabled="true" />
                    </publisher>
                    <publisher topic="topic.message.queue.config.02">
                        <connection name="amqp" exchange="magento2" disabled="true"/>
                        <connection name="db" exchange="magento2" disabled="true" />
                    </publisher>
                    <publisher topic="topic.message.queue.config.03" disabled="true" />
                </config>',
                [],
            ],
            'non unique publisher topic' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.01">
                        <connection name="amqp" exchange="magento2" />
                    </publisher>
                    <publisher topic="topic.message.queue.config.01">
                        <connection name="amqp" exchange="magento2" disabled="true"/>
                    </publisher>
                </config>',
                [
                    "Element 'publisher': Duplicate key-sequence ['topic.message.queue.config.01'] in unique identity-constraint 'unique-publisher-topic'."
                ],
            ],
            'non unique publisher connection name' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.01">
                        <connection name="amqp" exchange="magento2" />
                        <connection name="amqp" exchange="magento2" />
                    </publisher>
                </config>',
                [
                    "Element 'connection': Duplicate key-sequence ['amqp'] in unique identity-constraint 'unique-connection-name'."
                ],
            ],
            'missed required publisher attribute' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher disabled="false">
                        <connection name="amqp" exchange="magento2" />                        
                    </publisher>
                </config>',
                [
                    "Element 'publisher': The attribute 'topic' is required but missing."
                ],
            ],
            'missed required connection attribute' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="top01">
                        <connection exchange="magento2" />                        
                    </publisher>
                </config>',
                [
                    "Element 'connection': The attribute 'name' is required but missing."
                ],
            ],
            'unexpected publisher element' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <unexpected name="10">20</unexpected>
                    <publisher topic="topic.message.queue.config.03" disabled="true" />
                </config>',
                [
                    "Element 'unexpected': This element is not expected. Expected is ( publisher )."
                ],
            ],
            'unexpected connection element' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.03" disabled="true">
                        <connection name="amqp" exchange="magento2" />
                        <unexpected name="10">20</unexpected>
                    </publisher>
                </config>',
                [
                    "Element 'unexpected': This element is not expected. Expected is ( connection )."
                ],
            ],
            'unexpected publisher attribute' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.03" disabled="true" unexpected="10"/>
                </config>',
                [
                    "Element 'publisher', attribute 'unexpected': The attribute 'unexpected' is not allowed.",
                ],
            ],
            'unexpected connection attribute' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.03" disabled="true">
                        <connection name="amqp" exchange="magento2" unexpected="10"/>
                    </publisher>
                </config>',
                [
                    "Element 'connection', attribute 'unexpected': The attribute 'unexpected' is not allowed.",
                ],
            ],
            'invalid connection attribute value' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.03" disabled="true">
                        <connection name="amqp" exchange="magento2" disabled="disabled"/>
                    </publisher>
                </config>',
                [
                    "Element 'connection', attribute 'disabled': 'disabled' is not a valid value of the atomic type 'xs:boolean'.",
                ],
            ],
            'invalid publisher attribute value' => [
                '<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework-message-queue:etc/publisher.xsd">
                    <publisher topic="topic.message.queue.config.03" disabled="disabled">
                        <connection name="amqp" exchange="magento2" />
                    </publisher>
                </config>',
                [
                    "Element 'publisher', attribute 'disabled': 'disabled' is not a valid value of the atomic type 'xs:boolean'.",
                ],
            ],
        ];
        // @codingStandardsIgnoreEnd
    }
}