Ensure that empty values do not add to or from conditions

This commit is contained in:
Oliver Davies 2018-06-01 19:31:24 +01:00
parent 634fc87897
commit 9f1d80f968
2 changed files with 31 additions and 6 deletions

View file

@ -48,9 +48,11 @@ class Filter
*/
public function from($values)
{
$this->properties['from'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
if (!empty($values)) {
$this->properties['from'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
}
return $this;
}
@ -62,9 +64,11 @@ class Filter
*/
public function to($values)
{
$this->properties['to'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
if (!empty($values)) {
$this->properties['to'] = collect($values)->map(function ($value) {
return trim($value);
})->all();
}
return $this;
}

View file

@ -58,6 +58,18 @@ class FilterTest extends TestCase
['from' => ['foo@example.com', 'bar@example.com']],
$this->filter->from(['foo@example.com', 'bar@example.com'])->getProperties()
);
}
/**
* Test that no 'from' key exists if no values were entered.
*
* @covers Filter::from()
*/
public function testNoFromPropertyExistsIfTheValueIsEmpty()
{
$this->assertArrayNotHasKey('from', $this->filter->from('')->getProperties());
$this->assertArrayNotHasKey('from', $this->filter->from([])->getProperties());
}
/**
@ -76,6 +88,15 @@ class FilterTest extends TestCase
);
}
/**
* Test that no 'to' key exists if values were entered.
*/
public function testNoToPropertyExistsIfTheValueIsEmpty()
{
$this->assertArrayNotHasKey('to', $this->filter->to('')->getProperties());
$this->assertArrayNotHasKey('to', $this->filter->to([])->getProperties());
}
/**
* @covers Filter::subject()
*/