Skip to content

Commit 0a1a88d

Browse files
Merge pull request #20 from Laragear/fix/stub-creation
[3.x] Fixes stub creation command
2 parents 5f192c4 + 924c843 commit 0a1a88d

File tree

4 files changed

+55
-35
lines changed

4 files changed

+55
-35
lines changed

src/Console/Commands/Stub.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Contracts\Config\Repository as ConfigContract;
77
use Illuminate\Filesystem\Filesystem;
8+
use Laragear\Preload\Preloader;
89
use Symfony\Component\Console\Attribute\AsCommand;
910

1011
/**
@@ -39,25 +40,30 @@ class Stub extends Command
3940
*/
4041
public function handle(Filesystem $file, ConfigContract $config): void
4142
{
42-
$path = $config->get('preload.path');
43+
$dir = $config->get('preload.path');
44+
$filePath = $dir.'/'.Preloader::NAME_PRELOAD;
4345

44-
if ($file->exists($path)) {
46+
$file->ensureDirectoryExists($dir);
47+
48+
if ($file->exists($filePath)) {
4549
$this->info('A preload script file already exists.');
4650

4751
return;
4852
}
4953

50-
$file->put($path, <<<'STUB'
54+
$file->put($filePath, <<<'STUB'
5155
<?php
5256
53-
\fwrite(\STDOUT, 'Info: This is a stub file to be replaced for the application at runtime.');
57+
$date = (new DateTime())->format('d-m-Y H:i:s');
58+
59+
echo "[$date] Info: This is a preload stub file to be replaced for the application at runtime.";
5460

5561
STUB
5662
);
5763

58-
$this->info("Stub copied at [$path].");
64+
$this->info("Stub copied at [$filePath].");
5965
$this->newLine();
6066
$this->comment('Remember to edit your [php.ini] file:');
61-
$this->comment("opcache.preload = $path");
67+
$this->comment("opcache.preload = $filePath");
6268
}
6369
}

src/Preloader.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@
1010
class Preloader
1111
{
1212
/**
13-
* The location of the statistic file.
13+
* The filename for the statistics file.
1414
*
1515
* @const string
1616
*/
17-
public const STUB_STATISTICS = __DIR__.'/../stubs/statistics.md';
17+
public const NAME_STATISTICS = 'statistics.md';
1818

1919
/**
20-
* The location of the preload script stub.
20+
* The filename for the preload script file.
21+
*
22+
* @const string
2123
*/
22-
public const STUB_PRELOAD = __DIR__.'/../stubs/preload.php.stub';
24+
public const NAME_PRELOAD = 'preload.php';
2325

2426
/**
25-
* The filename for the statistics file.
27+
* The location of the statistic file.
2628
*
2729
* @const string
2830
*/
29-
public const NAME_STATISTICS = 'statistics.md';
31+
public const STUB_STATISTICS = __DIR__.'/../stubs/'.self::NAME_STATISTICS;
3032

3133
/**
32-
* The filename for the preload script file.
33-
*
34-
* @const string
34+
* The location of the preload script stub.
3535
*/
36-
public const NAME_PRELOAD = 'preload.php';
36+
public const STUB_PRELOAD = __DIR__.'/../stubs/'.self::NAME_PRELOAD.'.stub';
3737

3838
/**
3939
* The filename of the list of preload files.

tests/Console/Commands/StubTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,44 @@
33
namespace Tests\Console\Commands;
44

55
use Illuminate\Filesystem\Filesystem;
6+
use Laragear\Preload\Preloader;
67
use Mockery\MockInterface;
78
use Tests\TestCase;
89

910
class StubTest extends TestCase
1011
{
1112
public function test_stores_placeholder_in_output_path(): void
1213
{
13-
$path = $this->app->basePath();
14+
$dir = $this->app->basePath();
15+
$filePath = $dir.'/'.Preloader::NAME_PRELOAD;
1416

15-
$this->mock(Filesystem::class, function (MockInterface $mock) use ($path) {
16-
$mock->expects('exists')->andReturnFalse();
17-
$mock->expects('put')->with($path, <<<'PHP'
17+
$this->mock(Filesystem::class, function (MockInterface $mock) use ($dir, $filePath) {
18+
$mock->expects('ensureDirectoryExists')->with($dir);
19+
$mock->expects('exists')->with($filePath)->andReturnFalse();
20+
$mock->expects('put')->with($filePath, <<<'PHP'
1821
<?php
1922
20-
\fwrite(\STDOUT, 'Info: This is a stub file to be replaced for the application at runtime.');
23+
$date = (new DateTime())->format('d-m-Y H:i:s');
24+
25+
echo "[$date] Info: This is a preload stub file to be replaced for the application at runtime.";
2126

2227
PHP
2328
);
2429
});
2530

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

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

3237
$command->assertSuccessful();
3338
}
3439

3540
public function test_doesnt_overwrite_same_placeholder(): void
3641
{
3742
$this->mock(Filesystem::class, function (MockInterface $mock) {
43+
$mock->expects('ensureDirectoryExists');
3844
$mock->expects('exists')->andReturnTrue();
3945
$mock->expects('put')->never();
4046
});

tests/ServiceProviderTest.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
use Laragear\Preload\Http\Middleware\PreloadMiddleware;
1111
use Laragear\Preload\Preloader;
1212
use Laragear\Preload\PreloadServiceProvider;
13+
use Orchestra\Testbench\Attributes\DefineEnvironment;
14+
15+
use function get_class;
16+
use function method_exists;
1317

1418
class ServiceProviderTest extends TestCase
1519
{
@@ -46,29 +50,33 @@ protected function usesProductionEnvironment(Application $app): void
4650
$app['env'] = 'production';
4751
}
4852

49-
/**
50-
* @define-env usesProductionEnvironment
51-
*/
53+
#[DefineEnvironment('usesProductionEnvironment')]
5254
public function test_registers_global_middleware_on_production(): void
5355
{
54-
static::assertTrue(
55-
$this->app->make(Kernel::class)->hasMiddleware(PreloadMiddleware::class)
56-
);
56+
$http = $this->app->make(Kernel::class);
57+
58+
if (! method_exists($http, 'pushMiddleware')) {
59+
$this->markTestSkipped('The '.get_class($http).' does not have a pushMiddleware() method to test.');
60+
}
61+
62+
static::assertTrue($http->hasMiddleware(PreloadMiddleware::class));
5763
}
5864

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

64-
/**
65-
* @define-env setConfigEnableTrue
66-
*/
70+
#[DefineEnvironment('setConfigEnableTrue')]
6771
public function test_registers_global_middleware_when_config_is_true(): void
6872
{
69-
static::assertTrue(
70-
app(Kernel::class)->hasMiddleware(PreloadMiddleware::class)
71-
);
73+
$http = $this->app->make(Kernel::class);
74+
75+
if (! method_exists($http, 'pushMiddleware')) {
76+
$this->markTestSkipped('The '.get_class($http).' does not have a pushMiddleware() method to test.');
77+
}
78+
79+
static::assertTrue($http->hasMiddleware(PreloadMiddleware::class));
7280
}
7381

7482
public function test_registers_command(): void

0 commit comments

Comments
 (0)