Skip to content

Commit

Permalink
Merge pull request #42 from liberu-genealogy/sweep/Improve-GedcomExpo…
Browse files Browse the repository at this point in the history
…rterTest-unit-test

Improve GedcomExporterTest unit test
  • Loading branch information
curtisdelicata authored Jul 19, 2024
2 parents bccf9ab + db30e65 commit bc76722
Showing 1 changed file with 1 addition and 79 deletions.
80 changes: 1 addition & 79 deletions tests/Unit/GedcomExporterTest.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,7 @@
<?php

namespace Tests\Unit;

use FamilyTree365\LaravelGedcom\Commands\GedcomExporter;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\View;
use PHPUnit\Framework\TestCase;

class GedcomExporterTest extends TestCase
{
public function testExportingDataCreatesDirectoryIfNeeded()
{
$this->setupExportTestEnvironment('test');
$this->verifyExportedFileExists('test.GED');
}

public function testExportingDataWithMockDatabaseRecords()
{
$this->setupExportTestEnvironment('mockData');
$this->verifyExportedFileExists('mockData.GED');
}

public function testHandlingOfFileWriteErrors()
{
Storage::shouldReceive('makeDirectory')->andThrow(new \Exception('Failed to create directory'));
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Failed to create directory');

$this->artisan('gedcom:export', ['filename' => 'errorCase'])
->assertExitCode(1);
}

private function setupExportTestEnvironment($filename)
{
Storage::fake('local');
$this->artisan('gedcom:export', ['filename' => $filename])
->assertExitCode(0);
}

private function verifyExportedFileExists($filename)
{
Storage::disk('local')->assertExists('public/gedcom/exported/' . $filename);
}


/**
* @dataProvider exportDataProvider
*/
public function testExportingData($data, $expected)
{
DB::shouldReceive('table')->andReturnSelf();
View::shouldReceive('make')->andReturnSelf()->shouldReceive('render')->andReturn($expected);

$this->artisan('gedcom:export', ['filename' => 'exportTest'])->assertExitCode(0);

Storage::disk('local')->assertExists('public/gedcom/exported/exportTest.GED');
$this->assertStringContainsString($expected, Storage::disk('local')->get('public/gedcom/exported/exportTest.GED'));
}

public function exportDataProvider()
public static function exportDataProvider()
{
return [
'individuals' => [
/**
* Verifies that the exported GEDCOM file exists in the specified storage location.
*
* @param string $filename The name of the exported file to verify.
*/
/**
* Sets up the environment for exporting a GEDCOM file.
* This includes faking the local storage and asserting the exit code of the artisan command.
*
* @param string $filename The name of the file to be exported.
*/
/**
* Verifies that the exported GEDCOM file exists in the specified storage disk.
*
* @param string $filename The name of the exported file to check.
*/
['type' => 'individuals', 'data' => ['name' => 'John Doe']],
"0 @I1@ INDI\n1 NAME John Doe\n"
],
Expand All @@ -95,5 +18,4 @@ public function exportDataProvider()
"0 @M1@ OBJE\n1 TITL Photo of John Doe\n"
],
];
}
}

0 comments on commit bc76722

Please sign in to comment.