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
18 changes: 12 additions & 6 deletions src/Console/Commands/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Filesystem\Filesystem;
use Laragear\Preload\Preloader;
use Symfony\Component\Console\Attribute\AsCommand;

/**
Expand Down Expand Up @@ -39,25 +40,30 @@ class Stub extends Command
*/
public function handle(Filesystem $file, ConfigContract $config): void
{
$path = $config->get('preload.path');
$dir = $config->get('preload.path');
$filePath = $dir.'/'.Preloader::NAME_PRELOAD;

if ($file->exists($path)) {
$file->ensureDirectoryExists($dir);

if ($file->exists($filePath)) {
$this->info('A preload script file already exists.');

return;
}

$file->put($path, <<<'STUB'
$file->put($filePath, <<<'STUB'
<?php

\fwrite(\STDOUT, 'Info: This is a stub file to be replaced for the application at runtime.');
$date = (new DateTime())->format('d-m-Y H:i:s');

echo "[$date] Info: This is a preload stub file to be replaced for the application at runtime.";

STUB
);

$this->info("Stub copied at [$path].");
$this->info("Stub copied at [$filePath].");
$this->newLine();
$this->comment('Remember to edit your [php.ini] file:');
$this->comment("opcache.preload = $path");
$this->comment("opcache.preload = $filePath");
}
}
20 changes: 10 additions & 10 deletions src/Preloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@
class Preloader
{
/**
* The location of the statistic file.
* The filename for the statistics file.
*
* @const string
*/
public const STUB_STATISTICS = __DIR__.'/../stubs/statistics.md';
public const NAME_STATISTICS = 'statistics.md';

/**
* The location of the preload script stub.
* The filename for the preload script file.
*
* @const string
*/
public const STUB_PRELOAD = __DIR__.'/../stubs/preload.php.stub';
public const NAME_PRELOAD = 'preload.php';

/**
* The filename for the statistics file.
* The location of the statistic file.
*
* @const string
*/
public const NAME_STATISTICS = 'statistics.md';
public const STUB_STATISTICS = __DIR__.'/../stubs/'.self::NAME_STATISTICS;

/**
* The filename for the preload script file.
*
* @const string
* The location of the preload script stub.
*/
public const NAME_PRELOAD = 'preload.php';
public const STUB_PRELOAD = __DIR__.'/../stubs/'.self::NAME_PRELOAD.'.stub';

/**
* The filename of the list of preload files.
Expand Down
20 changes: 13 additions & 7 deletions tests/Console/Commands/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,44 @@
namespace Tests\Console\Commands;

use Illuminate\Filesystem\Filesystem;
use Laragear\Preload\Preloader;
use Mockery\MockInterface;
use Tests\TestCase;

class StubTest extends TestCase
{
public function test_stores_placeholder_in_output_path(): void
{
$path = $this->app->basePath();
$dir = $this->app->basePath();
$filePath = $dir.'/'.Preloader::NAME_PRELOAD;

$this->mock(Filesystem::class, function (MockInterface $mock) use ($path) {
$mock->expects('exists')->andReturnFalse();
$mock->expects('put')->with($path, <<<'PHP'
$this->mock(Filesystem::class, function (MockInterface $mock) use ($dir, $filePath) {
$mock->expects('ensureDirectoryExists')->with($dir);
$mock->expects('exists')->with($filePath)->andReturnFalse();
$mock->expects('put')->with($filePath, <<<'PHP'
<?php

\fwrite(\STDOUT, 'Info: This is a stub file to be replaced for the application at runtime.');
$date = (new DateTime())->format('d-m-Y H:i:s');

echo "[$date] Info: This is a preload stub file to be replaced for the application at runtime.";

PHP
);
});

$command = $this->artisan('preload:stub');

$command->expectsOutput("Stub copied at [$path].");
$command->expectsOutput("Stub copied at [$filePath].");
$command->expectsOutput('Remember to edit your [php.ini] file:');
$command->expectsOutput("opcache.preload = $path");
$command->expectsOutput("opcache.preload = $filePath");

$command->assertSuccessful();
}

public function test_doesnt_overwrite_same_placeholder(): void
{
$this->mock(Filesystem::class, function (MockInterface $mock) {
$mock->expects('ensureDirectoryExists');
$mock->expects('exists')->andReturnTrue();
$mock->expects('put')->never();
});
Expand Down
32 changes: 20 additions & 12 deletions tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use Laragear\Preload\Http\Middleware\PreloadMiddleware;
use Laragear\Preload\Preloader;
use Laragear\Preload\PreloadServiceProvider;
use Orchestra\Testbench\Attributes\DefineEnvironment;

use function get_class;
use function method_exists;

class ServiceProviderTest extends TestCase
{
Expand Down Expand Up @@ -46,29 +50,33 @@ protected function usesProductionEnvironment(Application $app): void
$app['env'] = 'production';
}

/**
* @define-env usesProductionEnvironment
*/
#[DefineEnvironment('usesProductionEnvironment')]
public function test_registers_global_middleware_on_production(): void
{
static::assertTrue(
$this->app->make(Kernel::class)->hasMiddleware(PreloadMiddleware::class)
);
$http = $this->app->make(Kernel::class);

if (! method_exists($http, 'pushMiddleware')) {
$this->markTestSkipped('The '.get_class($http).' does not have a pushMiddleware() method to test.');
}

static::assertTrue($http->hasMiddleware(PreloadMiddleware::class));
}

protected function setConfigEnableTrue(Application $app): void
{
$app->make('config')->set('preload.enabled', true);
}

/**
* @define-env setConfigEnableTrue
*/
#[DefineEnvironment('setConfigEnableTrue')]
public function test_registers_global_middleware_when_config_is_true(): void
{
static::assertTrue(
app(Kernel::class)->hasMiddleware(PreloadMiddleware::class)
);
$http = $this->app->make(Kernel::class);

if (! method_exists($http, 'pushMiddleware')) {
$this->markTestSkipped('The '.get_class($http).' does not have a pushMiddleware() method to test.');
}

static::assertTrue($http->hasMiddleware(PreloadMiddleware::class));
}

public function test_registers_command(): void
Expand Down