Skip to content

Commit

Permalink
ci: fix testes
Browse files Browse the repository at this point in the history
  • Loading branch information
medeirosinacio committed Jul 8, 2024
1 parent e630a07 commit 5ed8415
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 46 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: 35 additions & 45 deletions tests/MongoStreamListenerCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use MongoDB\Database;
use MongoDB\Model\BSONDocument;
use Psr\SimpleCache\CacheInterface;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

use function PHPUnit\Framework\assertEquals;

uses(MockeryPHPUnitIntegration::class);

beforeEach(function () {
$this->mongo = Mockery::mock(Client::class);
Expand All @@ -15,61 +20,46 @@
$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->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);

$collection->expects('watch')
->andReturn($changeStream);
$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');

$changeStream->expects('rewind');
$changeStream->expects('next')->andReturnUsing(function () use ($changeStream) {
static $count = 0;
if ($count < 2) {
$count++;
} else {
$changeStream->valid = false;
}
});
$changeStream->expects('next')->andThrow(new Exception('Test end of stream'));
$changeStream->expects('current')->andReturn($insertEvent);
$changeStream->expects('getResumeToken')->andReturn($resumeToken);

$changeStream->expects('current')
->andReturn($event, null);
$this->cache->expects('set')->with('mongo_resume_token', $resumeToken)->andReturn(true);

$changeStream->expects('getResumeToken')
->andReturn($resumeToken);
ob_start();
$this->listener->run();
$output = ob_get_clean();

$this->cache->expects('set')
->with('mongo_resume_token', $resumeToken)
->andReturn(true);
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"}}
Error: Test end of stream
MESSAGE_ASSERT
, $output);

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

0 comments on commit 5ed8415

Please sign in to comment.