Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
Fix PHP 7 compatibility
Browse files Browse the repository at this point in the history
Tested on 7.2. The removed test checks for non-standard
JSON handling present only in HHVM.
Adds example data from Spark 2.1.2, available under the
same license as this project.

Bug: T177273
Change-Id: I159203ac7629cf4c67504d600db4b3bfdf4208f2
  • Loading branch information
MaxSem committed Mar 28, 2018
1 parent 1cfd651 commit dfe0ddc
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A library for using [Avro](http://avro.apache.org/) with PHP.

Requirements
------------
* PHP 5
* PHP 5.5.9+
* On 32-bit platforms, the [GMP PHP extension](http://php.net/gmp)
* For testing, [PHPUnit](http://www.phpunit.de/)

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
}
],
"require": {
"php": ">=5.3.3"
"php": ">=5.5.9"
},
"require-dev": {
"jakub-onderka/php-parallel-lint": "0.9.2",
"jakub-onderka/php-console-highlighter": "0.3.2",
"phpunit/phpunit": "^4.0.0"
"phpunit/phpunit": "4.8.36"
},
"suggest": {
"ext-gmp": "Large integer support for 32-bit platforms."
Expand Down
2 changes: 1 addition & 1 deletion lib/avro/datum.php
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ public function skip_int() { return $this->skip_long(); }
protected function skip_long()
{
$b = $this->next_byte();
while (0 != ($b & 0x80))
while (0 != (ord($b) & 0x80))
$b = $this->next_byte();
}

Expand Down
2 changes: 2 additions & 0 deletions test/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require_once('FloatIntEncodingTest.php');
require_once('DatumIOTest.php');
require_once('ProtocolFileTest.php');
require_once('FileIOTest.php');
// InterOpTest tests are run separately.

/**
Expand All @@ -48,6 +49,7 @@ public static function suite()
$suite->addTestSuite('FloatIntEncodingTest');
$suite->addTestSuite('DatumIOTest');
$suite->addTestSuite('ProtocolFileTest');
$suite->addTestSuite('FileIOTest');
return $suite;
}
}
28 changes: 16 additions & 12 deletions test/DataFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ protected static function remove_data_file($data_file)

protected function remove_data_files()
{
if (self::REMOVE_DATA_FILES
&& 0 < count($this->data_files))
if (self::REMOVE_DATA_FILES && $this->data_files)
foreach ($this->data_files as $data_file)
$this->remove_data_file($data_file);
}
Expand All @@ -82,9 +81,9 @@ public function test_write_read_nothing_round_trip()
$dw->close();

$dr = AvroDataIO::open_file($data_file);
$read_data = array_shift($dr->data());
$data = $dr->data();
$dr->close();
$this->assertEquals(null, $read_data);
$this->assertEmpty($data);
}

public function test_write_read_null_round_trip()
Expand All @@ -97,7 +96,8 @@ public function test_write_read_null_round_trip()
$dw->close();

$dr = AvroDataIO::open_file($data_file);
$read_data = array_shift($dr->data());
$read_data = $dr->data();
$read_data = reset($read_data);
$dr->close();
$this->assertEquals($data, $read_data);
}
Expand All @@ -112,7 +112,8 @@ public function test_write_read_string_round_trip()
$dw->close();

$dr = AvroDataIO::open_file($data_file);
$read_data = array_shift($dr->data());
$read_data = $dr->data();
$read_data = reset($read_data);
$dr->close();
$this->assertEquals($data, $read_data);
}
Expand All @@ -130,7 +131,8 @@ public function test_write_read_round_trip()
$dw->close();

$dr = AvroDataIO::open_file($data_file);
$read_data = array_shift($dr->data());
$read_data = $dr->data();
$read_data = reset($read_data);
$dr->close();
$this->assertEquals($data, $read_data);

Expand All @@ -146,9 +148,10 @@ public function test_write_read_true_round_trip()
$dw->close();

$dr = AvroDataIO::open_file($data_file);
$read_datum = array_shift($dr->data());
$read_data = $dr->data();
$read_data = reset($read_data);
$dr->close();
$this->assertEquals($datum, $read_datum);
$this->assertEquals($datum, $read_data);
}

public function test_write_read_false_round_trip()
Expand All @@ -161,15 +164,16 @@ public function test_write_read_false_round_trip()
$dw->close();

$dr = AvroDataIO::open_file($data_file);
$read_datum = array_shift($dr->data());
$read_data = $dr->data();
$read_data = reset($read_data);
$dr->close();
$this->assertEquals($datum, $read_datum);
$this->assertEquals($datum, $read_data);
}
public function test_write_read_int_array_round_trip()
{
$data_file = $this->add_data_file('data-wr-int-ary.avr');
$writers_schema = '"int"';
$data = array(10, 20, 30, 40, 50, 60, 70);
$data = array(10, 20, 30, 40, 50, 60, 70, 567, 89012345);
$dw = AvroDataIO::open_file($data_file, 'w', $writers_schema);
foreach ($data as $datum)
$dw->append($datum);
Expand Down
95 changes: 95 additions & 0 deletions test/FileIOTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

require_once('test_helper.php');

/**
* Tests against a preexisting file created by a different library
*/
class FileIOTest extends PHPUnit_Framework_TestCase
{
protected function tearDown()
{
$file = $this->getTmpFile();
if (file_exists($file))
unlink($file);
}

private function getFileName()
{
return __DIR__ . '/data/users.avro';
}

private function getTmpFile()
{
return __DIR__ . '/tmp/users.avro';
}

private function read()
{
$fileName = $this->getFileName();
$reader = AvroDataIO::open_file($fileName);
return $reader->data();
}

public function testReading()
{
$expected = [
[
'name' => 'Alyssa',
'favorite_color' => null,
'favorite_numbers' => [3, 9, 15, 20],
],
[
'name' => 'Ben',
'favorite_color' => 'red',
'favorite_numbers' => [],
]
];
$this->assertEquals($expected, $this->read());
}

/**
* Doesn't work because due to Avro format peculiarities mean that no two
* encodings of the same data will be binary equal.
*/
public function disabled_testRoundTrip()
{
$inFile = $this->getFileName();
$outFile = $this->getTmpFile();
$schemaFile = __DIR__ . '/data/user.avsc';
$data = $this->read();
$schema = file_get_contents($schemaFile);
$writer = AvroDataIO::open_file($outFile, 'w', $schema);
foreach ($data as $record)
{
$writer->append($record);
}
$writer->close();

$oldData = file_get_contents($inFile);
$newData = file_get_contents($outFile);
if ($oldData !== $newData)
{
$diff = shell_exec("bash -c \"diff -y -W 150 <(xxd '$inFile') <(xxd '$outFile')\"");
$this->fail("Round trip failed, files not equal:\n$diff");
}
$this->assertTrue(true, 'Dummy assert to prevent this test from being marked as risky');
}
}
4 changes: 0 additions & 4 deletions test/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,6 @@ function test_json_decode()
array("bar", "baz", "blurfl"));
$this->assertFalse(is_array(json_decode('null', true)));
$this->assertEquals(json_decode('{"type": "null"}', true), array("type" => 'null'));
foreach (array('true', 'True', 'TRUE', 'tRue') as $truthy)
{
$this->assertEquals(json_decode($truthy, true), true, $truthy);
}
$this->assertEquals(json_decode('"boolean"'), 'boolean');
}

Expand Down
1 change: 1 addition & 0 deletions test/data/user.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "record", "namespace": "example.avro", "name": "User", "fields": [{"type": "string", "name": "name"}, {"type": ["string", "null"], "name": "favorite_color"}, {"type": {"items": "int", "type": "array"}, "name": "favorite_numbers"}]}
Binary file added test/data/users.avro
Binary file not shown.

0 comments on commit dfe0ddc

Please sign in to comment.