adrema/tests/Lib/TestsInertia.php

62 lines
1.6 KiB
PHP
Raw Normal View History

2022-02-12 00:41:52 +01:00
<?php
namespace Tests\Lib;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\Assert as PHPUnit;
trait TestsInertia {
2022-02-12 16:44:43 +01:00
/**
* @param mixed $value
* @param TestResponse $response
* @param ?string $key
*
* @return void
*/
2022-02-12 01:25:22 +01:00
public function assertInertiaHas($value, TestResponse $response, ?string $key = null): void
2022-02-12 00:41:52 +01:00
{
$bindings = json_decode(json_encode($value), true);
$viewData = json_decode(json_encode(
data_get($response->viewData('page')['props'], $key)
), true);
$bindings = is_array($bindings) ? $bindings : [$bindings];
$viewData = is_array($viewData) ? $viewData : [$viewData];
$this->assertInertiaDeepNest($bindings, $viewData);
}
2022-02-12 01:16:37 +01:00
public function assertComponent(string $component, TestResponse $response): void
{
2022-02-12 00:41:52 +01:00
PHPUnit::assertEquals($component, $response->viewData('page')['component']);
}
2022-02-12 16:44:43 +01:00
/**
* @param mixed $should
* @param mixed $is
*
* @return void
*/
2022-02-12 01:25:22 +01:00
public function assertInertiaDeepNest($should, $is): void
2022-02-12 01:16:37 +01:00
{
2022-02-12 00:41:52 +01:00
foreach ($should as $key => $value) {
PHPUnit::assertArrayHasKey($key, $is);
if (is_array($value)) {
$this->assertInertiaDeepNest($value, $is[$key]);
continue;
}
PHPUnit::assertSame($value, $is[$key]);
}
}
2022-02-12 01:25:22 +01:00
/**
* @return mixed
*/
public function inertia(TestResponse $response, string $key)
2022-02-12 01:16:37 +01:00
{
2022-02-12 00:41:52 +01:00
return data_get($response->viewData('page')['props'], $key);
}
}