Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed breadcrumbs to accept empty string #427

Merged
merged 7 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Breadcrumbs/Breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,16 @@ class Breadcrumb
*/
public function __construct($name, $type, array $metaData = [])
{
if (!is_string($name) || $name === '') {
throw new InvalidArgumentException('The breadcrumb name must be a non-empty string.');
if (!is_string($name)) {
if (is_null($name)) {
$name = 'NULL breadcrumb name given';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these cases can be condensed to the same name, "<no name>" with a validation error in the breadcrumb metadata to indicate a configuration issue. That way, the presentation is consistent and any future interface to searching/filtering breadcrumbs can aggregate on a single name value or breadcrumbs with a BreadcrumbError, etc.

  • Breadcrumb name is null?
    • <no name> - ['BreadcrumbError' => 'NULL provided as the breadcrumb name']
  • Breadcrumb name is empty?
    • <no name> - ['BreadcrumbError' => 'Empty string provided as the breadcrumb name']
  • Breadcrumb name is not a string?
    • <no name> - ['BreadcrumbError' => 'Breadcrumb name must be a string - "Integer" provided instead']

} else {
$name = 'INVALID breadcrumb name given';
}
}

if ($name === '') {
$name = 'EMPTY breadcrumb name given';
}

if (strlen($name) > static::MAX_LENGTH) {
Expand Down
20 changes: 10 additions & 10 deletions tests/Breadcrumbs/BreadcrumbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@

class BreadcrumbTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The breadcrumb name must be a non-empty string.
*/
public function testBadName()
{
new Breadcrumb(123, 'error');
$breadcrumb = new Breadcrumb(123, 'error');
$this->assertSame('INVALID breadcrumb name given', $breadcrumb->toArray()['name']);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The breadcrumb name must be a non-empty string.
*/
public function testEmptyName()
{
new Breadcrumb('', 'error');
$breadcrumb = new Breadcrumb('', 'error');
$this->assertSame('EMPTY breadcrumb name given', $breadcrumb->toArray()['name']);
}

public function testNullName()
{
$breadcrumb = new Breadcrumb(null, 'error');
$this->assertSame('NULL breadcrumb name given', $breadcrumb->toArray()['name']);
}

/**
Expand Down