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

#166: Automatically add .json extension #172

Closed
wants to merge 1 commit into from
Closed
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/Traits/FixturesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ trait FixturesTrait
protected array $prepareSequencesExceptTables = ['migrations', 'password_resets'];

protected string $dumpFileName = 'dump.sql';
protected string $extension = '.json';
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 make const instead


protected function loadTestDump(): void
{
Expand Down Expand Up @@ -96,7 +97,7 @@ public function getFixture(string $fixtureName, $failIfNotExists = true): string

public function getJsonFixture(string $fixtureName, $assoc = true)
{
return json_decode($this->getFixture($fixtureName), $assoc);
return json_decode($this->getFixture($fixtureName . $this->extension), $assoc);
Copy link
Collaborator

Choose a reason for hiding this comment

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

need to add extension ONLY if it not set

}

public function assertEqualsFixture(string $fixture, $data, bool $exportMode = false): void
Expand All @@ -116,7 +117,7 @@ public function exportJson($fixture, $data): void
$data = $data->json();
}

$this->exportContent(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), $fixture);
$this->exportContent(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), $fixture . $this->extension);
}

public function clearDatabase(array $tables, array $except): void
Expand Down
18 changes: 9 additions & 9 deletions tests/CsvIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testOpenNotExistsFile()

public function testParseColumns()
{
$header = $this->getJsonFixture('header.json');
Copy link
Collaborator

Choose a reason for hiding this comment

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

all presented test changes will become useless after correct implementation of task

$header = $this->getJsonFixture('header');

$this->csvIteratorClass->parseColumns($header);

Expand All @@ -47,7 +47,7 @@ public function testParseColumns()

public function testSetColumns()
{
$header = $this->getJsonFixture('header.json');
$header = $this->getJsonFixture('header');

$this->csvIteratorClass->setColumns($header);

Expand All @@ -60,7 +60,7 @@ public function testCurrent()
{
$currentLine = $this->csvIteratorClass->current();

$this->assertEqualsFixture('current_line.json', $currentLine);
$this->assertEqualsFixture('current_line', $currentLine);
}

public function testKey()
Expand All @@ -78,7 +78,7 @@ public function testNext()
$currentLine = $this->csvIteratorClass->current();

$this->assertEquals(1, $rowKey);
$this->assertEqualsFixture('current_after_next_line.json', $currentLine);
$this->assertEqualsFixture('current_after_next_line', $currentLine);
}

public function testRewind()
Expand All @@ -90,7 +90,7 @@ public function testRewind()
$currentLine = $this->csvIteratorClass->current();

$this->assertEquals(1, $rowKey);
$this->assertEqualsFixture('current_after_next_line.json', $currentLine);
$this->assertEqualsFixture('current_after_next_line', $currentLine);
}

public function testGeneratorWithoutSettingColumnHeaders()
Expand All @@ -105,13 +105,13 @@ public function testGeneratorWithoutSettingColumnHeaders()
$rowKey = $this->csvIteratorClass->key();

$this->assertEquals(7, $rowKey);
$this->assertEqualsFixture('all_data.json', $result);
$this->assertEqualsFixture('all_data', $result);
}

public function testGeneratorWithSettingColumnHeaders()
{
$result = [];
$header = $this->getJsonFixture('header.json');
$header = $this->getJsonFixture('header');

$this->filePathProperty->setValue($this->csvIteratorClass, $this->getFixturePath('addresses_without_header.csv'));
$this->csvIteratorClass->setColumns($header);
Expand All @@ -125,15 +125,15 @@ public function testGeneratorWithSettingColumnHeaders()
$rowKey = $this->csvIteratorClass->key();

$this->assertEquals(6, $rowKey);
$this->assertEqualsFixture('all_data.json', $result);
$this->assertEqualsFixture('all_data', $result);
}

public function testGeneratorWithHeadersInvalidCount()
{
$this->expectException(IncorrectCSVFileException::class);
$this->expectExceptionMessage('Incorrect CSV file');

$header = $this->getJsonFixture('header_invalid_count.json');
$header = $this->getJsonFixture('header_invalid_count');

$this->csvIteratorClass->setColumns($header);

Expand Down
2 changes: 1 addition & 1 deletion tests/EntityControlTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function setUp(): void

$this->attachedRelationsCountProperty = new ReflectionProperty(TestRepository::class, 'attachedRelationsCount');

self::$selectResult ??= $this->getJsonFixture('select_query_result.json');
self::$selectResult ??= $this->getJsonFixture('select_query_result');

Carbon::setTestNow('2020-01-01 00:00:00');
}
Expand Down
38 changes: 19 additions & 19 deletions tests/FixturesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function getFixtureData(): array
{
return [
[
'input' => 'get_fixture/exists_fixture.json',
'input' => 'get_fixture/exists_fixture',
],
];
}
Expand All @@ -49,11 +49,11 @@ public function testGetFixture(string $input)

