Skip to content

Commit

Permalink
[5.7] Make MessageBag constructor behaviour consistent with add (#2…
Browse files Browse the repository at this point in the history
…3485)

* Make MessageBag constructor behaviour consistent with `add`

Addresses laravel/ideas#56 and the (long-closed) #13196.

This change in constructor behaviour aims to bring consistency with the
public `add` method on the MessageBag class, which would prevent
duplicate values in any given MessageBag's key.

* update test to show consistency of behaviour
  • Loading branch information
michaeldyrynda authored and taylorotwell committed Mar 12, 2018
1 parent f4b8568 commit cae41f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Illuminate/Support/MessageBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class MessageBag implements Arrayable, Countable, Jsonable, JsonSerializable, Me
public function __construct(array $messages = [])
{
foreach ($messages as $key => $value) {
$this->messages[$key] = $value instanceof Arrayable
? $value->toArray() : (array) $value;
$value = $value instanceof Arrayable ? $value->toArray() : (array) $value;

$this->messages[$key] = array_unique($value);
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/Support/SupportMessageBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,19 @@ public function testGetFormat()
$container->setFormat(':message');
$this->assertEquals(':message', $container->getFormat());
}

public function testConstructorUniquenessConsistency()
{
$messageBag = new MessageBag(['messages' => ['first', 'second', 'third', 'third']]);
$messages = $messageBag->getMessages();
$this->assertEquals(['first', 'second', 'third'], $messages['messages']);

$messageBag = new MessageBag;
$messageBag->add('messages', 'first');
$messageBag->add('messages', 'second');
$messageBag->add('messages', 'third');
$messageBag->add('messages', 'third');
$messages = $messageBag->getMessages();
$this->assertEquals(['first', 'second', 'third'], $messages['messages']);
}
}

0 comments on commit cae41f6

Please sign in to comment.