InputExceptionTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Exception\Test\Unit;
  7. use \Magento\Framework\Exception\InputException;
  8. use Magento\Framework\Phrase;
  9. class InputExceptionTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * Verify that the constructor creates a single instance of InputException with the proper
  13. * message and array of parameters.
  14. *
  15. * @return void
  16. */
  17. public function testConstructor()
  18. {
  19. $params = ['fieldName' => 'quantity', 'value' => -100, 'minValue' => 0];
  20. $inputException = new InputException(
  21. new Phrase('The %fieldName value of "%value" must be greater than or equal to %minValue.', $params)
  22. );
  23. $this->assertEquals(
  24. 'The %fieldName value of "%value" must be greater than or equal to %minValue.',
  25. $inputException->getRawMessage()
  26. );
  27. $this->assertStringMatchesFormat('%s greater than or equal to %s', $inputException->getMessage());
  28. $this->assertEquals(
  29. 'The quantity value of "-100" must be greater than or equal to 0.',
  30. $inputException->getLogMessage()
  31. );
  32. }
  33. /**
  34. * Verify that adding multiple errors works correctly.
  35. *
  36. * @return void
  37. */
  38. public function testAddError()
  39. {
  40. $inputException = new InputException();
  41. $this->assertEquals('One or more input exceptions have occurred.', $inputException->getRawMessage());
  42. $this->assertEquals(
  43. 'One or more input exceptions have occurred.',
  44. $inputException->getMessage()
  45. );
  46. $this->assertEquals('One or more input exceptions have occurred.', $inputException->getLogMessage());
  47. $this->assertFalse($inputException->wasErrorAdded());
  48. $this->assertCount(0, $inputException->getErrors());
  49. $inputException->addError(
  50. new Phrase(
  51. 'The %fieldName value of "%value" must be greater than or equal to %minValue.',
  52. ['fieldName' => 'weight', 'value' => -100, 'minValue' => 1]
  53. )
  54. );
  55. $this->assertTrue($inputException->wasErrorAdded());
  56. $this->assertCount(0, $inputException->getErrors());
  57. $this->assertEquals(
  58. 'The %fieldName value of "%value" must be greater than or equal to %minValue.',
  59. $inputException->getRawMessage()
  60. );
  61. $this->assertEquals(
  62. 'The weight value of "-100" must be greater than or equal to 1.',
  63. $inputException->getMessage()
  64. );
  65. $this->assertEquals(
  66. 'The weight value of "-100" must be greater than or equal to 1.',
  67. $inputException->getLogMessage()
  68. );
  69. $inputException->addError(
  70. new Phrase('"%fieldName" is required. Enter and try again.', ['fieldName' => 'name'])
  71. );
  72. $this->assertTrue($inputException->wasErrorAdded());
  73. $this->assertCount(2, $inputException->getErrors());
  74. $this->assertEquals('One or more input exceptions have occurred.', $inputException->getRawMessage());
  75. $this->assertEquals(
  76. 'One or more input exceptions have occurred.',
  77. $inputException->getMessage()
  78. );
  79. $this->assertEquals('One or more input exceptions have occurred.', $inputException->getLogMessage());
  80. $errors = $inputException->getErrors();
  81. $this->assertCount(2, $errors);
  82. $this->assertEquals(
  83. 'The %fieldName value of "%value" must be greater than or equal to %minValue.',
  84. $errors[0]->getRawMessage()
  85. );
  86. $this->assertEquals(
  87. 'The weight value of "-100" must be greater than or equal to 1.',
  88. $errors[0]->getMessage()
  89. );
  90. $this->assertEquals(
  91. 'The weight value of "-100" must be greater than or equal to 1.',
  92. $errors[0]->getLogMessage()
  93. );
  94. $this->assertEquals('"%fieldName" is required. Enter and try again.', $errors[1]->getRawMessage());
  95. $this->assertEquals('"name" is required. Enter and try again.', $errors[1]->getMessage());
  96. $this->assertEquals('"name" is required. Enter and try again.', $errors[1]->getLogMessage());
  97. }
  98. /**
  99. * Verify the message and params are not used to determine the call count
  100. *
  101. * @return void
  102. */
  103. public function testAddErrorWithSameMessage()
  104. {
  105. $rawMessage = 'Foo "%var"';
  106. $params = ['var' => 'Bar'];
  107. $expectedProcessedMessage = 'Foo "Bar"';
  108. $inputException = new InputException(new Phrase($rawMessage, $params));
  109. $this->assertEquals($rawMessage, $inputException->getRawMessage());
  110. $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
  111. $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
  112. $this->assertFalse($inputException->wasErrorAdded());
  113. $this->assertCount(0, $inputException->getErrors());
  114. $inputException->addError(new Phrase($rawMessage, $params));
  115. $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
  116. $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
  117. $this->assertTrue($inputException->wasErrorAdded());
  118. $this->assertCount(0, $inputException->getErrors());
  119. $inputException->addError(new Phrase($rawMessage, $params));
  120. $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
  121. $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
  122. $this->assertTrue($inputException->wasErrorAdded());
  123. $errors = $inputException->getErrors();
  124. $this->assertCount(2, $errors);
  125. $this->assertEquals($expectedProcessedMessage, $errors[0]->getMessage());
  126. $this->assertEquals($expectedProcessedMessage, $errors[0]->getLogMessage());
  127. $this->assertEquals($expectedProcessedMessage, $errors[1]->getMessage());
  128. $this->assertEquals($expectedProcessedMessage, $errors[1]->getLogMessage());
  129. }
  130. }