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 6 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
22 changes: 22 additions & 0 deletions tests/ModelTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ public function testInitialization()
$this->assertEquals($originRecords, $state);
}

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

$mock = $this
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$mock = $this
$testCaseMock = $this

->getMockBuilder(TestCase::class)
->disableOriginalConstructor()
->getMockForAbstractClass();

$reflection = new ReflectionClass($mock);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$reflection = new ReflectionClass($mock);
$testCase = new ReflectionClass($mock);


$reflection->getMethod('setGlobalExportMode')->invoke($mock);

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

$testCaseGlobalExportMode = $reflection->getProperty('globalExportMode')->getValue($mock);

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

public function testAssertChangesEqualsFixture()
{
$initialDatasetMock = collect($this->getJsonFixture('changes_equals_fixture/initial_dataset.json'));
Expand Down
22 changes: 22 additions & 0 deletions tests/TableTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ public function testInitialization()
$this->assertEquals($originRecords, $state);
}

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

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

$reflection = new ReflectionClass($mock);

$reflection->getMethod('setGlobalExportMode')->invoke($mock);

$prepareTableTestState = $reflection->getMethod('prepareTableTestState')->invoke($mock, 'test_models');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move it to the separate method which will return reflection of the TestCase class with enabled global export mode


$testCaseGlobalExportMode = $reflection->getProperty('globalExportMode')->getValue($mock);

$this->assertTrue($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);
}
}
Loading