Remove $this reference in static method
authorErik Bernhardson <ebernhardson@wikimedia.org>
Wed, 23 Sep 2015 21:07:44 +0000 (14:07 -0700)
committerEBernhardson <ebernhardson@wikimedia.org>
Wed, 23 Sep 2015 21:08:36 +0000 (21:08 +0000)
Adds a couple tests to demonstrate the problem and fixes
it.

Change-Id: Ib15088e83ad333fb126446fad86f97ae12ff6e74

includes/utils/AvroValidator.php
tests/phpunit/includes/utils/AvroValidatorTest.php

index 4f8e0b1..89341ea 100644 (file)
@@ -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 );
index 52c242c..d63af9a 100644 (file)
@@ -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' )
+                       ),
                );
        }