From 971ab1a84f0993c717ad7331eb34f71e000b312f Mon Sep 17 00:00:00 2001 From: Fabien Villepinte Date: Sun, 1 Oct 2017 10:48:07 +0200 Subject: [PATCH] Simplify setFilterData() --- src/Expose/FilterCollection.php | 8 +++---- tests/FilterCollectionTest.php | 42 +++++++++++++++++++++++++++++++++ tests/FilterTest.php | 27 +++++++++++++++++++-- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/Expose/FilterCollection.php b/src/Expose/FilterCollection.php index b8b7a00..eb28d2b 100644 --- a/src/Expose/FilterCollection.php +++ b/src/Expose/FilterCollection.php @@ -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); } } diff --git a/tests/FilterCollectionTest.php b/tests/FilterCollectionTest.php index 5108d95..eb9255a 100644 --- a/tests/FilterCollectionTest.php +++ b/tests/FilterCollectionTest.php @@ -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 * diff --git a/tests/FilterTest.php b/tests/FilterTest.php index 2f7ce1f..238d1ac 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -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]+'; @@ -147,4 +147,27 @@ public function testOutputAsArray() $filter->toArray(), $data ); } -} \ No newline at end of file + + /** + * 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); + } +}