I have spent my fare share of hours trying to create a proper Phpunit test for my repository function and still no success, so that's why I'm turning on you guys for help
I'm trying to write a test, which is testing my repository function, without connecting to database
I have this repository function (Symfony 4)
class RequestRepository extends EntityRepository
{
public function getCompletedRequestByName($name)
{
$requestsStatuses = $this->createQueryBuilder('r')
->join('r.domains', 'd', Join::WITH, 'r.id = d.request')
->where('r.name = :name')
->andWhere('r.status = :completed')
->setParameter('name', $name)
->setParameter('completed', 'COMPLETED')
->getQuery()
->getOneOrNullResult();
return $requestsStatuses;
}
}
And this is my test
public function testGetCompletedRequestByName()
{
$entityM =
$this->getMockBuilder(EntityManagerInterface::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$classMetaData =
$this->getMockBuilder(ClassMetadata::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$repo = new RequestRepository($entityM, $classMetaData);
var_dump($repo->getCompletedRequestByName('antrax.com'));
die();
}
And the error I'm getting is
1) App\Repository\RequestRepositoryTest::testGetCompletedRequestByName
Error: Call to a member function select() on null
Can some one please help me out, how to write a test, which test my repository function, without connection to DB and executing any queries. If you need any additional informations, please let me know and I will provide! Thank you!
I have found a solution for my case. If any body need's it, this is how my test looks like
public function testGetCompletedRequestByName()
{
$queryBuilder = $this->getMockBuilder(QueryBuilder::class)->disableOriginalConstructor()
->setMethods(['setParameter', 'getQuery', 'getOneOrNullResult', 'join', 'andWhere', 'where'])->getMock();
$queryBuilder->method('getQuery')->willReturnSelf();
$queryBuilder->method('setParameter')->willReturnCallback(function ($field, $value) use ($queryBuilder){
if ($field == 'name') {
$this->assertSame('antrax.com', $value);
} elseif ($field == 'completed') {
$this->assertSame('COMPLETED', $value);
}
return $queryBuilder;
});
$queryBuilder->method('join')->willReturnCallback(function ($field, $alias, $operator, $cond) use ($queryBuilder){
$this->assertSame('r.domains', $field);
$this->assertSame('d', $alias);
$this->assertSame(Join::WITH, $operator);
$this->assertSame('r.id = d.request', $cond);
return $queryBuilder;
});
$queryBuilder->method('where')->willReturnCallback(function ($cond) use ($queryBuilder){
$this->assertSame('r.name = :name', $cond);
return $queryBuilder;
});
$queryBuilder->method('andWhere')->willReturnCallback(function ($cond) use ($queryBuilder){
$this->assertSame('r.status = :completed', $cond);
return $queryBuilder;
});
$repositoryMock =
$this->getMockBuilder(RequestRepository::class)->disableOriginalConstructor()
->setMethods(['createQueryBuilder'])->getMock();
$repositoryMock->method('createQueryBuilder')->willReturnCallback(function ($alias) use ($queryBuilder){
$this->assertSame('r', $alias);
return $queryBuilder;
});
$repositoryMock->getCompletedRequestByName('antrax.com');
}
Related
I am using the mockery to test a method that make a lot of doctrine repository invocations with different repositories.
This is the method that i set up all my repository mocks:
public function testService()
{
$mockDoctrine = $this->getMockDoctrine();
$mockDoctrine->shouldReceive('getRepository')->once()
->andReturn($this->getRepositoryAMock());
$mockDoctrine->shouldReceive('getRepository')->once()
->andReturn($this->getRepositoryBMock());
$mockDoctrine->shouldReceive('getRepository')->once()
->andReturn($this->getRepositoryCMock());
//here is where i hit my test
$products = $this->service->fire(1, 1);
$this->assertInstanceOf('Illuminate\Support\Collection', $products);
foreach ($products as $v) {
$this->assertInstanceOf('Illuminate\Support\Collection', $v);
}
}
This is the method that i mock the Doctrine:
public function getMockDoctrine()
{
$mockDoctrine = \App::make('Doctrine');
$mockDoctrine->shouldReceive('persist')
->andReturn(true);
$mockDoctrine->shouldReceive('flush')
->andReturn(true);
return $mockDoctrine;
}
These are my repositories mock
public function getRepositoryAMock()
{
$repository = \Mockery::mock('MyARepository');
$repository->shouldReceive('findBy')
->with(['paramA' => 1, 'paramB' => 1])
->andReturn($this->getMockA());
return $repository;
}
public function getRepositoryBMock()
{
$repository = \Mockery::mock('MyBRepository');
$repository->shouldReceive('findById')
->with(1)
->andReturn($this->getMockA());
return $repository;
}
public function getRepositoryCMock()
{
$repository = \Mockery::mock('MyCRepository');
$repository->shouldReceive('findOneBy')
->with(['paramA' => 1, 'paramB' => 1])
->andReturn($this->getMockA());
return $repository;
}
This is where in fact i set the return of my mock
public function getMockA()
{
$obj = new MyClass();
$reflection = new \ReflectionClass($obj);
$id = $reflection->getProperty('id');
$id->setAccessible(true);
$id->setValue($obj, 1);
$obj
->setLogin('foo')
->setPassword('bar')
->setCode(1);
return $obj;
}
And then i receive an error like this:
1) MyClassTest::testService
BadMethodCallException: Method Mockery_2_ClassBRepository::findOneBy() does not exist on this mock object
Assuming that i have 3 methods with repositories being called in testService() method, the method that mockery is not finding is in the third one, but mockery thinks it is in the second, so obviously he won't find, because in the second one, does not exist the "findOneBy()" doctrine method just in the third.
How can i solve this ?
You should be able to use mockery's with().
For example:
$mockDoctrine
->shouldReceive('getRepository')
->with('MyAReposiotry')->once()
->andReturn($this->getRepositoryAMock());
And like that for every repository (with different value in with).
But I would rather inject repositories in that service instead of getting it from entity manager inside service. It is way better for testing. Take a look at this blog post.
Thanks to Mr Ivan,
Now my method is working.
In my real class, i was getting the repository with the entity class,
like:
Doctrine::getRepository('MyEntityClassA')
->findBy(['paramA' => 1, 'paramB' => 1]);
so i changed my test method to use the "with" passing the entity class:
public function testService()
{
$mockDoctrine = $this->getMockDoctrine();
$mockDoctrine->shouldReceive('getRepository')
->with('MyEntityClassA')->once()
->andReturn($this->getRepositoryAMock());
$mockDoctrine->shouldReceive('getRepository')->once()
->with('MyEntityClassB')->once()
->andReturn($this->getRepositoryBMock());
$mockDoctrine->shouldReceive('getRepository')->once()
->with('MyEntityClassC')->once()
->andReturn($this->getRepositoryCMock());
//here is where i hit my test
$products = $this->service->fire(1, 1);
$this->assertInstanceOf('Illuminate\Support\Collection', $products);
foreach ($products as $v) {
$this->assertInstanceOf('Illuminate\Support\Collection', $v);
}
}
I have installed php unit in my local server, but I dont understand (reading the php unit help) how to test my action create. My action is this, and the only thing I want to test is if it saves on database.
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate() {
$_class = $this->getClassName();
$model = new $_class;
if (isset($_POST)) {
$model->attributes = $_POST;
$this->armaMensajeABMGrilla($model->save(), $model);
}
$this->renderPartial('create', array(
'model' => $model,), false, true);
}
protected function armaMensajeABMGrilla($guardoOk, $modelo = null) {
if ($guardoOk == true) {
$this->respuestaJSON = array('success' => true, 'mensaje' => 'ok');
} else {
$erroresMensaje = 'Listado de Errores: <br/><ul>';
$i = 0;
if (isset($modelo->errors)) {
foreach ($modelo->errors as $error) {
$erroresMensaje .= '<li>Error(' . $i . '): ' . $error[0] . '</li>';
$i++;
}
$erroresMensaje.='</ul>';
}
$this->respuestaJSON = array('success' => false, 'mensaje' => $erroresMensaje);
}
$this->renderJSON($this->respuestaJSON);
}
How will be the test method? something like this?
public function actionCreateTest(){
$model = new Model;
$this->asserttrue($model->save());
}
write functional tests for testing controllers functionality instead of unit tests,also
the thing that you are asserting here
$this->assertEquals(true,$controller->actionCreate());
if the outcome of $controller->actionCreate() is the value true, which is not!
you are $this->renderPartial() in that and returning nothing, so that statement will never be true.
i am writing webservice in symfony2 but i facing some problem regarding the output ,as it is giving blank output.
class DefaultController extends Controller {
/**
*
* #Route("/webservices/activity/{id}", name="user_json_activity")
* #Method("get")
*/
public function activityAction($id) {
$em = $this->getDoctrine()->getEntityManager();
$list = $em->getRepository('FitugowebserviceBundle:activity')->findOneById($id);
$r_array = $this->routes2Array($list);
$r = array('activity' => $r_array);
return new Response(json_encode($r));
}
private function routes2Array($routes) {
$points_array = array();
foreach ($routes as $route) {
$r_array = array('activity' => $route->getActivity(),
'icon' => $route->getIcon());
$points_array[] = $r_array;
}
return $points_array;
}
}
When i try to fetch data for id=1 http://domain.org/fitugo/web/app_dev.php/webservices/activity/1 it is giving output as follows
{"activity":[]}
It look very strange that you want get array with findOneById method. The first thing I suggest to add a check that the entity founded by id exist. Then look that findOneById returns and check your controller logic.
I've been bashing my head against the wall trying to figure out why I can't get my fixture to load properly. When I attempt to run my test, my layout is rendered. If I comment out the fixture, the test runs properly. I've gone over this 100 times and I can't seem to see what's wrong.
Heres my Videosview.test.php
App::import('Model','Videosview');
class VideosviewTest extends Videosview {
var $name = 'Videosview';
//var $useDbConfig = 'test_suite';
}
class VideosviewTestCase extends CakeTestCase {
var $fixtures = array( 'app.videosview' );
function testIncrementTimer() {
$this->autoLayout = $this->autoRender = false;
$this->Videosview =& ClassRegistry::init('Videosview');
//$video_view = $this->find('first');
$result = $this->Videosview->increment_timer($video_view['Videosview']['video_id'],$video_view['Videosview']['user_id'],1);
$this->assertTrue(true);
}
}
This is my videosview_fixture.php
class VideosviewFixture extends CakeTestFixture {
var $name = 'Videosview';
var $import = array('model' => 'Videosview', 'records' => true);
}
Lot of strange code there, it looks like you don't understand how test are used. The problem doesn't come from your fixture import.
$this->assertTrue(true) will always return true. There is no need to declare VideosviewTest.
As I don't know what your increment_timer method is supposed to do I cannot write a test for it, but let's suppose it returns the value passed + 1:
function increment_timer($id = null){
return $id++;
}
Your test case should be
App::import('Model', 'Videosview');
class VideosviewTestCase extends CakeTestCase {
var $fixtures = array('app.videosview');
function startTest() {
$this->Videosview =& ClassRegistry::init('Videosview');
}
function endTest() {
unset($this->Videosview);
ClassRegistry::flush();
}
function testIncrementTimer() {
$input = 1;
// let's test increment_timer function by asserting true that return value is $input + 1, green bar
$this->assertTrue( $this->Videosview->increment_timer($input) == ($input+1), 'Should return 2' );
// let's test increment_timer function by asserting false that return value is $input + 2, green bar
$this->assertFalse( $this->Videosview->increment_timer($input) == ($input+2), 'Should not return 3' );
//the following returns an error as return value is not equal to $input + 2, Red bar
$this->assertTrue( $this->Videosview->increment_timer($input) == ($input+2), 'Should return 2' );
}
}
This is what you should get, and have expected
I have been getting into Unit Testing with Zend Framework. I am getting used to the other things it provide but I am having a hard time understanding Mock Objects.
For this example, I am trying to use a Mock Object to test out my model.
<?php
class Twitter_Model_Twitter
{
private $_twitter;
/**
* Make the options injectable.
* __contruct($auth, $key)
*/
public function __construct()
{
$config = new Zend_Config_Ini(APPLICATION_INI, APPLICATION_ENV);
$key = $config->encryption->salt;
$iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$password = mcrypt_decrypt(MCRYPT_XTEA, $key, $password, MCRYPT_MODE_ECB, $iv);
$this->_twitter = new Zend_Service_Twitter($username, $password);
}
public function verifyCredentials()
{
return $this->_twitter->account->verifyCredentials();
}
public function friendsTimeline($params)
{
return $this->_twitter->status->friendsTimeline($params);
}
}
For my unit test:
require_once ('../application/models/Twitter.php');
class Model_TwitterTest extends ControllerTestCase
{
/**
* #var Model_Twitter
*/
protected $_twitter;
public function testfriendsTimeline()
{
$mockPosts = array('foo', 'bar');
//my understanding below is:
//get a mock of Zend_Service_Twitter with the friendsTimeline method
$twitterMock = $this->getMock('Zend_Service_Twitter', array('friendsTimeline'));
/*
line above will spit out an error:
1) testfriendsTimeline(Model_TwitterTest)
Missing argument 1 for Mock_Zend_Service_Twitter_9fe2aeaa::__construct(), called in
/Applications/MAMP/bin/php5/lib/php/PHPUnit/Framework/TestCase.php on line 672 and
defined /htdocs/twitter/tests/application/models/TwitterTest.php:38
*/
$twitterMock->expects($this->once())
->method('friendsTimeline')
->will($this->returnValue($mockPosts));
$model = new Twitter_Model_Twitter();
$model->setOption('twitter', $twitterMock);
$posts = $model->friendsTimeline(array('count'=>20));
$this->assertEquals($posts, $mockPosts);
}
}
How would you test the following?
1) verifyCredentials()
2) friendsTimeline()
Thanks,
Wenbert
I am going to answer this question. I think I have made this work thanks to zomg from #zftalk.
Here is my new Twitter Model:
<?php
//application/models/Twitter.php
class Twitter_Model_Twitter
{
private $_twitter;
private $_username;
private $_password;
public function __construct(array $options = null)
{
if (is_array($options)) {
$this->setOptions($options);
$this->_twitter = new Zend_Service_Twitter($this->_username, $this->_password);
} else {
$twitterAuth = new Zend_Session_Namespace('Twitter_Auth');
$config = new Zend_Config_Ini(APPLICATION_INI, APPLICATION_ENV);
$key = $config->encryption->salt;
$iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_ECB);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$password = mcrypt_decrypt(MCRYPT_XTEA, $key, $twitterAuth->password, MCRYPT_MODE_ECB, $iv);
$username = $twitterAuth->username;
$this->_twitter = new Zend_Service_Twitter($username, $password);
}
}
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$pieces = explode('_', $key);
foreach($pieces AS $piece_key => $piece_value) {
$pieces[$piece_key] = ucfirst($piece_value);
}
$name = implode('',$pieces);
$method = 'set' . $name;
//$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
//I added this method. So that I could "inject"/set the $_twitter obj
public function setTwitter($obj)
{
$this->_twitter = $obj;
return $this;
}
public function verifyCredentials()
{
return $this->_twitter->account->verifyCredentials();
}
public function friendsTimeline($params)
{
return $this->_twitter->status->friendsTimeline($params);
}
//in the real code, more will go here...
}
And in my Unit Test, I have this:
<?php
// tests/application/models/TwitterTest.php
require_once ('../application/models/Twitter.php');
class Model_TwitterTest extends ControllerTestCase
{
public function testVerifyCredentials()
{
$stub = $this->getMock('Zend_Service_Twitter', array('verifyCredentials'),array(),'',FALSE);
//FALSE is actually the 5th parameter to flag getMock not to call the main class. See Docs for this.
//Now that I have set the $_twitter variable to use the mock, it will not call the main class - Zend_Rest_Client (i think)
$stub->expects($this->once())
->method('verifyCredentials');
$model = new Twitter_Model_Twitter();
//this is the part where i set the $_twitter variable in my model to use the $stub
$model->setOptions(array('twitter'=>$stub));
$model->verifyCredentials();
}
}
Anyways, I think I got it working.
1) The unit test no longer tried to connect to twitter.com:80
2) After I got the setOptions() working in the Twitter_Model, $model->verifyCredentials() in my unit test was successfully called.
I will wait for others in Stackoverflow to confirm that is the right answer. For the meantime, would like to hear from you guys.
Thanks!!!