Skip to content

Commit a4de1ec

Browse files
iosifchIosif Chiriluta | eMAG, Technology
authored andcommitted
chore: Upgrade dependencies, environment, and modernize codebase
- Upgrades PHP to 8.4 and the MongoDB extension in the Docker environment. - Updates major versions of key dependencies, including PHPStan, Symfony components, and the MongoDB library. - Modernizes the CI workflow by updating GitHub Actions. - Refactors the codebase to align with the new dependency versions, improving type safety and code style. - Adds GEMINI.md for AI agent context.
1 parent 54568f9 commit a4de1ec

File tree

8 files changed

+160
-50
lines changed

8 files changed

+160
-50
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ jobs:
99
php-version: ['8.1', '8.2', '8.3', '8.4']
1010
name: PHP ${{ matrix.php-version }}
1111
steps:
12-
- uses: actions/checkout@v2
12+
- uses: actions/checkout@v4
1313
- name: Install PHP
1414
uses: shivammathur/setup-php@v2
1515
with:
1616
coverage: pcov
1717
php-version: ${{ matrix.php-version }}
1818
tools: pecl, composer:v2
19-
extensions: mongodb-1.13
19+
extensions: mongodb
2020
- name: Composer config
2121
run: composer config --no-plugins allow-plugins.infection/extension-installer true
2222
- name: Composer install
23-
run: composer install --no-interaction --no-progress --no-suggest
23+
run: composer install --no-interaction --no-progress
2424
- name: PHPUnit
2525
run: ./vendor/bin/phpunit --coverage-clover coverage.xml
2626
- name: PHPStan
@@ -30,7 +30,7 @@ jobs:
3030
env:
3131
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}
3232
- name: Codecov
33-
uses: codecov/codecov-action@v1
33+
uses: codecov/codecov-action@v4
3434
with:
3535
token: ${{ secrets.CODECOV_TOKEN }}
36-
file: ./coverage.xml
36+
files: ./coverage.xml

GEMINI.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Project Overview
2+
3+
This project is a Symfony Bundle that provides a MongoDB transport for the Symfony Messenger component. It allows developers to use MongoDB as a message queue for their Symfony applications. This is particularly useful when an application already uses MongoDB and wants to avoid adding another dependency for a message queue.
4+
5+
The core of the bundle is the `MongoTransport` class, which implements the Symfony `TransportInterface`. This class handles the sending, receiving, acknowledging, and rejecting of messages using a MongoDB collection.
6+
7+
The project uses Composer for dependency management, PHPUnit for testing, Infection for mutation testing, and PHPStan for static analysis.
8+
9+
# Building and Running
10+
11+
## Dependencies
12+
13+
Install dependencies using Composer:
14+
15+
```bash
16+
composer install
17+
```
18+
19+
## Running Tests
20+
21+
Run the test suite using PHPUnit:
22+
23+
```bash
24+
vendor/bin/phpunit
25+
```
26+
27+
## Static Analysis
28+
29+
Run PHPStan for static analysis:
30+
31+
```bash
32+
vendor/bin/phpstan analyse src tests
33+
```
34+
35+
## Mutation Testing
36+
37+
Run Infection for mutation testing:
38+
39+
```bash
40+
vendor/bin/infection
41+
```
42+
43+
# Development Conventions
44+
45+
## Coding Style
46+
47+
The project follows the PSR-12 coding style guide.
48+
49+
## Testing
50+
51+
The project has a comprehensive test suite using PHPUnit. Tests are located in the `tests` directory and are separated into unit and integration tests.
52+
53+
## Contribution
54+
55+
Contributions are welcome. Please open an issue or create a pull request on the [GitHub repository](https://github.com/eMAGTechLabs/messenger-mongo-bundle).

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
"require": {
2020
"php": "^8.1",
2121
"ext-json": "*",
22-
"ext-mongodb": "*",
23-
"symfony/messenger": "^5.0 || ^6.0 || ^7.0",
24-
"mongodb/mongodb": "^1.12"
22+
"ext-mongodb": "^2.1",
23+
"symfony/messenger": "^6.0 || ^7.0",
24+
"mongodb/mongodb": "^2.1"
2525
},
2626
"require-dev": {
27-
"symfony/serializer": "^5.0 || ^6.0 || ^7.0",
28-
"symfony/property-access": "^5.0 || ^6.0 || ^7.0",
29-
"symfony/var-dumper": "^5.0 || ^6.0 || ^7.0",
30-
"symfony/framework-bundle": "^5.0 || ^6.0 || ^7.0",
27+
"symfony/serializer": "^6.0 || ^7.0",
28+
"symfony/property-access": "^6.0 || ^7.0",
29+
"symfony/var-dumper": "^6.0 || ^7.0",
30+
"symfony/framework-bundle": "^6.0 || ^7.0",
3131
"phpunit/phpunit": "^10.5",
3232
"infection/infection": "^0.27.9",
33-
"phpstan/phpstan": "^1.10"
33+
"phpstan/phpstan": "^2.1"
3434
},
3535
"autoload": {
3636
"psr-4": {

docker-compose.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3.6'
21
services:
32
php:
43
build:
@@ -14,5 +13,3 @@ services:
1413
environment:
1514
- MONGO_INITDB_ROOT_USERNAME=root
1615
- MONGO_INITDB_ROOT_PASSWORD=rootpass
17-
18-

docker/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM php:8.3-alpine
1+
FROM php:8.4-alpine
22

33
WORKDIR /var/www/html
44

@@ -7,7 +7,7 @@ RUN apk update \
77
&& apk add --no-cache git zip make autoconf g++ openssl-dev linux-headers \
88
&& apk add --no-cache --virtual .build-deps $PHPIZE_DEPS
99

10-
RUN pecl -q install mongodb-1.17.2 \
10+
RUN pecl -q install mongodb-2.1.4 \
1111
&& docker-php-ext-enable mongodb \
1212
&& pecl -q install xdebug \
1313
&& docker-php-ext-enable xdebug
@@ -18,4 +18,4 @@ RUN curl --silent --show-error https://getcomposer.org/installer | php \
1818
&& mv composer.phar /usr/local/bin/composer \
1919
&& chmod o+x /usr/local/bin/composer
2020

21-
ENTRYPOINT ["tail", "-f", "/dev/null"]
21+
ENTRYPOINT ["tail", "-f", "/dev/null"]

phpstan.neon.dist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
parameters:
22
level: 5
3-
checkMissingIterableValueType: false
43
paths:
54
- src
65
- tests
7-
ignoreErrors:
8-
- '#Access to an undefined property MongoDB\\Collection::\$documents.#'

src/MongoTransport.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public function get(): iterable
102102
*/
103103
private function removeMessage(Envelope $envelope): void
104104
{
105-
/** @var TransportMessageIdStamp $transportMessageIdStamp */
106105
$transportMessageIdStamp = $envelope->last(TransportMessageIdStamp::class);
107106

108107
if (!$transportMessageIdStamp instanceof TransportMessageIdStamp) {
@@ -161,7 +160,7 @@ public function send(Envelope $envelope): Envelope
161160
return $envelope->with(new TransportMessageIdStamp($objectId));
162161
}
163162

164-
public function all(int $limit = null): iterable
163+
public function all(?int $limit = null): iterable
165164
{
166165
$documents = $this->collection->find([], ['limit' => $limit]);
167166

@@ -170,7 +169,7 @@ public function all(int $limit = null): iterable
170169
}
171170
}
172171

173-
public function find($id): ?Envelope
172+
public function find(mixed $id): ?Envelope
174173
{
175174
$document = $this->collection->findOne(['_id' => is_string($id) ? new ObjectId($id) : $id]);
176175

tests/Unit/MongoTransportTest.php

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66

77
use EmagTechLabs\MessengerMongoBundle\MongoTransport;
88
use EmagTechLabs\MessengerMongoBundle\Tests\Unit\Fixtures\HelloMessage;
9+
use MongoDB\BSON\Int64;
910
use MongoDB\BSON\ObjectId;
1011
use MongoDB\Collection;
12+
use MongoDB\Driver\CursorInterface;
13+
use MongoDB\Driver\Server;
1114
use MongoDB\InsertOneResult;
1215
use PHPUnit\Framework\Attributes\Test;
1316
use PHPUnit\Framework\TestCase;
17+
use RuntimeException;
1418
use Symfony\Component\Messenger\Envelope;
1519
use Symfony\Component\Messenger\Stamp\DelayStamp;
1620
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
@@ -128,11 +132,11 @@ public function itShouldListAllMessages(): void
128132

129133
$collection = $this->createMock(Collection::class);
130134
$collection->method('find')
131-
->willReturn([
135+
->willReturn($this->createCursor([
132136
$this->createDocument(),
133137
$this->createDocument(),
134138
$this->createDocument(),
135-
]);
139+
]));
136140

137141
$transport = new MongoTransport(
138142
$collection,
@@ -195,7 +199,24 @@ public function itShouldReturnNothingIfIdCouldNotBeFound(): void
195199
#[Test]
196200
public function itShouldSendAMessage(): void
197201
{
198-
$collection = $this->createCollection();
202+
$collection = new class extends Collection {
203+
public array $documents = [];
204+
205+
public function __construct()
206+
{
207+
}
208+
209+
public function insertOne($document, array $options = []): InsertOneResult
210+
{
211+
$this->documents[] = $document;
212+
213+
return new class extends InsertOneResult {
214+
public function __construct()
215+
{
216+
}
217+
};
218+
}
219+
};
199220

200221
$transport = new MongoTransport(
201222
$collection,
@@ -252,28 +273,6 @@ public function itShouldDeleteTheDocumentOnAckOrReject(): void
252273
$transport->reject($envelope);
253274
}
254275

255-
private function createCollection(array $documents = []): Collection
256-
{
257-
return new class extends Collection {
258-
public array $documents = [];
259-
260-
public function __construct()
261-
{
262-
}
263-
264-
public function insertOne($document, array $options = []): InsertOneResult
265-
{
266-
$this->documents[] = $document;
267-
268-
return new class extends InsertOneResult {
269-
public function __construct()
270-
{
271-
}
272-
};
273-
}
274-
};
275-
}
276-
277276
private function createDocument(): array
278277
{
279278
return [
@@ -290,4 +289,67 @@ private function createSerializer(): SerializerInterface
290289
{
291290
return new Serializer();
292291
}
292+
293+
private function createCursor(array $documents): CursorInterface
294+
{
295+
return new class($documents) implements CursorInterface
296+
{
297+
private array $data;
298+
private int $position = 0;
299+
300+
public function __construct(array $data)
301+
{
302+
$this->data = array_values($data);
303+
}
304+
305+
public function current(): array|null|object
306+
{
307+
return $this->data[$this->position];
308+
}
309+
310+
public function key(): int
311+
{
312+
return $this->position;
313+
}
314+
315+
public function next(): void
316+
{
317+
$this->position++;
318+
}
319+
320+
public function rewind(): void
321+
{
322+
$this->position = 0;
323+
}
324+
325+
public function valid(): bool
326+
{
327+
return array_key_exists($this->position, $this->data);
328+
}
329+
330+
public function toArray(): array
331+
{
332+
return $this->data;
333+
}
334+
335+
public function isDead(): bool
336+
{
337+
return false;
338+
}
339+
340+
public function setTypeMap(array $typemap): void
341+
{
342+
}
343+
344+
public function getId(): Int64
345+
{
346+
throw new RuntimeException('Not implemented in test cursor.');
347+
}
348+
349+
public function getServer(): Server
350+
{
351+
throw new RuntimeException('Not implemented in test cursor.');
352+
}
353+
};
354+
}
293355
}

0 commit comments

Comments
 (0)