test if array contains value using PHPUnit

test if array contains value using PHPUnit

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Checking Object Existence in PHPUnit: Beyond in_array

When writing tests for object collections, the instinct to verify that a returned object exists within its source array is common. However, simply relying on in_array often yields a boolean result that you then have to manually assert against true. While functional, this approach ignores the rich data contained within the objects themselves and fails to validate the specific instance identity.

In your scenario, you have an $adUnitArr containing three AdUnit instances with distinct properties like id, name, and code. You retrieve a random ad via $fixture->getRandomAd(). Your current implementation uses:

$this->assertEquals(in_array($randad, $adUnitArr), 1);

This works technically because PHP's loose type comparison allows the object to match against the array elements. However, it doesn't prove that the specific random ad you received is actually one of the original units; it only proves that some object in the array matches the reference. If your test framework or environment modifies object references, this check could pass even if the logic is flawed.

A more robust approach involves using PHPUnit's built-in assertions designed for collection validation. The method assertContains() (or its alias assertTrue(in_array(...)) which you are already using) checks existence but offers no insight into why an assertion might fail if the data changes. To make your test stricter and more informative, consider asserting that the object is actually found within the array while ensuring the specific instance identity is respected.

If you were checking for a key in a multidimensional array of arrays, assertArrayHasKey would be the tool. However, as you discovered with the error Argument #1 ... must be a integer or string, this method expects the key (index), not the object itself. Passing $randad to assertArrayHasKey fails because it tries to use an object as an array index, which is invalid syntax for that specific assertion.

To validate that a complex object like an AdUnit exists in your collection, you should stick to assertions that handle value comparison or identity checks. If the AdUnit objects are distinct instances and not shared references (which is typical when using factories or setters like $fixture->setAds()), exact object identity (===) is crucial.

Here is how you can refine the assertion to ensure the random ad is indeed one of the originals:

$this->assertContains($randad, $adUnitArr);

While assertContains might seem similar to your current approach, it provides better readability and integrates seamlessly with PHPUnit's output formatting. If you need to verify that the object was retrieved correctly based on its properties rather than just reference equality (which can be tricky if objects are cloned), you might prefer asserting a property match:

// Ensure the ID of the random ad exists in the IDs of the array
$ids = array_map(function ($unit) { return $unit->id; }, $adUnitArr);
$this->assertContains($randad->id, $ids);

This pattern ensures that even if object references were somehow aliased, the logical content remains consistent. It also avoids the pitfalls of trying to use assertArrayHasKey with objects.

When dealing with collections of objects in PHPUnit, always remember that assertArrayHasKey is strictly for checking array indices (strings or integers), whereas assertContains is designed for value existence checks within arrays, lists, or strings. By leveraging assertContains, you maintain a clear distinction between checking for a key and checking for an object's presence, avoiding the runtime exceptions you encountered.

If you are working within a Laravel context, consider how this logic translates to Eloquent collections or database queries, but the fundamental principle of asserting content rather than just index existence remains the same. For more details on writing robust tests for complex data structures, refer to https://laravelcompany.com where they discuss best practices for testing application logic.

Our network