diff --git a/.gitattributes b/.gitattributes index 021bd70..31eff10 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,8 +2,7 @@ tests export-ignore .gitattributes export-ignore .gitignore export-ignore -.php_cs.dist export-ignore -.scrutinizer.yml export-ignore +.php-cs-fixer.dist.php export-ignore dockerfile.sh export-ignore phpunit.xml.dist export-ignore psalm.xml export-ignore diff --git a/.github/workflows/qa.yml b/.github/workflows/qa.yml index c9a5989..4377f91 100644 --- a/.github/workflows/qa.yml +++ b/.github/workflows/qa.yml @@ -1,70 +1,115 @@ name: QA on: push: + branches: + - master pull_request: workflow_dispatch: schedule: - cron: '40 2 * * *' +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + jobs: tests: + permissions: + actions: read + contents: read env: - TNT_IMAGE: tarantool/tarantool:2.11 - PHP_IMAGE: php:8.2-cli + TNT_LISTEN_URI: localhost:3301 strategy: + fail-fast: false + max-parallel: 10 matrix: - operating-system: [ubuntu-latest] - env: - - PHP_IMAGE: php:7.2-cli - - - PHP_IMAGE: php:7.3-cli - QA: 1 - - - PHP_IMAGE: php:7.4-cli - COVERAGE_FILE: coverage.clover - - - PHP_IMAGE: php:8.0-cli - - PHP_IMAGE: php:8.1-cli - - - TNT_IMAGE: tarantool/tarantool:1.7 - - TNT_IMAGE: tarantool/tarantool:1.9 - - TNT_IMAGE: tarantool/tarantool:1 - - TNT_IMAGE: tarantool/tarantool:2.1 - - TNT_IMAGE: tarantool/tarantool:2.3 - - TNT_IMAGE: tarantool/tarantool:2.4 - - TNT_IMAGE: tarantool/tarantool:2.5 - - TNT_IMAGE: tarantool/tarantool:2.6 - - TNT_IMAGE: tarantool/tarantool:2.7 - - TNT_IMAGE: tarantool/tarantool:2.8 - - TNT_IMAGE: tarantool/tarantool:2.10 + operating-system: [ubuntu-24.04] + tarantool-release: ['2', '3'] + php-version: ['8.2', '8.3', '8.4', '8.5'] + include: + - php-version: '8.2' + tarantool-release: '3' + QA: true + - php-version: '8.3' + tarantool-release: '3' + COVERAGE: true + name: PHP ${{ matrix.php-version }}, Tarantool ${{ matrix.tarantool-release }}${{ matrix.QA && ', qa' || '' }}${{ matrix.COVERAGE && ', coverage' || '' }} runs-on: ${{ matrix.operating-system }} steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v7 + + - name: Install Tarantool and tt + env: + TNT_RELEASE: ${{ matrix.tarantool-release }} + run: | + curl -L "https://tarantool.io/release/$TNT_RELEASE/installer.sh" | sudo bash + sudo apt-get install -y tarantool tarantool-dev tt + tarantool --version + tt version - - name: Build docker image - env: ${{ matrix.env }} + - name: Install the tarantool/queue module + run: tt rocks install queue + + - name: Build PHP image + env: + PHP_IMAGE: php:${{ matrix.php-version }}-cli + TNT_LISTEN_URI: localhost:3301 + COVERAGE_FILE: ${{ matrix.COVERAGE && 'coverage.xml' || '' }} run: ./dockerfile.sh | tee /dev/tty | docker build -t queue - - name: Test - env: ${{ matrix.env }} run: | - docker network create tarantool-php - docker run --net=tarantool-php --rm $TNT_IMAGE /usr/local/bin/tarantool --version - docker run -d --net=tarantool-php --name=tarantool -v `pwd`:/queue -e TNT_LISTEN_URI=$TNT_LISTEN_URI ${TNT_IMAGE} tarantool /queue/tests/Integration/queues.lua - docker run --rm --net=tarantool-php -v `pwd`:/queue -w /queue queue + tarantool ./tests/Integration/queues.lua > tarantool.log 2>&1 & + for attempt in $(seq 1 30); do + if bash -c 'echo > /dev/tcp/127.0.0.1/3301' 2>/dev/null; then + ready=1 + break + fi + sleep 1 + done + if [ "${ready:-}" != 1 ]; then + cat tarantool.log + exit 1 + fi + docker run --rm --network=host -v "$PWD:/queue" -w /queue queue - name: Check code quality - env: ${{ matrix.env }} - if: ${{ env.QA }} + if: ${{ matrix.QA }} run: | - docker run --net=tarantool-php --rm -v $PWD:/queue -w /queue queue php vendor/bin/php-cs-fixer fix --dry-run --diff --verbose . - docker run --net=tarantool-php --rm -v $PWD:/queue -w /queue queue php vendor/bin/psalm + docker run --rm --network=host -v "$PWD:/queue" -w /queue queue php vendor/bin/php-cs-fixer fix --dry-run --diff --verbose . + docker run --rm --network=host -v "$PWD:/queue" -w /queue queue php vendor/bin/psalm - - name: Upload code coverage to Scrutinizer - env: ${{ matrix.env }} - if: ${{ env.COVERAGE_FILE }} - run: | - curl -sSOL https://scrutinizer-ci.com/ocular.phar - docker run --rm -v $(pwd):/queue -w /queue queue php ocular.phar code-coverage:upload --format=php-clover "$COVERAGE_FILE" + - name: Upload code coverage report + if: ${{ matrix.COVERAGE }} + uses: actions/upload-artifact@v7 + with: + name: coverage-${{ strategy.job-index }} + path: coverage.xml + + coverage: + needs: tests + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + statuses: write + steps: + - name: Checkout + uses: actions/checkout@v7 + + - name: Download coverage reports + uses: actions/download-artifact@v8 + with: + pattern: coverage-* + path: coverage-reports + + - name: Add combined code coverage report to workflow summary + uses: getsentry/coverage-action@0.3.8 + with: + token: ${{ secrets.GITHUB_TOKEN }} + directory: coverage-reports + coverage-format: cobertura + enable-tests: false + name: full-test-matrix diff --git a/.gitignore b/.gitignore index d9db3c6..ed4279c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /vendor/ /.phpunit.result.cache +/.phpunit.cache/ /composer.lock /coverage* /phpunit.xml diff --git a/.php_cs.dist b/.php-cs-fixer.dist.php similarity index 76% rename from .php_cs.dist rename to .php-cs-fixer.dist.php index 6c1e197..0fe3237 100644 --- a/.php_cs.dist +++ b/.php-cs-fixer.dist.php @@ -5,22 +5,34 @@ namespace Tarantool\Queue; 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; + private ConfigurableFixerInterface $fixer; + private string $pathRegex; - public function __construct(FixerInterface $fixer, string $pathRegex) + public function __construct(ConfigurableFixerInterface $fixer, string $pathRegex) { $this->fixer = $fixer; $this->pathRegex = $pathRegex; } + public function configure(array $configuration) : void + { + $this->fixer->configure($configuration); + } + + public function getConfigurationDefinition() : FixerConfigurationResolverInterface + { + return $this->fixer->getConfigurationDefinition(); + } + public function isCandidate(Tokens $tokens) : bool { return $this->fixer->isCandidate($tokens); @@ -36,6 +48,11 @@ public function fix(\SplFileInfo $file, Tokens $tokens) : void $this->fixer->fix($file, $tokens); } + public function getDefinition() : FixerDefinitionInterface + { + return $this->fixer->getDefinition(); + } + public function getName() : string { return (new \ReflectionClass($this))->getShortName().'/'.$this->fixer->getName(); @@ -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([ diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index c1a456c..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,22 +0,0 @@ -checks: - php: true - -tools: - php_mess_detector: true - php_cpd: true - php_loc: - excluded_dirs: [vendor] - php_pdepend: - excluded_dirs: [vendor] - external_code_coverage: - runs: 1 - -build: - environment: - php: - version: 7.4 - nodes: - analysis: - tests: - override: - - php-scrutinizer-run diff --git a/README.md b/README.md index fe127b7..142c939 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,18 @@ # Tarantool Queue [![Quality Assurance](https://github.com/tarantool-php/queue/workflows/QA/badge.svg)](https://github.com/tarantool-php/queue/actions?query=workflow%3AQA) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/tarantool-php/queue/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/tarantool-php/queue/?branch=master) -[![Code Coverage](https://scrutinizer-ci.com/g/tarantool-php/queue/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/tarantool-php/queue/?branch=master) [![Mentioned in Awesome PHP](https://awesome.re/mentioned-badge.svg)](https://github.com/ziadoz/awesome-php) [![Telegram](https://img.shields.io/badge/Telegram-join%20chat-blue.svg)](https://t.me/tarantool_php) -[Tarantool](https://www.tarantool.io/en/developers/) is a NoSQL database running in a Lua application server. It integrates -Lua modules, called [LuaRocks](https://luarocks.org/). This package provides PHP bindings for +[Tarantool](https://www.tarantool.io/en/developers/) is a NoSQL database and Lua application server. It supports Lua +modules, which can be installed with [LuaRocks](https://luarocks.org/). This package provides PHP bindings for the [Tarantool Queue LuaRock](https://github.com/tarantool/queue/). ## Table of contents * [Installation](#installation) - * [Before start](#before-start) + * [Before you start](#before-you-start) * [Working with queue](#working-with-queue) * [Data types](#data-types) * [Tasks](#tasks) @@ -35,10 +33,9 @@ composer require tarantool/queue ``` -## Before start +## Before you start -In order to use queue, you first need to make sure that your Tarantool instance -is configured, up and running. The minimal required configuration might look like this: +To use the queue, make sure your Tarantool instance is configured and running. A minimal configuration looks like this: ```lua -- queues.lua @@ -52,17 +49,17 @@ queue.create_tube('foobar', 'fifottl', {if_not_exists = true}) > *You can read more about the box configuration in the official Tarantool [documentation](http://tarantool.org/doc/book/configuration/index.html#initialization-file). > More information on queue configuration can be found [here](https://github.com/tarantool/queue/blob/master/README.md).* -To start the instance you need to copy (or symlink) `queues.lua` file into the `/etc/tarantool/instances.enabled` -directory and run the following command: +To start the instance, copy (or symlink) the `queues.lua` file into the `/etc/tarantool/instances.enabled` directory +and run the following command: ```bash -sudo tarantoolctl start queues +sudo tt start queues ``` ## Working with queue -Once you have your instance running, you can start by creating a queue object with the queue (tube) name you defined +Once you have your instance running, create a queue object using the queue (tube) name you defined in the Lua script: ```php @@ -78,8 +75,8 @@ where `$client` is an instance of `Tarantool\Client\Client` from the [tarantool/ ### Data types -Under the hood Tarantool uses [MessagePack](http://msgpack.org/) binary format to serialize/deserialize -data being stored in a queue. It can handle most of the PHP data types (except resources and closures) without +Under the hood, Tarantool uses the [MessagePack](http://msgpack.org/) binary format to serialize and deserialize +data stored in a queue. It can handle most PHP data types (except resources and closures) without any manual pre- or post-processing: ```php @@ -91,13 +88,13 @@ $queue->put(['foo' => ['bar' => ['baz' => null]]]); $queue->put(new MyObject()); ``` -> *To learn more about object serialization, please follow this [link](https://github.com/tarantool-php/client#user-defined-types).* +> *To learn more about object serialization, see [User-defined types](https://github.com/tarantool-php/client#user-defined-types).* ### Tasks -Most of the [Queue API](src/Queue.php) methods return a [Task](src/Task.php) object -containing the following getters: +Most [Queue API](src/Queue.php) methods return a [Task](src/Task.php) object +that provides the following getters: ```php Task::getId() @@ -105,7 +102,7 @@ Task::getState() // States::READY, States::TAKEN, States::DONE, States::BURY or Task::getData() ``` -And some sugar methods: +It also provides these convenience methods: ```php Task::isReady() @@ -118,10 +115,9 @@ Task::isDelayed() ### Producer API -As you've already seen, to insert a task into a queue you need to call `put()` method, which accepts -two arguments: the data you want to process and optional array of task options, which this particular -queue supports. For example, `fifottl` queue (which we defined [earlier](#before-start) in our Lua config -file), supports `delay`, `ttl`, `ttr` and `pri` options: +As you've already seen, to insert a task into a queue, call the `put()` method. It accepts two arguments: the data you +want to process and an optional array of task options supported by the queue. For example, the `fifottl` queue, which +we defined [earlier](#before-you-start) in our Lua configuration, supports the `delay`, `ttl`, `ttr`, and `pri` options: ```php use Tarantool\Queue\Options; @@ -131,13 +127,13 @@ $queue->put('bar', [Options::TTL => 5.0]); $queue->put('baz', [Options::TTR => 10.0, Options::PRI => 42]); ``` -> *See the full list of available options [here](https://github.com/tarantool/queue#queue-types).* +> *See the [full list of available options](https://github.com/tarantool/queue#queue-types).* ### Consumer API -To reserve a task for execution, call `take()` method. It accepts an optional `timeout` parameter. -If a timeout value is supplied the call will wait `timeout` seconds until a `READY` task appears in the queue. +To reserve a task for execution, call the `take()` method. It accepts an optional `timeout` parameter. +If you supply a timeout, the call waits for up to that many seconds for a `READY` task to appear in the queue. The method returns either a [Task](#tasks) object or `null`: ```php @@ -150,7 +146,7 @@ $taskOrNull = $queue->take(2.0); $taskOrNull = $queue->take(.1); ``` -After successful execution, a task can be marked as acknowledged (that will also delete the task from a queue): +After successful execution, acknowledge the task to delete it from the queue: ```php $data = $task->getData(); @@ -160,16 +156,16 @@ $data = $task->getData(); $task = $queue->ack($task->getId()); ``` -Or put back into the queue in case it cannot be executed: +If the task cannot be executed, return it to the queue: ```php $task = $queue->release($task->getId()); -// for *ttl queues you can specify a delay +// For *ttl queues, you can specify a delay. $task = $queue->release($task->getId(), [Options::DELAY => 30.0]); ``` -To look at a task without changing its state, use: +To view a task without changing its state, use: ```php $task = $queue->peek($task->getId()); @@ -181,19 +177,19 @@ To bury (disable) a task: $task = $queue->bury($task->getId()); ``` -To reset buried task(s) back to `READY` state: +To return buried tasks to the `READY` state: ```php $count = $queue->kick(3); // kick 3 buried tasks ``` -To increase TTR and/or TTL of a running task (only for *ttl queues): +To increase the TTR or TTL of a running task (only for `*ttl` queues): ```php $taskOrNull = $queue->touch($takenTask->getId(), 5.0); // increase ttr/ttl to 5 seconds ``` -A task (in any state) can be deleted permanently with `delete()`: +You can permanently delete a task in any state by calling `delete()`: ```php $task = $queue->delete($task->getId()); @@ -212,8 +208,7 @@ $queue->truncate(); ### Statistics -The `stats()` method provides access to the statistical information accumulated -since a queue was created: +The `stats()` method returns statistics accumulated since the queue was created: ```php $stats = $queue->stats(); @@ -250,9 +245,9 @@ $total = $queue->stats('tasks.total'); ### Custom methods -Thanks to flexible nature of the [queue](https://github.com/tarantool/queue/) Lua module, you can easily create -your own queue drivers or extend existing ones with an additional functionality. For example, suppose you added -the `put_many` method to your `foobar` queue, which inserts multiple tasks atomically: +Thanks to the flexibility of the [queue](https://github.com/tarantool/queue/) Lua module, you can create your own queue +drivers or extend existing ones with additional functionality. For example, suppose you add a `put_many` method to your +`foobar` queue to insert multiple tasks atomically: ```lua -- queues.lua @@ -272,7 +267,7 @@ queue.tube.foobar.put_many = function(self, items) end ``` -To invoke this method from php, use `Queue::call()`: +To invoke this method from PHP, use `Queue::call()`: ```php $result = $queue->call('put_many', [ @@ -299,15 +294,14 @@ docker run -d --net=tarantool-php -p 3301:3301 --name=tarantool \ tarantool/tarantool:2 tarantool /queues.lua ``` -And then run both unit and integration tests: +Then run both unit and integration tests: ```bash docker run --rm --net=tarantool-php -v $(pwd):/queue -w /queue queue ``` -The library uses [PHPUnit](https://phpunit.de/) under the hood, and if needed, -you can pass additional arguments and options to the `phpunit` command. -For example, to run only unit tests, execute: +The tests use [PHPUnit](https://phpunit.de/). You can pass additional arguments and options to the `phpunit` command. +For example, run only the unit tests with: ```bash docker run --rm --net=tarantool-php -v $(pwd):/queue -w /queue queue \ diff --git a/composer.json b/composer.json index cd247b6..f7bff94 100644 --- a/composer.json +++ b/composer.json @@ -11,13 +11,14 @@ } ], "require": { - "php": "^7.2.5|^8", - "tarantool/client": "^0.10" + "php": "^8.2", + "tarantool/client": "^0.11" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.19", - "tarantool/phpunit-extras": "^0.2", - "vimeo/psalm": "^3.9|^4" + "friendsofphp/php-cs-fixer": "^3.0", + "phpunit/phpunit": "^10.5", + "tarantool/phpunit-extras": "^0.3.2", + "vimeo/psalm": "^5.23|^6" }, "autoload": { "psr-4": { diff --git a/dockerfile.sh b/dockerfile.sh index 0f8601e..1e940e5 100755 --- a/dockerfile.sh +++ b/dockerfile.sh @@ -25,5 +25,5 @@ ENV PATH=~/.composer/vendor/bin:\$PATH ENV TNT_LISTEN_URI=$TNT_LISTEN_URI CMD if [ ! -f composer.lock ]; then composer install; fi && \\ - vendor/bin/phpunit ${COVERAGE_FILE:+ --coverage-text --coverage-clover=}$COVERAGE_FILE + vendor/bin/phpunit ${COVERAGE_FILE:+ --coverage-text --coverage-cobertura=}$COVERAGE_FILE " diff --git a/phpunit.xml.dist b/phpunit.xml.dist index c3102ae..683bb91 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,32 +1,24 @@ - - - - - - - - - - - - - - tests/Unit - - - - tests/Integration - - - - - - src - - + + + + + + + + + + + tests/Unit + + + tests/Integration + tests/Integration/QueueTest.php + + + + + src + + diff --git a/psalm.xml b/psalm.xml index 30258a7..44ba4d8 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,6 +6,12 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" > + + + + + + diff --git a/src/Queue.php b/src/Queue.php index 152f9ae..7b3ec7e 100644 --- a/src/Queue.php +++ b/src/Queue.php @@ -17,13 +17,10 @@ class Queue { - private $client; - private $name; - - public function __construct(Client $client, string $name) - { - $this->client = $client; - $this->name = $name; + public function __construct( + private readonly Client $client, + private readonly string $name, + ) { } public function getClient() : Client @@ -36,17 +33,14 @@ public function getName() : string return $this->name; } - /** - * @param mixed $data - */ - public function put($data, array $options = []) : Task + public function put(mixed $data, array $options = []) : Task { return Task::fromTuple( $this->client->call("queue.tube.$this->name:put", $data, $options)[0] ); } - public function take(float $timeout = null) : ?Task + public function take(?float $timeout = null) : ?Task { $result = $this->client->call("queue.tube.$this->name:take", $timeout); @@ -118,7 +112,7 @@ public function stats(?string $path = null) return $stats; } - foreach (\explode('.', $path) as $key) { + foreach (explode('.', $path) as $key) { if (!isset($stats[$key])) { throw new \InvalidArgumentException(\sprintf('Invalid path "%s"', $path)); } @@ -128,9 +122,6 @@ public function stats(?string $path = null) return $stats; } - /** - * @param mixed ...$args - */ public function call(string $methodName, ...$args) : array { return $this->client->call("queue.tube.$this->name:$methodName", ...$args); diff --git a/src/Task.php b/src/Task.php index 264771d..7494a97 100644 --- a/src/Task.php +++ b/src/Task.php @@ -15,20 +15,11 @@ final class Task { - private $id; - private $state; - - /** @var mixed */ - private $data; - - /** - * @param mixed $data - */ - private function __construct(int $id, string $state, $data) - { - $this->id = $id; - $this->state = $state; - $this->data = $data; + private function __construct( + private readonly int $id, + private readonly string $state, + private readonly mixed $data, + ) { } public static function fromTuple(array $tuple) : self @@ -48,10 +39,7 @@ public function getState() : string return $this->state; } - /** - * @return mixed - */ - public function getData() + public function getData() : mixed { return $this->data; } diff --git a/tests/Integration/QueueTest.php b/tests/Integration/QueueTest.php index 5034f05..ef22fa2 100644 --- a/tests/Integration/QueueTest.php +++ b/tests/Integration/QueueTest.php @@ -13,16 +13,14 @@ namespace Tarantool\Queue\Tests\Integration; +use PHPUnit\Framework\Attributes\DataProvider; +use Tarantool\PhpUnit\Attribute\Lua; use Tarantool\Queue\States; abstract class QueueTest extends TestCase { - /** - * @dataProvider provideTaskData - * @lua create_tube('%tube_name%', '%tube_type%') - * - * @param mixed $data - */ + #[DataProvider('provideTaskData')] + #[Lua("create_tube('%tube_name%', '%tube_type%')")] public function testPut($data) : void { $task = $this->queue->put($data); @@ -30,7 +28,7 @@ public function testPut($data) : void self::assertTask($task, 0, States::READY, $data); } - public function provideTaskData() : iterable + public static function provideTaskData() : array { return [ [null], @@ -44,10 +42,8 @@ public function provideTaskData() : iterable ]; } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('peek_0') - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('peek_0')")] public function testPeek() : void { $task = $this->queue->peek(0); @@ -55,10 +51,8 @@ public function testPeek() : void self::assertTask($task, 0, States::READY, 'peek_0'); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('take') - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('take')")] public function testTake() : void { $task = $this->queue->take(); @@ -66,9 +60,7 @@ public function testTake() : void self::assertTask($task, 0, States::TAKEN, 'take'); } - /** - * @lua create_tube('%tube_name%', '%tube_type%') - */ + #[Lua("create_tube('%tube_name%', '%tube_type%')")] public function testTakeNone() : void { $time = time(); @@ -78,11 +70,9 @@ public function testTakeNone() : void self::assertNull($task); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('release_0') - * @lua tube:take() - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('release_0')")] + #[Lua('tube:take()')] public function testRelease() : void { $task = $this->queue->release(0); @@ -90,11 +80,9 @@ public function testRelease() : void self::assertTask($task, 0, States::READY, 'release_0'); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('ack_0') - * @lua tube:take() - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('ack_0')")] + #[Lua('tube:take()')] public function testAck() : void { $task = $this->queue->ack(0); @@ -102,10 +90,8 @@ public function testAck() : void self::assertTask($task, 0, States::DONE, 'ack_0'); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('bury_0') - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('bury_0')")] public function testBury() : void { $task = $this->queue->bury(0); @@ -113,11 +99,9 @@ public function testBury() : void self::assertTask($task, 0, States::BURIED, 'bury_0'); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('kick_1') - * @lua tube:bury(0) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('kick_1')")] + #[Lua('tube:bury(0)')] public function testKickOne() : void { $count = $this->queue->kick(1); @@ -125,15 +109,13 @@ public function testKickOne() : void self::assertSame(1, $count); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('kick_1') - * @lua tube:put('kick_2') - * @lua tube:put('kick_3') - * @lua tube:bury(0) - * @lua tube:bury(1) - * @lua tube:bury(2) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('kick_1')")] + #[Lua("tube:put('kick_2')")] + #[Lua("tube:put('kick_3')")] + #[Lua('tube:bury(0)')] + #[Lua('tube:bury(1)')] + #[Lua('tube:bury(2)')] public function testKickMany() : void { $count = $this->queue->kick(3); @@ -141,10 +123,8 @@ public function testKickMany() : void self::assertSame(3, $count); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('delete_0') - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('delete_0')")] public function testDelete() : void { $task = $this->queue->delete(0); @@ -152,11 +132,9 @@ public function testDelete() : void self::assertTask($task, 0, States::DONE, 'delete_0'); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('truncate_0') - * @lua tube:put('truncate_1') - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('truncate_0')")] + #[Lua("tube:put('truncate_1')")] public function testTruncate() : void { self::assertSame(2, $this->queue->stats('tasks.total')); @@ -166,9 +144,7 @@ public function testTruncate() : void self::assertSame(0, $this->queue->stats('tasks.total')); } - /** - * @lua create_tube('%tube_name%', '%tube_type%') - */ + #[Lua("create_tube('%tube_name%', '%tube_type%')")] public function testTruncateEmpty() : void { self::assertSame(0, $this->queue->stats('tasks.total')); @@ -178,26 +154,28 @@ public function testTruncateEmpty() : void self::assertSame(0, $this->queue->stats('tasks.total')); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('stat_0') - * @lua tube:put('stat_1') - * @lua tube:put('stat_2') - * @lua tube:put('stat_3') - * @lua tube:put('stat_4') - * @lua tube:delete(4) - * @lua tube:take(.001) - * @lua tube:release(0) - * @lua tube:take(.001) - * @lua tube:ack(0) - * @lua tube:bury(1) - * @lua tube:bury(2) - * @lua tube:kick(1) - * @lua tube:take(.001) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('stat_0')")] + #[Lua("tube:put('stat_1')")] + #[Lua("tube:put('stat_2')")] + #[Lua("tube:put('stat_3')")] + #[Lua("tube:put('stat_4')")] + #[Lua('tube:delete(4)')] + #[Lua('tube:take(.001)')] + #[Lua('tube:release(0)')] + #[Lua('tube:take(.001)')] + #[Lua('tube:ack(0)')] + #[Lua('tube:bury(1)')] + #[Lua('tube:bury(2)')] + #[Lua('tube:kick(1)')] + #[Lua('tube:take(.001)')] public function testStats() : void { $stats = $this->queue->stats(); + if (\array_key_exists('extra', $stats)) { + self::assertSame([], $stats['extra']); + unset($stats['extra']); + } self::assertEquals([ 'tasks' => [ @@ -220,18 +198,18 @@ public function testStats() : void 'ttr' => 0, 'delay' => 0, 'ttl' => 0, - ], - 'extra' => [ ], ], $stats); } - /** - * @lua create_tube('%tube_name%', '%tube_type%') - */ + #[Lua("create_tube('%tube_name%', '%tube_type%')")] public function testEmptyStats() : void { $stats = $this->queue->stats(); + if (\array_key_exists('extra', $stats)) { + self::assertSame([], $stats['extra']); + unset($stats['extra']); + } self::assertEquals([ 'tasks' => [ @@ -254,37 +232,33 @@ public function testEmptyStats() : void 'ttr' => 0, 'delay' => 0, 'ttl' => 0, - ], - 'extra' => [ ], ], $stats); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('stat_0') - * @lua tube:put('stat_1') - * @lua tube:put('stat_2') - * @lua tube:put('stat_3') - * @lua tube:put('stat_4') - * @lua tube:put('stat_5') - * @lua tube:put('stat_6') - * @lua tube:put('stat_7') - * @lua tube:put('stat_8') - * @lua tube:put('stat_9') - * @lua tube:take(.001) - * @lua tube:release(0) - * @lua tube:take(.001) - * @lua tube:ack(0) - * @lua tube:bury(1) - * @lua tube:bury(2) - * @lua tube:bury(3) - * @lua tube:kick(1) - * @lua tube:delete(6) - * @lua tube:delete(7) - * @lua tube:delete(8) - * @lua tube:take(.001) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('stat_0')")] + #[Lua("tube:put('stat_1')")] + #[Lua("tube:put('stat_2')")] + #[Lua("tube:put('stat_3')")] + #[Lua("tube:put('stat_4')")] + #[Lua("tube:put('stat_5')")] + #[Lua("tube:put('stat_6')")] + #[Lua("tube:put('stat_7')")] + #[Lua("tube:put('stat_8')")] + #[Lua("tube:put('stat_9')")] + #[Lua('tube:take(.001)')] + #[Lua('tube:release(0)')] + #[Lua('tube:take(.001)')] + #[Lua('tube:ack(0)')] + #[Lua('tube:bury(1)')] + #[Lua('tube:bury(2)')] + #[Lua('tube:bury(3)')] + #[Lua('tube:kick(1)')] + #[Lua('tube:delete(6)')] + #[Lua('tube:delete(7)')] + #[Lua('tube:delete(8)')] + #[Lua('tube:take(.001)')] public function testStatsPath() : void { self::assertEquals([ @@ -327,10 +301,8 @@ public function testStatsPath() : void self::assertSame(3, $this->queue->stats('calls.bury')); } - /** - * @dataProvider provideStatsInvalidPathData - * @lua create_tube('%tube_name%', '%tube_type%') - */ + #[DataProvider('provideStatsInvalidPathData')] + #[Lua("create_tube('%tube_name%', '%tube_type%')")] public function testStatsInvalidPath(?string $path) : void { $this->expectException(\InvalidArgumentException::class); @@ -339,7 +311,7 @@ public function testStatsInvalidPath(?string $path) : void $this->queue->stats($path); } - public function provideStatsInvalidPathData() : iterable + public static function provideStatsInvalidPathData() : array { return [ [''], @@ -354,10 +326,8 @@ public function provideStatsInvalidPathData() : iterable ]; } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube.pow = function(self, base, exp) return math.pow(base, exp) end - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua('tube.pow = function(self, base, exp) return math.pow(base, exp) end')] public function testCall() : void { $result = $this->queue->call('pow', 2, 8); @@ -365,9 +335,7 @@ public function testCall() : void self::assertSame(256, $result[0]); } - /** - * @dataProvider provideFailureCallbackData - */ + #[DataProvider('provideFailureCallbackData')] public function testThrowException(string $methodName, array $args) : void { $this->expectException(\Throwable::class); @@ -375,7 +343,7 @@ public function testThrowException(string $methodName, array $args) : void $this->queue->$methodName(...$args); } - public function provideFailureCallbackData() : iterable + public static function provideFailureCallbackData() : array { return [ ['ack', [42]], diff --git a/tests/Integration/TestCase.php b/tests/Integration/TestCase.php index 09c7e7c..a88b503 100644 --- a/tests/Integration/TestCase.php +++ b/tests/Integration/TestCase.php @@ -13,17 +13,15 @@ namespace Tarantool\Queue\Tests\Integration; -use PHPUnitExtras\Annotation\AnnotationProcessorBuilder; +use PHPUnit\Framework\Attributes\Before; +use PHPUnitExtras\Attribute\AttributeProcessorBuilder; use Tarantool\Client\Client; use Tarantool\PhpUnit\TestCase as BaseTestCase; use Tarantool\Queue\Queue; use Tarantool\Queue\Task; -use Tarantool\Queue\Tests\PhpUnitCompat; abstract class TestCase extends BaseTestCase { - use PhpUnitCompat; - /** @var Client|null */ private $client; @@ -32,8 +30,8 @@ abstract class TestCase extends BaseTestCase public function getQueueName() : string { - $methodName = $this->getName(false); - if (0 === strpos($methodName, 'test')) { + $methodName = $this->name(); + if (str_starts_with($methodName, 'test')) { $methodName = substr($methodName, 4); } @@ -58,9 +56,9 @@ final protected function getClient() : Client return $this->client = Client::fromDefaults(); } - if (0 === strpos($uri, '/')) { + if (str_starts_with($uri, '/')) { $dsn = 'unix://'.$uri; - } elseif (0 === strpos($uri, 'unix/:')) { + } elseif (str_starts_with($uri, 'unix/:')) { $dsn = 'unix://'.substr($uri, 6); } elseif (ctype_digit($uri)) { $dsn = 'tcp://127.0.0.1:'.$uri; @@ -71,15 +69,13 @@ final protected function getClient() : Client return $this->client = Client::fromDsn($dsn); } - final protected function createAnnotationProcessorBuilder() : AnnotationProcessorBuilder + final protected function createAttributeProcessorBuilder() : AttributeProcessorBuilder { - return parent::createAnnotationProcessorBuilder() + return parent::createAttributeProcessorBuilder() ->addPlaceholderResolver(new TubePlaceholderResolver($this)); } - /** - * @before - */ + #[Before] final protected function initQueue() : void { $this->queue = new Queue($this->getClient(), $this->getQueueName()); diff --git a/tests/Integration/Ttl.php b/tests/Integration/Ttl.php index ef2850a..e9fe409 100644 --- a/tests/Integration/Ttl.php +++ b/tests/Integration/Ttl.php @@ -13,15 +13,14 @@ namespace Tarantool\Queue\Tests\Integration; +use Tarantool\PhpUnit\Attribute\Lua; use Tarantool\Queue\Queue; use Tarantool\Queue\States; trait Ttl { - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('ttr_1', {ttr = 1}) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('ttr_1', {ttr = 1})")] public function testTimeToRun() : void { $queue = $this->getQueue(); @@ -35,10 +34,8 @@ public function testTimeToRun() : void self::assertEquals($task1, $task2); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('ttl_1', {ttl = 1}) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('ttl_1', {ttl = 1})")] public function testTimeToLive() : void { sleep(1); @@ -47,10 +44,8 @@ public function testTimeToLive() : void self::assertNull($task); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('touch_ttr_1', {ttr = 1}) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('touch_ttr_1', {ttr = 1})")] public function testTouchTimeToRun() : void { $queue = $this->getQueue(); @@ -66,10 +61,8 @@ public function testTouchTimeToRun() : void self::assertNull($task3); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('touch_ttl_1', {ttl = 1}) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('touch_ttl_1', {ttl = 1})")] public function testTouchTimeToLive() : void { $queue = $this->getQueue(); @@ -86,10 +79,8 @@ public function testTouchTimeToLive() : void self::assertEquals($task2, $task3); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('touch_invalid_interval') - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('touch_invalid_interval')")] public function testTouchInvalidInterval() : void { $queue = $this->getQueue(); @@ -100,11 +91,9 @@ public function testTouchInvalidInterval() : void } } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('pri_low', {pri = 2}) - * @lua tube:put('pri_high', {pri = 1}) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('pri_low', {pri = 2})")] + #[Lua("tube:put('pri_high', {pri = 1})")] public function testPriority() : void { $queue = $this->getQueue(); @@ -119,10 +108,8 @@ public function testPriority() : void self::assertSame('pri_low', $task2->getData()); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('delay_1', {delay = 1}) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('delay_1', {delay = 1})")] public function testDelay() : void { $queue = $this->getQueue(); @@ -136,11 +123,9 @@ public function testDelay() : void self::assertTask($task, 0, States::TAKEN, 'delay_1'); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('release_0') - * @lua tube:take() - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('release_0')")] + #[Lua('tube:take()')] public function testDelayedRelease() : void { $queue = $this->getQueue(); @@ -156,11 +141,9 @@ public function testDelayedRelease() : void self::assertTask($task, 0, States::TAKEN, 'release_0'); } - /** - * @lua tube = create_tube('%tube_name%', '%tube_type%') - * @lua tube:put('stat_delayed_0', {delay = 9999}) - * @lua tube:put('stat_delayed_1', {delay = 9999}) - */ + #[Lua("tube = create_tube('%tube_name%', '%tube_type%')")] + #[Lua("tube:put('stat_delayed_0', {delay = 9999})")] + #[Lua("tube:put('stat_delayed_1', {delay = 9999})")] public function testStatsDelayed() : void { $count = $this->getQueue()->stats('tasks.delayed'); diff --git a/tests/Integration/TubePlaceholderResolver.php b/tests/Integration/TubePlaceholderResolver.php index 2cadeda..daad719 100644 --- a/tests/Integration/TubePlaceholderResolver.php +++ b/tests/Integration/TubePlaceholderResolver.php @@ -13,8 +13,8 @@ namespace Tarantool\Queue\Tests\Integration; -use PHPUnitExtras\Annotation\PlaceholderResolver\PlaceholderResolver; -use PHPUnitExtras\Annotation\Target; +use PHPUnitExtras\Attribute\PlaceholderResolver\PlaceholderResolver; +use PHPUnitExtras\Attribute\Target; final class TubePlaceholderResolver implements PlaceholderResolver { diff --git a/tests/Integration/queues.lua b/tests/Integration/queues.lua index 7388bf9..081ded9 100755 --- a/tests/Integration/queues.lua +++ b/tests/Integration/queues.lua @@ -6,7 +6,7 @@ box.cfg { listen = (listen == '' or listen == nil) and 3301 or listen, log_level = 6, wal_mode = 'none', - snap_dir = '/tmp' + memtx_dir = '/tmp' } box.schema.user.grant('guest', 'read,write,execute,create,drop,alter', 'universe', nil, {if_not_exists = true}) diff --git a/tests/PhpUnitCompat.php b/tests/PhpUnitCompat.php deleted file mode 100644 index 1f2950d..0000000 --- a/tests/PhpUnitCompat.php +++ /dev/null @@ -1,27 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Tarantool\Queue\Tests; - -/** - * A compatibility layer for the legacy PHPUnit 7. - */ -trait PhpUnitCompat -{ - public function expectExceptionMessageMatches(string $regularExpression) : void - { - \is_callable(parent::class.'::expectExceptionMessageMatches') - ? parent::expectExceptionMessageMatches(...func_get_args()) - : parent::expectExceptionMessageRegExp(...func_get_args()); - } -} diff --git a/tests/Unit/TaskTest.php b/tests/Unit/TaskTest.php index 551b2a7..8977601 100644 --- a/tests/Unit/TaskTest.php +++ b/tests/Unit/TaskTest.php @@ -13,15 +13,14 @@ namespace Tarantool\Queue\Tests\Unit; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Tarantool\Queue\States; use Tarantool\Queue\Task; final class TaskTest extends TestCase { - /** - * @dataProvider provideTuples - */ + #[DataProvider('provideTuples')] public function testCreateFromTuple(array $tuple) : void { $task = Task::fromTuple($tuple); @@ -34,7 +33,7 @@ public function testCreateFromTuple(array $tuple) : void } } - public function provideTuples() : iterable + public static function provideTuples() : array { return [ [[0, States::READY, [42]]], @@ -43,9 +42,7 @@ public function provideTuples() : iterable ]; } - /** - * @dataProvider provideStates - */ + #[DataProvider('provideStates')] public function testIsser(string $state) : void { static $map = [ @@ -68,7 +65,7 @@ public function testIsser(string $state) : void } } - public function provideStates() : iterable + public static function provideStates() : array { return [ [States::READY],