Skip to content

Commit

Permalink
Merge pull request #162 from RonasIT/124-implemented-helper-to-genera…
Browse files Browse the repository at this point in the history
…te-test-state

#124 Implemented helper to generate test state
  • Loading branch information
DenTray authored Dec 26, 2024
2 parents 22bd3bb + d1abf05 commit ffb76d3
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Tests/ModelTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand Down
15 changes: 11 additions & 4 deletions src/Tests/TableTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
24 changes: 24 additions & 0 deletions tests/ModelTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
24 changes: 24 additions & 0 deletions tests/TableTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
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 ffb76d3

Please sign in to comment.