Laravel unit testing same function twice and different output - unit-testing

I'm a bit new to Laravel unit testing. I need to get different outputs by calling same repo function for the unit testing.
So far my test is like this:
public function testReportOffdayWorked()
{
$input = [
'from_date' => '2016/01/01',
'to_date' => '2016/01/03',
];
$webServiceRepositoryMock = Mockery::mock('App\Repositories\WebServiceRepository');
$webServiceRepositoryMock->shouldReceive('callGet')->twice()->andReturn($this->issues);
$this->app->instance('App\Repositories\WebServiceRepository', $webServiceRepositoryMock);
$this->call('post', '/reporting/portal/report-offdays', $input);
$this->assertResponseOk();
$this->assertTrue($this->response->original->getName() == "Reporting::report_offday_worked");
}
I would like to get two different outputs for callGet function.

Set up a sequence of return values or closures for callGet().
andReturn(value1, value2, ...)
Sets up a sequence of return values or closures. For example, the first call will return value1 and the second value2. Note that all subsequent calls to a mocked method will always return the final value (or the only value) given to this declaration.
— docs.mockery
The following shows how to do it in PHPUnit mocks and mockery.
<?php
class The {
public function answer() { }
}
class MockingTest extends \PHPUnit_Framework_TestCase
{
public function testMockConsecutiveCalls()
{
$mock = $this->getMock('The');
$mock->expects($this->exactly(2))
->method('answer')
->will($this->onConsecutiveCalls(4, 2));
$this->assertSame(4, $mock->answer());
$this->assertSame(2, $mock->answer());
}
public function testMockeryConsecutiveCalls()
{
$mock = Mockery::mock('The');
$mock->shouldReceive('answer')->andReturn(4, 2);
$this->assertSame(4, $mock->answer());
$this->assertSame(2, $mock->answer());
}
}

How about using PHPUnit mocking framework?
$mock = $this->getMock('ClassName');
$mock->expects($this->at(0))
->method('getInt')
->will($this->returnValue('one'));
$mock->expects($this->at(1))
->method('getInt')
->will($this->returnValue('two'));
echo $mock->getInt(); //will return one
echo $mock->getInt(); //will return two

Related

How to write PHPunit test code with external object (having DAO)?

I'm trying to write test code for monosilic code like below.
Q1. How to write test code which has access to DB?
Q2. How to refactor these code testable?
Q3. Is there any way to write test code with fewer change to production code?
I need your help!
Thanks!!
Example Production Code)
<?
class Sample_Model_Service_A
{
private $_result
private $_options
private $_someValue
public function __construct($params, $ids, $data) {
$this->_options = Sample_Model_Service_B::getOption($data);
}
private function setSomeValue() {
// some code shaping $_params to $someValue with $this->_options
$this->_someValue= $someValue;
}
// want to write test for this function
// changed this function's logic
private function setResult() {
// some code shaping $_someValue to $result
$this->_result = $result;
}
public function getter() {
retrn $this->_result;
}
}
?>
<?
class Sample_Model_Service_B
{
// get option from DB
public static function getOption($data) {
$dao = new Model_Dao_Option();
$option = $dao->getOption($data['id']);
return $option;
}
}
?>
My Test Code so far)
public function testsetResult()
{
// just make sure these variables are defined
$params = $ids = $data = [];
// try to make test for private function
$sample = new Sample_Model_Service_A($params, $ids, $data);
$reflection = new ReflectionClass($sample);
// get Method
$method = $reflection->getMethod('setresult');
$method->setAccessible(true);
// wondering how to get $result
$result = $method->invoke($sample);
// assert
$this->assertSame($result);
}
Mockery solved my issue.
Sample Code)
/**
* #dataProvider sampleProvider
*/
public function testsetResult($sampleData)
{
// mock Sample_Model_Service_B
$mockSample_Model_Service_B = Mockery::mock('alias:' . Sample_Model_Service_B::class);
$mockSample_Model_Service_B->shouldReceive('getOption')->andReturn($sampleData['option']);
$sample = new Sample_Model_Service_A($sampleData['params'], $sampleData['ids'], $sampleData['data']);
$sample->setResult();
$result = $sample->getter();
// assert
$this->assertSame($result, $sampleData['result']);
}

Mockk AssertionError: Verification failed: call 1 of 1

