Skip to content

Commit

Permalink
refactor: TestState
Browse files Browse the repository at this point in the history
  • Loading branch information
yogyrton committed Dec 26, 2024
1 parent f533533 commit d1abf05
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
22 changes: 12 additions & 10 deletions tests/ModelTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,27 @@ public function testInitialization()
$this->assertEquals($originRecords, $state);
}

public function testInitializationViaPrepareModelTestState()
public function testInitializationViaPrepareModelTestStateWithGlobalExportMode()
{
$datasetMock = collect($this->getJsonFixture('initialization/dataset.json'));
$this->mockGettingDataset($datasetMock);

$mock = $this
->getMockBuilder(TestCase::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$testCaseGlobalExportMode = true;

$reflection = new ReflectionClass($mock);
$prepareModelTestState = $this->getTestState('prepareModelTestState', TestModel::class, $testCaseGlobalExportMode);

$reflection->getMethod('setGlobalExportMode')->invoke($mock);
$this->assertEquals($prepareModelTestState->globalExportMode, $testCaseGlobalExportMode);
}

public function testInitializationViaPrepareModelTestStateWithoutGlobalExportMode()
{
$datasetMock = collect($this->getJsonFixture('initialization/dataset.json'));
$this->mockGettingDataset($datasetMock);

$prepareModelTestState = $reflection->getMethod('prepareModelTestState')->invoke($mock, TestModel::class);
$testCaseGlobalExportMode = false;

$testCaseGlobalExportMode = $reflection->getProperty('globalExportMode')->getValue($mock);
$prepareModelTestState = $this->getTestState('prepareModelTestState', TestModel::class, $testCaseGlobalExportMode);

$this->assertTrue($testCaseGlobalExportMode);
$this->assertEquals($prepareModelTestState->globalExportMode, $testCaseGlobalExportMode);
}

Expand Down
22 changes: 12 additions & 10 deletions tests/TableTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,27 @@ public function testInitialization()
$this->assertEquals($originRecords, $state);
}

public function testInitializationViaPrepareTableTestState()
public function testInitializationViaPrepareTableTestStateWithGlobalExportMode()
{
$datasetMock = collect($this->getJsonFixture('initialization/dataset.json'));
$this->mockGettingDataset($datasetMock);

$mock = $this
->getMockBuilder(TestCase::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$testCaseGlobalExportMode = true;

$reflection = new ReflectionClass($mock);
$prepareTableTestState = $this->getTestState('prepareTableTestState', 'test_models', $testCaseGlobalExportMode);

$reflection->getMethod('setGlobalExportMode')->invoke($mock);
$this->assertEquals($prepareTableTestState->globalExportMode, $testCaseGlobalExportMode);
}

public function testInitializationViaPrepareTableTestStateWithoutGlobalExportMode()
{
$datasetMock = collect($this->getJsonFixture('initialization/dataset.json'));
$this->mockGettingDataset($datasetMock);

$prepareTableTestState = $reflection->getMethod('prepareTableTestState')->invoke($mock, 'test_models');
$testCaseGlobalExportMode = false;

$testCaseGlobalExportMode = $reflection->getProperty('globalExportMode')->getValue($mock);
$prepareTableTestState = $this->getTestState('prepareTableTestState', 'test_models', $testCaseGlobalExportMode);

$this->assertTrue($testCaseGlobalExportMode);
$this->assertEquals($prepareTableTestState->globalExportMode, $testCaseGlobalExportMode);
}

Expand Down
19 changes: 19 additions & 0 deletions tests/support/Traits/TableTestStateMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit d1abf05

Please sign in to comment.