Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/Tests/ModelTestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ protected function getFixturePath(string $fixtureName): string
$testClassTrace = Arr::first(debug_backtrace(), fn ($trace) => str_ends_with($trace['file'], 'Test.php'));
$testFileName = Arr::last(explode('/', $testClassTrace['file']));
$testClass = Str::remove('.php', $testFileName);
$tableName = $this->model->getTable();

return base_path("tests/fixtures/{$testClass}/{$fixtureName}");
return base_path("tests/fixtures/{$testClass}/db_changes/{$tableName}/{$fixtureName}");
}

protected function getDataSet(string $table, string $orderField = 'id'): Collection
Expand Down
24 changes: 19 additions & 5 deletions src/Traits/FixturesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use RonasIT\Support\Exceptions\ForbiddenExportModeException;

Expand Down Expand Up @@ -213,15 +214,28 @@ protected function getSequences()
return self::$sequences;
}

protected function exportContent($content, string $fixture): void
protected function exportContent(string $content, string $fixture): void
{
if (env('FAIL_EXPORT_JSON', true)) {
throw new ForbiddenExportModeException();
}

file_put_contents(
$this->getFixturePath($fixture),
$content
);
$path = $this->getFixturePath($fixture);

$this->makeFixtureDir($path);

file_put_contents($path, $content);
}

protected function makeFixtureDir(string $path): void
{
$dir = Str::beforeLast($path, '/');

if (!is_dir($dir)) {
mkdir(
directory: $dir,
recursive: true,
);
}
}
}
24 changes: 24 additions & 0 deletions tests/FixturesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Testing\TestResponse;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -72,6 +73,29 @@ public function testExportJson()
$this->assertFileExists($this->getFixturePath('export_json/response.json'));
}

public function testExportJsonDirNotExists()
{
putenv('FAIL_EXPORT_JSON=false');

$result = [
'value' => 1234567890,
];

$fixtureName = 'export_json/some_directory/response.json';

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

$this->assertEquals($this->getJsonFixture($fixtureName), $result);

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

$this->assertFileExists($fixturePath);

unlink($fixturePath);

rmdir(Str::beforeLast($fixturePath, '/'));
}

public function testExportFile()
{
putenv('FAIL_EXPORT_JSON=false');
Expand Down