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..efb0264 100644 --- a/tests/MongoStreamListenerCommandTest.php +++ b/tests/MongoStreamListenerCommandTest.php @@ -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); @@ -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( + <<listener->run(); });