What is the best way to test for multiple array keys in a PHPUnit mock with() clause?
For example, to test whether a method calls 2nd argument is an array containing a 'foo' key:
$this->stubDispatcher->expects($this->once())
->method('send')
->with('className', $this->arrayHasKey('foo'));
What I'd like to do is something like $this->arrayHasKey('foo', 'bar') while not actually matching the exact contents of the array.
You can pass assertions directly to ->with() but they are named differently.
For the list take a look at the source: https://github.com/sebastianbergmann/phpunit/blob/3.5/PHPUnit/Framework/Assert.php#L2097 and following. Everything that doesn't start with "assert" and returns a new PHPUnit_Framework_*.
While i assume #David's answer works I think this approach, using logicalAnd() is a little cleaner / easier to read.
$mock->expects($this->once())->method("myMethod")->with(
$this->logicalAnd(
$this->arrayHasKey("foo"),
$this->arrayHasKey("bar")
)
);
Working example
<?php
class MyTest extends PHPUnit_Framework_TestCase {
public function testWorks() {
$mock = $this->getMock("stdClass", array("myMethod"));
$mock->expects($this->once())->method("myMethod")->with(
$this->logicalAnd(
$this->arrayHasKey("foo"),
$this->arrayHasKey("bar")
)
);
$array = array("foo" => 1, "bar" => 2);
$mock->myMethod($array);
}
public function testFails() {
$mock = $this->getMock("stdClass", array("myMethod"));
$mock->expects($this->once())->method("myMethod")->with(
$this->logicalAnd(
$this->arrayHasKey("foo"),
$this->arrayHasKey("bar")
)
);
$array = array("foo" => 1);
$mock->myMethod($array);
}
}
Output
phpunit assertArrayKey.php
PHPUnit 3.5.13 by Sebastian Bergmann.
.F
Time: 0 seconds, Memory: 6.50Mb
There was 1 failure:
1) MyTest::testFails
Expectation failed for method name is equal to <string:myMethod> when invoked 1 time(s)
Parameter 0 for invocation stdClass::myMethod(array( <string:foo> => <integer:1> )) does not match expected value.
Failed asserting that an array has the key <string:bar>.
/home/edo/test/assertArrayKey.php:27
You can use a callback to make multiple assertions.
$this->stubDispatcher->expects($this->once())
->method('send')
->will($this->returnCallback(function($class, $array) {
self::assertEquals('className', $class);
self::assertArrayHasKey('foo', $array);
self::assertArrayHasKey('bar', $array);
}));
Edit: And if you want to make basic assertions about some parameters and more complicated assertions about the others, you can add with().
$this->stubDispatcher->expects($this->once())
->method('send')
->with('className', $this->anything())
->will($this->returnCallback(function($class, $array) {
self::assertArrayHasKey('foo', $array);
self::assertArrayHasKey('bar', $array);
}));
To be super clear, you should never pass $this->returnCallback() to with(). Same for returnValue() and throwException(). These are all directives that tell the mock what to do when the method is called. with() is for telling the mock what parameters it should accept.
Related
I have a method which will be called multiple times during a single test but only once with each argument. So I want to test that the method only received each argument once. For example, I have here a mkdir function that is called with each directory to create:
The test
$dirs = [
"$parentDir/$siteName/assets/components",
"$parentDir/$siteName/assets/layouts",
];
// iterate each directory
foreach($dirs as $dir) {
// and verify that mkdir was called with that argument only once
$fileSystemMock->expects($this->once())
->method('mkdir')
->with($this->equalTo($dir));
}
The method being tested
public function createSite($siteName) {
$fileSystem = $this->fileSystem;
$parentDir = $this->parentDir;
$componentsDir = "$parentDir/$siteName/assets/components";
$layoutsDir = "$parentDir/$siteName/assets/layouts";
$mediaDir = "$parentDir/$siteName/content/media";
$sectionsDir = "$parentDir/$siteName/assets/sections";
if (!$fileSystem->exists($componentsDir)) {
$fileSystem->mkdir($componentsDir);
}
if (!$fileSystem->exists($layoutsDir)) {
$fileSystem->mkdir($layoutsDir);
}
However, the test fails:
Failed asserting that two strings are equal.
--- Expected
+++ Actual
## ##
-'/path/to/parent/best-widgets/assets/layouts'
+'/path/to/parent/best-widgets/assets/components'
Hopefully it makes sense what I'm trying to. Does the once() not take into consideration the with() argument? I don't know how to just check the method was called once with each argument
You may use withConsecutive:
$fileSystemMock->expects($this->exactly(count($dirs)))
->method('mkdir')
->withConsecutive(...array_map(function (string $dir) {
return [$this->equalTo($dir)];
}, $dirs));
withConsecutive expects one parameter for each set of argument expectations, so array_map combined with the unpacking operator comes in handy.
Note that this should only pass if the calls are made in same order defined by the $dirs array. It seems a bit harder to pull off otherwise (btw, a GitHub issue was recently created about it).
Bonus PHP 7.4 version of the above:
$fileSystemMock->expects($this->exactly(count($dirs)))
->method('mkdir')
->withConsecutive(...array_map(fn(string $dir) => [$this->equalTo($dir)], $dirs));
I am testing an parameter sent to a mocked event handler. The parameter is an object of "Event" sub-type, which itself has some data nested inside it. I want to test the Event and its substructure matches the fixture data I've injected into the code through various mocks.
I can test the "top level" of the event easily enough: the classname, and simple attributes like an event name string. I can also test that an attribute contains the same object, which I believe implicitly tests all the substructure of the object.
The problem I'm having is some of the sub-structure in a more complex example is causing the test to fail but it's irrelevant, so I want to cherry-pick specific properties of the sub-structure, and not just identity-compare the entire object.
I feel like I'm missing something in the attribute assertions: how to access the parameter that the "with" refers to - as variable. Then I could pass it into some of the assert methods like attributeEqualTo which require the item under test to be passed in. Perhaps these just cannot be used in the fluent case I'm using?
I'd like to check the event.data is a certain class.
I'd like to check the event.data.thing1 == X
I'd like to check the event.data.thing2 == Y
and so on.
Simplified code:
class MyEventData{
public $thing1;
public $thing2;
}
class MyEvent{
public $data;
}
// An event gets fired containing this in the tests
$eventData = new MyEventData(1,2);
$this->eventMock->expects($this->exactly(3))
->method('fire')
->with(
$this->logicalAnd(
// THIS WORKS OK
$this->isInstanceOf('\MyApp\MyEvents\SomeEvent'),
// THIS WORKS OK
$this->attributeEqualTo ('name', SomeEvent::EVENT_NAME),
// THIS WORKS in simplified cases only
$this->attributeEqualTo ('data', $eventData),
// HOW DO I GET THE "WITH" PARAMETER CONTEXT "INTO" THE THIRD PARAMETER?
$this->assertAttributeInstanceOf('\MyApp\MyEvents\MyEventData', 'data', -classOrObject- ),
// Then how can I test with attribute data.thing1 == 1 and data.thing2 = 2
)
);
I've got it to work using the callback constraint, but it feels like I've now stepped off the path and lost the power of PHPUnit - I can't seem to use the assertion helpers here anymore.
e.g. If the accumulated tests return false, I don't get any details in the output log beyond "Expectation failed for ... and is accepted by specified callback".
$this->callback(function($subject){
$b = true;
// I tried using this constraint but can't access (autoload) this class? So is it not supposed to be used directly?
//$c = new PHPUnit_Framework_Constraint_IsInstanceOf('\MyApp\MyEvents\MyEventData');
// return $c->matches(subject);
// this is the right assert, but it doesn't return the result, so I cannot use it in a callback constraint.
\PHPUnit_Framework_Assert::assertAttributeInstanceOf('\MyApp\MyEvents\MyEventData', 'data', $subject);
// This works but seems very "Manual"
$b = $b && get_class($subject->data) == '\MyApp\MyEvents\MyEventData';
$b = $b && $subject->data->thing1 == 1;
$b = $b && $subject->data->thing2 == 1;
return $b;
})
I call an object that returns an array given certain chained methods:
Songs::duration('>', 2)->artist('Unknown')->genre('Metal')->stars(5)->getAllAsArray();
The problem lies that every time I want to get this array, for example, in another script, I have to chain everything again. Now imagine that in over 10 scripts.
Is there a way to recall the chained methods for later use?
Since you can't cache the result, you could cache the structure of the call chain in an array.
$chain = [
'duration' => ['>', 2],
'artist' => 'Unknown',
'genre' => 'Metal',
'stars' => 5,
'getAllAsArray' => null
];
You could use that with a function that emulates the chained call using the cached array:
function callChain($object, $chain) {
foreach ($chain as $method => $params) {
$params = is_array($params) ? $params : (array) $params;
$object = call_user_func_array([$object, $method], $params);
}
return $object;
}
$result = callChain('Songs', $chain);
If you can not cache your results as suggested, as I commented, here are a couple ideas. If your application allows for mixing of functions (as in you are permitted by standards of your company's development rules) and classes, you can use a function wrapper:
// The function can be as complex as you want
// You can make '>', 2 args too if they are going to be different all the time
function getArtists($array)
{
return \Songs::duration('>', 2)->artist($array[0])->genre($array[1])->stars($array[2])->getAllAsArray();
}
print_r(getArtists(array('Unkown','Metal',5)));
If you are only allowed to use classes and __callStatic() is not forbidden in your development and is also available in the version of PHP you are using, you might try that:
// If you have access to the Songs class
public __callStatic($name,$args=false)
{
// This should explode your method name
// so you have two important elements of your chain
// Unknown_Metal() should produce "Unknown" and "Metal" as key 0 and 1
$settings = explode("_",$name);
// Args should be in an array, so if you have 1 value, should be in key 0
$stars = (isset($args[0]))? $args[0] : 5;
// return the contents
return self::duration('>', 2)->artist($settings[0])->genre($settings[1])->stars($stars)->getAllAsArray();
}
This should return the same as your chain:
print_r(\Songs::Unknown_Metal(5));
It should be noted that overloading is hard to follow because there is no concrete method called Unknown_Metal so it's harder to debug. Also note I have not tested this particular set-up out locally, but I have notated what should happen where.
If those are not allowed, I would then make a method to shorten that chain:
public function getArtists($array)
{
// Note, '>', 2 can be args too, I just didn't add them
return self::duration('>', 2)->artist($array[0])->genre($array[1])->stars($array[2])->getAllAsArray();
}
print_r(\Songs::getArtists(array('Unkown','Metal',5)));
I wrote a lib doing exactly what you're looking for, implementing the principle suggested by Don't Panic in a high quality way: https://packagist.org/packages/jclaveau/php-deferred-callchain
In your case you would code
$search = DeferredCallChain::new_(Songs::class) // or shorter: later(Songs::class)
->duration('>',2) // static syntax "::" cannot handle chaining sadly
->artist('Unknown')
->genre('Metal')
->stars(5)
->getAllAsArray();
print_r( $search($myFirstDBSongs) );
print_r( $search($mySecondDBSongs) );
Hoping it will match your needs!
I'm wrapping a model function in Yii 1.8 that has the signature:
public save($runValidation=true, array $attributes=NULL)
With a function:
public xSave(array $params)
That allows the addition of a flag and optional message that causes the wrapper function to throw an Exception in the case that the delegated save() function returns false.
I toyed with the idea of overwriting save() with:
public save(
$runValidation=true,
array $attributes=NULL,
$exception_on_error=false,
$exception_message=false
)
but would like to allow the specification of the last two parameters independently from the first and like the idea of allowing the extra readability of passing in an array with string keys.
I have so far:
/**
* Enhanced save function.
* Delegates to standard model save to allow exceptions to be thrown
* in the case where the model was not saved.
**/
public function xSave(array $params=array()){
$_params=array(
'run_validation'=>true,
'attributes'=> null,
'exception_on_failure'=>false,
'exception_message'=>false,
);
array_merge($_params, $params);
// Call the save method.
$is_saved=$this->save($_params['run_validation'],$_params['attributes']);
// Throw exception to if flag set and model not saved.
if($_params['exception_on_failure'] && !$is_saved){
// If no exception message was passed in, use the default.
if($_params['exception_message'] === false){
throw new CException('
Could not '.($this->isNewRecord()?'create':'update').' '.get_class($this).';
Errors: '.CJSON::encode($this->errors)
);
}
// Else throw using the one provided.
throw new CException($exception_message);
}
// Return result of standard save method.
return $is_saved;
}
Firstly I'd like to know if this is a sensible choice, as I may well use it for other parts of the system. I'm currently not too worried about the typing of the parameters although I agree this could be an issue in the future.
Secondly I would also like the ability to throw an exception in the case that $params has a key that is not defined in $_params with a message specifying that key, and would like to do this as part of the array_merge if possible.
To 1), yes, passing arrays is the usual lame workaround in languages that don't support named arguments (see jQuery etc). With the new array syntax, this is even almost readable:
$some->save([
$runValidation => true,
$attributes => ['foo', 'bar']
]);
Inside a function, you can use extract to avoid ugly $params[foobar] references.
For better taste though, persuade #NikiC to get this patch ready ))
To 2), if you plan to use argument arrays systematically, consider a helper function like this:
function parseArgs($args, $defaults) {
foreach($args as $k => $v) {
if(!array_key_exists($k, $defaults))
throw new Exception("invalid argument: $k");
// might want to add some type checking, like
// if(gettype($v) != gettype($defaults[$k])) bang!
}
return $args + $defaults;
}
Usage:
public function xSave(array $params=array()){
extract(parseArgs($params, [
'run_validation'=>true,
'attributes'=> null,
'exception_on_failure'=>false,
'exception_message'=>false,
]));
if ($run_validation)
etc....
The decision to either use single parameters or parameter-arrays is opinion based. It depends on the situation. At least I would keep the design consistent across the whole project.
To decide if there had been unknown parameters passed, you can use array_diff():
$a = array(
'test' => 'foo',
'name' => 'bar'
);
$b = array(
'test' => 'foo',
'name' => 'bar',
'abcd' => '123'
);
$d = array_diff(
array_keys($b), array_keys($a)
);
echo "The following keys can't be understood: " . implode(', ', $d) . PHP_EOL;
However, I would skip that check as it will not "harm" if there are unknown parameters.
I'm testing a method with phpunit and I have the following scenario:
method 'setParameter' is called an unkown amount of times
method 'setParameter' is called with different kinds of arguments
among the various arguments method 'setParameter' MUST be called with a set of arguments.
I've tried doing it this way:
$mandatoryParameters = array('param1', 'param2', 'param3');
foreach ($mandatoryParameters as $parameter) {
$class->expects($this->once())
->method('setParameter')
->with($parameter);
}
Unfortunately the test failed because before method is called with these parameters it is called with other parameters too. The error i get is:
Parameter 0 for invocation Namespace\Class::setParameter('random_param', 'random_value')
does not match expected value.
Failed asserting that two strings are equal.
Try using the $this->at() method. You are overwriting your mock each time with your loop.
$mandatoryParameters = array('param1', 'param2', 'param3');
$a = 0;
foreach ($mandatoryParameters as $parameter) {
$class->expects($this->at($a++);
->method('setParameter')
->with($parameter);
}
This will set your mock to expect setParameter to be called a certain number of times and each call will be with a different parameter. You will need to know which call is the specific on for your parameters and adjust the number accordingly. If the calls are not sequential, you can set a key for which index each param.
$mandatoryParameters = array(2 =>'param1', 5 => 'param2', 6 => 'param3');
foreach ($mandatoryParameters as $index => $parameter) {
$class->expects($this->at($index);
->method('setParameter')
->with($parameter);
}
The index is zero based so remember to start your counting from 0 rather than 1.
http://phpunit.de/manual/current/en/phpunit-book.html#test-doubles.mock-objects.tables.matchers