diff --git a/services/Helpers/InfoboParamsValidator.php b/services/Helpers/InfoboParamsValidator.php index 5bad406..0a4a3ae 100644 --- a/services/Helpers/InfoboParamsValidator.php +++ b/services/Helpers/InfoboParamsValidator.php @@ -13,13 +13,16 @@ class InfoboParamsValidator { * @param array $params * @throws InvalidInfoboxParamsException * @todo: consider using hashmap instead of array ones validator grows + * @returns boolean */ public function validateParams( $params ) { foreach ( array_keys( $params ) as $param ) { - if ( !in_array( $param, $this->$supportedParams ) ) { + if ( !in_array( $param, $this->supportedParams ) ) { throw new InvalidInfoboxParamsException( $param ); } } + + return true; } } diff --git a/tests/InfoboxParamsValidatorTest.php b/tests/InfoboxParamsValidatorTest.php new file mode 100644 index 0000000..68502c1 --- /dev/null +++ b/tests/InfoboxParamsValidatorTest.php @@ -0,0 +1,68 @@ +setupFile = dirname( __FILE__ ) . '/../PortableInfobox.setup.php'; + parent::setUp(); + + $this->InfoboxParamsValidator = new \Wikia\PortableInfobox\Helpers\InfoboParamsValidator(); + } + + /** + * @param array $params + * @dataProvider testInfoboxParamsFailValidationDataProvider + */ + public function testInfoboxParamsFailValidation ( $params ) { + $this->setExpectedException( $this->invalidParamsExpectionNname ); + $this->InfoboxParamsValidator->validateParams( $params ); + } + + /** + * @param array $params + * @dataProvider testInfoboxParamsPassValidationDataProvider + */ + public function testInfoboxParamsPassValidation ( $params ) { + $this->assertEquals( true, $this->InfoboxParamsValidator->validateParams( $params ) ); + } + + public function testInfoboxParamsFailValidationDataProvider() { + return [ + [ + 'params' => [ + 'theme' => 'test', + 'abc' => 'def', + 'layout' => 'myLayout' + ] + ], + [ + 'params' => [ + 'abc' => 'def', + ] + ], + ]; + } + + public function testInfoboxParamsPassValidationDataProvider() { + return [ + [ + 'params' => [], + ], + [ + 'params' => [ + 'theme' => 'test', + 'theme-source' => 'loremIpsum', + 'layout' => 'myLayout' + ] + ], + [ + 'params' => [ + 'theme' => 'test', + ] + ] + ]; + } +}