@@ -35,9 +35,9 @@ protected function setUp()
3535 public function testAddMessage ()
3636 {
3737 $ messages = [
38- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
39- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
40- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
38+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_1 ' ] ),
39+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_2 ' ] ),
40+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_3 ' ] ),
4141 ];
4242
4343 foreach ($ messages as $ message ) {
@@ -58,12 +58,12 @@ public function testAddMessage()
5858 public function testGetItems ()
5959 {
6060 $ messages = [
61- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
62- $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class),
63- $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class),
64- $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class),
65- $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class),
66- $ this ->objectManager ->getObject (\Magento \Framework \Message \Success::class),
61+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_1 ' ] ),
62+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class, [ ' text ' => ' notice_1 ' ] ),
63+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class, [ ' text ' => ' notice_2 ' ] ),
64+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class, [ ' text ' => ' warning_1 ' ] ),
65+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class, [ ' text ' => ' warning_2 ' ] ),
66+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Success::class, [ ' text ' => ' success_1 ' ] ),
6767 ];
6868
6969 foreach ($ messages as $ message ) {
@@ -83,13 +83,13 @@ public function testGetItems()
8383 public function testGetItemsByType ()
8484 {
8585 $ messages = [
86- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
87- $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class),
88- $ this ->objectManager ->getObject (\Magento \Framework \Message \Success::class),
89- $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class),
90- $ this ->objectManager ->getObject (\Magento \Framework \Message \Success::class),
91- $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class),
92- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
86+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_1 ' ] ),
87+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class, [ ' text ' => ' notice_1 ' ] ),
88+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Success::class, [ ' text ' => ' success_1 ' ] ),
89+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class, [ ' text ' => ' notice_2 ' ] ),
90+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Success::class, [ ' text ' => ' success_2 ' ] ),
91+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class, [ ' text ' => ' warning_1 ' ] ),
92+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_2 ' ] ),
9393 ];
9494
9595 $ messageTypes = [
@@ -124,12 +124,12 @@ public function testGetItemsByType()
124124 public function testGetErrors ()
125125 {
126126 $ messages = [
127- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
128- $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class),
129- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
130- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
131- $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class),
132- $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class),
127+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_1 ' ] ),
128+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Notice::class, [ ' text ' => ' notice_1 ' ] ),
129+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_2 ' ] ),
130+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_3 ' ] ),
131+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Warning::class, [ ' text ' => ' warning_1 ' ] ),
132+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, [ ' text ' => ' error_4 ' ] ),
133133 ];
134134
135135 foreach ($ messages as $ message ) {
@@ -208,4 +208,54 @@ public function testClearWithSticky()
208208 $ this ->model ->clear ();
209209 $ this ->assertEquals (1 , $ this ->model ->getCount ());
210210 }
211+
212+ /**
213+ * Test adding duplicate messages is not allowed.
214+ *
215+ * @covers \Magento\Framework\Message\Collection::addMessage
216+ */
217+ public function testAddingDuplicateMessagesCount ()
218+ {
219+ $ messages = [
220+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, ['text ' => 'some_text ' ]),
221+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, ['text ' => 'some_text ' ]),
222+ ];
223+
224+ foreach ($ messages as $ message ) {
225+ $ this ->model ->addMessage ($ message );
226+ }
227+
228+ $ this ->assertCount (
229+ 1 ,
230+ $ this ->model ->getItemsByType (MessageInterface::TYPE_ERROR )
231+ );
232+ }
233+
234+ /**
235+ * Test getting last added message after adding duplicate messages.
236+ *
237+ * @covers \Magento\Framework\Message\Collection::addMessage
238+ */
239+ public function testAddingDuplicateMessagesLast ()
240+ {
241+ $ messages = [
242+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, ['text ' => 'some_text ' ]),
243+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, ['text ' => 'another_text ' ]),
244+ $ this ->objectManager ->getObject (\Magento \Framework \Message \Error::class, ['text ' => 'some_text ' ]),
245+ ];
246+
247+ foreach ($ messages as $ message ) {
248+ $ this ->model ->addMessage ($ message );
249+ }
250+ $ lastMessage = array_pop ($ messages );
251+
252+ $ this ->assertEquals (
253+ $ messages ,
254+ $ this ->model ->getItems ($ messages )
255+ );
256+ $ this ->assertEquals (
257+ $ lastMessage ,
258+ $ this ->model ->getLastAddedMessage ()
259+ );
260+ }
211261}
0 commit comments