Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:21:36+00:00 2026-05-24T07:21:36+00:00

What is the best way to test for multiple array keys in a PHPUnit

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:21:36+00:00Added an answer on May 24, 2026 at 7:21 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to unit test a method that calls into multiple
What is the best way to unit test a method that doesn't return anything?
What is the best way to test my webforms applications? Looks like people are
What is the best way to test the speed of a LAMP based site,
What is the best way to test GWT code? GWTTestCase in hosted mode is
What is the best way to test web service using NUnit. I want it
What is the best way to test an implementation of a mutex is indeed
Just wondering whats the best way to test Python CGI while developing a site?
I am looking for the best way to test if a website is alive
What is the best way to verify/test that a text string is serialized to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.