Skip to content

Commit

Permalink
Drop PHP 7.1 from build environment and allow beberlei/assert v… (#29)
Browse files Browse the repository at this point in the history
* [CI] Drop PHP 7.1 from build environment
* [UR] Going full PHP 7.2 mode
* [CS] Fixing coding standards
* [CI] Upgrade `phpstan` to 0.11.x
* [TEST] Removed unneeded assertion in `DefaultRecordSerializerFactoryTest`
* [DOCKER] Fix build to support 7.2 and 7.3
* [TEST] Serializer factory should always produce new instances
* [DEPEND] `beberlei/assert` fails with PHP 7.3
* [DEPEND] Remove support for `symfony/serializer` 2.x
  • Loading branch information
tPl0ch authored Aug 30, 2019
1 parent 40e05df commit 267c1b3
Show file tree
Hide file tree
Showing 29 changed files with 260 additions and 94 deletions.
13 changes: 6 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ services:
- docker

php:
- '7.1'
- '7.2'
- '7.3'

env:
global:
- DOCKER_COMPOSE_VERSION=1.22.0
matrix:
- DEPENDENCIES="low"
- DEPENDENCIES="stable"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="3.2.4"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="3.3.2"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="4.0.2"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="4.1.2"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="5.0.1"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="3.3.3"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="4.1.3"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="5.2.3"
- DEPENDENCIES="stable" INTEGRATION_TEST="enabled" CONFLUENT_VERSION="latest"

before_script:
- make install-phars
Expand All @@ -38,7 +37,7 @@ script:
jobs:
include:
- stage: coverage
php: 7.2
php: '7.2'
env:
- DEPENDENCIES="stable"
script:
Expand Down
9 changes: 5 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
ARG PHP_VERSION=7.1
ARG PHP_VERSION=7.2

FROM php:${PHP_VERSION}-cli-alpine

ARG XDEBUG_VERSION=2.6.1
ARG XDEBUG_VERSION=2.7.2

RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS zlib-dev \
&& apk add --no-cache --virtual .runtime-deps git \
RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
&& apk add --no-cache --virtual .runtime-deps git libzip-dev \
&& docker-php-ext-configure zip --with-libzip=/usr/include \
&& docker-php-ext-install zip \
&& pecl install xdebug-$XDEBUG_VERSION \
&& docker-php-ext-enable xdebug \
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ZOOKEEPER_IPV4 ?= 192.168.104.101
COMPOSER ?= bin/composer.phar
PHP ?= bin/php
PHP_VERSION ?= 7.2
XDEBUG_VERSION ?= 2.6.1
XDEBUG_VERSION ?= 2.7.2
export

docker:
Expand Down
71 changes: 45 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Avro SerDe for PHP 7.1+
# Avro SerDe for PHP 7.2+

[![Build Status](https://travis-ci.org/flix-tech/avro-serde-php.svg?branch=master)](https://travis-ci.org/flix-tech/avro-serde-php)
[![Latest Stable Version](https://poser.pugx.org/flix-tech/avro-serde-php/version)](https://packagist.org/packages/flix-tech/avro-serde-php)
Expand Down Expand Up @@ -27,7 +27,7 @@ integrates FlixTech's [Schema Registry Client](https://github.com/flix-tech/sche
This library is using the [composer package manager](https://getcomposer.org/) for PHP.

```bash
composer require 'flix-tech/avro-serde-php:^1.0'
composer require 'flix-tech/avro-serde-php:^2.0'
```

## Quickstart
Expand All @@ -45,11 +45,16 @@ for more detailed information.
```php
<?php

$schemaRegistryClient = new \FlixTech\SchemaRegistryApi\Registry\CachedRegistry(
new \FlixTech\SchemaRegistryApi\Registry\PromisingRegistry(
new \GuzzleHttp\Client(['base_uri' => 'registry.example.com'])
use FlixTech\SchemaRegistryApi\Registry\Cache\AvroObjectCacheAdapter;
use FlixTech\SchemaRegistryApi\Registry\CachedRegistry;
use FlixTech\SchemaRegistryApi\Registry\PromisingRegistry;
use GuzzleHttp\Client;

$schemaRegistryClient = new CachedRegistry(
new PromisingRegistry(
new Client(['base_uri' => 'registry.example.com'])
),
new \FlixTech\SchemaRegistryApi\Registry\Cache\AvroObjectCacheAdapter()
new AvroObjectCacheAdapter()
);
```

Expand All @@ -61,14 +66,16 @@ The `RecordSerializer` is the main way you interact with this library. It provid
```php
<?php

use FlixTech\AvroSerializer\Objects\RecordSerializer;

/** @var \FlixTech\SchemaRegistryApi\Registry $schemaRegistry */
$recordSerializer = new \FlixTech\AvroSerializer\Objects\RecordSerializer(
$recordSerializer = new RecordSerializer(
$schemaRegistry,
[
// If you want to auto-register missing schemas set this to true
\FlixTech\AvroSerializer\Objects\RecordSerializer::OPTION_REGISTER_MISSING_SCHEMAS => false,
RecordSerializer::OPTION_REGISTER_MISSING_SCHEMAS => false,
// If you want to auto-register missing subjects set this to true
\FlixTech\AvroSerializer\Objects\RecordSerializer::OPTION_REGISTER_MISSING_SUBJECTS => false,
RecordSerializer::OPTION_REGISTER_MISSING_SUBJECTS => false,
]
);
```
Expand Down Expand Up @@ -126,17 +133,19 @@ is targeting a key schema.
namespace MyNamespace;

use FlixTech\AvroSerializer\Objects\SchemaResolvers\FileResolver;
use function get_class;use function is_object;
use function str_replace;

class MyRecord {}

$record = new MyRecord();

$baseDir = __DIR__ . '/files';

$inflector = function ($record, bool $isKey) {
$inflector = static function ($record, bool $isKey) {
$ext = $isKey ? '.key.avsc' : '.avsc';
$fileName = \is_object($record)
? \str_replace('\\', '.', \get_class($record))
$fileName = is_object($record)
? str_replace('\\', '.', get_class($record))
: 'default';

return $fileName . $ext;
Expand All @@ -160,6 +169,10 @@ value- or key-schemas respectively. A key schema resolver is optional.
```php
<?php

use FlixTech\AvroSerializer\Objects\SchemaResolvers\CallableResolver;
use PHPUnit\Framework\Assert;
use function Widmogrod\Functional\constt;

$valueSchemaJson = '
{
"type": "record",
Expand All @@ -170,19 +183,19 @@ $valueSchemaJson = '
]
}
';
$valueSchema = \AvroSchema::parse($valueSchemaJson);
$valueSchema = AvroSchema::parse($valueSchemaJson);

$resolver = new \FlixTech\AvroSerializer\Objects\SchemaResolvers\CallableResolver(
\Widmogrod\Functional\constt(
\AvroSchema::parse($valueSchemaJson)
$resolver = new CallableResolver(
constt(
AvroSchema::parse($valueSchemaJson)
)
);

$record = [ 'foo' => 'bar' ];

$schema = $resolver->valueSchemaFor($record);

\PHPUnit\Framework\Assert::assertEquals($schema, $valueSchema);
Assert::assertEquals($schema, $valueSchema);
```

### DefinitionInterfaceResolver
Expand Down Expand Up @@ -265,6 +278,12 @@ This library provides integrations with the [Symfony Serializer component](https
```php
<?php

use FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder;
use FlixTech\AvroSerializer\Objects\DefaultRecordSerializerFactory;
use PHPUnit\Framework\Assert;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;

class User
{
/** @var string */
Expand Down Expand Up @@ -300,7 +319,7 @@ class User
}
}

$recordSerializer = \FlixTech\AvroSerializer\Objects\DefaultRecordSerializerFactory::get(
$recordSerializer = DefaultRecordSerializerFactory::get(
getenv('SCHEMA_REGISTRY_HOST')
);

Expand All @@ -315,27 +334,27 @@ $avroSchemaJson = '{

$user = new User('Thomas', 38);

$normalizer = new \Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer();
$encoder = new \FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder($recordSerializer);
$normalizer = new GetSetMethodNormalizer();
$encoder = new AvroSerDeEncoder($recordSerializer);

$symfonySerializer = new \Symfony\Component\Serializer\Serializer([$normalizer], [$encoder]);
$symfonySerializer = new Serializer([$normalizer], [$encoder]);

$serialized = $symfonySerializer->serialize(
$user,
\FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder::FORMAT_AVRO,
AvroSerDeEncoder::FORMAT_AVRO,
[
\FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder::CONTEXT_ENCODE_SUBJECT => 'users-value',
\FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder::CONTEXT_ENCODE_WRITERS_SCHEMA => \AvroSchema::parse($avroSchemaJson),
AvroSerDeEncoder::CONTEXT_ENCODE_SUBJECT => 'users-value',
AvroSerDeEncoder::CONTEXT_ENCODE_WRITERS_SCHEMA => AvroSchema::parse($avroSchemaJson),
]
);

$deserializedUser = $symfonySerializer->deserialize(
$serialized,
User::class,
\FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder::FORMAT_AVRO
AvroSerDeEncoder::FORMAT_AVRO
);

\PHPUnit\Framework\Assert::assertEquals($deserializedUser, $user);
Assert::assertEquals($deserializedUser, $user);

```

Expand Down
12 changes: 11 additions & 1 deletion benchmarks/AvroEncodingBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class AvroEncodingBench
*/
private $schema;

/**
* @throws \AvroSchemaParseException
* @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException
*/
public function setUp(): void
{
$this->schema = \AvroSchema::parse(self::SCHEMA_JSON);
Expand Down Expand Up @@ -194,7 +198,13 @@ public function benchDecodeWithSyncCachedRegistry(): void
$this->serializers[self::SYNC_CACHED]->decodeMessage($this->messages[self::SYNC_CACHED]);
}

private function prepareTestForMode(string $mode, Registry $registry)
/**
* @param string $mode
* @param \FlixTech\SchemaRegistryApi\Registry $registry
*
* @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException
*/
private function prepareTestForMode(string $mode, Registry $registry): void
{
$result = $registry->register('test', $this->schema);
!$result instanceof PromiseInterface ?: $result->wait();
Expand Down
2 changes: 1 addition & 1 deletion bin/php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ command -v docker >/dev/null 2>&1 || { echo "docker is required to run this bina
USER=${USER:-$( id -un )}
GROUP=${GROUP:-$( id -gn )}
COMPOSER_HOME=${COMPOSER_HOME:-${HOME}/.composer}
PHP_VERSION=${PHP_VERSION:-7.1}
PHP_VERSION=${PHP_VERSION:-7.2}
DOCKER_OPTS=${DOCKER_OPTS:-'-it'}

exec docker run ${DOCKER_OPTS} --rm \
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@
}
],
"require": {
"php": "^7.1",
"beberlei/assert": "~2.7",
"flix-tech/confluent-schema-registry-api": "^6.0.0",
"php": "^7.2",
"beberlei/assert": "^2.9.9|~3.0",
"flix-tech/confluent-schema-registry-api": "~7.0",
"flix-tech/avro-php": "^3.0.0",
"widmogrod/php-functional": "^4.2|^5.0"
},
"require-dev": {
"phpunit/phpunit": "~7.0",
"phpstan/phpstan-shim": "^0.10",
"phpunit/phpunit": "~7.0,<8.0",
"phpstan/phpstan-shim": "^0.11",
"phpbench/phpbench": "~0.9",
"vlucas/phpdotenv": "~2.4",
"symfony/serializer": "^2.8|^3.1|^4.0"
"symfony/serializer": "^3.4|^4.3"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 11 additions & 0 deletions integrations/Symfony/Serializer/AvroSerDeEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function __construct(RecordSerializer $recordSerializer)
$this->recordSerializer = $recordSerializer;
}

/**
* {@inheritdoc}
*
* @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException
*/
public function decode($data, $format, array $context = [])
{
$readersSchema = $context[self::CONTEXT_DECODE_READERS_SCHEMA] ?? null;
Expand All @@ -40,6 +45,12 @@ public function supportsDecoding($format): bool
return self::FORMAT_AVRO === $format;
}

/**
* {@inheritdoc}
*
* @throws \AvroSchemaParseException
* @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException
*/
public function encode($data, $format, array $context = [])
{
$this->validateEncodeContext($context);
Expand Down
3 changes: 2 additions & 1 deletion src/Objects/Exceptions/AvroDecodingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace FlixTech\AvroSerializer\Objects\Exceptions;

use FlixTech\AvroSerializer\Objects\AvroSerializerException;
use RuntimeException;

class AvroDecodingException extends \RuntimeException implements AvroSerializerException
class AvroDecodingException extends RuntimeException implements AvroSerializerException
{
}
3 changes: 2 additions & 1 deletion src/Objects/Exceptions/AvroEncodingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace FlixTech\AvroSerializer\Objects\Exceptions;

use FlixTech\AvroSerializer\Objects\AvroSerializerException;
use RuntimeException;

class AvroEncodingException extends \RuntimeException implements AvroSerializerException
class AvroEncodingException extends RuntimeException implements AvroSerializerException
{
}
2 changes: 1 addition & 1 deletion src/Objects/Exceptions/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function forEncode($record, \AvroSchema $schema, \Exception $previ
"$exportedRecord"
Schema:
"{$schema->__toString()}"
"{(string) $schema}"
MESSAGE;

return new AvroEncodingException($message, self::ERROR_ENCODING, $previous);
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/HasSchemaDefinitionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ interface HasSchemaDefinitionInterface
{
public static function valueSchemaJson(): string;

public static function keySchemaJson();
public static function keySchemaJson(): ?string;
}
2 changes: 1 addition & 1 deletion src/Objects/RecordSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function decodeMessage(string $binaryMessage, AvroSchema $readersSchema =

/** @var \Widmogrod\Monad\Either\Either $read */
$read = $validated
->orElse(function () { throw new \InvalidArgumentException('Could not validate message wire protocol.'); })
->orElse(static function () { throw new \InvalidArgumentException('Could not validate message wire protocol.'); })
->bind($this->avroBinaryGetter)
->bind($cachedReader($readersSchema));

Expand Down
2 changes: 1 addition & 1 deletion src/Objects/SchemaResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ public function valueSchemaFor($record): AvroSchema;
*
* @return AvroSchema|null
*/
public function keySchemaFor($record);
public function keySchemaFor($record): ?AvroSchema;
}
12 changes: 11 additions & 1 deletion src/Objects/SchemaResolvers/CallableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,27 @@ class CallableResolver implements SchemaResolverInterface
*/
private $keySchemaResolverCallable;

public function __construct(callable $valueSchemaResolverCallable, callable $keySchemaResolverCallable = null)
public function __construct(callable $valueSchemaResolverCallable, ?callable $keySchemaResolverCallable = null)
{
$this->valueSchemaResolverCallable = $valueSchemaResolverCallable;
$this->keySchemaResolverCallable = $keySchemaResolverCallable;
}

/**
* {@inheritdoc}
*
* @throws \AvroSchemaParseException
*/
public function valueSchemaFor($record): AvroSchema
{
return AvroSchema::parse(\call_user_func($this->valueSchemaResolverCallable, $record));
}

/**
* {@inheritdoc}
*
* @throws \AvroSchemaParseException
*/
public function keySchemaFor($record): ?AvroSchema
{
if (!$this->keySchemaResolverCallable) {
Expand Down
Loading

0 comments on commit 267c1b3

Please sign in to comment.