///Here is my class
class State {
var state: Int = 10
}
open class Car {
var state:State = State()
fun changState(data: Int = 1) {
setState(data)
}
fun setState(data: Int = 0) {
state.state = data
}
}
/// Here is my Test
#Test
fun `test 1`() {
var mockCar = mockk<Car>()
every { mockCar.changState(any()) } just runs
every { mockCar.setState(any()) } just runs
mockCar.changState(10)
verify(exactly = 1) { mockCar.changState(any()) }
verify { mockCar.setState(any()) }
}
But it fails with this error
################################
java.lang.AssertionError: Verification failed: call 1 of 1: Car(#1).setState(any())) was not called.
Calls to same mock:
Car(#1).changState(10)
############################
You need to remove verify { mockCar.setState(any()) } - there is no way that this will ever be called, because you mocked
every { mockCar.changState(any()) } just runs
This means the stubbed method will do nothing, it just runs, so to speak.
I don't recommend writing tests that only test mocks, because it will lead to a bias that the code is fine when you just use outputs of what you think is correct behavior. Instead, write a separate unit test for Car.
For your use-case a mock is not the intended thing to use, you should be using a spy instead if you mix real method calls with mocked behavior.

phpunit testing methods with $this values

OK, probably a bad example. Here is an actual example. I would be wanting to unit test the total() method from a shopping cart class to see if the value is what I think it should be, which uses the getAll() (cart items), subTotal() and shipping() methods. Is total() something you could test using PHPUnit ?
class Cart extends ObjectModel
{
protected $uniqueID = 10;
public function getAll()
{
return $this->execute("SELECT *, cart.id AS cart_id FROM cart LEFT JOIN products ON products.id = cart.product_id WHERE cart.unique_id = ? AND cart.quantity > '0' AND cart.deleted_at IS NULL ", [$this->uniqueID] );
}
public function subTotal()
{
$subTotal = 0;
foreach($this->getAll() as $row){
$subTotal += ( $row->quantity * $row->cart_price );
}
return $subTotal;
}
public function total()
{
return $this->subTotal() + $this->shipping();
}
public function shipping()
{
if(isset($_SESSION[SALT.'shipping']) && $_SESSION[SALT.'shipping'] != ''){
return $_SESSION[SALT.'shipping'];
}
/* OR RETURN DEFAULT IF SHIPPING IS NOT SET */
return 0;
}
}
The execute method is as follows, there is no constructor method for the ObjectModel class
public function execute($query, array $array) {
$query = Db::conn()->prepare($query);
$query->execute($array);
return $query->fetchAll(PDO::FETCH_OBJ);
}
Db::conn() is just the PDO database handle.
The class is not too well designed for unit-testing because it is tightly coupled to the database. In a unit-test, you want to test the subject-under-test in isolation of external components (here: the database). Usually, you mock or stub the external components with a test double.
Because the DB instance is hardcoded in the execute method of the ObjectModel, you cannot easily replace it with a test double. You could turn the test into an integration test and change the DB connection to a test database which you then populate with expected values in the tests setup method.
If you don't want to use the database at all, you can self-shunt the subject-under-test. This means, you use a test double of the subject-under-test itself and make execute return the expected values for your test case.

Is it possible to mock a method in a Groovy class-under-test such that other methods within the class will use the mocked version?

For example:
class CutService {
String delegateToSelf(){
aMethod()
}
String aMethod(){
"real groovy value from CUT"
}
}
I've tried a variety of approaches, including:
#TestFor(CutService)
class CutServiceSpec extends Specification {
def expectedValue = "expected value"
void "test mocking CUT method using MockFor"() {
given:
MockFor mockCutService = new MockFor(CutService)
mockCutService.ignore.aMethod {expectedValue}
def cutServiceProxy = mockCutService.proxyDelegateInstance()
when:
String actualValue = null
mockCutService.use {
actualValue = cutServiceProxy.delegateToSelf()
}
then:
expectedValue == actualValue
}
}
Which gives:
| Failure: test mocking CUT method using MockFor(com...CutServiceSpec)
| junit.framework.AssertionFailedError: No more calls to 'delegateToSelf' expected at this point. End of demands.
at com...CutServiceSpec.test mocking CUT method using MockFor_closure4(CutServiceSpec.groovy:45)
at com...CutServiceSpec.test mocking CUT method using MockFor(CutServiceSpec.groovy:44)
Using metaClass appears to do what I want:
void "test mocking CUT method using metaClass"() {
given:
service.metaClass.aMethod = { expectedValue }
when:
String actualValue = service.delegateToSelf()
then:
expectedValue == actualValue
}
This test runs green.

How to mock Shiro accessControl() method in grails unit test case

I am using shiro security in my grail application.
Grails version : 2.2.1
shiro : 1.2.0
I have a problem in writing grails unit test case for the controller with filter enabled. When the test case run without filters then it is working fine, if it runs withFilters then it is failing for accessControl() method not found in the controller. I dont know how to make Shiro's api to be visible while running the test case.I referred shiro unit test case link http://shiro.apache.org/testing.html but I couldn't get any info regarding accessControl().I have given sample code how my classes and test case looks like
MyController.groovy
def create() {
// getting request parameters and validation
String emailId = params.emailId
def ret = myService.createUser(emailId)
return ret
}
MyControllerFilters.groovy
def filters = {
loginCheck(controller: 'user') {
before = {
//do some business checks
// Access control by convention.
accessControl() // This is a dynamic method injected by ShiroGrailsPlugin to FilterConfig, but this is not visible during the test case.
}
}
MyControllerTests.groovy
#TestFor(MyController)
#Mock(MyControllerFilters)
class MyControllerTests {
#Before
void setup() {
// initializing some variables
}
void testCreateUserWithFilter() {
request.accessAllowed = true
withFilters(action:"create") {
controller.create()
}
assert response.message == "success"
}
}
Try to use:
ControllerOrService.metaClass.method = {
return "" //what you need to be returned
}
Add parameters to closure if method take parameters:
ControllerOrService.metaClass.method = { def a, def b ->
return a + b
}
Don't forget to use full name of method when you mock them in that way.