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

namespace Magento\Framework\Exception\Test\Unit;

use \Magento\Framework\Exception\InputException;
use Magento\Framework\Phrase;

class InputExceptionTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Verify that the constructor creates a single instance of InputException with the proper
     * message and array of parameters.
     *
     * @return void
     */
    public function testConstructor()
    {
        $params = ['fieldName' => 'quantity', 'value' => -100, 'minValue' => 0];
        $inputException = new InputException(
            new Phrase('The %fieldName value of "%value" must be greater than or equal to %minValue.', $params)
        );

        $this->assertEquals(
            'The %fieldName value of "%value" must be greater than or equal to %minValue.',
            $inputException->getRawMessage()
        );
        $this->assertStringMatchesFormat('%s greater than or equal to %s', $inputException->getMessage());
        $this->assertEquals(
            'The quantity value of "-100" must be greater than or equal to 0.',
            $inputException->getLogMessage()
        );
    }

    /**
     * Verify that adding multiple errors works correctly.
     *
     * @return void
     */
    public function testAddError()
    {
        $inputException = new InputException();

        $this->assertEquals('One or more input exceptions have occurred.', $inputException->getRawMessage());
        $this->assertEquals(
            'One or more input exceptions have occurred.',
            $inputException->getMessage()
        );
        $this->assertEquals('One or more input exceptions have occurred.', $inputException->getLogMessage());

        $this->assertFalse($inputException->wasErrorAdded());
        $this->assertCount(0, $inputException->getErrors());

        $inputException->addError(
            new Phrase(
                'The %fieldName value of "%value" must be greater than or equal to %minValue.',
                ['fieldName' => 'weight', 'value' => -100, 'minValue' => 1]
            )
        );
        $this->assertTrue($inputException->wasErrorAdded());
        $this->assertCount(0, $inputException->getErrors());

        $this->assertEquals(
            'The %fieldName value of "%value" must be greater than or equal to %minValue.',
            $inputException->getRawMessage()
        );
        $this->assertEquals(
            'The weight value of "-100" must be greater than or equal to 1.',
            $inputException->getMessage()
        );
        $this->assertEquals(
            'The weight value of "-100" must be greater than or equal to 1.',
            $inputException->getLogMessage()
        );

        $inputException->addError(
            new Phrase('"%fieldName" is required. Enter and try again.', ['fieldName' => 'name'])
        );
        $this->assertTrue($inputException->wasErrorAdded());
        $this->assertCount(2, $inputException->getErrors());

        $this->assertEquals('One or more input exceptions have occurred.', $inputException->getRawMessage());
        $this->assertEquals(
            'One or more input exceptions have occurred.',
            $inputException->getMessage()
        );
        $this->assertEquals('One or more input exceptions have occurred.', $inputException->getLogMessage());

        $errors = $inputException->getErrors();
        $this->assertCount(2, $errors);

        $this->assertEquals(
            'The %fieldName value of "%value" must be greater than or equal to %minValue.',
            $errors[0]->getRawMessage()
        );
        $this->assertEquals(
            'The weight value of "-100" must be greater than or equal to 1.',
            $errors[0]->getMessage()
        );
        $this->assertEquals(
            'The weight value of "-100" must be greater than or equal to 1.',
            $errors[0]->getLogMessage()
        );

        $this->assertEquals('"%fieldName" is required. Enter and try again.', $errors[1]->getRawMessage());
        $this->assertEquals('"name" is required. Enter and try again.', $errors[1]->getMessage());
        $this->assertEquals('"name" is required. Enter and try again.', $errors[1]->getLogMessage());
    }

    /**
     * Verify the message and params are not used to determine the call count
     *
     * @return void
     */
    public function testAddErrorWithSameMessage()
    {
        $rawMessage = 'Foo "%var"';
        $params = ['var' => 'Bar'];
        $expectedProcessedMessage = 'Foo "Bar"';
        $inputException = new InputException(new Phrase($rawMessage, $params));
        $this->assertEquals($rawMessage, $inputException->getRawMessage());
        $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
        $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
        $this->assertFalse($inputException->wasErrorAdded());
        $this->assertCount(0, $inputException->getErrors());

        $inputException->addError(new Phrase($rawMessage, $params));
        $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
        $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
        $this->assertTrue($inputException->wasErrorAdded());
        $this->assertCount(0, $inputException->getErrors());

        $inputException->addError(new Phrase($rawMessage, $params));
        $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
        $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
        $this->assertTrue($inputException->wasErrorAdded());

        $errors = $inputException->getErrors();
        $this->assertCount(2, $errors);
        $this->assertEquals($expectedProcessedMessage, $errors[0]->getMessage());
        $this->assertEquals($expectedProcessedMessage, $errors[0]->getLogMessage());
        $this->assertEquals($expectedProcessedMessage, $errors[1]->getMessage());
        $this->assertEquals($expectedProcessedMessage, $errors[1]->getLogMessage());
    }
}