public function testGetFixtureWithSave()
{
$response = $this->getJsonFixture('get_fixture/exists_fixture.json');
$response = $this->getJsonFixture('get_fixture/exists_fixture');

$this->expectException(ForbiddenExportModeException::class);

$this->assertEqualsFixture('get_fixture/exists_fixture.json', $response, true);
$this->assertEqualsFixture('get_fixture/exists_fixture', $response, true);
}

public function testExportJson()
Expand All @@ -64,13 +64,13 @@ public function testExportJson()
'value' => 1234567890,
];

$this->exportJson('export_json/response.json', new TestResponse(
$this->exportJson('export_json/response', new TestResponse(
new Response(json_encode($result))
));

$this->assertEquals($this->getJsonFixture('export_json/response.json'), $result);
$this->assertEquals($this->getJsonFixture('export_json/response'), $result);

$this->assertFileExists($this->getFixturePath('export_json/response.json'));
$this->assertFileExists($this->getFixturePath('export_json/response.json'));
}

public function testExportJsonDirNotExists()
Expand All @@ -85,7 +85,7 @@ public function testExportJsonDirNotExists()

$this->exportContent(json_encode($result), $fixtureName);

$this->assertEquals($this->getJsonFixture($fixtureName), $result);
$this->assertEquals($this->getJsonFixture(Str::beforeLast($fixtureName, '.')), $result);

$fixturePath = $this->getFixturePath($fixtureName);

Expand All @@ -112,24 +112,24 @@ public function testExportFile()
$this->exportFile($response, 'export_file/content_result.txt');

$this->assertEquals(
expected: $this->getJsonFixture('export_file/result.txt'),
actual: $this->getJsonFixture('export_file/content_result.txt'),
expected: $this->getFixture('export_file/result.txt'),
actual: $this->getFixture('export_file/content_result.txt'),
);
}

public function testGetFixtureNotExistsWithoutException()
{
$response = $this->getFixture('get_fixture/not_exists_fixture.json', false);
$response = $this->getFixture('get_fixture/not_exists_fixture', false);

$this->assertEquals('', $response);
}

public function testGetFixtureNotExistsWithException()
{
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('not_exists_fixture.json fixture does not exist');
$this->expectExceptionMessage('not_exists_fixture fixture does not exist');

$this->getFixture('get_fixture/not_exists_fixture.json');
$this->getFixture('get_fixture/not_exists_fixture');
}

public function testLoadEmptyTestDump()
Expand Down Expand Up @@ -159,7 +159,7 @@ public function testLoadTestDumpForMysql()

Config::set('database.default', 'mysql');

self::$tables = $this->getJsonFixture('clear_database/tables.json');
self::$tables = $this->getJsonFixture('clear_database/tables');

$this->loadTestDump();
}
Expand All @@ -181,15 +181,15 @@ public function testLoadTestDumpForPgsql()

Config::set('database.default', 'pgsql');

self::$tables = $this->getJsonFixture('clear_database/tables.json');
self::$tables = $this->getJsonFixture('clear_database/tables');

$this->loadTestDump();
}

public function testGetTables()
{
$mock = $this->mockClass(MySqlBuilder::class, [
$this->functionCall('getTables', [], $this->getJsonFixture('get_tables/tables.json')),
$this->functionCall('getTables', [], $this->getJsonFixture('get_tables/tables')),
], true);

$connection = $this->mockClass(MySqlConnection::class, [
Expand All @@ -206,12 +206,12 @@ public function testGetTables()

$this->getTables();

$this->assertEqualsFixture('get_tables/tables.json', self::$tables);
$this->assertEqualsFixture('get_tables/tables', self::$tables);
}

public function testPrepareSequences()
{
$sequences = collect($this->getJsonFixture('prepare_sequences/information_schema.json'))
$sequences = collect($this->getJsonFixture('prepare_sequences/information_schema'))
->map(fn ($item) => (object) $item);

$connection = $this->mockClass(PostgresConnection::class, [
Expand Down Expand Up @@ -239,10 +239,10 @@ public function testPrepareSequences()

public function testGetFixtureWithoutGlobalExportMode()
{
$content = $this->getJsonFixture('get_fixture/export_fixture.json');
$content = $this->getJsonFixture('get_fixture/export_fixture');

unset($this->globalExportMode);

$this->assertEqualsFixture('get_fixture/export_fixture.json', $content);
$this->assertEqualsFixture('get_fixture/export_fixture', $content);
}
}
Loading
Loading