I'm trying to test the all method of a repository, but I do not know how to do the test, I want to use Profecy. I'm using DoctrineMongoDBBundle.
Entity
class UsuarioEntidade{
/**
* @MongoDB\Id
*/
protected $usuId;
/**
* @MongoDB\Field(type="string", name="usua_nome")
*/
protected $usuNome;
/**
* @MongoDB\Field(type="string", name="usua_telefone")
*/
protected $usuTelefone;
/**
* @MongoDB\Field(type="string", name="usua_email")
*/
protected $usuEmail;
/**
* @MongoDB\Field(type="string", name="usua_imagem_perfil")
*/
protected $usuImagemPerfil;
/**
* @MongoDB\Field(type="date")
*/
protected $createdAt;
##get and set
Repository
class UsuarioRepositorio
{
protected $dm;
public function __construct(DocumentManager $documentManager)
{
$this->dm = $documentManager;
}
public function all(array $input = null)
{
$usuario = UsuarioEntidade::class;
$all = $this->dm->createQueryBuilder($usuario);
return $all->getQuery();
}
This is the test that I do not know how to perform based on the repository class.
class UsuarioRepositorioTest extends TestCase
{
protected $repositorio;
protected $documento;
public function setUp()
{
$this->documento = $this->prophesize(DocumentManager::class);
$this->repositorio = new UsuarioRepositorio($this->documento->reveal());
}
public function testObtendoTodosOsDados()
{
$output = $this->usuario();
$this->documento->createQueryBuilder(UsuarioEntidade::class)->willReturn($output)->shouldBeCalled();
$all = $this->repositorio->all();
$this->assertEquals($output, $all);
}
protected function usuario()
{
$usuarioEntidade = new UsuarioEntidade();
$usuarioEntidade->setUsuNome('Humanos');
$usuarioEntidade->setUsuTelefone('89855236554');
$usuarioEntidade->setUsuEmail('mano@email.com');
$usuarioEntidade->setUsuImagemPerfil('/application/sam/tests/Usuario/ImagemPerfil/sam.png');
$data = new \DateTime();
$usuarioEntidade->setCreatedAt($data->format('Y-m-d'));
return $usuarioEntidade;
}
I do not know how to mock the method "getQuery()". This error always occurs: Error: Call to a member function getQuery() on array
The problem in the code is that your createQUeryBuilder()
expectation returns an array and you are trying to call a function on that array.
You have to mock a QueryBuilder
object and set up an expectation for its getQuery()
function to return your desired output. The QueryBuilder mock has to be returned when you set up the expectation for the DocumentManager createQueryBuilder()
function.
Something like this:
public function testObtendoTodosOsDados()
{
$output = $this->usuario();
$mockQueryBuilder = $this->prophesize(QueryBuilder::class); // Remember to import the proper QueryBuilder namespace
$mockQueryBuilder->getQuery()->willReturn($output)->shouldBeCalled();
$this->documento->createQueryBuilder(UsuarioEntidade::class)->willReturn($mockQueryBuilder)->shouldBeCalled();
$all = $this->repositorio->all();
$this->assertEquals($output, $all);
}