Note that there are some explanatory texts on larger screens.

plurals
  1. POPHPUnit: Verifying that array has a key with given value
    text
    copied!<p>Given the following class:</p> <pre><code>&lt;?php class Example { private $Other; public function __construct ($Other) { $this-&gt;Other = $Other; } public function query () { $params = array( 'key1' =&gt; 'Value 1' , 'key2' =&gt; 'Value 2' ); $this-&gt;Other-&gt;post($params); } } </code></pre> <p>And this testcase:</p> <pre><code>&lt;?php require_once 'Example.php'; require_once 'PHPUnit/Framework.php'; class ExampleTest extends PHPUnit_Framework_TestCase { public function test_query_key1_value () { $Mock = $this-&gt;getMock('Other', array('post')); $Mock-&gt;expects($this-&gt;once()) -&gt;method('post') -&gt;with(YOUR_IDEA_HERE); $Example = new Example($Mock); $Example-&gt;query(); } </code></pre> <p>How do I verify that <code>$params</code> (which is an array) and is passed to <code>$Other-&gt;post()</code> contains a key named 'key1' that has a value of 'Value 1'?</p> <p>I do not want to verify all of the array - this is just a sample code, in actual code the passed array has a lot more values, I want to verify just a single key/value pair in there.</p> <p>There is <code>$this-&gt;arrayHasKey('keyname')</code> that I can use to verify that the key exists.</p> <p>There is also <code>$this-&gt;contains('Value 1')</code>, which can be used to verify that the array has this value.</p> <p>I could even combine those two with <code>$this-&gt;logicalAnd</code>. But this of course does not give the desired result.</p> <p>So far I have been using returnCallback, capturing the whole $params and then doing asserts on that, but is there perhaps another way to do what I want? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload