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 5 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
14 changes: 10 additions & 4 deletions src/Tests/TableTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ class TableTestState extends Assert
use FixturesTrait;

protected string $tableName;
protected Collection $state;
protected ?string $connectionName;
protected array $jsonFields;
protected ?string $connectionName;
protected bool $globalExportMode;
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->getMockBuilder(TestCase::class)
->disableOriginalConstructor()
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->getMockBuilder(TestCase::class)
->disableOriginalConstructor()
$mock = $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);


$this->assertFalse($reflection->getProperty('globalExportMode')->getValue($mock));
Copy link
Collaborator

Choose a reason for hiding this comment

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

no reasons to check the initial value of the test case class


$setGlobalExportMode = $reflection->getMethod('setGlobalExportMode');
$setGlobalExportMode->invoke($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
$setGlobalExportMode = $reflection->getMethod('setGlobalExportMode');
$setGlobalExportMode->invoke($mock);
$reflection->getMethod('setGlobalExportMode')->invoke($mock);


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

$this->assertTrue($reflection->getProperty('globalExportMode')->getValue($mock));
Copy link
Collaborator

Choose a reason for hiding this comment

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

what this test case testing? you need to test that the global export mode property of created test state has the same value as in the TestCase class

}

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);

$this->assertFalse($reflection->getProperty('globalExportMode')->getValue($mock));

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

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

$this->assertTrue($reflection->getProperty('globalExportMode')->getValue($mock));
}

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