Skip to content

Commit

Permalink
ci: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
medeirosinacio committed Jul 8, 2024
1 parent e630a07 commit e9856c6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/formats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [8.2]
dependency-version: [prefer-lowest, prefer-stable]
dependency-version: [prefer-stable]

name: Formats P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}

Expand Down
80 changes: 36 additions & 44 deletions tests/MongoStreamListenerCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,67 @@
<?php

use Medeirosinacio\MongoTtlIndexChangeStream\MongoStreamListenerCommand;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use MongoDB\ChangeStream;
use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Model\BSONDocument;
use Psr\SimpleCache\CacheInterface;

use function PHPUnit\Framework\assertEquals;

uses(MockeryPHPUnitIntegration::class);

beforeEach(function () {
$this->mongo = Mockery::mock(Client::class);
$this->mongoDatabase = Mockery::mock(Database::class);
$this->cache = Mockery::mock(CacheInterface::class);
$this->listener = new MongoStreamListenerCommand($this->mongo, $this->cache);
});

afterEach(function () {
Mockery::close();
});

test('it listens and processes changes', function () {
test('it processes different types of events', function () {
$collection = Mockery::mock(Collection::class);
$changeStream = Mockery::mock(ChangeStream::class);
$resumeToken = (object) ['_data' => 'resume_token_data'];
$event = new BSONDocument([
'ns' => ['db' => 'default', 'coll' => 'records'],
'documentKey' => ['_id' => 'some_id'],

$insertEvent = new BSONDocument([
'operationType' => 'insert',
'fullDocument' => ['_id' => 'some_id', 'field' => 'value'],
'fullDocument' => ['_id' => '123', 'name' => 'Test'],
'ns' => ['db' => 'default', 'coll' => 'records'],
'documentKey' => ['_id' => '123'],
]);

$this->mongo->expects('selectDatabase')
->with('default')
->andReturn($this->mongoDatabase);
$this->mongo->expects('selectDatabase')->andReturn($this->mongoDatabase);
$this->mongoDatabase->expects('selectCollection')->andReturn($collection);
$this->cache->expects('get')->andReturn($resumeToken);
$collection->expects('watch')->andReturn($changeStream);
$collection->expects('getDatabaseName')->andReturn('default');
$collection->expects('getCollectionName')->andReturn('records');

$this->mongoDatabase->expects('selectCollection')
->with('records')
->andReturn($collection);

$collection->expects('getDatabaseName')
->andReturn('default');

$collection->expects('getCollectionName')
->andReturn('records');

$this->cache->expects('get')
->with('mongo_resume_token')
->andReturn($resumeToken);
$changeStream->expects('rewind');
$changeStream->expects('next')->andThrow(new Exception('Test end of stream'));
$changeStream->expects('current')->andReturn($insertEvent);
$changeStream->expects('getResumeToken')->andReturn($resumeToken);

$collection->expects('watch')
->andReturn($changeStream);
$this->cache->expects('set')->with('mongo_resume_token', $resumeToken)->andReturn(true);

$changeStream->expects('rewind');
$changeStream->expects('next')->andReturnUsing(function () use ($changeStream) {
static $count = 0;
if ($count < 2) {
$count++;
} else {
$changeStream->valid = false;
}
});
ob_start();
$this->listener->run();
$output = ob_get_clean();

$changeStream->expects('current')
->andReturn($event, null);
assertEquals(
<<<'MESSAGE_ASSERT'
Listening for changes in default.records
Resume token: resume_token_data
Inserted new document in default.records
{"operationType":"insert","fullDocument":{"_id":"123","name":"Test"},"ns":{"db":"default","coll":"records"},"documentKey":{"_id":"123"}}
$changeStream->expects('getResumeToken')
->andReturn($resumeToken);
Error: Test end of stream

$this->cache->expects('set')
->with('mongo_resume_token', $resumeToken)
->andReturn(true);
MESSAGE_ASSERT
,
$output
);

$this->listener->run();
});

0 comments on commit e9856c6

Please sign in to comment.