Skip to content

Commit

Permalink
Finalize the changes to the new testcase for verifying snippets are p…
Browse files Browse the repository at this point in the history
…roperly added to a specific template thanks to some package detected. i.e, filament cache commands should be available in the Dockerfiles generated when v3 is detected
  • Loading branch information
Kathryn Anne S Tan committed Apr 7, 2024
1 parent f016c60 commit 3d6b0ee
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 38 deletions.
14 changes: 14 additions & 0 deletions app/Services/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,18 @@ public function composerJsonContent( $directory )

return json_decode( file_get_contents( $path ), 1 );
}

public function deleteDir( $dir )
{
// First level delete for now
if( is_dir($dir) ){
$fileNames = scandir( $dir );
foreach( $fileNames as $fileName ){
$filePath = $dir.'/'.$fileName;
if( is_file($filePath) )
unlink( $filePath);
}
}
rmdir( $dir );
}
}
75 changes: 37 additions & 38 deletions tests/Feature/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ function ignoreFiles( )
}

/**
* Sets up a test directory containing:
* new composer json
* Sets up a test directory containing files combining base and snippet configuration:
* new composer json with merged details from base and snippet directories
* supporting files from base directory being tested
*/
function setupTestDirectoryForExtensionBaseCombo( $baseDirectory, $newComposerArr, $testDirNameToCreate )
* */
function setUpDirectoryForBaseSnippetCombo( $baseDirectory, $newComposerArr, $testDir )
{
// Files to delete later
$filesCreatedInTestDir = [ 'composer.json'];
// Delete any previous combination folder
if( is_dir($testDir) ){
$fh = new \App\Services\File();
$fh->deleteDir($testDir);
}

// Create combination directory
mkdir($testDir);

// Create merged composer.json in test directory
$testDir = $testDirNameToCreate;
if( !is_dir($testDir) )
mkdir($testDir);
// Create new composer file using merged content
file_put_contents( $testDir.'/composer.json', json_encode($newComposerArr, JSON_UNESCAPED_SLASHES));

// Copy over supporting files from base directory to test directory
$supportingFileNames = ignoreFiles();

foreach( $supportingFileNames as $name ){
$pathToBaseFile = $baseDirectory.'/'.$name;
if( file_exists($pathToBaseFile) ){
Expand All @@ -34,15 +35,15 @@ function setupTestDirectoryForExtensionBaseCombo( $baseDirectory, $newComposerAr
$pathToTestFile,
file_get_contents($pathToBaseFile)
);
$filesCreatedInTestDir[] = $name;
}
}
}
return $filesCreatedInTestDir;
}

// Test that supported combinations' templates are successfully generated
it('generates proper templates for each supported combination', function ( )
// Test that Dockerfiles are generated properly for specific "base specifications"
// Base specifications can include "Laravel version", "Octane flavor"--specifications that would require different config, that will not work in harmony with all other bases.
// For example.
it('generates proper templates for each supported base', function ( )
{
$directories = \File::directories( 'tests/Feature/Supported' );
foreach($directories as $dir) {
Expand Down Expand Up @@ -85,44 +86,48 @@ function setupTestDirectoryForExtensionBaseCombo( $baseDirectory, $newComposerAr
});

// Tests whether snippets are added into generated files for special occasions/configurations
it('generates templates with proper snippets', function ( )
it('generates templates with proper snippets', function ()
{
$baseDirectories = \File::directories( 'tests/Feature/Supported' );
// Folders providing details that can combined with Base test directories
$extDirectories = \File::directories( 'tests/Feature/Snippets' );
// Base test directories
$baseDirectories = \File::directories( 'tests/Feature/Supported' );

foreach($baseDirectories as $base) {
// composer.json content of base directory reference
// composer.json of base directory
$baseComposer = (new \App\Services\File())->composerJsonContent( $base );

foreach( $extDirectories as $ext ){

// composer.json content of extension directory reference
// composer.json of snippet directory
$extComposer = (new \App\Services\File())->composerJsonContent( $ext );

// Merge content together
// Merge composer together
$newComposer = [
'require' => array_merge( $extComposer['require'],$baseComposer['require'])
'require' => array_merge( $extComposer['require'], $baseComposer['require'] )
];

// Create new test directory for the base+snippet combination
$testDir = 'tests/Feature/Combination';
$filesCreatedInTestDir = setupTestDirectoryForExtensionBaseCombo( $base, $newComposer, $testDir );
setUpDirectoryForBaseSnippetCombo( $base, $newComposer, $testDir );

// Generate Dockerfile, by scanning contents of files in the current directory, set through --path
// Generate templates, by scanning contents of files in the combination folder
// FIRST assert: command successfully runs and exits
$this->artisan('generate --path="'.$testDir.'"')->assertExitCode(0);

// Get templates to check for the extension
// Verify that specific templates still matches the expected content from each base reference
// BUT! contain the snippet expected from the snippet/extension folder
foreach($extComposer['extra']['templates'] as $templateName){
$failedFor = "Failed for:\n Base Dir: ".$base."\n Template Name: ".$templateName;

// Check Generated file
// Get generated file content
$generatedFileName = './'.$templateName;
$generatedContent = explode("\n", file_get_contents( $generatedFileName ) );

// Compare with Expected file for that base
// Get base reference file content
$referenceFileName = $base.'/'.$templateName;
$referenceContent = explode("\n", file_get_contents($referenceFileName) );

// Get Difference
// Get Difference between generated and base reference
$diff = new \Diff($referenceContent, $generatedContent);
$renderer = new \Diff_Renderer_Text_Unified();
$differenceFound = '';
Expand All @@ -135,23 +140,17 @@ function setupTestDirectoryForExtensionBaseCombo( $baseDirectory, $newComposerAr
}
}

// Compare differenceFound with expected difference
// There difference between the two should be the snippet added thanks to combining the base composer with snippet composer
$differenceFound = trim( $differenceFound, "\n");
$referenceContent = trim( file_get_contents( $ext.'/'.$templateName ),"\n");
$this->assertEquals( $referenceContent, $differenceFound, $failedFor );


// Delete unnecessary files
unlink( $generatedFileName );

// Delete Combination Folder Files
foreach( $filesCreatedInTestDir as $fileName ){
unlink( $testDir.'/'.$fileName );
}
rmdir($testDir );

// Delete combination folder and files
$fh = new \App\Services\File();
$fh->deleteDir('tests/Feature/Combination');
}

}
}
});
Expand Down

0 comments on commit 3d6b0ee

Please sign in to comment.