4

In my business logic I don't care about the order of elements.

Here is my example of a test

$this->getJson('/api/order-attempts')
     ->assertJson([
         'data' => [
             ['status' => 'error'],
             ['status' => 'error'],
             ['status' => 'confirmed'],
             ['status' => 'confirmed'],
             ['status' => 'confirmed'],
         ],
     ])->isOk();

It fails from time to time only because of the order. I do NOT want to put sorting to the api, cause I don't need it.

Is it possible to assertJsonIgnoringOrder() somehow?

D.R.
  • 2,540
  • 3
  • 24
  • 49

2 Answers2

1

You can check the structure and alignment of json by using assertSame()

For example try this

 $this->assertSame(json_encode([
     'data' => [
         ['status' => 'error'],
         ['status' => 'error'],
         ['status' => 'confirmed'],
         ['status' => 'confirmed'],
         ['status' => 'confirmed'],
     ]),$response->getContent(), '');
Jayashree
  • 153
  • 1
  • 6
-1

There is a wide range of JSON-assertions and you might be able to build what you are looking for from that.

For example you can assert for parts of the json using assertJsonFragment() or just whether the structure is correct, ignoring the actual content using assertJsonStructure().

For example you could look if the json contains the correct number of elements with the key status using assertJsonCount($number, $key).

Unfortunately there is no particular assertion that already matches your requirement, at least not by default. There might be some library out there providing it for your.

dbrumann
  • 16,803
  • 2
  • 42
  • 58