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
9 changes: 9 additions & 0 deletions src/Illuminate/Foundation/Console/ConfigCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use LogicException;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;
Expand Down Expand Up @@ -69,6 +70,14 @@ public function handle()
} catch (Throwable $e) {
$this->files->delete($configPath);

foreach (Arr::dot($config) as $key => $value) {
try {
eval(var_export($value, true).';');
} catch (Throwable $e) {
throw new LogicException("Your configuration files could not be serialized because the value at \"{$key}\" is non-serializable.", 0, $e);
}
}

throw new LogicException('Your configuration files are not serializable.', 0, $e);
}

Expand Down
127 changes: 127 additions & 0 deletions tests/Integration/Foundation/Console/ConfigCacheCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Console;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Tests\Integration\Generators\TestCase;
use LogicException;
use Orchestra\Testbench\Concerns\InteractsWithPublishedFiles;

class ConfigCacheCommandTest extends TestCase
{
use InteractsWithPublishedFiles;

protected $files = [
'bootstrap/cache/config.php',
'config/testconfig.php',
];

protected function setUp(): void
{
$files = new Filesystem;

$this->afterApplicationCreated(function () use ($files) {
$files->ensureDirectoryExists($this->app->configPath());
});

$this->beforeApplicationDestroyed(function () use ($files) {
$files->delete($this->app->configPath('testconfig.php'));
});

parent::setUp();
}

public function testConfigurationCanBeCachedSuccessfully()
{
$files = new Filesystem;
$files->put($this->app->configPath('testconfig.php'), <<<'PHP'
<?php

return [
'string' => 'value',
'number' => 123,
'boolean' => true,
'array' => ['foo', 'bar'],
'from_env' => env('SOMETHING_FROM_ENV', 10),
'nested' => [
'key' => 'value',
],
];
PHP
);

$this->artisan('config:cache')
->assertSuccessful()
->expectsOutputToContain('Configuration cached successfully');

$this->assertFileExists($this->app->getCachedConfigPath());
}

public function testConfigurationCacheFailsWithNonSerializableValue()
{
$files = new Filesystem;
$files->put($this->app->configPath('testconfig.php'), <<<'PHP'
<?php

return [
'closure' => function () {
return 'test';
},
];
PHP
);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Your configuration files could not be serialized because the value at "testconfig.closure" is non-serializable.');

$this->artisan('config:cache');
}

public function testConfigurationCacheFailsWithNestedNonSerializableValue()
{
$files = new Filesystem;
$files->put($this->app->configPath('testconfig.php'), <<<'PHP'
<?php

return [
'nested' => [
'deep' => [
'closure' => function () {
return 'test';
},
],
],
];
PHP
);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('Your configuration files could not be serialized because the value at "testconfig.nested.deep.closure" is non-serializable.');

$this->artisan('config:cache');
}

public function testConfigurationCacheIsDeletedWhenSerializationFails()
{
$files = new Filesystem;
$files->put($this->app->configPath('testconfig.php'), <<<'PHP'
<?php

return [
'closure' => function () {
return 'test';
},
];
PHP
);

try {
$this->artisan('config:cache');
$this->fail('should have thrown an exception');
} catch (LogicException) {
// Expected exception
}

$this->assertFileDoesNotExist($this->app->getCachedConfigPath());
}
}