Skip to content

Instantly share code, notes, and snippets.

@vicentimartins
Last active June 30, 2022 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicentimartins/4d7c79c31adc056a2af846b30d6077a4 to your computer and use it in GitHub Desktop.
Save vicentimartins/4d7c79c31adc056a2af846b30d6077a4 to your computer and use it in GitHub Desktop.
Teste de repository
// $query é um Eloquent\Builder
$query = parent::newQuery();
$query->with('relation') // with() é um método de Eloquent\Builder
->join( // join() é método de Query\Builder
'table2',
'table1.user_id',
'table2.another_user_id'
)
->where('table2.another_user_id', $parametro); // where() está em ambos, Query\Builder e Eloquent\Builder
return $query->get(); //get() é método de Query\Builder
$repository = new Repository();
$filter = ['another_user_id' => 100];
$data = Collection::make([]); // Representa meus dados esperados de retorno
$queryBuilderMock = Mockery::mock(QueryBuilder::class, function (MockInterface $qb) use ($filter, $data) {
$qb->shouldReceive('join')
->atLeast(1)
->with(Mockery::type('string'), Mockery::type('string'), Mockery::type('string'))
->andReturnSelf();
$qb->shouldReceive('where')
->atLeast(1)
->with(Mockery::type('string'), $filter['tax_assistant_id'])
->andReturnSelf();
});
$builderMock = Mockery::mock(EloquentBuilder::class, function (MockInterface $eb) {
$eb->shouldReceive('with')
->atLeast(1)
->with('Conversations')
->andReturnSelf();
});
$baseRepositoryMock = Mockery::mock(BaseRepository::class)
->shouldAllowMockingProtectedMethods();
$baseRepositoryMock->shouldReceive('newQuery')
->atLeast(1)
->andReturn($builderMock);
$this->assertEquals(1, $repository->listAll($filter)->count());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment