Skip to content

Commit

Permalink
Merge pull request #155 from RonasIT/153-modelteststate-change-fixtur…
Browse files Browse the repository at this point in the history
…es-directory

#153: ModelTestState db_changes fixtures directory
  • Loading branch information
DenTray authored Dec 2, 2024
2 parents 934cb9d + acb4205 commit 098b4cc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
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

0 comments on commit 098b4cc

Please sign in to comment.