generated from medeirosinacio/basic-php-project-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e630a07
commit e9856c6
Showing
2 changed files
with
37 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |