Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
InputExceptionTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Framework\Exception\InputException;
11 
12 class InputExceptionTest extends \PHPUnit\Framework\TestCase
13 {
20  public function testConstructor()
21  {
22  $params = ['fieldName' => 'quantity', 'value' => -100, 'minValue' => 0];
23  $inputException = new InputException(
24  new Phrase('The %fieldName value of "%value" must be greater than or equal to %minValue.', $params)
25  );
26 
27  $this->assertEquals(
28  'The %fieldName value of "%value" must be greater than or equal to %minValue.',
29  $inputException->getRawMessage()
30  );
31  $this->assertStringMatchesFormat('%s greater than or equal to %s', $inputException->getMessage());
32  $this->assertEquals(
33  'The quantity value of "-100" must be greater than or equal to 0.',
34  $inputException->getLogMessage()
35  );
36  }
37 
43  public function testAddError()
44  {
45  $inputException = new InputException();
46 
47  $this->assertEquals('One or more input exceptions have occurred.', $inputException->getRawMessage());
48  $this->assertEquals(
49  'One or more input exceptions have occurred.',
50  $inputException->getMessage()
51  );
52  $this->assertEquals('One or more input exceptions have occurred.', $inputException->getLogMessage());
53 
54  $this->assertFalse($inputException->wasErrorAdded());
55  $this->assertCount(0, $inputException->getErrors());
56 
57  $inputException->addError(
58  new Phrase(
59  'The %fieldName value of "%value" must be greater than or equal to %minValue.',
60  ['fieldName' => 'weight', 'value' => -100, 'minValue' => 1]
61  )
62  );
63  $this->assertTrue($inputException->wasErrorAdded());
64  $this->assertCount(0, $inputException->getErrors());
65 
66  $this->assertEquals(
67  'The %fieldName value of "%value" must be greater than or equal to %minValue.',
68  $inputException->getRawMessage()
69  );
70  $this->assertEquals(
71  'The weight value of "-100" must be greater than or equal to 1.',
72  $inputException->getMessage()
73  );
74  $this->assertEquals(
75  'The weight value of "-100" must be greater than or equal to 1.',
76  $inputException->getLogMessage()
77  );
78 
79  $inputException->addError(
80  new Phrase('"%fieldName" is required. Enter and try again.', ['fieldName' => 'name'])
81  );
82  $this->assertTrue($inputException->wasErrorAdded());
83  $this->assertCount(2, $inputException->getErrors());
84 
85  $this->assertEquals('One or more input exceptions have occurred.', $inputException->getRawMessage());
86  $this->assertEquals(
87  'One or more input exceptions have occurred.',
88  $inputException->getMessage()
89  );
90  $this->assertEquals('One or more input exceptions have occurred.', $inputException->getLogMessage());
91 
92  $errors = $inputException->getErrors();
93  $this->assertCount(2, $errors);
94 
95  $this->assertEquals(
96  'The %fieldName value of "%value" must be greater than or equal to %minValue.',
97  $errors[0]->getRawMessage()
98  );
99  $this->assertEquals(
100  'The weight value of "-100" must be greater than or equal to 1.',
101  $errors[0]->getMessage()
102  );
103  $this->assertEquals(
104  'The weight value of "-100" must be greater than or equal to 1.',
105  $errors[0]->getLogMessage()
106  );
107 
108  $this->assertEquals('"%fieldName" is required. Enter and try again.', $errors[1]->getRawMessage());
109  $this->assertEquals('"name" is required. Enter and try again.', $errors[1]->getMessage());
110  $this->assertEquals('"name" is required. Enter and try again.', $errors[1]->getLogMessage());
111  }
112 
118  public function testAddErrorWithSameMessage()
119  {
120  $rawMessage = 'Foo "%var"';
121  $params = ['var' => 'Bar'];
122  $expectedProcessedMessage = 'Foo "Bar"';
123  $inputException = new InputException(new Phrase($rawMessage, $params));
124  $this->assertEquals($rawMessage, $inputException->getRawMessage());
125  $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
126  $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
127  $this->assertFalse($inputException->wasErrorAdded());
128  $this->assertCount(0, $inputException->getErrors());
129 
130  $inputException->addError(new Phrase($rawMessage, $params));
131  $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
132  $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
133  $this->assertTrue($inputException->wasErrorAdded());
134  $this->assertCount(0, $inputException->getErrors());
135 
136  $inputException->addError(new Phrase($rawMessage, $params));
137  $this->assertEquals($expectedProcessedMessage, $inputException->getMessage());
138  $this->assertEquals($expectedProcessedMessage, $inputException->getLogMessage());
139  $this->assertTrue($inputException->wasErrorAdded());
140 
141  $errors = $inputException->getErrors();
142  $this->assertCount(2, $errors);
143  $this->assertEquals($expectedProcessedMessage, $errors[0]->getMessage());
144  $this->assertEquals($expectedProcessedMessage, $errors[0]->getLogMessage());
145  $this->assertEquals($expectedProcessedMessage, $errors[1]->getMessage());
146  $this->assertEquals($expectedProcessedMessage, $errors[1]->getLogMessage());
147  }
148 }
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$errors
Definition: overview.phtml:9