I'm new to InertiaJS and I'm really enjoying working with it so far and thank you for this testing package.
In the following test example (similar to that in the docs under the Automatic Eloquent heading) the attributes returned in the users prop from the route are the same as those returned from the User factory, but the order of attributes is different as the model factory will return the id as the last element of the array.
I think this difference in order causes the test to fail because of the assertSame assertion, which is looking for an exact match in both attributes and order.
$users = User::factory()->count(2)->create();
$response = $this->get('/users');
$response->assertInertia(fn (InertiaAssert $page) =>
$page->component('Users/Index')
->where('users', $users->toArray())
);
Would it be worth adding a third parameter, maybe called exact or strict to the where method in the Matching trait.
This could default to true and use assertSame but if it was set to false then it could fallback to the less strict assertEquals to allow a test like this to pass. As an example implementation.
public function where($key, $value, $exact = true): self
{
$this->has($key);
if ($value instanceof Closure) {
$prop = $this->prop($key);
PHPUnit::assertTrue(
$value(is_array($prop) ? Collection::make($prop) : $prop),
sprintf('Inertia property [%s] was marked as invalid using a closure.', $this->dotPath($key))
);
} elseif ($value instanceof Arrayable) {
if ($exact) {
PHPUnit::assertSame(
$value->toArray(),
$this->prop($key),
sprintf('Inertia property [%s] does not exactly match the expected Arrayable.', $this->dotPath($key))
);
} else {
PHPUnit::assertEquals(
$value->toArray(),
$this->prop($key),
sprintf('Inertia property [%s] does not match the expected Arrayable.', $this->dotPath($key))
);
}
} elseif ($value instanceof Responsable) {
if ($exact) {
PHPUnit::assertSame(
json_decode(json_encode($value->toResponse(request())->getData()), true),
$this->prop($key),
sprintf('Inertia property [%s] does not exactly match the expected Responsable.', $this->dotPath($key))
);
} else {
PHPUnit::assertEquals(
json_decode(json_encode($value->toResponse(request())->getData()), true),
$this->prop($key),
sprintf('Inertia property [%s] does not match the expected Responsable.', $this->dotPath($key))
);
}
} else {
if ($exact) {
PHPUnit::assertSame(
$value,
$this->prop($key),
sprintf('Inertia property [%s] does not exactly match the expected value.', $this->dotPath($key))
);
} else {
PHPUnit::assertEquals(
$value,
$this->prop($key),
sprintf('Inertia property [%s] does not match the expected value.', $this->dotPath($key))
);
}
}
return $this;
}