From 91aebb607209837c52899047ac68a17297382aa6 Mon Sep 17 00:00:00 2001 From: Erik Bernhardson Date: Wed, 23 Sep 2015 14:07:44 -0700 Subject: [PATCH 1/1] Remove $this reference in static method Adds a couple tests to demonstrate the problem and fixes it. Change-Id: Ib15088e83ad333fb126446fad86f97ae12ff6e74 --- includes/utils/AvroValidator.php | 9 +++------ .../phpunit/includes/utils/AvroValidatorTest.php | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/includes/utils/AvroValidator.php b/includes/utils/AvroValidator.php index 4f8e0b177b..89341eaf23 100644 --- a/includes/utils/AvroValidator.php +++ b/includes/utils/AvroValidator.php @@ -93,22 +93,19 @@ class AvroValidator { } $errors = array(); foreach ($datum as $d) { - $result = $this->validate( $schema->items(), $d ); + $result = self::getErrors( $schema->items(), $d ); if ( $result ) { $errors[] = $result; } } - if ( $errors ) { - return $errors; - } - return array(); + return $errors; case AvroSchema::MAP_SCHEMA: if (!is_array($datum)) { return self::wrongType( 'array', $datum ); } $errors = array(); foreach ($datum as $k => $v) { - if ( !is_string($k) ) { + if ( !is_string($k) ) { $errors[] = self::wrongType( 'string key', $k ); } $result = self::getErrors( $schema->values(), $v ); diff --git a/tests/phpunit/includes/utils/AvroValidatorTest.php b/tests/phpunit/includes/utils/AvroValidatorTest.php index 52c242c17d..d63af9af57 100644 --- a/tests/phpunit/includes/utils/AvroValidatorTest.php +++ b/tests/phpunit/includes/utils/AvroValidatorTest.php @@ -19,6 +19,10 @@ class AvroValidatorTest extends PHPUnit_Framework_TestCase { public function getErrorsProvider() { $stringSchema = AvroSchema::parse( json_encode( array( 'type' => 'string' ) ) ); + $stringArraySchema = AvroSchema::parse( json_encode( array( + 'type' => 'array', + 'items' => 'string', + ) ) ); $recordSchema = AvroSchema::parse( json_encode( array( 'type' => 'record', 'name' => 'ut', @@ -80,6 +84,18 @@ class AvroValidatorTest extends PHPUnit_Framework_TestCase { ) ) ) ), + array( + 'Empty array is accepted', + $stringArraySchema, array(), array() + ), + array( + 'correct array element accepted', + $stringArraySchema, array( 'fizzbuzz' ), array() + ), + array( + 'incorrect array element rejected', + $stringArraySchema, array( '12', 34 ), array( 'Expected string, but recieved integer' ) + ), ); } -- 2.20.1