-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathModelTestStateTest.php
More file actions
144 lines (121 loc) · 5.6 KB
/
Copy pathModelTestStateTest.php
File metadata and controls
144 lines (121 loc) · 5.6 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace RonasIT\Support\Tests;
use PHPUnit\Framework\Attributes\DataProvider;
use ReflectionClass;
use RonasIT\Support\Testing\ModelTestState;
use RonasIT\Support\Tests\Support\Mock\Models\TestModel;
use RonasIT\Support\Tests\Support\Mock\Models\TestModelNonIdPrimaryKey;
use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithCastable;
use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithCrossAttributeCast;
use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithCustomCast;
use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithNativeJsonCasts;
use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithParameterizedCast;
use RonasIT\Support\Tests\Support\Mock\Models\TestModelWithPrimitiveCasts;
use RonasIT\Support\Tests\Support\Traits\TableTestStateMockTrait;
class ModelTestStateTest extends TestCase
{
use TableTestStateMockTrait;
public function setUp(): void
{
parent::setUp();
self::$tables = null;
putenv('FAIL_EXPORT_JSON=false');
}
public function testInitialization(): void
{
$datasetMock = collect($this->getJsonFixture('initialization/dataset'));
$originRecords = collect($this->getJsonFixture('initialization/origin_records'));
$this->mockGettingDataset($datasetMock);
$modelTestState = new ModelTestState(TestModel::class);
$reflectionClass = new ReflectionClass($modelTestState);
$customCastFields = $this->getProtectedProperty($reflectionClass, 'castFields', $modelTestState);
$state = $this->getProtectedProperty($reflectionClass, 'state', $modelTestState);
$this->assertEquals(['id', 'settings', 'deleted_at'], $customCastFields);
$this->assertEquals($originRecords, $state);
}
public static function getInitializationViaPrepareModelTestStateFilters(): array
{
return [
[
'testCaseGlobalExportMode' => true,
],
[
'testCaseGlobalExportMode' => false,
],
];
}
#[DataProvider('getInitializationViaPrepareModelTestStateFilters')]
public function testInitializationViaPrepareTableTestState(bool $testCaseGlobalExportMode): void
{
$datasetMock = collect($this->getJsonFixture('initialization/dataset'));
$this->mockGettingDataset($datasetMock);
$actualGlobalExportModeValue = $this->mockTestStateCreationSetGlobalExportMode('prepareModelTestState', TestModel::class, $testCaseGlobalExportMode);
$this->assertEquals($actualGlobalExportModeValue, $testCaseGlobalExportMode);
}
public static function getChangeScenarios(): array
{
return [
'base' => [
'fixtureDir' => 'changes_equals_fixture',
'table' => 'test_models',
'modelClass' => TestModel::class,
],
'primitive casts' => [
'fixtureDir' => 'changes_equals_fixture_with_primitive_casts',
'table' => 'test_model_with_primitive_casts',
'modelClass' => TestModelWithPrimitiveCasts::class,
],
'native json casts' => [
'fixtureDir' => 'changes_equals_fixture_with_native_json_casts',
'table' => 'test_model_with_native_json_casts',
'modelClass' => TestModelWithNativeJsonCasts::class,
],
'custom cast' => [
'fixtureDir' => 'changes_equals_fixture_with_custom_cast',
'table' => 'test_model_with_custom_casts',
'modelClass' => TestModelWithCustomCast::class,
],
'parameterized cast' => [
'fixtureDir' => 'changes_equals_fixture_with_parameterized_cast',
'table' => 'test_model_with_parameterized_casts',
'modelClass' => TestModelWithParameterizedCast::class,
],
'castable' => [
'fixtureDir' => 'changes_equals_fixture_with_castable',
'table' => 'test_model_with_castables',
'modelClass' => TestModelWithCastable::class,
],
'cross attribute cast' => [
'fixtureDir' => 'changes_equals_fixture_with_cross_attribute_cast',
'table' => 'test_model_with_cross_attribute_casts',
'modelClass' => TestModelWithCrossAttributeCast::class,
],
'custom primary key' => [
'fixtureDir' => 'changes_equals_fixture_primary_key',
'table' => 'test_model_non_id_primary_keys',
'modelClass' => TestModelNonIdPrimaryKey::class,
'uniqueKey' => 'name',
],
];
}
#[DataProvider('getChangeScenarios')]
public function testAssertChanges(
string $fixtureDir,
string $table,
string $modelClass,
string $uniqueKey = 'id',
): void {
$initialDatasetMock = collect($this->getJsonFixture("{$fixtureDir}/initial_dataset"));
$changedDatasetMock = collect($this->getJsonFixture("{$fixtureDir}/changed_dataset"));
$this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock, $table, $uniqueKey);
$modelTestState = new ModelTestState($modelClass);
$modelTestState->assertChangesEqualsFixture('assertion_fixture');
}
public function testAssertNoChanges(): void
{
$datasetMock = collect($this->getJsonFixture('get_without_changes/dataset'));
$this->mockGettingDatasetForChanges($datasetMock, $datasetMock, 'test_models');
$modelTestState = new ModelTestState(TestModel::class);
$modelTestState->assertNotChanged();
}
}