-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseModelTest.php
More file actions
65 lines (56 loc) · 1.63 KB
/
BaseModelTest.php
File metadata and controls
65 lines (56 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Inspirum\Arrayable\Tests;
use Inspirum\Arrayable\BaseModel;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Throwable;
use function json_encode;
final class BaseModelTest extends TestCase
{
/**
* @param array<mixed> $data
*/
#[DataProvider('providesToArray')]
public function testJsonSerialize(array $data, string|Throwable $result): void
{
if ($result instanceof Throwable) {
$this->expectException($result::class);
$this->expectExceptionMessage($result->getMessage());
}
$model = new class ($data) extends BaseModel {
/**
* @param array<mixed> $data
*/
public function __construct(private array $data)
{
}
/**
* @return array<mixed>
*/
public function __toArray(): array
{
return $this->data;
}
};
self::assertSame($data, $model->__toArray());
self::assertSame($data, $model->toArray());
self::assertSame($data, $model->jsonSerialize());
self::assertSame($result, (string) $model);
self::assertSame($result, json_encode($model));
}
/**
* @return iterable<array<string,mixed>>
*/
public static function providesToArray(): iterable
{
yield [
'data' => [1, 3, 4],
'result' => '[1,3,4]',
];
yield [
'data' => ['a' => 1, 3, 'c' => true, 4],
'result' => '{"a":1,"0":3,"c":true,"1":4}',
];
}
}