How to assert input on a chained mock method - php

Im using PHPunit 8.3.5 and I'm trying to check wether a method gets the right parameter.
Example code:
$this->registry->get($thing)->apply(EXAMPLE::SUBMIT);
$this->registry->get($thing)->apply(EXAMPLE::CANCEL);
I have two functions, functionA uses the first example line, functionB the second. I need to make sure functionA uses SUBMIT and nothing else, and the same for Bs case.
The problem:
I can use a ->method('apply')->with() with a callback to test wether it gets the right input
I can create a willReturn for ->method('get')->with() to return a simple class with apply as function
I can't figure out how to combine the two
$registryMock = $this->createMock(Registry::class);
$registryMock->method('get')->willReturn(new class {
public function apply(){} // <-- I need to assert the input of this method
});
$registryMock->method('apply')->with(self::callback(function($order, $status){
return $status === EXAMPLE::SUBMIT;
}));
How can I combine those two methods? I've also tried get->apply, but that wasnt it.
Please note: Rewriting the actual code is not an option.

Based on the comment of Dirk:
You create a mock first the 2nd function like you would normally. You then create a mock that returns the previous mock:
// first we create a mock for the last in the chain, here '->apply()'
$registryMockApply = $this->createMock(Registry::class);
$registryMockApply->expects(self::once())->method('apply')->with(
self::equalTo(EXAMPLE::SUBMIT),
);
// Then the one before that, here '->get()', which returns the previous mock
$registryMock = $this->createMock(Registry::class);
$registryMock->method('get')->willReturn($registryMockApply);
// Together resulting in '->get()->apply()'

Related

Laravel Class/Controller level variable doesnt get update from Method

I have this variable on class/Controller level $songRating and i am calling this method through ajax, when i first time call this method it runs the if block which is good. and now $songRating should be 1.
But this is not a case. When i call this method it again runs the if block. Dont know why :/
public $songRating;
public function GetHighRatedSong()
{
if($this->songRating == null){
$this->songRating=1;
}else{
$this->songRating=2;
}
return response()->json($this->songRating);
}
Try with Replacing
$this->songrating=1;
to
$this->songRating=1; # song+Rating != song+rating
Read PHP & Case Sensitivity
It's because everytime you call that function or make a new instantiation to the class, it will automatically reset to it's original value. Try to use service container

PHPUnit how to test a specific method with specific parameter with specific answer for more than one call

I have tried testing multiple parameters for a specific method, and get different answers by the same mock for different parameters.
This is what I've done so far:
$mock = $this->getMockBuilder('MyClass')->disableOriginalConstructor()->getMock();
$mock->expects($this->any())
->method('myMethod')
->with($this->equalTo('param1'))
->will($this->returnValue('test1'));
$mock->expects($this->any())
->method('myMethod')
->with($this->equalTo('param2'))
->will($this->returnValue('test2'));
When I call $myClass->myMethod('param1') all is well and I get the 'test1'
However, here's the problem:
When I call $myClass->myMethod('param2') I get an error
Failed asserting that two strings are equal.
--- Expected
+++ Actual ## ##
-'param1'
+'param2'
A solution I have found is to just create a new mock for each call.
$mock1 = $this->getMockBuilder('MyClass')->disableOriginalConstructor()->getMock();
$mock1->expects($this->any())
->method('myMethod')
->with($this->equalTo('param1'))
->will($this->returnValue('test1'));
$mock2 = $this->getMockBuilder('MyClass')->disableOriginalConstructor()->getMock();
$mock2->expects($this->any())
->method('myMethod')
->with($this->equalTo('param2'))
->will($this->returnValue('test2'));
I do not know though why is this needed, perhaps I am using it wrong.
So the question remains:
How do I mock the same class, with a specific method, for different parameters and get different return values?
You can also simplify these statements a bit. If all you're needing to do is mock the function so that when you pass 'param1' you get back 'test1', then this should work:
$mock->method('myMethod')
->with('param1')
->willReturn('test1');
$mock->method('myMethod')
->with('param2')
->willReturn('test2');
If it's about dependency for a tested class, that is called twice inside tested method then it could be made like this
$mock = $this->getMockBuilder('MyClass')->disableOriginalConstructor()->getMock();
$mock->expects($this->at(0))
->method('myMethod')
->with($this->equalTo('param1'))
->will($this->returnValue('test1'));
$mock->expects($this->at(1))
->method('myMethod')
->with($this->equalTo('param2'))
->will($this->returnValue('test2'));
The first time it must be called with arg param1 then $mock->myMethod('param1') and will return test1, second - with arg param2 and will return test2.

Mockery: Testing chained method calls with different return values (here: Symfony ProcessBuilder)

I'm trying to test a method that creates 2+ Process objects (also see API docs) using Symfony's ProcessBuilder and I've run into problems when trying to return different mock process objects. In fact, I'm a bit unsure about whether Mockery can even do this.
Is it possible to choose the value in Mockery's andReturn() based on the call chain of an object (including arguments)?
Ideally I'm looking for something like this:
<?php
$processBuilderMock
->shouldReceive('setPrefix("test")->add("-f")->getProcess')
->andReturn($testProcess);
Full example:
The following code creates a file /tmp/dummy if it does not exist. It uses the two commands test -f /tmp/dummy and touch /tmp/dummy for that (I know it's a dumb example).
<?php
class Toggler
{
public function toggleFile(ProcessBuilder $builder)
{
$testProcess = $builder
->setPrefix('test')->setArguments(array('-f', '/tmp/dummy'))
->getProcess();
$testProcess->run();
if (!$testProcess->isSuccessful()) { // (a)
$touchProcess = $builder
->setPrefix('touch')->setArguments(array('/tmp/dummy'))
->getProcess();
$touchProcess->run();
return $touchProcess->isSuccessful(); // (b)
} else {
// ...
}
}
}
To test all cases, I need to be able to mock the Process objects for the corresponding commands test and touch (here: $testProcess and $touchProcess). Ideally, the test code for this would look like this:
<?php
public function testToggleFileFileDoesNotExist()
{
$testProcess = \Mockery::mock('\Symfony\Component\Process\Process');
$testProcess->shouldReceive('isSuccessful')->andReturn(false); // (a)
$testProcess->shouldReceive('run');
$touchProcess = \Mockery::mock('\Symfony\Component\Process\Process');
$touchProcess->shouldReceive('isSuccessful')->andReturn(false); // (b)
$touchProcess->shouldReceive('run');
$builder = \Mockery::mock('\Symfony\Component\Process\ProcessBuilder');
$builder->shouldReceive('setPrefix("test")->getProcess')->andReturn($testProcess); // (c) Doesn't work!
$builder->shouldReceive('setPrefix("touch")->getProcess')->andReturn($touchProcess); // (c) Doesn't work!
$toggler = new Toggler();
$this->assertTrue($toggler->toggleFile($builder)); // see (b)
}
However, since Mockery does not allow arguments in the call chain, I'm a bit lost on how to test such a scenario. Any ideas?
This is slightly terrible, but you could try entering each step of the call chain separately and having it return the mock $builder. Which process is returned at the end would depend on the order of the calls.
$builder->shouldRecieve('setPrefix')->with('test')->andReturn($builder);
$builder->shouldRecieve('setPrefix')->with('touch')->andReturn($builder);
$builder->shouldRecieve('getProcess')
->andReturnValues(array($testProcess, $touchProcess));
This feels very messy, but it's the only way I've been able to find so far. (If you came up with anything better since posting this, I'd be curious to see.)

How do I unit test a function that loops over objects?

Let me explain myself.
function insertObjects($objs) {
foreach ($objs as $obj) {
$this->repository->insert($obj);
}
}
I don't want to test that insertion into the database worked because I assume it works (it's a different unit). I also don't want to test foreach because obviously foreach is going to work. So the only thing to test here is that $objs is a well formed array. But if $objs is the mock data that I will be passing in... so does this mean there is nothing to test for this function?
If there's any chance of invalid input (a not well-formed array, null value, etc.), you need to handle that case in your method by explicitly checking for it.
In your test, you would then try to call your method with various invalid values, and check whether your method responds correctly, i.e. the database insert method is not called, exceptions are thrown, errors are logged, etc.
Other than that, the only thing to test is whether your database insert method is called with the parameters that correspond to the values in the valid test array you pass in.

Returning Different Results in PHPUnit Mock Object

I've been working on getting our systems more compatible with PHPUnit so we can do more unit testing of our classes and have managed to get some of them working with mock objects, but I've run across a problem which I can't seem to get around.
One of the classes we have (which I'm creating a mock version of) is for queries. You pass the query string into it's "query()" method, it logs the query, runs it and returns the result. It also wraps the mysql_fetch_assoc with a method called "get_row()", which returns an array value much like the original.
The problem is that, in some methods, there's more than one query being passed to the "query()" method and as a result it needs to run through multiple while loops to load the data into different variables. I've created a simplified version below:
class object{
public function __construct($query){
$this->query = $query;
}
public function loadData(){
$data1 = queryDataSource("SELECT * FROM data1");
$data2 = queryDataSource("SELECT * FROM data2");
return Array(
"data1" => $data1,
"data2" => $data2,
);
}
private function queryDataSource($query){
$this->query->query($query)
while($row = $this->query->get_row()){
$result[] = $row;
}
return $result
}
}
class testObject extends PHPUnit_Framework_TestCase{
method testLoadData(){
$test_data = Array('name' => 'Bob', 'number' => '98210');
$query = $this->getMock('Query');
$query->expects($this->any())->method('query');
$query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
$query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
$query->expects($this->at(3))->method('get_row')->will($this->returnValue($test_data);
$query->expects($this->at(4))->method('get_row')->will($this->returnValue(False);
}
}
In order to escape the first while loop in $object->queryDataSource() I'm returning a boolean FALSE value, as would happen when doing mysql_fetch_assoc. The problem is that, when it tries to run the second query and fetch the data through get_row(), the mock object seems to keep returning FALSE ratehr than moving on to the at(3) point. This happens even with 4 objects, only the first will get the test data as a return value then get FALSE the second time, the others will get FALSE every time.
Does anyone know if there's a way to get around this? I tried removing the FALSE flags and just having the odd values in at(), but that had the same problem, and I tried just having it return the data for at(1-2), but that just passed all the data into the first while loop and nothing for the other.
Thanks for any help you can give, hope the description of the problem's clear enough
I can't run the code as it seems to only be pseudocode but from what I understood is that you are trying to mock like this:
Call to query, get_row, get_row, query, get_row, get_row.
The issue you seem to have run into is that the number in the ->at() matcher doesn't count up per method but per object.
So what you probably want to write is:
$query->expects($this->any())->method('query');
$query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
$query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
$query->expects($this->at(4))->method('get_row')->will($this->returnValue($test_data);
$query->expects($this->at(5))->method('get_row')->will($this->returnValue(False);
Or to make it a litte easer to read maybe even:
$query->expects($this->at(0))->method('query');
$query->expects($this->at(1))->method('get_row')->will($this->returnValue($test_data);
$query->expects($this->at(2))->method('get_row')->will($this->returnValue(False);
$query->expects($this->at(3))->method('query');
$query->expects($this->at(4))->method('get_row')->will($this->returnValue($test_data);
$query->expects($this->at(5))->method('get_row')->will($this->returnValue(False);
With your mocks you ran into the issue that the second call to "query" was counting up one "call" and hence skipping over the second return($test_data);.
Unfortunately the at() binds your tests to the implementations very strongly.
Imagine if you rearranged 2 method calls inside a tested method, the functionality is exactly the same but all tests using at() would now fail, often with cryptic messages such as method doesn't exist at index N
On the occasions you want to specifically say "this called exactly this way then this called exactly this way" that's great, but if you just want assertions then one of the PHPUnit Mock Extensions seems more friendly, particularly Mockery and a guide here (a touch out of date I believe)
There are others also.

Categories