Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#124 Implemented helper to generate test state #162

Merged
merged 7 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading