From 96ced03f8a1d66c7f46de97197014e7a679b6937 Mon Sep 17 00:00:00 2001 From: Rafal Leszczynski Date: Thu, 11 Jun 2015 12:54:10 +0200 Subject: [PATCH] add unit tests for params validator --- services/Helpers/InfoboParamsValidator.php | 5 +- tests/InfoboxParamsValidatorTest.php | 68 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/InfoboxParamsValidatorTest.php 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', + ] + ] + ]; + } +}