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

Simplify setFilterData() #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 3 additions & 5 deletions src/Expose/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ public function load($path = null)
*/
public function setFilterData($data)
{
foreach ($data as $index => $config) {
if (is_object($config)) {
$config = get_object_vars($config);
}
$filter = new \Expose\Filter($config);
foreach ($data as $config) {
$filter = new \Expose\Filter();
$filter->load($config);
$this->addFilter($filter);
}
}
Expand Down
42 changes: 42 additions & 0 deletions tests/FilterCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,48 @@ public function testGetSetFilterData()
$this->assertEquals($result[0], $filter);
}

/**
* Test the getter/setter for the filter data in collection with an object
*
* @covers \Expose\FilterCollection::getFilterData
* @covers \Expose\FilterCollection::setFilterData
*/
public function testGetSetFilterDataWithObject()
{
$data = array(
(object) array('id' => 1234)
);
$filter = new \Expose\Filter();
$filter->setId(1234);

$this->collection->setFilterData($data);

$result = $this->collection->getFilterData();
$this->assertEquals($result[0], $filter);
}

/**
* Test the getter/setter for the filter data in collection with several filters
*
* @covers \Expose\FilterCollection::getFilterData
* @covers \Expose\FilterCollection::setFilterData
*/
public function testGetSetFilterDataWithMultipleFilters()
{
$filter1 = new \Expose\Filter(array('id' => 1234));
$filter2 = new \Expose\Filter(array('id' => 5678));
$data = array(
array('id' => 1234),
array('id' => 5678),
);

$this->collection->setFilterData($data);

$result = $this->collection->getFilterData();
$this->assertEquals($result[0], $filter1);
$this->assertEquals($result[1], $filter2);
}

/**
* Test the getter for the filter data in collection when requesting a single id
*
Expand Down
27 changes: 25 additions & 2 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testMatchDataValid()
* @covers \Expose\Filter::setRule
* @covers \Expose\Filter::execute
*/
public function testMAtchDataInvalid()
public function testMatchDataInvalid()
{
$data = 'barbaz';
$rule = '^foo[0-9]+';
Expand Down Expand Up @@ -147,4 +147,27 @@ public function testOutputAsArray()
$filter->toArray(), $data
);
}
}

/**
* Test that an object can be loaded
*
* @covers \Expose\Filter::load
*/
public function testLoadDataAsObject()
{
$data = new \stdClass();
$data->id = 1234;
$data->rule = '^foo[0-9]+';
$data->description = 'this is a test';
$data->tags = array('csrf', 'xss');
$data->impact = 3;

$filter = new \Expose\Filter();
$filter->load($data);
$this->assertEquals($filter->getId(), $data->id);
$this->assertEquals($filter->getRule(), $data->rule);
$this->assertEquals($filter->getDescription(), $data->description);
$this->assertEquals($filter->getTags(), $data->tags);
$this->assertEquals($filter->getImpact(), $data->impact);
}
}