Skip to content

Commit

Permalink
bump phpunit add test for attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiabasone committed Aug 5, 2024
1 parent 293af94 commit 2d0f12f
Show file tree
Hide file tree
Showing 26 changed files with 290 additions and 295 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
{
"name": "Thomas Ploch",
"email": "[email protected]"
},
{
"name": "Mattia Basone",
"email": "[email protected]"
}
],
"require": {
Expand All @@ -32,11 +36,12 @@
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "^9.4.2",
"phpunit/phpunit": "^10.5",
"phpbench/phpbench": "1.0.0-alpha2",
"vlucas/phpdotenv": "~2.4",
"symfony/serializer": "^6.4|^7.0",
"doctrine/annotations": "^1.11"
"doctrine/annotations": "^1.11",
"rector/rector": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 7 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="test/bootstrap.php" colors="true">
<coverage>
<include>
<directory suffix=".php">src/</directory>
<directory suffix=".php">integrations/</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="test/bootstrap.php" colors="true">
<testsuites>
<testsuite name="FlixTech Avro Serializer Test Suite">
<directory>test/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
<directory suffix=".php">integrations/</directory>
</include>
</source>
</phpunit>
22 changes: 6 additions & 16 deletions test/AbstractFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace FlixTech\AvroSerializer\Test;

use AvroSchema;
use PHPUnit\Framework\TestCase;

abstract class AbstractFunctionalTestCase extends TestCase
Expand Down Expand Up @@ -53,28 +52,19 @@ abstract class AbstractFunctionalTestCase extends TestCase
public const INVALID_AVRO_ENCODED_RECORD_HEX_BIN = '0c54615f6d608348';
public const INVALID_BIN_WRONG_VERSION = '44686f6d617348';

/**
* @var AvroSchema
*/
protected $avroSchema;
protected ?\AvroSchema $avroSchema;

/**
* @var AvroSchema
*/
protected $readersSchema;
protected ?\AvroSchema $readersSchema;

/**
* @var AvroSchema
*/
protected $invalidSchema;
protected ?\AvroSchema $invalidSchema;

/**
* @throws \AvroSchemaParseException
*/
protected function setUp(): void
{
$this->avroSchema = AvroSchema::parse(self::SCHEMA_JSON);
$this->readersSchema = AvroSchema::parse(self::READERS_SCHEMA_JSON);
$this->invalidSchema = AvroSchema::parse(self::INVALID_READERS_SCHEMA_JSON);
$this->avroSchema = \AvroSchema::parse(self::SCHEMA_JSON);
$this->readersSchema = \AvroSchema::parse(self::READERS_SCHEMA_JSON);
$this->invalidSchema = \AvroSchema::parse(self::INVALID_READERS_SCHEMA_JSON);
}
}
9 changes: 3 additions & 6 deletions test/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace FlixTech\AvroSerializer\Test;

use PHPUnit\Framework\Attributes\Test;
use Widmogrod\Monad\Maybe\Just;
use Widmogrod\Monad\Maybe\Nothing;

Expand All @@ -18,9 +19,7 @@

class CommonTest extends AbstractFunctionalTestCase
{
/**
* @test
*/
#[Test]
public function get_should_return_Maybe_monad(): void
{
$array = [
Expand All @@ -36,9 +35,7 @@ public function get_should_return_Maybe_monad(): void
$this->assertInstanceOf(Nothing::class, get('INVALID', $array));
}

/**
* @test
*/
#[Test]
public function getter_returns_curried_get(): void
{
$this->assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace FlixTech\AvroSerializer\Test\Integrations\Symfony\Serializer;

use PHPUnit\Framework\Attributes\Test;
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
use FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder;
use FlixTech\AvroSerializer\Integrations\Symfony\Serializer\NameConverter\AvroNameConverter;
Expand All @@ -30,19 +31,15 @@ protected function setUp(): void
);
}

/**
* @test
*/
#[Test]
public function it_should_return_the_normalized_property_name(): void
{
$normalizedName = $this->nameConverter
->normalize('name', SampleUserRecord::class, AvroSerDeEncoder::FORMAT_AVRO);
$this->assertEquals('Name', $normalizedName);
}

/**
* @test
*/
#[Test]
public function it_should_return_the_denormalized_property_name(): void
{
$normalizedName = $this->nameConverter
Expand Down
42 changes: 21 additions & 21 deletions test/Integrations/Symfony/Serializer/AvroSerDeEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace FlixTech\AvroSerializer\Test\Integrations\Symfony\Serializer;

use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\DataProvider;
use FlixTech\AvroSerializer\Integrations\Symfony\Serializer\AvroSerDeEncoder;
use FlixTech\AvroSerializer\Objects\RecordSerializer;
use FlixTech\AvroSerializer\Test\AbstractFunctionalTestCase;
Expand Down Expand Up @@ -33,27 +35,21 @@ protected function setUp(): void
$this->avroSerDeEncoder = new AvroSerDeEncoder($this->recordSerializerMock);
}

/**
* @test
*/
#[Test]
public function it_should_only_support_encoding_Avro_format(): void
{
$this->assertTrue($this->avroSerDeEncoder->supportsEncoding(AvroSerDeEncoder::FORMAT_AVRO));
$this->assertFalse($this->avroSerDeEncoder->supportsEncoding('any'));
}

/**
* @test
*/
#[Test]
public function it_should_only_support_decoding_Avro_format(): void
{
$this->assertTrue($this->avroSerDeEncoder->supportsDecoding(AvroSerDeEncoder::FORMAT_AVRO));
$this->assertFalse($this->avroSerDeEncoder->supportsDecoding('any'));
}

/**
* @test
*/
#[Test]
public function it_should_encode_with_valid_encode_context(): void
{
$context = [
Expand All @@ -75,15 +71,21 @@ public function it_should_encode_with_valid_encode_context(): void
$this->assertSame('success', $result);
}

/**
* @test
*/
#[Test]
public function it_should_decode_with_valid_decode_context(): void
{
$this->recordSerializerMock->expects($this->exactly(2))
->method('decodeMessage')
->withConsecutive([AbstractFunctionalTestCase::AVRO_ENCODED_RECORD_HEX_BIN, null], [AbstractFunctionalTestCase::AVRO_ENCODED_RECORD_HEX_BIN, $this->readersSchema])
->willReturnOnConsecutiveCalls('success-1', 'success-2');
$matcher = $this->exactly(2);
$this->recordSerializerMock->expects($matcher)
->method('decodeMessage')->willReturnCallback(function ($parameters) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([AbstractFunctionalTestCase::AVRO_ENCODED_RECORD_HEX_BIN, null], $parameters),
2 => self::assertEquals([AbstractFunctionalTestCase::AVRO_ENCODED_RECORD_HEX_BIN, $this->readersSchema], $parameters),
};
return match ($matcher->numberOfInvocations()) {
1 => 'success-1',
2 => 'success-2',
};
});

$result = $this->avroSerDeEncoder->decode(
AbstractFunctionalTestCase::AVRO_ENCODED_RECORD_HEX_BIN,
Expand All @@ -103,10 +105,8 @@ public function it_should_decode_with_valid_decode_context(): void
$this->assertSame('success-2', $result);
}

/**
* @test
* @dataProvider encodeContextValidationDataProvider
*/
#[DataProvider('encodeContextValidationDataProvider')]
#[Test]
public function it_should_validate_encode_context(array $context): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -120,7 +120,7 @@ public function it_should_validate_encode_context(array $context): void
);
}

public static function encodeContextValidationDataProvider(): ?\Generator
public static function encodeContextValidationDataProvider(): \Generator
{
yield 'Invalid writer\'s schema in encode context' => [
[
Expand Down
5 changes: 2 additions & 3 deletions test/Objects/DefaultRecordSerializerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace FlixTech\AvroSerializer\Test\Objects;

use PHPUnit\Framework\Attributes\Test;
use FlixTech\AvroSerializer\Objects\DefaultRecordSerializerFactory;
use PHPUnit\Framework\TestCase;

class DefaultRecordSerializerFactoryTest extends TestCase
{
/**
* @test
*/
#[Test]
public function it_should_produce_a_default_RecordSerializer(): void
{
$one = DefaultRecordSerializerFactory::get('http://localhost');
Expand Down
25 changes: 13 additions & 12 deletions test/Objects/RecordSerializerIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@

namespace FlixTech\AvroSerializer\Test\Objects;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\Depends;
use FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException;
use FlixTech\AvroSerializer\Objects\DefaultRecordSerializerFactory;
use FlixTech\AvroSerializer\Objects\RecordSerializer;
use FlixTech\AvroSerializer\Test\AbstractFunctionalTestCase;
use FlixTech\SchemaRegistryApi\Exception\IncompatibleAvroSchemaException;

/**
* @group integration
*/
#[Group('integration')]
class RecordSerializerIntegrationTest extends AbstractFunctionalTestCase
{
/**
* @test
*
* @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException
* @throws SchemaRegistryException
*/
#[Test]
public function it_encodes_valid_records(): RecordSerializer
{
$serializer = DefaultRecordSerializerFactory::get(\getenv('SCHEMA_REGISTRY_HOST'));
Expand All @@ -31,25 +32,25 @@ public function it_encodes_valid_records(): RecordSerializer
}

/**
* @test
*
* @depends it_encodes_valid_records
*
* @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException
* @throws SchemaRegistryException
*/
#[Depends('it_encodes_valid_records')]
#[Test]
public function it_cannot_evolve_incompatible_schema(RecordSerializer $serializer): void
{
$this->expectException(IncompatibleAvroSchemaException::class);
$serializer->encodeRecord('test-value', $this->invalidSchema, self::TEST_RECORD);
}

/**
* @test
*
* @depends it_encodes_valid_records
*
* @throws \FlixTech\SchemaRegistryApi\Exception\SchemaRegistryException
* @throws SchemaRegistryException
*/
#[Depends('it_encodes_valid_records')]
#[Test]
public function it_decodes_with_readers_schema(RecordSerializer $serializer): RecordSerializer
{
$encoded = $serializer->encodeRecord('test-value', $this->avroSchema, self::TEST_RECORD);
Expand Down
Loading

0 comments on commit 2d0f12f

Please sign in to comment.