Skip to content

Commit

Permalink
Merge pull request #1 from tarantool-php/php8
Browse files Browse the repository at this point in the history
Add PHP 8 support
  • Loading branch information
rybakit committed Jun 21, 2021
2 parents c4b9458 + 2b57e50 commit 67f3e11
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 29 deletions.
101 changes: 101 additions & 0 deletions .github/workflows/qa.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
name: QA
on:
push:
workflow_dispatch:
schedule:
- cron: '40 2 * * *'

jobs:
tests:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0']
runs-on: ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: msgpack
coverage: none

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Test with phpunit
run: vendor/bin/phpunit

static-analysis:
name: Static Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
coverage: none

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Static Analysis using Psalm
run: vendor/bin/psalm --show-info=true

coding-standard:
name: Coding Standard
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Check code style
run: vendor/bin/php-cs-fixer fix --dry-run --diff --verbose .
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/vendor/
/.php_cs
/composer.lock
/phpunit.xml
.phpunit.result.cache
/.php-cs-fixer.php
/.php-cs-fixer.cache
/.phpunit.result.cache
29 changes: 23 additions & 6 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
namespace Tarantool\JobQueue\JobBuilder;

use PhpCsFixer\Config;
use PhpCsFixer\Fixer\ConfigurableFixerInterface;
use PhpCsFixer\Fixer\ConstantNotation\NativeConstantInvocationFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;

final class FilterableFixer implements FixerInterface
final class FilterableFixer implements ConfigurableFixerInterface
{
private $fixer;
private $pathRegex;

public function __construct(FixerInterface $fixer, string $pathRegex)
public function __construct(ConfigurableFixerInterface $fixer, string $pathRegex)
{
$this->fixer = $fixer;
$this->pathRegex = $pathRegex;
Expand Down Expand Up @@ -54,6 +56,21 @@ public function supports(\SplFileInfo $file) : bool

return $this->fixer->supports($file);
}

public function getDefinition() : FixerDefinitionInterface
{
return $this->fixer->getDefinition();
}

public function configure(array $configuration): void
{
$this->fixer->configure($configuration);
}

public function getConfigurationDefinition(): FixerConfigurationResolverInterface
{
return $this->fixer->getConfigurationDefinition();
}
};

$header = <<<EOF
Expand All @@ -65,7 +82,7 @@ public function supports(\SplFileInfo $file) : bool
file that was distributed with this source code.
EOF;

