Refactor, allow chaining methods

This commit is contained in:
Oliver Davies 2017-11-03 23:22:13 +00:00
parent 792bfda017
commit 0594f9f0de
3 changed files with 72 additions and 25 deletions

View file

@ -10,6 +10,7 @@
}
],
"require": {
"tightenco/collect": "^5.4"
},
"require-dev": {
"phpunit/phpunit": "^5.7"

View file

@ -4,49 +4,71 @@ namespace Opdavies\GmailFilterBuilder;
class Filter
{
private $properties = [];
public function has($value)
{
return ['hasTheWord' => $value];
$this->properties['hasTheWord'] = $value;
return $this;
}
public function from()
{
return ['from' => func_get_args()];
$this->properties['from'] = collect(func_get_args())
->map(function ($address) {
return trim($address);
})->implode(',');
return $this;
}
public function label($label)
{
return ['label' => $label];
$this->properties['label'] = $label;
return $this;
}
public function archive()
{
return ['shouldArchive' => 'true'];
$this->properties['shouldArchive'] = 'true';
return $this;
}
public function labelAndArchive($label)
{
return $this->label($label) + $this->archive();
$this->label($label)->archive();
return $this;
}
public function spam()
{
return [
'shouldSpam' => 'true',
'shouldNeverSpam' => 'false',
];
$this->properties['shouldSpam'] = 'true';
$this->properties['shouldNeverSpam'] = 'false';
return $this;
}
public function neverSpam()
{
return [
'shouldSpam' => 'false',
'shouldNeverSpam' => 'true',
];
$this->properties['shouldSpam'] = 'false';
$this->properties['shouldNeverSpam'] = 'true';
return $this;
}
public function trash()
{
return ['shouldTrash' => 'true'];
$this->properties['shouldTrash'] = 'true';
return $this;
}
public function getProperties()
{
return $this->properties;
}
}

View file

@ -28,7 +28,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(
['hasTheWord' => 'something'],
$this->filter->has('something')
$this->filter->has('something')->getProperties()
);
}
@ -39,14 +39,16 @@ class FilterTest extends \PHPUnit_Framework_TestCase
{
// Ensure that we can set one from address.
$this->assertEquals(
['from' => ['foo@example.com']],
$this->filter->from('foo@example.com')
['from' => 'foo@example.com'],
$this->filter->from('foo@example.com')->getProperties()
);
// Ensure that we can set multiple from addresses.
$this->assertEquals(
['from' => ['foo@example.com', 'bar@example.com']],
$this->filter->from('foo@example.com', 'bar@example.com')
['from' => 'foo@example.com,bar@example.com'],
$this->filter
->from('foo@example.com', 'bar@example.com')
->getProperties()
);
}
@ -55,7 +57,10 @@ class FilterTest extends \PHPUnit_Framework_TestCase
*/
public function testLabel()
{
$this->assertEquals(['label' => 'Foo'], $this->filter->label('Foo'));
$this->assertEquals(
['label' => 'Foo'],
$this->filter->label('Foo')->getProperties()
);
}
/**
@ -65,7 +70,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(
['shouldArchive' => 'true'],
$this->filter->archive()
$this->filter->archive()->getProperties()
);
}
@ -76,7 +81,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(
['shouldArchive' => 'true', 'label' => 'Foo'],
$this->filter->labelAndArchive('Foo')
$this->filter->labelAndArchive('Foo')->getProperties()
);
}
@ -90,7 +95,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
'shouldSpam' => 'true',
'shouldNeverSpam' => 'false'
],
$this->filter->spam()
$this->filter->spam()->getProperties()
);
}
@ -104,7 +109,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase
'shouldSpam' => 'false',
'shouldNeverSpam' => 'true'
],
$this->filter->neverSpam()
$this->filter->neverSpam()->getProperties()
);
}
@ -115,7 +120,26 @@ class FilterTest extends \PHPUnit_Framework_TestCase
{
$this->assertEquals(
['shouldTrash' => 'true'],
$this->filter->trash()
$this->filter->trash()->getProperties()
);
}
public function testMethodsCanBeChained()
{
$this->assertEquals(
[
'from' => 'foo@example.com,bar@example.com',
'hasTheWord' => 'Something',
'label' => 'Foo',
'shouldArchive' => 'true',
'shouldNeverSpam' => 'true',
'shouldSpam' => 'false',
],
$this->filter->from('foo@example.com ', 'bar@example.com')
->has('Something')
->labelAndArchive('Foo')
->neverSpam()
->getProperties()
);
}
}