Skip to content

Commit

Permalink
chore: revert logic of data assertion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Lapkovsky committed Dec 4, 2023
1 parent f20a4e0 commit 5bcc289
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
50 changes: 27 additions & 23 deletions src/Tests/ModelTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Assert;
use RonasIT\Support\Traits\FixturesTrait;

class ModelTestState extends Assert
{
use FixturesTrait;

protected Collection $state;
protected Model $model;
protected array $jsonFields;
Expand All @@ -22,38 +25,33 @@ public function __construct(string $modelClassName)
$this->jsonFields = $this->getModelJSONFields();
}

public function getState(): Collection
public function assertNotChanged(): void
{
return $this->state;
}

public function getFixturePath(string $fixture): string
{
$table = $this->model->getTable();
$changes = $this->getChanges();

return "changes/{$table}/{$fixture}";
}

public function getExpectedEmptyState(): array
{
return [
$this->assertEquals([
'updated' => [],
'created' => [],
'deleted' => []
];
], $changes);
}

public function getChanges(): array
public function assertChangesEqualsFixture(string $fixture, bool $exportMode = false): void
{
$changes = $this->getChanges();

$this->assertEqualsFixture($fixture, $changes, $exportMode);
}

protected function getChanges(): array
{
$updatedData = $this->getDataSet($this->model->getTable());

$updatedRecords = [];
$deletedRecords = [];

$this->getState()->each(function ($originItem) use (&$updatedData, &$updatedRecords, &$deletedRecords) {
$updatedItemIndex = $updatedData->search(function ($updatedItem) use ($originItem) {
return $updatedItem['id'] === $originItem['id'];
});
$this->state->each(function ($originItem) use (&$updatedData, &$updatedRecords, &$deletedRecords) {
$updatedItemIndex = $updatedData->search(fn ($updatedItem) => $updatedItem['id'] === $originItem['id']);

if ($updatedItemIndex === false) {
$deletedRecords[] = $originItem;
Expand Down Expand Up @@ -106,11 +104,17 @@ protected function getModelJSONFields(): array

protected function isJsonCast(string $cast): bool
{
if ($cast === 'array') {
return true;
}
return ($cast === 'array') || (class_exists($cast) && is_subclass_of($cast, CastsAttributes::class));
}

public function getFixturePath(string $fixtureName): string
{
$class = get_class($this);
$explodedClass = explode('\\', $class);
$className = Arr::last($explodedClass);
$table = $this->model->getTable();

return class_exists($cast) && is_subclass_of($cast, CastsAttributes::class);
return base_path("tests/fixtures/{$className}/changes/{$table}/{$fixtureName}");
}

protected function getDataSet(string $table, string $orderField = 'id'): Collection
Expand Down
9 changes: 0 additions & 9 deletions tests/AuthTestTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ class AuthTestTraitTest extends HelpersTestCase
{
use FixturesTrait, MockTrait, AuthTestTrait;

public function setUp(): void
{
parent::setUp();

self::$tables = [];

putenv('FAIL_EXPORT_JSON=false');
}

public function testActingViaSession()
{
$userId = 1;
Expand Down
24 changes: 13 additions & 11 deletions tests/ModelTestStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ public function testInitialization()
$this->mockGettingDataset($datasetMock);

$modelTestState = new ModelTestState(TestModel::class);

$reflectionClass = new ReflectionClass($modelTestState);
$jsonFieldsProperty = $reflectionClass->getProperty('jsonFields');
$jsonFieldsProperty->setAccessible(true);

$jsonFields = $jsonFieldsProperty->getValue($modelTestState);
$jsonFields = $this->getProtectedProperty($reflectionClass, 'jsonFields', $modelTestState);
$state = $this->getProtectedProperty($reflectionClass, 'state', $modelTestState);

$this->assertNotEmpty($jsonFields);
$this->assertEquals(['json_field', 'castable_field'], $jsonFields);
$this->assertEquals($originRecords, $modelTestState->getState());
$this->assertEquals($originRecords, $state);
}

public function testAssertChangesEqualsFixture()
Expand All @@ -48,11 +46,7 @@ public function testAssertChangesEqualsFixture()
$this->mockGettingDatasetForChanges($changedDatasetMock, $initialDatasetMock);

$modelTestState = new ModelTestState(TestModel::class);

$this->assertEqualsFixture(
$modelTestState->getFixturePath('changes_equals_fixture/assertion_fixture.json'),
$modelTestState->getChanges()
);
$modelTestState->assertChangesEqualsFixture('assertion_fixture.json');
}

public function testAssertNoChanges()
Expand All @@ -61,6 +55,14 @@ public function testAssertNoChanges()
$this->mockGettingDataset($datasetMock);

$modelTestState = new ModelTestState(TestModel::class);
$this->assertEquals($modelTestState->getExpectedEmptyState(), $modelTestState->getChanges());
$modelTestState->assertNotChanged();
}

protected function getProtectedProperty(ReflectionClass $reflectionClass, string $methodName, $objectInstance)
{
$property = $reflectionClass->getProperty($methodName);
$property->setAccessible(true);

return $property->getValue($objectInstance);
}
}

0 comments on commit 5bcc289

Please sign in to comment.