Skip to content

Commit

Permalink
#176: Move global export mode to FixturesTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
yogyrton committed Jan 9, 2025
1 parent ffb76d3 commit 9125e72
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/Tests/ModelTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@

class ModelTestState extends TableTestState
{
public function __construct(string $modelClassName, bool $globalExportMode = false)
public function __construct(string $modelClassName)
{
$model = new $modelClassName();

parent::__construct(
tableName: $model->getTable(),
jsonFields: $this->getModelJSONFields($model),
globalExportMode: $globalExportMode,
connectionName: $model->getConnectionName($model),
);
}
Expand Down
4 changes: 0 additions & 4 deletions src/Tests/TableTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class TableTestState extends Assert
{
use FixturesTrait;

public bool $globalExportMode;

protected string $tableName;
protected array $jsonFields;
protected ?string $connectionName;
Expand All @@ -23,13 +21,11 @@ class TableTestState extends Assert
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
13 changes: 10 additions & 3 deletions src/Traits/FixturesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ trait FixturesTrait

protected string $dumpFileName = 'dump.sql';

protected bool $globalExportMode = false;

protected function setGlobalExportMode(bool $value = true): self
{
$this->globalExportMode = $value;

return $this;
}

protected function loadTestDump(): void
{
$dump = $this->getFixture($this->dumpFileName, false);
Expand Down Expand Up @@ -103,9 +112,7 @@ public function getJsonFixture(string $fixtureName, $assoc = true)

public function assertEqualsFixture(string $fixture, $data, bool $exportMode = false): void
{
$globalExportMode = $this->globalExportMode ?? false;

if ($globalExportMode || $exportMode) {
if ($this->globalExportMode || $exportMode) {
$this->exportJson($fixture, $data);
}

Expand Down
11 changes: 2 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ abstract class TestCase extends BaseTest
protected static string $startedTestSuite = '';
protected static bool $isWrappedIntoTransaction = true;

protected bool $globalExportMode = false;

protected function setGlobalExportMode(): void
{
$this->globalExportMode = true;
}

public function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -115,11 +108,11 @@ protected function rollbackTransaction(): void

protected function prepareModelTestState(string $modelClassName): ModelTestState
{
return new ModelTestState($modelClassName, $this->globalExportMode);
return (new ModelTestState($modelClassName))->setGlobalExportMode($this->globalExportMode);
}

protected function prepareTableTestState(string $tableName, array $jsonFields = [], ?string $connectionName = null): TableTestState
{
return new TableTestState($tableName, $jsonFields, $this->globalExportMode, $connectionName);
return (new TableTestState($tableName, $jsonFields, $connectionName))->setGlobalExportMode($this->globalExportMode);
}
}

0 comments on commit 9125e72

Please sign in to comment.