Skip to content

Commit

Permalink
feat(BE-2913): use code style for tests, too; upgrade rdkafka (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
haeber authored Jul 14, 2022
1 parent 85903ef commit 79bed1d
Show file tree
Hide file tree
Showing 22 changed files with 142 additions and 102 deletions.
2 changes: 1 addition & 1 deletion docker/dev/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ RUN ln -s /run/secrets/ssh_host_key /root/.ssh/id_rsa && \

# PHP: Install php extensions
RUN pecl channel-update pecl.php.net && \
pecl install pcov rdkafka-6.0.2 && \
pecl install pcov rdkafka-6.0.3 && \
php-ext-enable pcov rdkafka

# COMPOSER: install binary
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<file>src</file>
<file>tests</file>
<rule ref="PSR12"/>
<arg name="report-gitblame"/>
<arg name="report-full"/>
Expand Down
34 changes: 22 additions & 12 deletions tests/AbstractSchemaRegistryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
abstract class AbstractSchemaRegistryTestCase extends TestCase
{
/**
* @param $class
* @param string $class
* @param array|null $methodMap (array of method returns, or NULL)
* 1. NULL - Methods won't be mocked, it will run original code
* 2. Array of method names - ['method_name' => 'method_value',...].
Expand All @@ -27,14 +27,24 @@ final public function makeMock(string $class, array $methodMap = []): MockObject
}

$methodNames = array_merge(
array_keys(array_filter($methodMap, static function ($key){
return !is_numeric($key);
}, ARRAY_FILTER_USE_KEY
)),
array_values(array_filter($methodMap, static function ($key){
return is_numeric($key);
}, ARRAY_FILTER_USE_KEY
))
array_keys(
array_filter(
$methodMap,
static function ($key) {
return !is_numeric($key);
},
ARRAY_FILTER_USE_KEY
)
),
array_values(
array_filter(
$methodMap,
static function ($key) {
return is_numeric($key);
},
ARRAY_FILTER_USE_KEY
)
)
);

$mock = $mockBuilder->onlyMethods($methodNames)->getMock();
Expand Down Expand Up @@ -65,12 +75,12 @@ final public function makeMock(string $class, array $methodMap = []): MockObject
* @param string $className
* @param array $array
*/
protected static function assertArrayHasInstanceOf(string $className, array $array):void
protected static function assertArrayHasInstanceOf(string $className, array $array): void
{
$filtered = array_filter($array, static function ($item) use ($className){
$filtered = array_filter($array, static function ($item) use ($className) {
return $item instanceof $className;
});

self::assertGreaterThan(0, count($filtered));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class CheckAllSchemaTemplatesDocCommentsCommandTest extends AbstractSchemaRegist
protected function setUp(): void
{
parent::setUp();
if (!file_exists(self::SCHEMA_DIRECTORY)){
if (!file_exists(self::SCHEMA_DIRECTORY)) {
mkdir(self::SCHEMA_DIRECTORY);
}
}
Expand All @@ -97,7 +97,7 @@ protected function tearDown(): void
}
}

public function testOutputWhenAllValid():void
public function testOutputWhenAllValid(): void
{
file_put_contents(
sprintf('%s/test.schema.%d.avsc', self::SCHEMA_DIRECTORY, 1),
Expand All @@ -119,7 +119,7 @@ public function testOutputWhenAllValid():void
self::assertEquals(0, $commandTester->getStatusCode());
}

public function testOutputWhenAllNotInvalid():void
public function testOutputWhenAllNotInvalid(): void
{
file_put_contents(
sprintf('%s/test.schema.bad.avsc', self::SCHEMA_DIRECTORY),
Expand Down Expand Up @@ -151,7 +151,7 @@ public function testOutputWhenAllNotInvalid():void
self::assertEquals(1, $commandTester->getStatusCode());
}

public function testExceptionWhenAllNotInvalid():void
public function testExceptionWhenAllNotInvalid(): void
{
file_put_contents(
sprintf('%s/test.schema.bad1.avsc', self::SCHEMA_DIRECTORY),
Expand Down
17 changes: 9 additions & 8 deletions tests/Command/CheckAllSchemasAreValidAvroCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class CheckAllSchemasAreValidAvroCommandTest extends AbstractSchemaRegistryTestC
protected function setUp(): void
{
parent::setUp();
if (!file_exists(self::SCHEMA_DIRECTORY)){
if (!file_exists(self::SCHEMA_DIRECTORY)) {
mkdir(self::SCHEMA_DIRECTORY);
}
}
Expand All @@ -78,7 +78,7 @@ protected function setUp(): void
protected function tearDown(): void
{
parent::tearDown();
if (file_exists(self::SCHEMA_DIRECTORY)){
if (file_exists(self::SCHEMA_DIRECTORY)) {
array_map('unlink', glob(self::SCHEMA_DIRECTORY . '/*.*'));
rmdir(self::SCHEMA_DIRECTORY);
}
Expand All @@ -88,10 +88,11 @@ protected function tearDown(): void
* @param int $numberOfFiles
* @param bool $makeBad
*/
protected function generateFiles(int $numberOfFiles, bool $makeBad = false): void {
$numbers = range(1,$numberOfFiles);
protected function generateFiles(int $numberOfFiles, bool $makeBad = false): void
{
$numbers = range(1, $numberOfFiles);

if($makeBad) {
if ($makeBad) {
file_put_contents(
sprintf('%s/test.schema.bad1.avsc', self::SCHEMA_DIRECTORY),
self::BAD_SCHEMA
Expand All @@ -103,15 +104,15 @@ protected function generateFiles(int $numberOfFiles, bool $makeBad = false): voi
);
}

array_walk($numbers , static function ($item) {
array_walk($numbers, static function ($item) {
file_put_contents(
sprintf('%s/test.schema.%d.avsc', self::SCHEMA_DIRECTORY, $item),
self::GOOD_SCHEMA
);
});
}

public function testOutputWhenAllValid():void
public function testOutputWhenAllValid(): void
{
$this->generateFiles(5);

Expand All @@ -130,7 +131,7 @@ public function testOutputWhenAllValid():void
self::assertEquals(0, $commandTester->getStatusCode());
}

public function testOutputWhenAllNotInvalid():void
public function testOutputWhenAllNotInvalid(): void
{
$this->generateFiles(5, true);

Expand Down
17 changes: 9 additions & 8 deletions tests/Command/CheckAllSchemasCompatibilityCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class CheckAllSchemasCompatibilityCommandTest extends AbstractSchemaRegistryTest
protected function setUp(): void
{
parent::setUp();
if (!file_exists(self::SCHEMA_DIRECTORY)){
if (!file_exists(self::SCHEMA_DIRECTORY)) {
mkdir(self::SCHEMA_DIRECTORY);
}
}
Expand All @@ -58,7 +58,7 @@ protected function setUp(): void
protected function tearDown(): void
{
parent::tearDown();
if (file_exists(self::SCHEMA_DIRECTORY)){
if (file_exists(self::SCHEMA_DIRECTORY)) {
array_map('unlink', glob(self::SCHEMA_DIRECTORY . '/*.*'));
rmdir(self::SCHEMA_DIRECTORY);
}
Expand All @@ -68,10 +68,11 @@ protected function tearDown(): void
* @param int $numberOfFiles
* @param string $contents
*/
protected function generateFiles(int $numberOfFiles, string $contents = self::DUMMY_SCHEMA): void {
$numbers = range(1,$numberOfFiles);
protected function generateFiles(int $numberOfFiles, string $contents = self::DUMMY_SCHEMA): void
{
$numbers = range(1, $numberOfFiles);

array_walk($numbers , static function ($item) use ($contents) {
array_walk($numbers, static function ($item) use ($contents) {
file_put_contents(
sprintf('%s/test.schema.%d.avsc', self::SCHEMA_DIRECTORY, $item),
$contents
Expand All @@ -84,13 +85,13 @@ protected function generateFiles(int $numberOfFiles, string $contents = self::DU
);
}

public function testOutputWhenAllCompatible():void
public function testOutputWhenAllCompatible(): void
{
$this->generateFiles(5);

/** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(KafkaSchemaRegistryApiClient::class, [
'checkSchemaCompatibilityForVersion' => TRUE,
'checkSchemaCompatibilityForVersion' => true,
'getSchemaDefinitionByVersion',
'getLatestSubjectVersion' => '1'
]);
Expand All @@ -115,7 +116,7 @@ public function testOutputWhenAllCompatible():void
self::assertEquals(0, $commandTester->getStatusCode());
}

public function testOutputWhenAllNotCompatible():void
public function testOutputWhenAllNotCompatible(): void
{
$this->generateFiles(5);

Expand Down
3 changes: 1 addition & 2 deletions tests/Command/CheckCompatibilityCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ public function testCommand(
$versionArgument,
string $expectedOutput,
int $expectedExitCode
):void
{
): void {
/** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(KafkaSchemaRegistryApiClient::class, [
'checkSchemaCompatibilityForVersion' => $actualCompatible,
Expand Down
12 changes: 8 additions & 4 deletions tests/Command/CheckDocCommentsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class CheckDocCommentsCommandTest extends AbstractSchemaRegistryTestCase
}
EOF;

public function testCommandSuccess(): void {
public function testCommandSuccess(): void
{
$application = new Application();
$application->add(new CheckDocCommentsCommand());
$command = $application->find('kafka-schema-registry:check:template:doc');
Expand All @@ -93,7 +94,8 @@ public function testCommandSuccess(): void {
self::assertEquals(0, $commandTester->getStatusCode());
}

public function testCommandBadSchema(): void {
public function testCommandBadSchema(): void
{
$application = new Application();
$application->add(new CheckDocCommentsCommand());
$command = $application->find('kafka-schema-registry:check:template:doc');
Expand All @@ -111,7 +113,8 @@ public function testCommandBadSchema(): void {
self::assertEquals(1, $commandTester->getStatusCode());
}

public function testCommandBadSchema1(): void {
public function testCommandBadSchema1(): void
{
$application = new Application();
$application->add(new CheckDocCommentsCommand());
$command = $application->find('kafka-schema-registry:check:template:doc');
Expand All @@ -126,7 +129,8 @@ public function testCommandBadSchema1(): void {
]);
}

public function testCommandBadSchema2(): void {
public function testCommandBadSchema2(): void
{
$application = new Application();
$application->add(new CheckDocCommentsCommand());
$command = $application->find('kafka-schema-registry:check:template:doc');
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/CheckIsRegisteredCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function argumentsDataProvider(): array
* @param string $expectedOutput
* @param int $expectedExitCode
*/
public function testCommand(?string $actualVersion, string $expectedOutput, int $expectedExitCode):void
public function testCommand(?string $actualVersion, string $expectedOutput, int $expectedExitCode): void
{
/** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(KafkaSchemaRegistryApiClient::class, [
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/GetCompatibilityModeForSchemaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class GetCompatibilityModeForSchemaCommandTest extends AbstractSchemaRegistryTestCase
{
public function testCommand():void
public function testCommand(): void
{
/** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(KafkaSchemaRegistryApiClient::class, [
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/GetLatestSchemaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testMissingSchema(): void
self::assertEquals(1, $commandTester->getStatusCode());
}

public function testUnknownClientErrorCodeThrowsException():void
public function testUnknownClientErrorCodeThrowsException(): void
{
$clientException = new ClientException('ERROR MESSAGE', 401);

Expand Down
4 changes: 2 additions & 2 deletions tests/Command/GetSchemaByVersionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GetSchemaByVersionCommandTest extends AbstractSchemaRegistryTestCase
{
protected const SCHEMA_TEST_FILE = '/tmp/test.avsc';

public function testCommand():void
public function testCommand(): void
{
$schema = [];

Expand Down Expand Up @@ -47,7 +47,7 @@ public function testCommand():void
self::assertEquals(json_encode($schema, JSON_THROW_ON_ERROR), $fileContents);
}

public function testCommandFailToReadFile():void
public function testCommandFailToReadFile(): void
{
$failurePath = '..';

Expand Down
2 changes: 1 addition & 1 deletion tests/Command/ListAllSchemasCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class ListAllSchemasCommandTest extends AbstractSchemaRegistryTestCase
{
public function testCommand():void
public function testCommand(): void
{
/** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(KafkaSchemaRegistryApiClient::class, [
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/ListVersionForSchemaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class ListVersionForSchemaCommandTest extends AbstractSchemaRegistryTestCase
{
public function testCommand():void
public function testCommand(): void
{
/** @var MockObject|KafkaSchemaRegistryApiClient $schemaRegistryApi */
$schemaRegistryApi = $this->makeMock(KafkaSchemaRegistryApiClient::class, [
Expand Down
Loading

0 comments on commit 79bed1d

Please sign in to comment.