Skip to content

Instantly share code, notes, and snippets.

@xiCO2k
Created October 3, 2022 23:11
Show Gist options
  • Save xiCO2k/531044992ed869056fdcacb85e84c60e to your computer and use it in GitHub Desktop.
Save xiCO2k/531044992ed869056fdcacb85e84c60e to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Foundation\Testing\RefreshDatabase;
use Symfony\Component\Console\Output\BufferedOutput;
use function Termwind\render;
use function Termwind\renderUsing;
uses(RefreshDatabase::class);
test('there are no down migrations', function () {
$migrator = app('migrator');
$resolvePath = tap((new ReflectionClass($migrator))->getMethod('resolvePath'))
->setAccessible(true);
$files = $migrator->getMigrationFiles([
$this->app->databasePath().DIRECTORY_SEPARATOR.'migrations',
]);
$migrator->requireFiles($files);
$filesWithDown = [];
foreach ($files as $file) {
$class = $resolvePath->invoke($migrator, $file);
if ((new ReflectionClass($class))->hasMethod('down')) {
$filesWithDown[] = basename($file);
}
}
$this->assertCount(0, $filesWithDown, errorMessage($filesWithDown));
});
function errorMessage($files)
{
//Tricks because of collision output adding two spaces before the content.
$columns = intval(getenv('COLUMNS'));
putenv('COLUMNS='.($columns - 4));
renderUsing($output = new BufferedOutput(decorated: true));
render(<<<HTML
<div>
<div class="text-gray">---</div>
<div class="mt-1 mb-1 text-yellow">
The following files still have a <i><u>down</u></i> method. Please remove it.
</div>
</div>
HTML);
collect($files)->each(fn ($name) => render(<<<HTML
<div class="space-x-1 flex w-full text-gray">
<span class="">⇂</span>
<span class="text-white font-bold">{$name}</span>
<span class="content-repeat-[.]"></span>
</div>
HTML));
putenv("COLUMNS={$columns}");
return $output->fetch();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment