Skip to content

Commit

Permalink
add support for messenger:stats command added in 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Iosif Chiriluta | eMAG, Technology committed Mar 29, 2024
1 parent 85ebd72 commit a9b2343
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: CI
on: [push, pull_request]
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-version: ['8.1', '8.3']
php-version: ['8.1', '8.2', '8.3']
name: PHP ${{ matrix.php-version }}
steps:
- uses: actions/checkout@v2
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ framework:
The features described [here](https://symfony.com/doc/current/messenger.html#saving-retrying-failed-messages) can be used also, therefore the following commands are available in order to manually debug the failed messages:
```bash
# see all messages in the failure transport
$ bin/console messenger:failed:show
$ php bin/console messenger:failed:show

# see details about a specific failed message
$ php bin/console messenger:failed:show 20 -vv
Expand All @@ -42,8 +42,10 @@ $ php bin/console messenger:failed:retry -vv
$ php bin/console messenger:failed:retry 20 30 --force

# remove a message without retrying it
$ bin/console messenger:failed:remove
$ php bin/console messenger:failed:remove

# displays the number of queued messages in all transports
$ php bin/console messenger:stats
```
### Submitting bugs and feature requests
If you found a nasty bug or want to propose a new feature, you're welcome to open an issue or create a pull request [here](https://github.com/eMAGTechLabs/messenger-mongo-bundle/issues).

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"require": {
"php": "^8.1",
"ext-json": "*",
"ext-mongodb": "*",
"symfony/messenger": "^5.0 || ^6.0 || ^7.0",
"mongodb/mongodb": "^1.12"
},
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ parameters:
- tests
ignoreErrors:
- '#Access to an undefined property MongoDB\\Collection::\$documents.#'
- '#Return type \(void\) of method class@anonymous\/tests\/Unit\/MongoTransportTest.php:254::insertOne\(\) should be compatible with return type \(MongoDB\\InsertOneResult\) of method MongoDB\\Collection::insertOne\(\)#'
8 changes: 7 additions & 1 deletion src/MongoTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;

use function is_array;

final class MongoTransport implements TransportInterface, ListableReceiverInterface
final class MongoTransport implements TransportInterface, ListableReceiverInterface, MessageCountAwareInterface
{
private Collection $collection;
private SerializerInterface $serializer;
Expand Down Expand Up @@ -202,4 +203,9 @@ private function createEnvelopeFromDocument(array $document): Envelope
new TransportMessageIdStamp((string)$document['_id'])
);
}

public function getMessageCount(): int
{
return $this->collection->countDocuments();
}
}
17 changes: 5 additions & 12 deletions tests/Unit/MongoTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

use EmagTechLabs\MessengerMongoBundle\MongoTransport;
use EmagTechLabs\MessengerMongoBundle\MongoTransportFactory;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use PHPUnit\Framework\TestCase;

class MongoTransportFactoryTest extends TestCase
{
/**
* @test
*/
#[Test]
public function itShouldSupportOnlyMongoSchema(): void
{
$factory = new MongoTransportFactory();
Expand All @@ -24,9 +23,7 @@ public function itShouldSupportOnlyMongoSchema(): void
$this->assertFalse($factory->supports('doctrine://', []));
}

/**
* @test
*/
#[Test]
public function itShouldFailIfUriOptionsHasInvalidType(): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -42,9 +39,7 @@ public function itShouldFailIfUriOptionsHasInvalidType(): void
);
}

/**
* @test
*/
#[Test]
public function itShouldFailIfDriverOptionsHasInvalidType(): void
{
$this->expectException(InvalidArgumentException::class);
Expand All @@ -60,9 +55,7 @@ public function itShouldFailIfDriverOptionsHasInvalidType(): void
);
}

/**
* @test
*/
#[Test]
public function itShouldCreateTransport(): void
{
$factory = new MongoTransportFactory();
Expand Down
65 changes: 37 additions & 28 deletions tests/Unit/MongoTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
use EmagTechLabs\MessengerMongoBundle\Tests\Unit\Fixtures\HelloMessage;
use MongoDB\BSON\ObjectId;
use MongoDB\Collection;
use MongoDB\Driver\CursorInterface;
use MongoDB\InsertOneResult;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Stamp\DelayStamp;
Expand All @@ -18,9 +19,25 @@

class MongoTransportTest extends TestCase
{
/**
* @test
*/
#[Test]
public function itShouldCountTheMessages(): void
{
$collection = $this->createMock(Collection::class);
$collection->method('countDocuments')
->willReturn(3);

$transport = new MongoTransport(
$collection,
$this->createSerializer(),
'consumer_id',
[]
);

$count = $transport->getMessageCount();

$this->assertSame(3, $count);
}
#[Test]
public function itShouldFetchAndDecodeADocumentFromDb(): void
{
$serializer = $this->createSerializer();
Expand Down Expand Up @@ -57,9 +74,7 @@ public function itShouldFetchAndDecodeADocumentFromDb(): void
);
}

/**
* @test
*/
#[Test]
public function itShouldNothingIfConsumerIdNotMatching(): void
{
$serializer = $this->createSerializer();
Expand All @@ -83,9 +98,7 @@ public function itShouldNothingIfConsumerIdNotMatching(): void
$this->assertCount(0, $transport->get());
}

/**
* @test
*/
#[Test]
public function itShouldNothingIfDocumentIsNotArray(): void
{
$serializer = $this->createSerializer();
Expand All @@ -108,9 +121,7 @@ public function itShouldNothingIfDocumentIsNotArray(): void
$this->assertCount(0, $transport->get());
}

/**
* @test
*/
#[Test]
public function itShouldListAllMessages(): void
{
$serializer = $this->createSerializer();
Expand All @@ -137,9 +148,7 @@ public function itShouldListAllMessages(): void
);
}

/**
* @test
*/
#[Test]
public function itShouldFindAMessageById(): void
{
$serializer = $this->createSerializer();
Expand All @@ -163,9 +172,7 @@ public function itShouldFindAMessageById(): void
);
}

/**
* @test
*/
#[Test]
public function itShouldReturnNothingIfIdCouldNotBeFound(): void
{
$serializer = $this->createSerializer();
Expand All @@ -185,9 +192,7 @@ public function itShouldReturnNothingIfIdCouldNotBeFound(): void
));
}

/**
* @test
*/
#[Test]
public function itShouldSendAMessage(): void
{
$collection = $this->createCollection();
Expand Down Expand Up @@ -225,9 +230,7 @@ public function itShouldSendAMessage(): void
);
}

/**
* @test
*/
#[Test]
public function itShouldDeleteTheDocumentOnAckOrReject(): void
{
$documentId = new ObjectId();
Expand All @@ -252,15 +255,21 @@ public function itShouldDeleteTheDocumentOnAckOrReject(): void
private function createCollection(array $documents = []): Collection
{
return new class extends Collection {
public $documents = [];
public array $documents = [];

public function __construct()
{
}

public function insertOne($a, array $options = []): void
public function insertOne($document, array $options = []): InsertOneResult
{
$this->documents[] = $a;
$this->documents[] = $document;

return new class extends InsertOneResult {
public function __construct()
{
}
};
}
};
}
Expand Down

0 comments on commit a9b2343

Please sign in to comment.