I am doing some PHPUnit testing with symfony2. I am having a problem with one particular test.
I am testing a response form one of my Class of course one response is true one false. I have a Mock of my Database and I have a stub for one of the methods from my databaseRepository.
The issue is that in one test I do a stub to a method with an valid array, second test i just want to query to be invalid null.
my db MOck:
//Setting up mock of database repository class
$this->db = $this->getMockBuilder('DatabaseRepository')
->disableOriginalConstructor()
->getMock();
$this->db->expects($this->any())
->method('getRecord')
->will($this->returnValue(self::$registrationRecord));
$this->db->expects($this->any())
->method('getRecord')
->willReturn(null);
So I am trying to have two different expects but this obviousley does not work.....is it possible to have a stub method to have two different returns..?
test1:
<?php
class UnsubscribeRegistrationTemplateTest extends \PHPUnit_Framework_TestCase
{
/**
* @var UnsubscribeRegistrationTemplate
*/
protected $object;
/**
* @var ValidationClass
*/
public $validate;
/**
* @var DatabaseRepository
*/
public $db;
//Database Record Mock
public static $registrationRecord = array
(
'rowid' => '96',
'unsubscription' => 'N',
'updated' => 'BB'
);
/**
*
*/
protected function setUp()
{
//Setting up mock of validation class
$this->validate = $this->getMockBuilder('ValidationClass')
->disableOriginalConstructor()
->getMock();
$this->validate->expects($this->any())
->method('validateInput')
->willReturn(true);
//Setting up mock of database repository class
$this->db = $this->getMockBuilder('DatabaseRepository')
->disableOriginalConstructor()
->getMock();
$this->db->expects($this->any())
->method('getRegistrationRecord')
->will($this->returnValue(self::$registrationRecord));
$this->db->expects($this->any())
->method('getRegistrationRecord')
->will($this->returnValue(null));
$this->db->expects($this->any())
->method('setPreRegistrationEnquiryUnsubscriptionEnabled')
->willReturn(true);
$this->object = $this->createUnsubscribeRegistrationTemplateInstance();
}
/**
* @return UnsubscribeRegistrationTemplate
*
*/
public function createUnsubscribeRegistrationTemplateInstance()
{
//initialize Unsubscribe Registration Template
return new UnsubscribeRegistrationTemplate
(
$this->validate,
$this->db
);
}
/**
* @param array $mapping
* @return Request
*/
public function createRequest(array $mapping)
{
$request = new Request();
foreach ( $mapping as $k =>$v)
{
$request->query->set($k, $v);
}
return $request;
}
/**
*
*/
public function testUnsubscribeRegistrationTemplateValidResponse()
{
$request = $this->createRequest(array(
'registration_id' => '96',
'source_channel' => 'BB'
));
$response = new Response(
true,
'Unsubscription successful'
);
$this->assertEquals($response, $this->object->create($request));
}
/**
*
*/
public function testUnsubscribeRegistrationTemplateEmptyResponse()
{
$request = $this->createRequest(array(
'registration_id' => '96',
'source_channel' => 'BB'
));
$response = new Response(
false,
'Registration Record Not Found.'
);
$this->assertEquals($response, $this->object->create($request));
}
/**
*
*/
public function testIsAlreadyRegisteredValidResponse()
{
//Testing record is already unsubscribed
$registrationRecord = array(
'unsubscription_enabled' => 'Y'
);
$this->assertTrue($this->object->isAlreadyUnsubscribed($registrationRecord));
}
/**
*
*/
public function testIsAlreadyRegisteredInValidResponse()
{
//Testing record not unsubscribed
$registrationRecord = array(
'unsubscription_enabled' => 'N'
);
$this->assertFalse($this->object->isAlreadyUnsubscribed($registrationRecord));
}
/**
*
*/
protected function tearDown()
{
unset($this->object);
}
}
You can do this in many ways.
Here are two ways that may suits your needs.
1 - Move the getRecord() expects to the tests
As you can see, there's a lot of duplication, so let's use dataprovider.
2 - Using @dataProvider
Using @dataProvider you can use as many values as you want to test all cases.