diff --git a/src/Tests/ModelTestState.php b/src/Tests/ModelTestState.php index 7bb5015..93c551f 100644 --- a/src/Tests/ModelTestState.php +++ b/src/Tests/ModelTestState.php @@ -7,14 +7,15 @@ class ModelTestState extends TableTestState { - public function __construct(string $modelClassName) + public function __construct(string $modelClassName, bool $globalExportMode = false) { $model = new $modelClassName(); parent::__construct( tableName: $model->getTable(), jsonFields: $this->getModelJSONFields($model), - connectionName: $model->getConnectionName(), + globalExportMode: $globalExportMode, + connectionName: $model->getConnectionName($model), ); } diff --git a/src/Tests/TableTestState.php b/src/Tests/TableTestState.php index c70f7a4..207eea2 100644 --- a/src/Tests/TableTestState.php +++ b/src/Tests/TableTestState.php @@ -13,16 +13,23 @@ class TableTestState extends Assert { use FixturesTrait; + public bool $globalExportMode; + protected string $tableName; - protected Collection $state; - protected ?string $connectionName; protected array $jsonFields; + protected ?string $connectionName; + protected Collection $state; - public function __construct(string $tableName, array $jsonFields = [], ?string $connectionName = null) - { + public function __construct( + string $tableName, + array $jsonFields = [], + bool $globalExportMode = false, + ?string $connectionName = null, + ) { $this->tableName = $tableName; $this->jsonFields = $jsonFields; $this->connectionName = $connectionName ?? DB::getDefaultConnection(); + $this->globalExportMode = $globalExportMode; $this->state = $this->getDataSet($tableName); } diff --git a/tests/ModelTestStateTest.php b/tests/ModelTestStateTest.php index 3fc9b81..c7857af 100644 --- a/tests/ModelTestStateTest.php +++ b/tests/ModelTestStateTest.php @@ -37,6 +37,30 @@ public function testInitialization() $this->assertEquals($originRecords, $state); } + public function testInitializationViaPrepareModelTestStateWithGlobalExportMode() + { + $datasetMock = collect($this->getJsonFixture('initialization/dataset.json')); + $this->mockGettingDataset($datasetMock); + + $testCaseGlobalExportMode = true; + + $prepareModelTestState = $this->getTestState('prepareModelTestState', TestModel::class, $testCaseGlobalExportMode); + + $this->assertEquals($prepareModelTestState->globalExportMode, $testCaseGlobalExportMode); + } + + public function testInitializationViaPrepareModelTestStateWithoutGlobalExportMode() + { + $datasetMock = collect($this->getJsonFixture('initialization/dataset.json')); + $this->mockGettingDataset($datasetMock); + + $testCaseGlobalExportMode = false; + + $prepareModelTestState = $this->getTestState('prepareModelTestState', TestModel::class, $testCaseGlobalExportMode); + + $this->assertEquals($prepareModelTestState->globalExportMode, $testCaseGlobalExportMode); + } + public function testAssertChangesEqualsFixture() { $initialDatasetMock = collect($this->getJsonFixture('changes_equals_fixture/initial_dataset.json')); diff --git a/tests/TableTestStateTest.php b/tests/TableTestStateTest.php index 1b1680a..bd68e8e 100644 --- a/tests/TableTestStateTest.php +++ b/tests/TableTestStateTest.php @@ -26,6 +26,30 @@ public function testInitialization() $this->assertEquals($originRecords, $state); } + public function testInitializationViaPrepareTableTestStateWithGlobalExportMode() + { + $datasetMock = collect($this->getJsonFixture('initialization/dataset.json')); + $this->mockGettingDataset($datasetMock); + + $testCaseGlobalExportMode = true; + + $prepareTableTestState = $this->getTestState('prepareTableTestState', 'test_models', $testCaseGlobalExportMode); + + $this->assertEquals($prepareTableTestState->globalExportMode, $testCaseGlobalExportMode); + } + + public function testInitializationViaPrepareTableTestStateWithoutGlobalExportMode() + { + $datasetMock = collect($this->getJsonFixture('initialization/dataset.json')); + $this->mockGettingDataset($datasetMock); + + $testCaseGlobalExportMode = false; + + $prepareTableTestState = $this->getTestState('prepareTableTestState', 'test_models', $testCaseGlobalExportMode); + + $this->assertEquals($prepareTableTestState->globalExportMode, $testCaseGlobalExportMode); + } + public function testAssertChangesEqualsFixture() { $initialDatasetMock = collect($this->getJsonFixture('changes_equals_fixture/initial_dataset.json')); diff --git a/tests/TestCase.php b/tests/TestCase.php index 35cf919..1b07ee5 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -112,4 +112,14 @@ protected function rollbackTransaction(): void $connection->disconnect(); } } + + protected function prepareModelTestState(string $modelClassName): ModelTestState + { + return new ModelTestState($modelClassName, $this->globalExportMode); + } + + protected function prepareTableTestState(string $tableName, array $jsonFields = [], ?string $connectionName = null): TableTestState + { + return new TableTestState($tableName, $jsonFields, $this->globalExportMode, $connectionName); + } } diff --git a/tests/support/Traits/TableTestStateMockTrait.php b/tests/support/Traits/TableTestStateMockTrait.php index 5500e99..239c06d 100644 --- a/tests/support/Traits/TableTestStateMockTrait.php +++ b/tests/support/Traits/TableTestStateMockTrait.php @@ -5,6 +5,9 @@ use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; +use ReflectionClass; +use RonasIT\Support\Tests\TableTestState; +use RonasIT\Support\Tests\TestCase; trait TableTestStateMockTrait { @@ -46,4 +49,20 @@ protected function mockGettingDatasetForChanges(Collection $responseMock, Collec ->method('get') ->willReturnOnConsecutiveCalls($initialState, $responseMock); } + + protected function getTestState(string $methodName, string $entity, bool $testCaseGlobalExportMode): TableTestState + { + $testCaseMock = $this + ->getMockBuilder(TestCase::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $testCase = new ReflectionClass($testCaseMock); + + if ($testCaseGlobalExportMode) { + $testCase->getMethod('setGlobalExportMode')->invoke($testCaseMock); + } + + return $testCase->getMethod($methodName)->invoke($testCaseMock, $entity); + } }