Is there a way that I can test that the getNumberOfProductsForCategory
method will return an integer rather than a specific value (18 in this case)?
I feel simply testing for "18", although correct just now, makes my test very brittle as that value will likely change at some point in the future, meaning the test will fail.
This is my PhpSpec function:
function it_returns_something_if_products_exist(
ProductRepository $productRepository,
Category $category
)
{
$category->getId()->willReturn(268);
$productRepository->getNumberOfProductsForCategory($category)->willReturn(18);
// call method here
}
This is my repository method:
/**
* @param Category $category
*
* @return mixed
*/
public function getNumberOfProductsForCategory(Category $category)
{
$dql = 'SELECT COUNT(tvp)
FROM \CRMPicco\ProductBundle\Entity\Product tvp
WHERE tvp.category = :category';
return $this->getEntityManager()
->createQuery($dql)
->setParameter('category', $category)
->getSingleScalarResult();
}
Aucun commentaire:
Enregistrer un commentaire