From e9856c63a270a7633bfafb0e32a999bd841dfceb Mon Sep 17 00:00:00 2001 From: Douglas Medeiros Date: Mon, 8 Jul 2024 14:54:10 -0300 Subject: [PATCH] ci: fix tests --- .github/workflows/formats.yml | 2 +- tests/MongoStreamListenerCommandTest.php | 80 +++++++++++------------- 2 files changed, 37 insertions(+), 45 deletions(-) diff --git a/.github/workflows/formats.yml b/.github/workflows/formats.yml index e782f65..0168510 100644 --- a/.github/workflows/formats.yml +++ b/.github/workflows/formats.yml @@ -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 }} diff --git a/tests/MongoStreamListenerCommandTest.php b/tests/MongoStreamListenerCommandTest.php index faa6178..9a75854 100644 --- a/tests/MongoStreamListenerCommandTest.php +++ b/tests/MongoStreamListenerCommandTest.php @@ -1,6 +1,7 @@ mongo = Mockery::mock(Client::class); $this->mongoDatabase = Mockery::mock(Database::class); @@ -15,61 +20,48 @@ $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(); });