return Config::create()
return (new Config())
->setUsingCache(false)
->setRiskyAllowed(true)
->registerCustomFixers([
Expand All @@ -80,8 +97,8 @@ public function supports(\SplFileInfo $file) : bool
'declare_strict_types' => true,
'native_constant_invocation' => false,
'native_function_invocation' => false,
'FilterableFixer/native_constant_invocation' => true,
'FilterableFixer/native_function_invocation' => true,
'FilterableFixer/native_constant_invocation' => ['fix_built_in' => true],
'FilterableFixer/native_function_invocation' => ['include' => ['@internal']],
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019-2020 Eugene Leonovich
Copyright (c) 2019-2021 Eugene Leonovich

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# JobBuilder

[![Quality Assurance](https://github.com/tarantool-php/jobbuilder/workflows/QA/badge.svg)](https://github.com/tarantool-php/jobbuilder/actions?query=workflow%3AQA)
[![Telegram](https://img.shields.io/badge/Telegram-join%20chat-blue.svg)](https://t.me/tarantool_php)

A set of utility classes to help creating complex jobs for [Tarantool JobQueue](https://github.com/tarantool-php/jobqueue).


Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
}
],
"require": {
"php": "^7.1",
"tarantool/queue": "^0.7.0|^0.8.0"
"php": "^7.1|^8",
"tarantool/queue": "^0.9"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^7.1|^8.0"
"friendsofphp/php-cs-fixer": "^3",
"phpunit/phpunit": "^7.1|^8|^9",
"vimeo/psalm": "^3.9|^4"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 15 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
2 changes: 1 addition & 1 deletion src/JobEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static function parseCallResponse(array $response, bool $isMapResponse)

$tasks = [];
foreach ($tuples as $key => $tuple) {
$tasks[$key] = Task::createFromTuple($tuple);
$tasks[$key] = Task::fromTuple($tuple);
}

return $tasks;
Expand Down
37 changes: 22 additions & 15 deletions tests/JobEmitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,38 @@

use PHPUnit\Framework\TestCase;
use Tarantool\JobQueue\JobBuilder\JobBuilder;
use Tarantool\JobQueue\JobBuilder\JobBuilders;
use Tarantool\JobQueue\JobBuilder\JobEmitter;
use Tarantool\Queue\Queue;
use Tarantool\Queue\States;
use Tarantool\Queue\Task;

final class JobEmitterTest extends TestCase
{
use PhpUnitCompat;

public function testEmitNoJobs() : void
{
$queue = $this->createMock(Queue::class);
$queue->expects(self::never())->method('put');

$emitter = new JobEmitter();
$tasks = $emitter->emit(new JobBuilders([]), $queue);
$tasks = $emitter->emit([], $queue);

self::assertSame([], $tasks);
}

public function testEmitOneJob() : void
{
$builder = JobBuilder::fromPayload('task_data');
$task = Task::createFromTuple([42, States::READY, 'task_data']);
$task = Task::fromTuple([42, States::READY, 'task_data']);

$queue = $this->createMock(Queue::class);
$queue->expects(self::once())->method('put')
->with('task_data', [])
->with(['payload' => 'task_data'], [])
->willReturn($task);

$emitter = new JobEmitter();
$tasks = $emitter->emit(new JobBuilders([$builder]), $queue);
$tasks = $emitter->emit([$builder], $queue);

self::assertSame($task, reset($tasks));
}
Expand All @@ -55,19 +56,22 @@ public function testEmitMultipleJobsArray() : void
$builder1 = JobBuilder::fromPayload('task_data1');
$builder2 = JobBuilder::fromPayload('task_data2');

$task1 = Task::createFromTuple([42, States::READY, 'task_data1']);
$task2 = Task::createFromTuple([43, States::READY, 'task_data2']);
$task1 = Task::fromTuple([42, States::READY, 'task_data1']);
$task2 = Task::fromTuple([43, States::READY, 'task_data2']);

$queue = $this->createMock(Queue::class);
$queue->expects(self::once())->method('call')
->with('put_many', [[['task_data1', []], ['task_data2', []]]])
->with('put_many', [
[['payload' => 'task_data1'], []],
[['payload' => 'task_data2'], []],
])
->willReturn([[
[42, States::READY, 'task_data1'],
[43, States::READY, 'task_data2'],
]]);

$emitter = new JobEmitter();
$tasks = $emitter->emit(new JobBuilders([$builder1, $builder2]), $queue);
$tasks = $emitter->emit([$builder1, $builder2], $queue);

self::assertEquals([$task1, $task2], $tasks);
}
Expand All @@ -77,19 +81,22 @@ public function testEmitMultipleJobsMap() : void
$builder1 = JobBuilder::fromPayload('task_data1');
$builder2 = JobBuilder::fromPayload('task_data2');

$task1 = Task::createFromTuple([42, States::READY, 'task_data1']);
$task2 = Task::createFromTuple([43, States::READY, 'task_data2']);
$task1 = Task::fromTuple([42, States::READY, 'task_data1']);
$task2 = Task::fromTuple([43, States::READY, 'task_data2']);

$queue = $this->createMock(Queue::class);
$queue->expects(self::once())->method('call')
->with('put_many', [[['task_data1', []], 'key' => ['task_data2', []]]])
->with('put_many', [
[['payload' => 'task_data1'], []],
'key' => [['payload' => 'task_data2'], []],
])
->willReturn([[[
[42, States::READY, 'task_data1'],
'key' => [43, States::READY, 'task_data2'],
]]]);

$emitter = new JobEmitter();
$tasks = $emitter->emit(new JobBuilders([$builder1, 'key' => $builder2]), $queue);
$tasks = $emitter->emit([$builder1, 'key' => $builder2], $queue);

self::assertEquals([$task1, 'key' => $task2], $tasks);
}
Expand All @@ -104,9 +111,9 @@ public function testThrowOnInvalidCallResult() : void
->willReturn(['invalid_response']);

$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessageRegExp('/Unable to parse call response as (map|array)\./');
$this->expectExceptionMessageMatches('/Unable to parse call response as (map|array)\./');

$emitter = new JobEmitter();
$emitter->emit(new JobBuilders([$builder1, $builder2]), $queue);
$emitter->emit([$builder1, $builder2], $queue);
}
}
31 changes: 31 additions & 0 deletions tests/PhpUnitCompat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Tarantool JobBuilder package.
*
* (c) Eugene Leonovich <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tarantool\JobQueue\JobBuilder\Tests;

/**
* A compatibility layer for the legacy PHPUnit 7.
*/
trait PhpUnitCompat
{
public function expectExceptionMessageMatches(string $regularExpression) : void
{
if (\is_callable('parent::expectExceptionMessageMatches')) {
parent::expectExceptionMessageMatches($regularExpression);

return;
}

parent::expectExceptionMessageRegExp($regularExpression);
}
}

0 comments on commit 67f3e11

Please sign in to comment.