diff --git a/README.md b/README.md index 5604baf..d9664ae 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/composer.json b/composer.json index cfab5e9..766f86a 100644 --- a/composer.json +++ b/composer.json @@ -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." diff --git a/lib/avro/datum.php b/lib/avro/datum.php index fb70760..3a5351f 100644 --- a/lib/avro/datum.php +++ b/lib/avro/datum.php @@ -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(); } diff --git a/test/AllTests.php b/test/AllTests.php index b2be17a..fc4921a 100644 --- a/test/AllTests.php +++ b/test/AllTests.php @@ -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. /** @@ -48,6 +49,7 @@ public static function suite() $suite->addTestSuite('FloatIntEncodingTest'); $suite->addTestSuite('DatumIOTest'); $suite->addTestSuite('ProtocolFileTest'); + $suite->addTestSuite('FileIOTest'); return $suite; } } diff --git a/test/DataFileTest.php b/test/DataFileTest.php index 1924d98..da23e9b 100644 --- a/test/DataFileTest.php +++ b/test/DataFileTest.php @@ -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); } @@ -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() @@ -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); } @@ -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); } @@ -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); @@ -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() @@ -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); diff --git a/test/FileIOTest.php b/test/FileIOTest.php new file mode 100644 index 0000000..cb5bd9d --- /dev/null +++ b/test/FileIOTest.php @@ -0,0 +1,95 @@ +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'); + } +} diff --git a/test/SchemaTest.php b/test/SchemaTest.php index 9a963b8..483177f 100644 --- a/test/SchemaTest.php +++ b/test/SchemaTest.php @@ -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'); } diff --git a/test/data/user.avsc b/test/data/user.avsc new file mode 100644 index 0000000..a396d73 --- /dev/null +++ b/test/data/user.avsc @@ -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"}]} diff --git a/test/data/users.avro b/test/data/users.avro new file mode 100644 index 0000000..27c526a Binary files /dev/null and b/test/data/users.avro differ