|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RonasIT\Support\Tests; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Database\Eloquent\CastsAttributes; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
| 7 | +use Illuminate\Support\Arr; |
| 8 | +use Illuminate\Support\Collection; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | +use PHPUnit\Framework\Assert; |
| 11 | +use RonasIT\Support\Traits\FixturesTrait; |
| 12 | + |
| 13 | +class ModelTestState extends Assert |
| 14 | +{ |
| 15 | + use FixturesTrait; |
| 16 | + |
| 17 | + protected Collection $state; |
| 18 | + protected Model $model; |
| 19 | + protected array $jsonFields; |
| 20 | + |
| 21 | + public function __construct(string $modelClassName) |
| 22 | + { |
| 23 | + $this->model = new $modelClassName(); |
| 24 | + $this->state = $this->getDataSet($this->model->getTable()); |
| 25 | + $this->jsonFields = $this->getModelJSONFields(); |
| 26 | + } |
| 27 | + |
| 28 | + public function assertNotChanged(): void |
| 29 | + { |
| 30 | + $changes = $this->getChanges(); |
| 31 | + |
| 32 | + $this->assertEquals([ |
| 33 | + 'updated' => [], |
| 34 | + 'created' => [], |
| 35 | + 'deleted' => [] |
| 36 | + ], $changes); |
| 37 | + } |
| 38 | + |
| 39 | + public function assertChangesEqualsFixture(string $fixture, bool $exportMode = false): void |
| 40 | + { |
| 41 | + $changes = $this->getChanges(); |
| 42 | + |
| 43 | + $this->assertEqualsFixture($fixture, $changes, $exportMode); |
| 44 | + } |
| 45 | + |
| 46 | + protected function getChanges(): array |
| 47 | + { |
| 48 | + $updatedData = $this->getDataSet($this->model->getTable()); |
| 49 | + |
| 50 | + $updatedRecords = []; |
| 51 | + $deletedRecords = []; |
| 52 | + |
| 53 | + $this->state->each(function ($originItem) use (&$updatedData, &$updatedRecords, &$deletedRecords) { |
| 54 | + $updatedItemIndex = $updatedData->search(fn ($updatedItem) => $updatedItem['id'] === $originItem['id']); |
| 55 | + |
| 56 | + if ($updatedItemIndex === false) { |
| 57 | + $deletedRecords[] = $originItem; |
| 58 | + } else { |
| 59 | + $updatedItem = $updatedData->get($updatedItemIndex); |
| 60 | + $changes = array_diff_assoc($updatedItem, $originItem); |
| 61 | + |
| 62 | + if (!empty($changes)) { |
| 63 | + $updatedRecords[] = array_merge(['id' => $originItem['id']], $changes); |
| 64 | + } |
| 65 | + |
| 66 | + $updatedData->forget($updatedItemIndex); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + return [ |
| 71 | + 'updated' => $this->prepareChanges($updatedRecords), |
| 72 | + 'created' => $this->prepareChanges($updatedData->values()->toArray()), |
| 73 | + 'deleted' => $this->prepareChanges($deletedRecords) |
| 74 | + ]; |
| 75 | + } |
| 76 | + |
| 77 | + protected function prepareChanges(array $changes): array |
| 78 | + { |
| 79 | + $jsonFields = Arr::wrap($this->jsonFields); |
| 80 | + |
| 81 | + if (empty($jsonFields)) { |
| 82 | + return $changes; |
| 83 | + } |
| 84 | + |
| 85 | + return array_map(function ($item) use ($jsonFields) { |
| 86 | + foreach ($jsonFields as $jsonField) { |
| 87 | + if (Arr::has($item, $jsonField)) { |
| 88 | + $item[$jsonField] = json_decode($item[$jsonField], true); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return $item; |
| 93 | + }, $changes); |
| 94 | + } |
| 95 | + |
| 96 | + protected function getModelJSONFields(): array |
| 97 | + { |
| 98 | + $casts = $this->model->getCasts(); |
| 99 | + |
| 100 | + $jsonCasts = array_filter($casts, fn ($cast) => $this->isJsonCast($cast)); |
| 101 | + |
| 102 | + return array_keys($jsonCasts); |
| 103 | + } |
| 104 | + |
| 105 | + protected function isJsonCast(string $cast): bool |
| 106 | + { |
| 107 | + return ($cast === 'array') || (class_exists($cast) && is_subclass_of($cast, CastsAttributes::class)); |
| 108 | + } |
| 109 | + |
| 110 | + protected function getFixturePath(string $fixtureName): string |
| 111 | + { |
| 112 | + $class = get_class($this); |
| 113 | + $explodedClass = explode('\\', $class); |
| 114 | + $className = Arr::last($explodedClass); |
| 115 | + $table = $this->model->getTable(); |
| 116 | + |
| 117 | + return base_path("tests/fixtures/{$className}/changes/{$table}/{$fixtureName}"); |
| 118 | + } |
| 119 | + |
| 120 | + protected function getDataSet(string $table, string $orderField = 'id'): Collection |
| 121 | + { |
| 122 | + return DB::table($table) |
| 123 | + ->orderBy($orderField) |
| 124 | + ->get() |
| 125 | + ->map(fn ($record) => (array) $record); |
| 126 | + } |
| 127 | +} |
0 commit comments